blob: 8afa5350c09a755904d0d1bc841cc39cb06e69c7 [file] [log] [blame]
Delphine CC Chiuccd7db02023-02-09 14:48:53 +08001
Delphine CC Chiuccd7db02023-02-09 14:48:53 +08002#include "button_config.hpp"
Delphine CC Chiu15c60e22024-04-12 13:01:32 -05003#include "config.hpp"
Delphine CC Chiuccd7db02023-02-09 14:48:53 +08004
5#include <error.h>
6#include <fcntl.h>
7#include <unistd.h>
8
9#include <phosphor-logging/lg2.hpp>
10
11const std::string cpldDev = "/sys/bus/i2c/devices/";
12
13std::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
21int 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}