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