xref: /openbmc/phosphor-bmc-code-mgmt/eeprom-device/pt5161l/pt5161l.cpp (revision 975fbbeb3f11e01a2ba4bcad5213935019e2568b)
1 #include "pt5161l.hpp"
2 
3 #include <phosphor-logging/lg2.hpp>
4 
5 #include <algorithm>
6 #include <fstream>
7 #include <iomanip>
8 #include <sstream>
9 
10 PHOSPHOR_LOG2_USING;
11 
getVersion()12 std::string PT5161LDeviceVersion::getVersion()
13 {
14     std::string version;
15     std::ostringstream busOss;
16     std::ostringstream addrOss;
17 
18     busOss << std::setw(2) << std::setfill('0') << static_cast<int>(bus);
19     addrOss << std::setw(4) << std::setfill('0') << std::hex << std::nouppercase
20             << static_cast<int>(address);
21 
22     // The PT5161L driver exposes the firmware version through the fw_ver node
23     std::string path = "/sys/kernel/debug/pt5161l/" + busOss.str() + "-" +
24                        addrOss.str() + "/fw_ver";
25 
26     std::ifstream file(path);
27     if (!file)
28     {
29         error("Failed to get version: unable to open file: {PATH}", "PATH",
30               path);
31         return version;
32     }
33 
34     if (!std::getline(file, version) || version.empty())
35     {
36         error("Failed to read version from file: {PATH}", "PATH", path);
37     }
38 
39     return version;
40 }
41 
isDeviceReady()42 bool PT5161LDeviceVersion::isDeviceReady()
43 {
44     std::string status;
45     std::ostringstream busOss;
46     std::ostringstream addrOss;
47 
48     busOss << std::setw(2) << std::setfill('0') << static_cast<int>(bus);
49     addrOss << std::setw(4) << std::setfill('0') << std::hex << std::nouppercase
50             << static_cast<int>(address);
51 
52     std::string fw_load_status = "/sys/kernel/debug/pt5161l/" + busOss.str() +
53                                  "-" + addrOss.str() + "/fw_load_status";
54 
55     std::ifstream file(fw_load_status);
56 
57     if (file && std::getline(file, status) && status == "normal")
58     {
59         return true;
60     }
61 
62     error("Status from file: {PATH} is invalid: {STATUS}", "PATH",
63           fw_load_status, "STATUS", status);
64 
65     return false;
66 }
67 
68 std::optional<HostPowerInf::HostState>
getHostStateToQueryVersion()69     PT5161LDeviceVersion::getHostStateToQueryVersion()
70 {
71     return HostPowerInf::HostState::Running;
72 }
73