| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "pmpowerstate.h" |
| 5 | |
| 6 | #include "core/icommandqueue.h" |
| 7 | #include <algorithm> |
| 8 | #include <utility> |
| 9 | |
| 10 | AMD::PMPowerState::PMPowerState( |
| 11 | std::unique_ptr<IDataSource<std::string>> &&powerDpmStateDataSource) noexcept |
| 12 | : Control(true) |
| 13 | , id_(AMD::PMPowerState::ItemID) |
| 14 | , powerDpmStateDataSource_(std::move(powerDpmStateDataSource)) |
| 15 | , mode_(State::Balanced) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | void AMD::PMPowerState::preInit(ICommandQueue &) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | void AMD::PMPowerState::postInit(ICommandQueue &) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | void AMD::PMPowerState::init() |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | std::string const &AMD::PMPowerState::ID() const |
| 32 | { |
| 33 | return id_; |
| 34 | } |
| 35 | |
| 36 | void AMD::PMPowerState::importControl(IControl::Importer &i) |
| 37 | { |
| 38 | auto &pmPowerStateImporter = dynamic_cast<AMD::PMPowerState::Importer &>(i); |
| 39 | mode(pmPowerStateImporter.providePMPowerStateMode()); |
| 40 | } |
| 41 | |
| 42 | void AMD::PMPowerState::exportControl(IControl::Exporter &e) const |
| 43 | { |
| 44 | auto &pmPowerStateExporter = dynamic_cast<AMD::PMPowerState::Exporter &>(e); |
| 45 | pmPowerStateExporter.takePMPowerStateModes(modes()); |
| 46 | pmPowerStateExporter.takePMPowerStateMode(mode()); |
| 47 | } |
| 48 | |
| 49 | void AMD::PMPowerState::cleanControl(ICommandQueue &) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | void AMD::PMPowerState::syncControl(ICommandQueue &ctlCmds) |
| 54 | { |
| 55 | if (powerDpmStateDataSource_->read(powerDpmStateEntry_)) { |
| 56 | if (powerDpmStateEntry_ != mode()) |
| 57 | ctlCmds.add({powerDpmStateDataSource_->source(), mode()}); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | std::string const &AMD::PMPowerState::mode() const |
| 62 | { |
| 63 | return mode_; |
| 64 | } |
| 65 | |
| 66 | void AMD::PMPowerState::mode(std::string const &mode) |
| 67 | { |
| 68 | // only assign known modes |
| 69 | auto iter = std::find_if( |
| 70 | modes().cbegin(), modes().cend(), |
| 71 | [&](auto const &availableMode) { return mode == availableMode; }); |
| 72 | if (iter != modes().cend()) |
| 73 | mode_ = mode; |
| 74 | } |
| 75 | |
| 76 | std::vector<std::string> const &AMD::PMPowerState::modes() const |
| 77 | { |
| 78 | return modes_; |
| 79 | } |
| 80 | |
| 81 | std::vector<std::string> const AMD::PMPowerState::modes_{ |
| 82 | std::string(AMD::PMPowerState::State::Battery), |
| 83 | std::string(AMD::PMPowerState::State::Balanced), |
| 84 | std::string(AMD::PMPowerState::State::Performance)}; |