| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include "core/components/controls/control.h" |
| 7 | #include "core/idatasource.h" |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | #include <string_view> |
| 11 | #include <units.h> |
| 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
| 15 | namespace AMD { |
| 16 | |
| 17 | class FanCurve : public Control |
| 18 | { |
| 19 | public: |
| 20 | static constexpr std::string_view ItemID{"AMD_FAN_CURVE"}; |
| 21 | |
| 22 | using Point = |
| 23 | std::pair<units::temperature::celsius_t, units::concentration::percent_t>; |
| 24 | |
| 25 | class Importer : public IControl::Importer |
| 26 | { |
| 27 | public: |
| 28 | virtual std::vector<FanCurve::Point> const &provideFanCurvePoints() const = 0; |
| 29 | virtual bool provideFanCurveFanStop() const = 0; |
| 30 | virtual units::concentration::percent_t |
| 31 | provideFanCurveFanStartValue() const = 0; |
| 32 | }; |
| 33 | |
| 34 | class Exporter : public IControl::Exporter |
| 35 | { |
| 36 | public: |
| 37 | virtual void |
| 38 | takeFanCurvePoints(std::vector<FanCurve::Point> const &points) = 0; |
| 39 | virtual void takeFanCurveFanStop(bool enabled) = 0; |
| 40 | virtual void |
| 41 | takeFanCurveFanStartValue(units::concentration::percent_t value) = 0; |
| 42 | virtual void |
| 43 | takeFanCurveTemperatureRange(units::temperature::celsius_t min, |
| 44 | units::temperature::celsius_t max) = 0; |
| 45 | }; |
| 46 | |
| 47 | FanCurve(std::unique_ptr<IDataSource<unsigned int>> &&pwmEnableDataSource, |
| 48 | std::unique_ptr<IDataSource<unsigned int>> &&pwmDataSource, |
| 49 | std::unique_ptr<IDataSource<int>> &&tempInputDataSource, |
| 50 | units::temperature::celsius_t tempMin, |
| 51 | units::temperature::celsius_t tempMax) noexcept; |
| 52 | |
| 53 | void preInit(ICommandQueue &ctlCmds) final override; |
| 54 | void postInit(ICommandQueue &ctlCmds) final override; |
| 55 | void init() final override; |
| 56 | |
| 57 | std::string const &ID() const final override; |
| 58 | |
| 59 | protected: |
| 60 | void importControl(IControl::Importer &i) final override; |
| 61 | void exportControl(IControl::Exporter &e) const final override; |
| 62 | |
| 63 | void cleanControl(ICommandQueue &ctlCmds) final override; |
| 64 | void syncControl(ICommandQueue &ctlCmds) final override; |
| 65 | |
| 66 | std::vector<FanCurve::Point> const &curve() const; |
| 67 | void curve(std::vector<FanCurve::Point> const &points); |
| 68 | |
| 69 | bool fanStop() const; |
| 70 | void fanStop(bool active); |
| 71 | |
| 72 | unsigned int fanStartValue() const; |
| 73 | void fanStartValue(unsigned int value); |
| 74 | |
| 75 | int hysteresis() const; |
| 76 | |
| 77 | int evaluatePwm(units::concentration::percent_t input) const; |
| 78 | int lerpFromPwm(units::concentration::percent_t input, |
| 79 | FanCurve::Point const &p1, FanCurve::Point const &p2) const; |
| 80 | |
| 81 | unsigned int evaluateTemp(units::temperature::celsius_t input) const; |
| 82 | unsigned int lerpFromTemp(units::temperature::celsius_t input, |
| 83 | FanCurve::Point const &p1, |
| 84 | FanCurve::Point const &p2) const; |
| 85 | |
| 86 | private: |
| 87 | std::string const id_; |
| 88 | |
| 89 | std::unique_ptr<IDataSource<unsigned int>> const pwmEnableDataSource_; |
| 90 | std::unique_ptr<IDataSource<unsigned int>> const pwmDataSource_; |
| 91 | std::unique_ptr<IDataSource<int>> const tempInputDataSource_; |
| 92 | std::pair<units::temperature::celsius_t, units::temperature::celsius_t> const |
| 93 | tempRange_; |
| 94 | |
| 95 | unsigned int pwmEnable_; |
| 96 | unsigned int pwm_; |
| 97 | int tempInput_; |
| 98 | |
| 99 | bool fanStop_; |
| 100 | unsigned int fanStartValue_; |
| 101 | int fanStartTemp_; |
| 102 | int const hysteresis_; |
| 103 | |
| 104 | unsigned int lastPwmValue_; |
| 105 | bool lastFanStop_; |
| 106 | |
| 107 | std::vector<FanCurve::Point> points_; |
| 108 | }; |
| 109 | |
| 110 | } // namespace AMD |