xref: /openbmc/phosphor-buttons/src/cpld.cpp (revision ccd7db054ccced63c25e66fe7ab8e1847f9d38ae)
1 
2 #include "config.h"
3 
4 #include "button_config.hpp"
5 
6 #include <error.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 
10 #include <phosphor-logging/lg2.hpp>
11 
12 const std::string cpldDev = "/sys/bus/i2c/devices/";
13 
14 std::string getCpldDevPath(const CpldInfo& info)
15 {
16     std::stringstream devPath;
17     devPath << cpldDev << info.i2cBus << "-" << std::hex << std::setw(4)
18             << std::setfill('0') << info.i2cAddress << "/" << info.registerName;
19     return devPath.str();
20 }
21 
22 int configCpld(ButtonConfig& buttonIFConfig)
23 {
24     std::string devPath = getCpldDevPath(buttonIFConfig.cpld);
25 
26     auto fd = ::open(devPath.c_str(), O_RDONLY | O_NONBLOCK);
27 
28     if (fd < 0)
29     {
30         lg2::error("Open {PATH} error: {ERROR}", "PATH", devPath, "ERROR",
31                    errno);
32         return -1;
33     }
34 
35     buttonIFConfig.cpld.cpldMappedFd = fd;
36     buttonIFConfig.fds.push_back(fd);
37     return 0;
38 }
39