Anthony Wilson | d1c3532 | 2018-12-12 16:10:35 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Anthony Wilson | b8f3c23 | 2018-12-12 16:19:04 -0600 | [diff] [blame] | 3 | #include <gpioplus/chip.hpp> |
| 4 | #include <gpioplus/handle.hpp> |
| 5 | #include <gpioplus/utility/aspeed.hpp> |
George Liu | 09fdcb5 | 2022-06-21 08:55:59 +0800 | [diff] [blame^] | 6 | #include <phosphor-logging/lg2.hpp> |
George Liu | b0e3319 | 2022-06-21 08:28:28 +0800 | [diff] [blame] | 7 | |
Anthony Wilson | b8f3c23 | 2018-12-12 16:19:04 -0600 | [diff] [blame] | 8 | #include <exception> |
Anthony Wilson | d1c3532 | 2018-12-12 16:10:35 -0600 | [diff] [blame] | 9 | #include <string> |
| 10 | |
| 11 | namespace utility |
| 12 | { |
| 13 | |
| 14 | /** @brief Set the value of a specified gpio |
| 15 | * |
| 16 | * @param[in] gpioName - GPIO to set |
| 17 | * @param[in] activeLow - is pin active at low voltage |
| 18 | * @param[in] asserted - is pin in active state |
| 19 | * |
| 20 | * @return bool - success of setting the GPIO |
| 21 | */ |
| 22 | bool gpioSetValue(const std::string& gpioName, bool activeLow, bool asserted) |
| 23 | { |
Anthony Wilson | b8f3c23 | 2018-12-12 16:19:04 -0600 | [diff] [blame] | 24 | uint32_t gpioOffset; |
| 25 | try |
| 26 | { |
| 27 | gpioOffset = gpioplus::utility::aspeed::nameToOffset(gpioName); |
| 28 | } |
George Liu | b0e3319 | 2022-06-21 08:28:28 +0800 | [diff] [blame] | 29 | catch (const std::logic_error& e) |
Anthony Wilson | b8f3c23 | 2018-12-12 16:19:04 -0600 | [diff] [blame] | 30 | { |
George Liu | 09fdcb5 | 2022-06-21 08:55:59 +0800 | [diff] [blame^] | 31 | lg2::error("Error in gpioplus - nameToOffset: {ERROR}", "ERROR", e); |
Anthony Wilson | b8f3c23 | 2018-12-12 16:19:04 -0600 | [diff] [blame] | 32 | return false; |
| 33 | } |
| 34 | |
| 35 | try |
| 36 | { |
George Liu | b0e3319 | 2022-06-21 08:28:28 +0800 | [diff] [blame] | 37 | // TODO: openbmc/phosphor-power-control#1 - Handle cases where gpiochip |
| 38 | // could be non-zero. |
Anthony Wilson | b8f3c23 | 2018-12-12 16:19:04 -0600 | [diff] [blame] | 39 | gpioplus::Chip chip(0); |
| 40 | gpioplus::HandleFlags flags(chip.getLineInfo(gpioOffset).flags); |
| 41 | flags.output = true; |
George Liu | b0e3319 | 2022-06-21 08:28:28 +0800 | [diff] [blame] | 42 | gpioplus::Handle handle(chip, {{gpioOffset, 0}}, flags, "chassiskill"); |
Anthony Wilson | b8f3c23 | 2018-12-12 16:19:04 -0600 | [diff] [blame] | 43 | |
| 44 | bool value = (asserted ^ activeLow); |
| 45 | handle.setValues({value}); |
| 46 | } |
| 47 | catch (const std::exception& e) |
| 48 | { |
George Liu | 09fdcb5 | 2022-06-21 08:55:59 +0800 | [diff] [blame^] | 49 | lg2::error("Error in gpioplus: {ERROR}", "ERROR", e); |
Anthony Wilson | b8f3c23 | 2018-12-12 16:19:04 -0600 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | |
Anthony Wilson | d1c3532 | 2018-12-12 16:10:35 -0600 | [diff] [blame] | 53 | return true; |
| 54 | } |
| 55 | |
| 56 | } // namespace utility |