| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "fancurve.h" |
| 5 | |
| 6 | #include "common/mathutils.h" |
| 7 | #include "core/components/commonutils.h" |
| 8 | #include "core/icommandqueue.h" |
| 9 | #include <algorithm> |
| 10 | #include <cmath> |
| 11 | #include <iterator> |
| 12 | #include <limits> |
| 13 | |
| 14 | AMD::FanCurve::FanCurve( |
| 15 | std::unique_ptr<IDataSource<unsigned int>> &&pwmEnableDataSource, |
| 16 | std::unique_ptr<IDataSource<unsigned int>> &&pwmDataSource, |
| 17 | std::unique_ptr<IDataSource<int>> &&tempInputDataSource, |
| 18 | units::temperature::celsius_t tempMin, |
| 19 | units::temperature::celsius_t tempMax) noexcept |
| 20 | : Control(false) |
| 21 | , id_(AMD::FanCurve::ItemID) |
| 22 | , pwmEnableDataSource_(std::move(pwmEnableDataSource)) |
| 23 | , pwmDataSource_(std::move(pwmDataSource)) |
| 24 | , tempInputDataSource_(std::move(tempInputDataSource)) |
| 25 | , tempRange_(std::make_pair(tempMin, tempMax)) |
| 26 | , fanStop_(false) |
| 27 | , fanStartValue_(54) |
| 28 | , hysteresis_(5) |
| 29 | , lastPwmValue_(std::numeric_limits<unsigned int>::max()) |
| 30 | , lastFanStop_(false) |
| 31 | { |
| 32 | // default curve |
| 33 | points_.emplace_back(units::temperature::celsius_t(35), |
| 34 | units::make_unit<units::concentration::percent_t>(20)); |
| 35 | points_.emplace_back(units::temperature::celsius_t(52), |
| 36 | units::make_unit<units::concentration::percent_t>(22)); |
| 37 | points_.emplace_back(units::temperature::celsius_t(67), |
| 38 | units::make_unit<units::concentration::percent_t>(30)); |
| 39 | points_.emplace_back(units::temperature::celsius_t(78), |
| 40 | units::make_unit<units::concentration::percent_t>(50)); |
| 41 | points_.emplace_back(units::temperature::celsius_t(85), |
| 42 | units::make_unit<units::concentration::percent_t>(82)); |
| 43 | |
| 44 | Utils::Common::normalizePoints(points_, tempRange_); |
| 45 | |
| 46 | // compute fan start temperature |
| 47 | fanStartTemp_ = evaluatePwm(std::round(fanStartValue_ / 2.55)); |
| 48 | } |
| 49 | |
| 50 | void AMD::FanCurve::preInit(ICommandQueue &) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | void AMD::FanCurve::postInit(ICommandQueue &) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | void AMD::FanCurve::init() |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | std::string const &AMD::FanCurve::ID() const |
| 63 | { |
| 64 | return id_; |
| 65 | } |
| 66 | |
| 67 | void AMD::FanCurve::importControl(IControl::Importer &i) |
| 68 | { |
| 69 | auto &fanCurveImporter = dynamic_cast<AMD::FanCurve::Importer &>(i); |
| 70 | curve(fanCurveImporter.provideFanCurvePoints()); |
| 71 | fanStop(fanCurveImporter.provideFanCurveFanStop()); |
| 72 | fanStartValue(static_cast<unsigned int>(std::round( |
| 73 | fanCurveImporter.provideFanCurveFanStartValue().to<double>() * 255))); |
| 74 | } |
| 75 | |
| 76 | void AMD::FanCurve::exportControl(IControl::Exporter &e) const |
| 77 | { |
| 78 | auto &fanCurveExporter = dynamic_cast<AMD::FanCurve::Exporter &>(e); |
| 79 | fanCurveExporter.takeFanCurveTemperatureRange(tempRange_.first, |
| 80 | tempRange_.second); |
| 81 | fanCurveExporter.takeFanCurvePoints(curve()); |
| 82 | fanCurveExporter.takeFanCurveFanStop(fanStop()); |
| 83 | fanCurveExporter.takeFanCurveFanStartValue(std::round(fanStartValue() / 2.55)); |
| 84 | } |
| 85 | |
| 86 | void AMD::FanCurve::cleanControl(ICommandQueue &) |
| 87 | { |
| 88 | lastPwmValue_ = std::numeric_limits<unsigned int>::max(); |
| 89 | lastFanStop_ = false; |
| 90 | } |
| 91 | |
| 92 | void AMD::FanCurve::syncControl(ICommandQueue &ctlCmds) |
| 93 | { |
| 94 | if (pwmEnableDataSource_->read(pwmEnable_) && pwmDataSource_->read(pwm_) && |
| 95 | tempInputDataSource_->read(tempInput_)) { |
| 96 | |
| 97 | unsigned int pwmValue; |
| 98 | if (fanStop() && |
| 99 | ((pwm_ > 0 && tempInput_ < fanStartTemp_ - hysteresis_) || // stop it |
| 100 | (pwm_ == 0 && tempInput_ < fanStartTemp_))) { // don't start it |
| 101 | pwmValue = 0; |
| 102 | } |
| 103 | else { |
| 104 | if (fanStop() && pwm_ > 0 && tempInput_ < fanStartTemp_) |
| 105 | pwmValue = fanStartValue_; // cap it to fan start value |
| 106 | else |
| 107 | pwmValue = evaluateTemp(units::temperature::celsius_t(tempInput_)); |
| 108 | } |
| 109 | |
| 110 | if (pwmEnable_ != 1) { |
| 111 | ctlCmds.add({pwmEnableDataSource_->source(), "1"}); |
| 112 | ctlCmds.add({pwmDataSource_->source(), std::to_string(pwmValue)}); |
| 113 | } |
| 114 | // NOTE pwm_ must be checked as well. The value that the hardware uses |
| 115 | // doesn't have to match the assigned value through pwm1. |
| 116 | // See: https://bugs.freedesktop.org/show_bug.cgi?id=110213 |
| 117 | else if ((pwm_ != pwmValue && lastPwmValue_ != pwmValue) || |
| 118 | lastFanStop_ != fanStop()) { // force sync |
| 119 | ctlCmds.add({pwmDataSource_->source(), std::to_string(pwmValue)}); |
| 120 | } |
| 121 | |
| 122 | lastPwmValue_ = pwmValue; |
| 123 | lastFanStop_ = fanStop(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | std::vector<AMD::FanCurve::Point> const &AMD::FanCurve::curve() const |
| 128 | { |
| 129 | return points_; |
| 130 | } |
| 131 | |
| 132 | void AMD::FanCurve::curve(std::vector<AMD::FanCurve::Point> const &points) |
| 133 | { |
| 134 | points_ = points; |
| 135 | Utils::Common::normalizePoints(points_, tempRange_); |
| 136 | fanStartTemp_ = evaluatePwm(std::round(fanStartValue_ / 2.55)); |
| 137 | } |
| 138 | |
| 139 | bool AMD::FanCurve::fanStop() const |
| 140 | { |
| 141 | return fanStop_; |
| 142 | } |
| 143 | |
| 144 | void AMD::FanCurve::fanStop(bool enable) |
| 145 | { |
| 146 | fanStop_ = enable; |
| 147 | } |
| 148 | |
| 149 | unsigned int AMD::FanCurve::fanStartValue() const |
| 150 | { |
| 151 | return fanStartValue_; |
| 152 | } |
| 153 | |
| 154 | void AMD::FanCurve::fanStartValue(unsigned int value) |
| 155 | { |
| 156 | fanStartValue_ = static_cast<unsigned int>(std::clamp(value, 0u, 255u)); |
| 157 | fanStartTemp_ = evaluatePwm(std::round(fanStartValue_ / 2.55)); |
| 158 | } |
| 159 | |
| 160 | int AMD::FanCurve::hysteresis() const |
| 161 | { |
| 162 | return hysteresis_; |
| 163 | } |
| 164 | |
| 165 | int AMD::FanCurve::evaluatePwm(units::concentration::percent_t input) const |
| 166 | { |
| 167 | if (points_.size() > 1) { |
| 168 | auto pIt = std::find_if(points_.cbegin(), points_.cend(), |
| 169 | [=](auto const &p) { return p.second > input; }); |
| 170 | |
| 171 | auto &p1 = (pIt == points_.cbegin() |
| 172 | ? *pIt |
| 173 | : (pIt == points_.cend() ? *std::prev(std::prev(pIt)) |
| 174 | : *std::prev(pIt))); |
| 175 | auto &p2 = (pIt == points_.cend() |
| 176 | ? *std::prev(pIt) |
| 177 | : (pIt == points_.cbegin() ? *std::next(pIt) : *pIt)); |
| 178 | |
| 179 | return lerpFromPwm(input, p1, p2); |
| 180 | } |
| 181 | else if (points_.size() == 1) |
| 182 | return static_cast<int>(std::round(points_.front().first.to<double>())); |
| 183 | else |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | int AMD::FanCurve::lerpFromPwm(units::concentration::percent_t input, |
| 188 | AMD::FanCurve::Point const &p1, |
| 189 | AMD::FanCurve::Point const &p2) const |
| 190 | { |
| 191 | input = std::clamp(input, p1.second, p2.second); |
| 192 | return static_cast<int>(std::round(Utils::Math::lerpY( |
| 193 | input.to<double>(), |
| 194 | std::make_pair(p1.first.to<double>(), p1.second.to<double>()), |
| 195 | std::make_pair(p2.first.to<double>(), p2.second.to<double>())))); |
| 196 | } |
| 197 | |
| 198 | unsigned int AMD::FanCurve::evaluateTemp(units::temperature::celsius_t input) const |
| 199 | { |
| 200 | if (points_.size() > 1) { |
| 201 | auto pIt = std::find_if(points_.cbegin(), points_.cend(), |
| 202 | [=](auto const &p) { return p.first > input; }); |
| 203 | |
| 204 | auto &p1 = (pIt == points_.cbegin() |
| 205 | ? *pIt |
| 206 | : (pIt == points_.cend() ? *std::prev(std::prev(pIt)) |
| 207 | : *std::prev(pIt))); |
| 208 | auto &p2 = (pIt == points_.cend() |
| 209 | ? *std::prev(pIt) |
| 210 | : (pIt == points_.cbegin() ? *std::next(pIt) : *pIt)); |
| 211 | |
| 212 | return lerpFromTemp(input, p1, p2); |
| 213 | } |
| 214 | else if (points_.size() == 1) |
| 215 | return static_cast<unsigned int>( |
| 216 | std::round(points_.front().second.to<double>() * 255)); |
| 217 | else |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | unsigned int AMD::FanCurve::lerpFromTemp(units::temperature::celsius_t input, |
| 222 | AMD::FanCurve::Point const &p1, |
| 223 | AMD::FanCurve::Point const &p2) const |
| 224 | { |
| 225 | input = std::clamp(input, p1.first, p2.first); |
| 226 | return static_cast<unsigned int>(std::round( |
| 227 | Utils::Math::lerpX( |
| 228 | input.to<double>(), |
| 229 | std::make_pair(p1.first.to<double>(), p1.second.to<double>()), |
| 230 | std::make_pair(p2.first.to<double>(), p2.second.to<double>())) * |
| 231 | 255)); // scale to [0, 255] range of pwm1 |
| 232 | } |