blob: 007ad37be8a4a4fca2cb2b2553ad867abbccaadf [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>
6#include <phosphor-logging/log.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 {
31 phosphor::logging::log<phosphor::logging::level::ERR>(
George Liub0e33192022-06-21 08:28:28 +080032 "Error in gpioplus - nameToOffset",
33 phosphor::logging::entry("ERROR=%s", e.what()));
Anthony Wilsonb8f3c232018-12-12 16:19:04 -060034 return false;
35 }
36
37 try
38 {
George Liub0e33192022-06-21 08:28:28 +080039 // TODO: openbmc/phosphor-power-control#1 - Handle cases where gpiochip
40 // could be non-zero.
Anthony Wilsonb8f3c232018-12-12 16:19:04 -060041 gpioplus::Chip chip(0);
42 gpioplus::HandleFlags flags(chip.getLineInfo(gpioOffset).flags);
43 flags.output = true;
George Liub0e33192022-06-21 08:28:28 +080044 gpioplus::Handle handle(chip, {{gpioOffset, 0}}, flags, "chassiskill");
Anthony Wilsonb8f3c232018-12-12 16:19:04 -060045
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 Liub0e33192022-06-21 08:28:28 +080052 "Error in gpioplus",
53 phosphor::logging::entry("ERROR=%s", e.what()));
Anthony Wilsonb8f3c232018-12-12 16:19:04 -060054 return false;
55 }
56
Anthony Wilsond1c35322018-12-12 16:10:35 -060057 return true;
58}
59
60} // namespace utility