psu-ng: Add in ability to get presence via GPIO
The device tree and entity-manager (D-Bus) properties have the power
supply GPIO presence lines named, so we can use those GPIO line names to
find the GPIO device to read for determining power supply presence.
Some systems have the power supply presence lines in a gpio-keys section
(notably IBM Rainier). To facilitate continued function while running
with a device tree that has not removed those device tree entries, allow
for a fallback to the old inventory D-Bus propertiesChanged or
interfaceAdded matches for presence.
Change-Id: I5002aa62e5b460463cc26328c889a3786b467a3c
Signed-off-by: B. J. Wyman <bjwyman@gmail.com>
diff --git a/phosphor-power-supply/util.cpp b/phosphor-power-supply/util.cpp
index c4555ae..c64de33 100644
--- a/phosphor-power-supply/util.cpp
+++ b/phosphor-power-supply/util.cpp
@@ -1,5 +1,7 @@
#include "util.hpp"
+#include <gpiod.hpp>
+
namespace phosphor::power::psu
{
@@ -9,4 +11,71 @@
return util;
}
+GPIOReader::GPIOReader(const std::string& namedGpio)
+{
+ try
+ {
+ line = gpiod::find_line(namedGpio);
+ }
+ catch (std::exception& e)
+ {
+ phosphor::logging::log<phosphor::logging::level::ERR>(
+ fmt::format("Failed to find line: {}", e.what()).c_str());
+ throw;
+ }
+}
+
+std::unique_ptr<GPIOInterface>
+ GPIOReader::createGPIO(const std::string& namedGpio)
+{
+ return std::make_unique<GPIOReader>(namedGpio);
+}
+
+int GPIOReader::read()
+{
+ using namespace phosphor::logging;
+
+ int value = -1;
+
+ if (!line)
+ {
+ log<level::ERR>("Failed line");
+ throw std::runtime_error{std::string{"Failed to find line"}};
+ }
+
+ try
+ {
+ line.request({__FUNCTION__, gpiod::line_request::DIRECTION_INPUT,
+ gpiod::line_request::FLAG_ACTIVE_LOW});
+ try
+ {
+ value = line.get_value();
+ }
+ catch (std::exception& e)
+ {
+ log<level::ERR>(
+ fmt::format("Failed to get_value of GPIO line: {}", e.what())
+ .c_str());
+ line.release();
+ throw;
+ }
+
+ log<level::DEBUG>("release() line");
+ line.release();
+ }
+ catch (std::exception& e)
+ {
+ log<level::ERR>("Failed to request GPIO line",
+ entry("MSG=%s", e.what()));
+ throw;
+ }
+
+ return value;
+}
+
+std::unique_ptr<GPIOInterface> createGPIO(const std::string& namedGpio)
+{
+ return GPIOReader::createGPIO(namedGpio);
+}
+
} // namespace phosphor::power::psu