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