xref: /openbmc/phosphor-hwmon/gpio_handle.cpp (revision e8771fd4)
1b28f432aSPatrick Venture #include "gpio_handle.hpp"
2b28f432aSPatrick Venture 
3b28f432aSPatrick Venture #include <gpioplus/chip.hpp>
4b28f432aSPatrick Venture #include <gpioplus/handle.hpp>
5b28f432aSPatrick Venture #include <phosphor-logging/log.hpp>
6*e8771fd4SPatrick Williams 
7*e8771fd4SPatrick Williams #include <cstdlib>
8*e8771fd4SPatrick Williams #include <memory>
9b28f432aSPatrick Venture #include <string>
10b28f432aSPatrick Venture 
11b28f432aSPatrick Venture namespace gpio
12b28f432aSPatrick Venture {
13b28f432aSPatrick Venture 
14b28f432aSPatrick Venture using namespace phosphor::logging;
15b28f432aSPatrick Venture 
16e3e0f97dSPatrick Venture std::unique_ptr<gpioplus::HandleInterface>
BuildGpioHandle(const std::string & gpiochip,const std::string & line)17e3e0f97dSPatrick Venture     BuildGpioHandle(const std::string& gpiochip, const std::string& line)
18b28f432aSPatrick Venture {
19b28f432aSPatrick Venture     char *gpioEnd, *lineEnd;
20b28f432aSPatrick Venture     unsigned long chipId = std::strtoul(gpiochip.c_str(), &gpioEnd, 10);
21b28f432aSPatrick Venture     unsigned long lineOffset = std::strtoul(line.c_str(), &lineEnd, 10);
22b28f432aSPatrick Venture 
23b28f432aSPatrick Venture     if (!gpioEnd || gpioEnd != &gpiochip.c_str()[gpiochip.length()])
24b28f432aSPatrick Venture     {
25b28f432aSPatrick Venture         log<level::ERR>("Unable to handle giochip entry",
26b28f432aSPatrick Venture                         entry("GPIOCHIP=%s", gpiochip.c_str()));
27b28f432aSPatrick Venture         return nullptr;
28b28f432aSPatrick Venture     }
29b28f432aSPatrick Venture 
30b28f432aSPatrick Venture     if (!lineEnd || lineEnd != &line.c_str()[line.length()])
31b28f432aSPatrick Venture     {
32b28f432aSPatrick Venture         log<level::ERR>("Unable to handle line entry",
33b28f432aSPatrick Venture                         entry("LINE=%s", line.c_str()));
34b28f432aSPatrick Venture         return nullptr;
35b28f432aSPatrick Venture     }
36b28f432aSPatrick Venture 
37b28f432aSPatrick Venture     try
38b28f432aSPatrick Venture     {
39b28f432aSPatrick Venture         gpioplus::Chip chip(chipId);
40b28f432aSPatrick Venture         gpioplus::HandleFlags flags(chip.getLineInfo(lineOffset).flags);
41b28f432aSPatrick Venture         flags.output = true;
42b28f432aSPatrick Venture         std::vector<gpioplus::Handle::Line> lines = {
43b28f432aSPatrick Venture             {static_cast<uint32_t>(lineOffset), 0}};
44b28f432aSPatrick Venture 
45b28f432aSPatrick Venture         return std::make_unique<gpioplus::Handle>(chip, lines, flags,
46b28f432aSPatrick Venture                                                   "phosphor-hwmon");
47b28f432aSPatrick Venture     }
48b28f432aSPatrick Venture     catch (const std::exception& e)
49b28f432aSPatrick Venture     {
50b28f432aSPatrick Venture         log<level::ERR>("Unable to set up GPIO handle",
51b28f432aSPatrick Venture                         entry("ERROR=%s", e.what()));
52b28f432aSPatrick Venture         return nullptr;
53b28f432aSPatrick Venture     }
54b28f432aSPatrick Venture }
55b28f432aSPatrick Venture 
56b28f432aSPatrick Venture } // namespace gpio
57