| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "fanfixed.h" |
| 5 | |
| 6 | #include "core/icommandqueue.h" |
| 7 | #include <algorithm> |
| 8 | #include <cmath> |
| 9 | #include <limits> |
| 10 | #include <utility> |
| 11 | |
| 12 | AMD::FanFixed::FanFixed( |
| 13 | std::unique_ptr<IDataSource<unsigned int>> &&pwmEnableDataSource, |
| 14 | std::unique_ptr<IDataSource<unsigned int>> &&pwmDataSource) noexcept |
| 15 | : Control(false) |
| 16 | , id_(AMD::FanFixed::ItemID) |
| 17 | , pwmEnableDataSource_(std::move(pwmEnableDataSource)) |
| 18 | , pwmDataSource_(std::move(pwmDataSource)) |
| 19 | , value_(64) |
| 20 | , lastValue_(std::numeric_limits<unsigned int>::max()) |
| 21 | , fanStop_(false) |
| 22 | , fanStartValue_(54) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | void AMD::FanFixed::preInit(ICommandQueue &) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | void AMD::FanFixed::postInit(ICommandQueue &) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | void AMD::FanFixed::init() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | std::string const &AMD::FanFixed::ID() const |
| 39 | { |
| 40 | return id_; |
| 41 | } |
| 42 | |
| 43 | void AMD::FanFixed::importControl(IControl::Importer &i) |
| 44 | { |
| 45 | auto &fanFixedImporter = dynamic_cast<AMD::FanFixed::Importer &>(i); |
| 46 | value(static_cast<unsigned int>( |
| 47 | std::round(fanFixedImporter.provideFanFixedValue().to<double>() * 255))); |
| 48 | fanStop(fanFixedImporter.provideFanFixedFanStop()); |
| 49 | fanStartValue(static_cast<unsigned int>(std::round( |
| 50 | fanFixedImporter.provideFanFixedFanStartValue().to<double>() * 255))); |
| 51 | } |
| 52 | |
| 53 | void AMD::FanFixed::exportControl(IControl::Exporter &e) const |
| 54 | { |
| 55 | auto &fanFixedExporter = dynamic_cast<AMD::FanFixed::Exporter &>(e); |
| 56 | fanFixedExporter.takeFanFixedValue(std::round(value() / 2.55)); |
| 57 | fanFixedExporter.takeFanFixedFanStop(fanStop()); |
| 58 | fanFixedExporter.takeFanFixedFanStartValue(std::round(fanStartValue() / 2.55)); |
| 59 | } |
| 60 | |
| 61 | void AMD::FanFixed::cleanControl(ICommandQueue &) |
| 62 | { |
| 63 | lastValue_ = std::numeric_limits<unsigned int>::max(); |
| 64 | } |
| 65 | |
| 66 | void AMD::FanFixed::syncControl(ICommandQueue &ctlCmds) |
| 67 | { |
| 68 | if (pwmEnableDataSource_->read(pwmEnable_) && pwmDataSource_->read(pwm_)) { |
| 69 | auto pwmValue = (fanStop() && value() < fanStartValue()) ? 0 : value(); |
| 70 | if (pwmEnable_ != 1) { |
| 71 | ctlCmds.add({pwmEnableDataSource_->source(), "1"}); |
| 72 | ctlCmds.add({pwmDataSource_->source(), std::to_string(pwmValue)}); |
| 73 | } |
| 74 | // NOTE pwm_ must be checked as well. The value that the hardware uses |
| 75 | // doesn't have to match the assigned value through pwm1. |
| 76 | // See: https://bugs.freedesktop.org/show_bug.cgi?id=110213 |
| 77 | else if (pwm_ != pwmValue && lastValue_ != pwmValue) { |
| 78 | ctlCmds.add({pwmDataSource_->source(), std::to_string(pwmValue)}); |
| 79 | } |
| 80 | lastValue_ = pwmValue; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | unsigned int AMD::FanFixed::value() const |
| 85 | { |
| 86 | return value_; |
| 87 | } |
| 88 | |
| 89 | void AMD::FanFixed::value(unsigned int value) |
| 90 | { |
| 91 | value_ = static_cast<unsigned int>(std::clamp(value, 0u, 255u)); |
| 92 | } |
| 93 | |
| 94 | bool AMD::FanFixed::fanStop() const |
| 95 | { |
| 96 | return fanStop_; |
| 97 | } |
| 98 | |
| 99 | void AMD::FanFixed::fanStop(bool enable) |
| 100 | { |
| 101 | fanStop_ = enable; |
| 102 | } |
| 103 | |
| 104 | unsigned int AMD::FanFixed::fanStartValue() const |
| 105 | { |
| 106 | return fanStartValue_; |
| 107 | } |
| 108 | |
| 109 | void AMD::FanFixed::fanStartValue(unsigned int value) |
| 110 | { |
| 111 | fanStartValue_ = static_cast<unsigned int>(std::clamp(value, 0u, 255u)); |
| 112 | } |