blob: 38c5d8f191a9138ecbf77485c615ce2cb8324e8a [file] [log] [blame]
Bob Kingd6820bb2020-04-28 15:37:02 +08001/**
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"
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050022#include "sensors.hpp"
Bob Kingd6820bb2020-04-28 15:37:02 +080023
24#include <cstdint>
25#include <optional>
Bob Kingd6820bb2020-04-28 15:37:02 +080026#include <string>
27
28namespace 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 */
47class 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 *
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050061 * @param type Sensor type.
Bob Kingd6820bb2020-04-28 15:37:02 +080062 * @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 */
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050069 explicit PMBusReadSensorAction(SensorType type, uint8_t command,
Bob Kingd6820bb2020-04-28 15:37:02 +080070 pmbus_utils::SensorDataFormat format,
71 std::optional<int8_t> exponent) :
72 type{type},
73 command{command}, format{format}, exponent{exponent}
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000074 {}
Bob Kingd6820bb2020-04-28 15:37:02 +080075
76 /**
77 * Executes this action.
78 *
Bob King717d2da2020-06-02 11:11:15 +080079 * Reads one sensor using the I2C interface.
80 *
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050081 * The sensor type is specified in the constructor.
Bob King717d2da2020-06-02 11:11:15 +080082 *
83 * The PMBus command code is specified in the constructor.
84 * It is the register to read on the device.
85 *
86 * The sensor data format is specified in the constructor. Currently
87 * the linear_11 and linear_16 formats are supported.
88 *
89 * The linear_16 data format requires an exponent value.
90 * If an exponent value was specified in the constructor, that
91 * value will be used. Otherwise the exponent value will be obtained from
92 * the VOUT_MODE command.
93 *
94 * The device is obtained from the ActionEnvironment.
95 *
96 * Throws an exception if an error occurs.
Bob Kingd6820bb2020-04-28 15:37:02 +080097 *
98 * @param environment Action execution environment.
99 * @return true
100 */
Bob King717d2da2020-06-02 11:11:15 +0800101 virtual bool execute(ActionEnvironment& environment) override;
Bob Kingd6820bb2020-04-28 15:37:02 +0800102
103 /**
104 * Returns the PMBus command code.
105 *
106 * @return command
107 */
108 uint8_t getCommand() const
109 {
110 return command;
111 }
112
113 /**
114 * Returns the optional exponent value for linear_16 data format.
115 *
116 * @return optional exponent value
117 */
118 std::optional<int8_t> getExponent() const
119 {
120 return exponent;
121 }
122
123 /**
124 * Returns the data format of the sensor value returned by the device.
125 *
126 * @return data format
127 */
128 pmbus_utils::SensorDataFormat getFormat() const
129 {
130 return format;
131 }
132
133 /**
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500134 * Returns the sensor type.
Bob Kingd6820bb2020-04-28 15:37:02 +0800135 *
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500136 * @return sensor type.
Bob Kingd6820bb2020-04-28 15:37:02 +0800137 */
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500138 SensorType getType() const
Bob Kingd6820bb2020-04-28 15:37:02 +0800139 {
140 return type;
141 }
142
143 /**
144 * Returns a string description of this action.
145 *
146 * @return description of action
147 */
Bob King717d2da2020-06-02 11:11:15 +0800148 virtual std::string toString() const override;
Bob Kingd6820bb2020-04-28 15:37:02 +0800149
150 private:
151 /**
Bob King717d2da2020-06-02 11:11:15 +0800152 * Gets the exponent value to use to convert a linear_16 format value to a
153 * decimal volts value.
154 *
155 * If an exponent value is defined for this action, that value is returned.
156 * Otherwise VOUT_MODE is read from the current device to obtain the
157 * exponent value.
158 *
159 * Throws an exception if an error occurs.
160 *
Shawn McCarney5b819f42021-03-16 14:41:15 -0500161 * @param environment action execution environment
Bob King717d2da2020-06-02 11:11:15 +0800162 * @param interface I2C interface to the current device
163 * @return exponent value
164 */
Shawn McCarney5b819f42021-03-16 14:41:15 -0500165 int8_t getExponentValue(ActionEnvironment& environment,
166 i2c::I2CInterface& interface);
Bob King717d2da2020-06-02 11:11:15 +0800167
168 /**
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500169 * Sensor type.
Bob Kingd6820bb2020-04-28 15:37:02 +0800170 */
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500171 const SensorType type{};
Bob Kingd6820bb2020-04-28 15:37:02 +0800172
173 /**
174 * PMBus command code.
175 */
176 const uint8_t command{};
177
178 /**
179 * Data format of the sensor value returned by the device.
180 */
181 const pmbus_utils::SensorDataFormat format{};
182
183 /**
184 * Optional exponent value for linear_16 data format.
185 */
186 const std::optional<int8_t> exponent{};
187};
188
189} // namespace phosphor::power::regulators