xref: /openbmc/phosphor-hwmon/fan_speed.cpp (revision 64129937)
1 #include "fan_speed.hpp"
2 
3 #include "env.hpp"
4 #include "hwmon.hpp"
5 #include "hwmonio.hpp"
6 #include "sensorset.hpp"
7 #include "sysfs.hpp"
8 
9 #include <phosphor-logging/elog-errors.hpp>
10 #include <xyz/openbmc_project/Control/Device/error.hpp>
11 
12 #include <format>
13 
14 using namespace phosphor::logging;
15 
16 namespace hwmon
17 {
18 
target(uint64_t value)19 uint64_t FanSpeed::target(uint64_t value)
20 {
21     auto curValue = FanSpeedObject::target();
22 
23     if (curValue != value)
24     {
25         // Write target out to sysfs
26         try
27         {
28             _ioAccess->write(value, _type, _id, entry::target, hwmonio::retries,
29                              hwmonio::delay);
30         }
31         catch (const std::system_error& e)
32         {
33             using namespace sdbusplus::xyz::openbmc_project::Control::Device::
34                 Error;
35             report<WriteFailure>(
36                 xyz::openbmc_project::Control::Device::WriteFailure::
37                     CALLOUT_ERRNO(e.code().value()),
38                 xyz::openbmc_project::Control::Device::WriteFailure::
39                     CALLOUT_DEVICE_PATH(_devPath.c_str()));
40 
41             auto file = sysfs::make_sysfs_path(_ioAccess->path(), _type, _id,
42                                                entry::target);
43 
44             log<level::INFO>(std::format("Failing sysfs file: {} errno: {}",
45                                          file, e.code().value())
46                                  .c_str());
47 
48             exit(EXIT_FAILURE);
49         }
50     }
51 
52     return FanSpeedObject::target(value);
53 }
54 
enable()55 void FanSpeed::enable()
56 {
57     auto enable = env::getEnv("ENABLE", _type, _id);
58     if (!enable.empty())
59     {
60         auto val = std::stoul(enable);
61 
62         try
63         {
64             _ioAccess->write(val, type::pwm, _id, entry::enable,
65                              hwmonio::retries, hwmonio::delay);
66         }
67         catch (const std::system_error& e)
68         {
69             using namespace sdbusplus::xyz::openbmc_project::Control::Device::
70                 Error;
71             phosphor::logging::report<WriteFailure>(
72                 xyz::openbmc_project::Control::Device::WriteFailure::
73                     CALLOUT_ERRNO(e.code().value()),
74                 xyz::openbmc_project::Control::Device::WriteFailure::
75                     CALLOUT_DEVICE_PATH(_devPath.c_str()));
76 
77             auto fullPath = sysfs::make_sysfs_path(_ioAccess->path(), type::pwm,
78                                                    _id, entry::enable);
79 
80             log<level::INFO>(std::format("Failing sysfs file: {} errno: {}",
81                                          fullPath, e.code().value())
82                                  .c_str());
83 
84             exit(EXIT_FAILURE);
85         }
86     }
87 }
88 
89 } // namespace hwmon
90