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> |
| 6 | #include <phosphor-logging/log.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 | { |
| 31 | phosphor::logging::log<phosphor::logging::level::ERR>( |
George Liu | b0e3319 | 2022-06-21 08:28:28 +0800 | [diff] [blame] | 32 | "Error in gpioplus - nameToOffset", |
| 33 | phosphor::logging::entry("ERROR=%s", e.what())); |
Anthony Wilson | b8f3c23 | 2018-12-12 16:19:04 -0600 | [diff] [blame] | 34 | return false; |
| 35 | } |
| 36 | |
| 37 | try |
| 38 | { |
George Liu | b0e3319 | 2022-06-21 08:28:28 +0800 | [diff] [blame] | 39 | // TODO: openbmc/phosphor-power-control#1 - Handle cases where gpiochip |
| 40 | // could be non-zero. |
Anthony Wilson | b8f3c23 | 2018-12-12 16:19:04 -0600 | [diff] [blame] | 41 | gpioplus::Chip chip(0); |
| 42 | gpioplus::HandleFlags flags(chip.getLineInfo(gpioOffset).flags); |
| 43 | flags.output = true; |
George Liu | b0e3319 | 2022-06-21 08:28:28 +0800 | [diff] [blame] | 44 | gpioplus::Handle handle(chip, {{gpioOffset, 0}}, flags, "chassiskill"); |
Anthony Wilson | b8f3c23 | 2018-12-12 16:19:04 -0600 | [diff] [blame] | 45 | |
| 46 | bool value = (asserted ^ activeLow); |
| 47 | handle.setValues({value}); |
| 48 | } |
| 49 | catch (const std::exception& e) |
| 50 | { |
| 51 | phosphor::logging::log<phosphor::logging::level::ERR>( |
George Liu | b0e3319 | 2022-06-21 08:28:28 +0800 | [diff] [blame] | 52 | "Error in gpioplus", |
| 53 | phosphor::logging::entry("ERROR=%s", e.what())); |
Anthony Wilson | b8f3c23 | 2018-12-12 16:19:04 -0600 | [diff] [blame] | 54 | return false; |
| 55 | } |
| 56 | |
Anthony Wilson | d1c3532 | 2018-12-12 16:10:35 -0600 | [diff] [blame] | 57 | return true; |
| 58 | } |
| 59 | |
| 60 | } // namespace utility |