Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 1 | #include "util.hpp" |
| 2 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 3 | #include <gpiod.hpp> |
| 4 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 5 | namespace phosphor::power::psu |
| 6 | { |
| 7 | |
| 8 | const UtilBase& getUtils() |
| 9 | { |
| 10 | static Util util; |
| 11 | return util; |
| 12 | } |
| 13 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 14 | GPIOInterface::GPIOInterface(const std::string& namedGpio) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 15 | { |
| 16 | try |
| 17 | { |
| 18 | line = gpiod::find_line(namedGpio); |
Adriana Kobylak | a588eaf | 2021-09-20 20:05:25 +0000 | [diff] [blame] | 19 | if (!line) |
| 20 | { |
| 21 | throw std::runtime_error("Line does not exist: " + namedGpio); |
| 22 | } |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 23 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 24 | catch (const std::exception& e) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 25 | { |
| 26 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 27 | fmt::format("Failed to find line: {}", e.what()).c_str()); |
| 28 | throw; |
| 29 | } |
| 30 | } |
| 31 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 32 | std::unique_ptr<GPIOInterfaceBase> |
| 33 | GPIOInterface::createGPIO(const std::string& namedGpio) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 34 | { |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 35 | return std::make_unique<GPIOInterface>(namedGpio); |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 38 | std::string GPIOInterface::getName() const |
B. J. Wyman | d8b8cb1 | 2021-07-15 22:03:34 +0000 | [diff] [blame] | 39 | { |
| 40 | return line.name(); |
| 41 | } |
| 42 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 43 | int GPIOInterface::read() |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 44 | { |
| 45 | using namespace phosphor::logging; |
| 46 | |
| 47 | int value = -1; |
| 48 | |
| 49 | if (!line) |
| 50 | { |
| 51 | log<level::ERR>("Failed line"); |
| 52 | throw std::runtime_error{std::string{"Failed to find line"}}; |
| 53 | } |
| 54 | |
| 55 | try |
| 56 | { |
| 57 | line.request({__FUNCTION__, gpiod::line_request::DIRECTION_INPUT, |
| 58 | gpiod::line_request::FLAG_ACTIVE_LOW}); |
| 59 | try |
| 60 | { |
| 61 | value = line.get_value(); |
| 62 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 63 | catch (const std::exception& e) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 64 | { |
| 65 | log<level::ERR>( |
| 66 | fmt::format("Failed to get_value of GPIO line: {}", e.what()) |
| 67 | .c_str()); |
| 68 | line.release(); |
| 69 | throw; |
| 70 | } |
| 71 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 72 | line.release(); |
| 73 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 74 | catch (const std::exception& e) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 75 | { |
| 76 | log<level::ERR>("Failed to request GPIO line", |
| 77 | entry("MSG=%s", e.what())); |
| 78 | throw; |
| 79 | } |
| 80 | |
| 81 | return value; |
| 82 | } |
| 83 | |
Adriana Kobylak | 52245b6 | 2021-09-13 15:46:21 +0000 | [diff] [blame] | 84 | void GPIOInterface::write(int value, std::bitset<32> flags) |
| 85 | { |
| 86 | using namespace phosphor::logging; |
| 87 | |
| 88 | if (!line) |
| 89 | { |
| 90 | log<level::ERR>("Failed line"); |
| 91 | throw std::runtime_error{std::string{"Failed to find line"}}; |
| 92 | } |
| 93 | |
| 94 | try |
| 95 | { |
| 96 | line.request( |
| 97 | {__FUNCTION__, gpiod::line_request::DIRECTION_OUTPUT, flags}, |
| 98 | value); |
| 99 | |
| 100 | line.release(); |
| 101 | } |
| 102 | catch (std::exception& e) |
| 103 | { |
| 104 | log<level::ERR>("Failed to set GPIO line", entry("MSG=%s", e.what()), |
| 105 | entry("VALUE=%d", value)); |
| 106 | throw; |
| 107 | } |
| 108 | } |
| 109 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 110 | std::unique_ptr<GPIOInterfaceBase> createGPIO(const std::string& namedGpio) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 111 | { |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 112 | return GPIOInterface::createGPIO(namedGpio); |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 115 | } // namespace phosphor::power::psu |