blob: 896830c94974b4b94288bafe27b8da3817aae6a0 [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
Shawn McCarney8c232452021-05-05 18:44:57 -050022#include <sdbusplus/exception.hpp>
23
Bob King717d2da2020-06-02 11:11:15 +080024#include <exception>
25#include <ios>
26#include <sstream>
27
28namespace phosphor::power::regulators
29{
30
31bool PMBusReadSensorAction::execute(ActionEnvironment& environment)
32{
33 try
34 {
Bob King717d2da2020-06-02 11:11:15 +080035 // Get I2C interface to current device
36 i2c::I2CInterface& interface = getI2CInterface(environment);
37
Shawn McCarney8c232452021-05-05 18:44:57 -050038 // Read two byte value of PMBus command code. I2CInterface method reads
39 // low byte first as required by PMBus.
40 uint16_t value{0x00};
Bob King717d2da2020-06-02 11:11:15 +080041 interface.read(command, value);
42
Shawn McCarney8c232452021-05-05 18:44:57 -050043 // Convert two byte PMBus value into a decimal sensor value
44 double sensorValue{0.0};
Bob King717d2da2020-06-02 11:11:15 +080045 switch (format)
46 {
47 case pmbus_utils::SensorDataFormat::linear_11:
Shawn McCarney8c232452021-05-05 18:44:57 -050048 sensorValue = pmbus_utils::convertFromLinear(value);
Bob King717d2da2020-06-02 11:11:15 +080049 break;
50 case pmbus_utils::SensorDataFormat::linear_16:
Shawn McCarney5b819f42021-03-16 14:41:15 -050051 int8_t exponentValue = getExponentValue(environment, interface);
Shawn McCarney8c232452021-05-05 18:44:57 -050052 sensorValue =
53 pmbus_utils::convertFromVoutLinear(value, exponentValue);
Bob King717d2da2020-06-02 11:11:15 +080054 break;
55 }
56
Shawn McCarney8c232452021-05-05 18:44:57 -050057 // Publish sensor value using the Sensors service
58 environment.getServices().getSensors().setValue(type, sensorValue);
Bob King717d2da2020-06-02 11:11:15 +080059 }
60 // Nest the following exception types within an ActionError so the caller
61 // will have both the low level error information and the action information
Shawn McCarney8c232452021-05-05 18:44:57 -050062 catch (const i2c::I2CException& e)
Bob King717d2da2020-06-02 11:11:15 +080063 {
64 std::throw_with_nested(ActionError(*this));
65 }
Shawn McCarney8c232452021-05-05 18:44:57 -050066 catch (const PMBusError& e)
67 {
68 std::throw_with_nested(ActionError(*this));
69 }
70 catch (const sdbusplus::exception_t& e)
Bob King717d2da2020-06-02 11:11:15 +080071 {
72 std::throw_with_nested(ActionError(*this));
73 }
74 return true;
75}
76
77std::string PMBusReadSensorAction::toString() const
78{
79 std::ostringstream ss;
80 ss << "pmbus_read_sensor: { ";
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050081 ss << "type: " << sensors::toString(type) << ", " << std::hex
Bob King717d2da2020-06-02 11:11:15 +080082 << std::uppercase;
83 ss << "command: 0x" << static_cast<uint16_t>(command) << ", " << std::dec
84 << std::nouppercase;
85 ss << "format: " << pmbus_utils::toString(format);
86
87 if (exponent.has_value())
88 {
89 ss << ", exponent: " << static_cast<int16_t>(exponent.value());
90 }
91
92 ss << " }";
93
94 return ss.str();
95}
96
Shawn McCarney5b819f42021-03-16 14:41:15 -050097int8_t PMBusReadSensorAction::getExponentValue(ActionEnvironment& environment,
98 i2c::I2CInterface& interface)
Bob King717d2da2020-06-02 11:11:15 +080099{
100 // Check if an exponent value is defined for this action
101 if (exponent.has_value())
102 {
103 return exponent.value();
104 }
105
106 // Read value of the VOUT_MODE command
107 uint8_t voutModeValue;
108 interface.read(pmbus_utils::VOUT_MODE, voutModeValue);
109
110 // Parse VOUT_MODE value to get data format and parameter value
111 pmbus_utils::VoutDataFormat format;
112 int8_t parameter;
113 pmbus_utils::parseVoutMode(voutModeValue, format, parameter);
114
115 // Verify format is linear; other formats not currently supported
116 if (format != pmbus_utils::VoutDataFormat::linear)
117 {
Shawn McCarney5b819f42021-03-16 14:41:15 -0500118 throw PMBusError("VOUT_MODE contains unsupported data format",
119 environment.getDeviceID(),
120 environment.getDevice().getFRU());
Bob King717d2da2020-06-02 11:11:15 +0800121 }
122
123 // Return parameter value; it contains the exponent when format is linear
124 return parameter;
125}
126
127} // namespace phosphor::power::regulators