blob: 1cc51dfb580b277610b86e047b70971ee78b812c [file] [log] [blame]
Kevin Tung994a77f2024-12-23 17:48:56 +08001#include "pt5161l.hpp"
2
3#include <phosphor-logging/lg2.hpp>
4
5#include <algorithm>
6#include <fstream>
7#include <iomanip>
8#include <sstream>
9
10PHOSPHOR_LOG2_USING;
11
12std::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
Daniel Hsu975fbbe2025-09-01 16:12:43 +080042bool 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
Kevin Tung994a77f2024-12-23 17:48:56 +080068std::optional<HostPowerInf::HostState>
69 PT5161LDeviceVersion::getHostStateToQueryVersion()
70{
71 return HostPowerInf::HostState::Running;
72}