blob: 89857fde304f91d8b62ede670fbed70d68f18963 [file] [log] [blame]
Anthony Wilsond1c35322018-12-12 16:10:35 -06001#pragma once
2
Anthony Wilsonb8f3c232018-12-12 16:19:04 -06003#include <gpioplus/chip.hpp>
4#include <gpioplus/handle.hpp>
5#include <gpioplus/utility/aspeed.hpp>
George Liu09fdcb52022-06-21 08:55:59 +08006#include <phosphor-logging/lg2.hpp>
George Liub0e33192022-06-21 08:28:28 +08007
Anthony Wilsonb8f3c232018-12-12 16:19:04 -06008#include <exception>
Anthony Wilsond1c35322018-12-12 16:10:35 -06009#include <string>
10
11namespace 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 */
22bool gpioSetValue(const std::string& gpioName, bool activeLow, bool asserted)
23{
Anthony Wilsonb8f3c232018-12-12 16:19:04 -060024 uint32_t gpioOffset;
25 try
26 {
27 gpioOffset = gpioplus::utility::aspeed::nameToOffset(gpioName);
28 }
George Liub0e33192022-06-21 08:28:28 +080029 catch (const std::logic_error& e)
Anthony Wilsonb8f3c232018-12-12 16:19:04 -060030 {
George Liu09fdcb52022-06-21 08:55:59 +080031 lg2::error("Error in gpioplus - nameToOffset: {ERROR}", "ERROR", e);
Anthony Wilsonb8f3c232018-12-12 16:19:04 -060032 return false;
33 }
34
35 try
36 {
George Liub0e33192022-06-21 08:28:28 +080037 // TODO: openbmc/phosphor-power-control#1 - Handle cases where gpiochip
38 // could be non-zero.
Anthony Wilsonb8f3c232018-12-12 16:19:04 -060039 gpioplus::Chip chip(0);
40 gpioplus::HandleFlags flags(chip.getLineInfo(gpioOffset).flags);
41 flags.output = true;
George Liub0e33192022-06-21 08:28:28 +080042 gpioplus::Handle handle(chip, {{gpioOffset, 0}}, flags, "chassiskill");
Anthony Wilsonb8f3c232018-12-12 16:19:04 -060043
44 bool value = (asserted ^ activeLow);
45 handle.setValues({value});
46 }
47 catch (const std::exception& e)
48 {
George Liu09fdcb52022-06-21 08:55:59 +080049 lg2::error("Error in gpioplus: {ERROR}", "ERROR", e);
Anthony Wilsonb8f3c232018-12-12 16:19:04 -060050 return false;
51 }
52
Anthony Wilsond1c35322018-12-12 16:10:35 -060053 return true;
54}
55
56} // namespace utility