blob: f8a15d7fdead7130219291105a8a112d1073cebb [file] [log] [blame]
Bob King717d2da2020-06-02 11:11:15 +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
17#include "pmbus_read_sensor_action.hpp"
18
19#include "action_error.hpp"
20#include "pmbus_error.hpp"
21
22#include <exception>
23#include <ios>
24#include <sstream>
25
26namespace phosphor::power::regulators
27{
28
29bool PMBusReadSensorAction::execute(ActionEnvironment& environment)
30{
31 try
32 {
33 pmbus_utils::SensorReading reading{};
34 reading.type = type;
35
36 // Get I2C interface to current device
37 i2c::I2CInterface& interface = getI2CInterface(environment);
38
39 // Read value of the PMBus command code
40 uint16_t value;
41 interface.read(command, value);
42
43 switch (format)
44 {
45 case pmbus_utils::SensorDataFormat::linear_11:
46 // Convert linear_11 format to a normal decimal number
47 reading.value = pmbus_utils::convertFromLinear(value);
48 break;
49 case pmbus_utils::SensorDataFormat::linear_16:
50 // Get exponent value for converting linear format to volts
Shawn McCarney5b819f42021-03-16 14:41:15 -050051 int8_t exponentValue = getExponentValue(environment, interface);
Bob King717d2da2020-06-02 11:11:15 +080052
53 // Convert linear_16 format to a normal decimal number
54 reading.value =
55 pmbus_utils::convertFromVoutLinear(value, exponentValue);
56 break;
57 }
58
59 environment.addSensorReading(reading);
60 }
61 // Nest the following exception types within an ActionError so the caller
62 // will have both the low level error information and the action information
63 catch (const i2c::I2CException& i2cError)
64 {
65 std::throw_with_nested(ActionError(*this));
66 }
67 catch (const PMBusError& pmbusError)
68 {
69 std::throw_with_nested(ActionError(*this));
70 }
71 return true;
72}
73
74std::string PMBusReadSensorAction::toString() const
75{
76 std::ostringstream ss;
77 ss << "pmbus_read_sensor: { ";
78 ss << "type: " << pmbus_utils::toString(type) << ", " << std::hex
79 << std::uppercase;
80 ss << "command: 0x" << static_cast<uint16_t>(command) << ", " << std::dec
81 << std::nouppercase;
82 ss << "format: " << pmbus_utils::toString(format);
83
84 if (exponent.has_value())
85 {
86 ss << ", exponent: " << static_cast<int16_t>(exponent.value());
87 }
88
89 ss << " }";
90
91 return ss.str();
92}
93
Shawn McCarney5b819f42021-03-16 14:41:15 -050094int8_t PMBusReadSensorAction::getExponentValue(ActionEnvironment& environment,
95 i2c::I2CInterface& interface)
Bob King717d2da2020-06-02 11:11:15 +080096{
97 // Check if an exponent value is defined for this action
98 if (exponent.has_value())
99 {
100 return exponent.value();
101 }
102
103 // Read value of the VOUT_MODE command
104 uint8_t voutModeValue;
105 interface.read(pmbus_utils::VOUT_MODE, voutModeValue);
106
107 // Parse VOUT_MODE value to get data format and parameter value
108 pmbus_utils::VoutDataFormat format;
109 int8_t parameter;
110 pmbus_utils::parseVoutMode(voutModeValue, format, parameter);
111
112 // Verify format is linear; other formats not currently supported
113 if (format != pmbus_utils::VoutDataFormat::linear)
114 {
Shawn McCarney5b819f42021-03-16 14:41:15 -0500115 throw PMBusError("VOUT_MODE contains unsupported data format",
116 environment.getDeviceID(),
117 environment.getDevice().getFRU());
Bob King717d2da2020-06-02 11:11:15 +0800118 }
119
120 // Return parameter value; it contains the exponent when format is linear
121 return parameter;
122}
123
124} // namespace phosphor::power::regulators