Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include "action_environment.hpp" |
| 19 | #include "i2c_action.hpp" |
| 20 | #include "i2c_interface.hpp" |
| 21 | #include "pmbus_utils.hpp" |
| 22 | |
| 23 | #include <cstdint> |
| 24 | #include <optional> |
| 25 | #include <stdexcept> |
| 26 | #include <string> |
| 27 | |
| 28 | namespace phosphor::power::regulators |
| 29 | { |
| 30 | |
| 31 | /** |
| 32 | * @class PMBusReadSensorAction |
| 33 | * |
| 34 | * Reads one sensor for a PMBus regulator rail. Communicates with the device |
| 35 | * directly using the I2C interface. |
| 36 | * |
| 37 | * Implements the pmbus_read_sensor action in the JSON config file. |
| 38 | * |
| 39 | * Currently supports the linear_11 and linear_16 sensor data formats. |
| 40 | * |
| 41 | * The linear_16 data format requires an exponent value. The exponent value |
| 42 | * can be specified in the constructor. Otherwise the exponent value will be |
| 43 | * obtained from the PMBus VOUT_MODE command. Note that some PMBus devices do |
| 44 | * not support the VOUT_MODE command. The exponent value for a device is often |
| 45 | * found in the device documentation (data sheet). |
| 46 | */ |
| 47 | class PMBusReadSensorAction : public I2CAction |
| 48 | { |
| 49 | public: |
| 50 | // Specify which compiler-generated methods we want |
| 51 | PMBusReadSensorAction() = delete; |
| 52 | PMBusReadSensorAction(const PMBusReadSensorAction&) = delete; |
| 53 | PMBusReadSensorAction(PMBusReadSensorAction&&) = delete; |
| 54 | PMBusReadSensorAction& operator=(const PMBusReadSensorAction&) = delete; |
| 55 | PMBusReadSensorAction& operator=(PMBusReadSensorAction&&) = delete; |
| 56 | virtual ~PMBusReadSensorAction() = default; |
| 57 | |
| 58 | /** |
| 59 | * Constructor. |
| 60 | * |
| 61 | * @param type Sensor value type. |
| 62 | * @param command PMBus command code. |
| 63 | * @param format Data format of the sensor value returned by the device. |
| 64 | * @param exponent Exponent value for linear_16 data format. |
| 65 | * Can be positive or negative. If not specified, the |
| 66 | * exponent value will be read from VOUT_MODE. |
| 67 | * Should not be specified if the data format is linear_11. |
| 68 | */ |
| 69 | explicit PMBusReadSensorAction(pmbus_utils::SensorValueType type, |
| 70 | uint8_t command, |
| 71 | pmbus_utils::SensorDataFormat format, |
| 72 | std::optional<int8_t> exponent) : |
| 73 | type{type}, |
| 74 | command{command}, format{format}, exponent{exponent} |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Executes this action. |
| 80 | * |
| 81 | * TODO: Not implemented yet |
| 82 | * |
| 83 | * @param environment Action execution environment. |
| 84 | * @return true |
| 85 | */ |
| 86 | virtual bool execute(ActionEnvironment& /* environment */) override |
| 87 | { |
| 88 | // TODO: Not implemented yet |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the PMBus command code. |
| 94 | * |
| 95 | * @return command |
| 96 | */ |
| 97 | uint8_t getCommand() const |
| 98 | { |
| 99 | return command; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the optional exponent value for linear_16 data format. |
| 104 | * |
| 105 | * @return optional exponent value |
| 106 | */ |
| 107 | std::optional<int8_t> getExponent() const |
| 108 | { |
| 109 | return exponent; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns the data format of the sensor value returned by the device. |
| 114 | * |
| 115 | * @return data format |
| 116 | */ |
| 117 | pmbus_utils::SensorDataFormat getFormat() const |
| 118 | { |
| 119 | return format; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns the sensor value type. |
| 124 | * |
| 125 | * @return sensor value type. |
| 126 | */ |
| 127 | pmbus_utils::SensorValueType getType() const |
| 128 | { |
| 129 | return type; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns a string description of this action. |
| 134 | * |
| 135 | * @return description of action |
| 136 | */ |
| 137 | virtual std::string toString() const override |
| 138 | { |
| 139 | std::ostringstream ss; |
| 140 | ss << "pmbus_read_sensor: { "; |
| 141 | ss << "type: " << pmbus_utils::toString(type) << ", " << std::hex |
| 142 | << std::uppercase; |
| 143 | ss << "command: 0x" << static_cast<uint16_t>(command) << ", " |
| 144 | << std::dec << std::nouppercase; |
| 145 | ss << "format: " << pmbus_utils::toString(format); |
| 146 | |
| 147 | if (exponent.has_value()) |
| 148 | { |
| 149 | ss << ", exponent: " << static_cast<int16_t>(exponent.value()); |
| 150 | } |
| 151 | |
| 152 | ss << " }"; |
| 153 | |
| 154 | return ss.str(); |
| 155 | } |
| 156 | |
| 157 | private: |
| 158 | /** |
| 159 | * Sensor value type. |
| 160 | */ |
| 161 | const pmbus_utils::SensorValueType type{}; |
| 162 | |
| 163 | /** |
| 164 | * PMBus command code. |
| 165 | */ |
| 166 | const uint8_t command{}; |
| 167 | |
| 168 | /** |
| 169 | * Data format of the sensor value returned by the device. |
| 170 | */ |
| 171 | const pmbus_utils::SensorDataFormat format{}; |
| 172 | |
| 173 | /** |
| 174 | * Optional exponent value for linear_16 data format. |
| 175 | */ |
| 176 | const std::optional<int8_t> exponent{}; |
| 177 | }; |
| 178 | |
| 179 | } // namespace phosphor::power::regulators |