Marc Olberding | 1e17db5 | 2025-08-27 12:25:28 -0700 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: 2025 NVIDIA |
| 3 | |
| 4 | #include "i2c.hpp" |
| 5 | |
| 6 | #include <format> |
| 7 | #include <fstream> |
| 8 | #include <iostream> |
| 9 | namespace i2c |
| 10 | { |
| 11 | |
| 12 | void rebind_controller(std::string_view number) |
| 13 | { |
| 14 | std::string bindpath = |
| 15 | std::format("/sys/bus/platform/drivers/aspeed-i2c-bus/unbind", number); |
| 16 | std::ofstream bindofs(bindpath); |
| 17 | if (!bindofs) |
| 18 | { |
| 19 | std::cerr << std::format("{} unable to open\n", bindpath); |
| 20 | return; |
| 21 | } |
| 22 | try |
| 23 | { |
| 24 | bindofs << std::format("{}.i2c\n", number); |
| 25 | } |
| 26 | catch (const std::system_error& e) |
| 27 | { |
| 28 | std::cerr << std::format("{} unable to write\n", bindpath); |
| 29 | return; |
| 30 | } |
| 31 | bindofs.close(); |
| 32 | std::cerr << std::format("{} unbound\n", number); |
| 33 | |
| 34 | std::string unbindpath = |
| 35 | std::format("/sys/bus/platform/drivers/aspeed-i2c-bus/bind", number); |
| 36 | std::ofstream unbindofs(unbindpath); |
| 37 | if (!unbindofs) |
| 38 | { |
| 39 | std::cerr << std::format("{} unable to open\n", unbindpath); |
| 40 | return; |
| 41 | } |
| 42 | try |
| 43 | { |
| 44 | unbindofs << std::format("{}.i2c\n", number); |
| 45 | } |
| 46 | catch (const std::system_error& e) |
| 47 | { |
| 48 | std::cerr << std::format("{} unable to write\n", unbindpath); |
| 49 | return; |
| 50 | } |
| 51 | std::cerr << std::format("{} bound\n", number); |
| 52 | } |
| 53 | |
| 54 | void new_device(unsigned int bus, unsigned int address, |
| 55 | std::string_view device_type) |
| 56 | { |
| 57 | std::string path = |
| 58 | std::format("/sys/bus/i2c/devices/i2c-{}/new_device", bus); |
| 59 | std::cerr << std::format("attempting to open {}", path); |
| 60 | std::ofstream new_device(path); |
| 61 | if (!new_device) |
| 62 | { |
| 63 | std::cerr << "Error: Unable to create I2C device\n"; |
| 64 | return; |
| 65 | } |
| 66 | new_device << std::format("{} 0x{:02x}", device_type, address); |
| 67 | new_device.close(); |
| 68 | |
| 69 | std::cerr << std::format("{} device created at bus {}", device_type, bus); |
| 70 | } |
| 71 | } // namespace i2c |