summaryrefslogtreecommitdiffstats
path: root/fan_speed.cpp
blob: 8e7043767d2166fd591c62491a7b4808b8fcf248 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "fan_speed.hpp"

#include "env.hpp"
#include "hwmon.hpp"
#include "hwmonio.hpp"
#include "sensorset.hpp"
#include "sysfs.hpp"

#include <phosphor-logging/elog-errors.hpp>
#include <xyz/openbmc_project/Control/Device/error.hpp>

using namespace phosphor::logging;

namespace hwmon
{

uint64_t FanSpeed::target(uint64_t value)
{
    auto directPWM = false;
    auto curValue = FanSpeedObject::target();
    auto enable = env::getEnv("ENABLE", _type, _id);
    auto rawpwm = env::getEnv("RAWPWM", _type, _id);
    if (!enable.empty())
    {
        auto val = std::stoul(enable);
        if (val == 1)
        {
            directPWM = true;
        }
    }
    if (!rawpwm.empty())
    {
        auto val = std::stoul(rawpwm);
        if (val == 1)
        {
            directPWM = true;
        }
    }

    if (curValue != value)
    {
        // Write target out to sysfs
        try
        {
            if (directPWM)
            {
                _ioAccess->write(
                        (value * 255) / 1000,
                        type::pwm,
                        _id,
                        entry::rawpwm,
                        hwmonio::retries,
                        hwmonio::delay);
            }
            else
            {
                _ioAccess->write(
                        value,
                        _type,
                        _id,
                        entry::target,
                        hwmonio::retries,
                        hwmonio::delay);
            }

        }
        catch (const std::system_error& e)
        {
            using namespace sdbusplus::xyz::openbmc_project::Control::Device::
                Error;
            report<WriteFailure>(
                xyz::openbmc_project::Control::Device::WriteFailure::
                    CALLOUT_ERRNO(e.code().value()),
                xyz::openbmc_project::Control::Device::WriteFailure::
                    CALLOUT_DEVICE_PATH(_devPath.c_str()));

            auto file = sysfs::make_sysfs_path(_ioAccess->path(), _type, _id,
                                               entry::target);

            log<level::INFO>("Logging failing sysfs file",
                             phosphor::logging::entry("FILE=%s", file.c_str()));

            exit(EXIT_FAILURE);
        }
    }

    return FanSpeedObject::target(value);
}

void FanSpeed::enable()
{
    auto enable = env::getEnv("ENABLE", _type, _id);
    if (!enable.empty())
    {
        auto val = std::stoul(enable);

        try
        {
            _ioAccess->write(val, type::pwm, _id, entry::enable,
                             hwmonio::retries, hwmonio::delay);
        }
        catch (const std::system_error& e)
        {
            using namespace sdbusplus::xyz::openbmc_project::Control::Device::
                Error;
            phosphor::logging::report<WriteFailure>(
                xyz::openbmc_project::Control::Device::WriteFailure::
                    CALLOUT_ERRNO(e.code().value()),
                xyz::openbmc_project::Control::Device::WriteFailure::
                    CALLOUT_DEVICE_PATH(_devPath.c_str()));

            auto fullPath = sysfs::make_sysfs_path(_ioAccess->path(), type::pwm,
                                                   _id, entry::enable);

            log<level::INFO>(
                "Logging failing sysfs file",
                phosphor::logging::entry("FILE=%s", fullPath.c_str()));

            exit(EXIT_FAILURE);
        }
    }
}

} // namespace hwmon
OpenPOWER on IntegriCloud