Bob King | 717d2da | 2020-06-02 11:11:15 +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 | |
| 17 | #include "pmbus_read_sensor_action.hpp" |
| 18 | |
| 19 | #include "action_error.hpp" |
| 20 | #include "pmbus_error.hpp" |
| 21 | |
Shawn McCarney | 8c23245 | 2021-05-05 18:44:57 -0500 | [diff] [blame] | 22 | #include <sdbusplus/exception.hpp> |
| 23 | |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 24 | #include <exception> |
| 25 | #include <ios> |
| 26 | #include <sstream> |
| 27 | |
| 28 | namespace phosphor::power::regulators |
| 29 | { |
| 30 | |
| 31 | bool PMBusReadSensorAction::execute(ActionEnvironment& environment) |
| 32 | { |
| 33 | try |
| 34 | { |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 35 | // Get I2C interface to current device |
| 36 | i2c::I2CInterface& interface = getI2CInterface(environment); |
| 37 | |
Shawn McCarney | 8c23245 | 2021-05-05 18:44:57 -0500 | [diff] [blame] | 38 | // 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 King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 41 | interface.read(command, value); |
| 42 | |
Shawn McCarney | 8c23245 | 2021-05-05 18:44:57 -0500 | [diff] [blame] | 43 | // Convert two byte PMBus value into a decimal sensor value |
| 44 | double sensorValue{0.0}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 45 | switch (format) |
| 46 | { |
| 47 | case pmbus_utils::SensorDataFormat::linear_11: |
Shawn McCarney | 8c23245 | 2021-05-05 18:44:57 -0500 | [diff] [blame] | 48 | sensorValue = pmbus_utils::convertFromLinear(value); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 49 | break; |
| 50 | case pmbus_utils::SensorDataFormat::linear_16: |
Shawn McCarney | 5b819f4 | 2021-03-16 14:41:15 -0500 | [diff] [blame] | 51 | int8_t exponentValue = getExponentValue(environment, interface); |
Shawn McCarney | 8c23245 | 2021-05-05 18:44:57 -0500 | [diff] [blame] | 52 | sensorValue = |
| 53 | pmbus_utils::convertFromVoutLinear(value, exponentValue); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 54 | break; |
| 55 | } |
| 56 | |
Shawn McCarney | 8c23245 | 2021-05-05 18:44:57 -0500 | [diff] [blame] | 57 | // Publish sensor value using the Sensors service |
| 58 | environment.getServices().getSensors().setValue(type, sensorValue); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 59 | } |
| 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 McCarney | 8c23245 | 2021-05-05 18:44:57 -0500 | [diff] [blame] | 62 | catch (const i2c::I2CException& e) |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 63 | { |
| 64 | std::throw_with_nested(ActionError(*this)); |
| 65 | } |
Shawn McCarney | 8c23245 | 2021-05-05 18:44:57 -0500 | [diff] [blame] | 66 | catch (const PMBusError& e) |
| 67 | { |
| 68 | std::throw_with_nested(ActionError(*this)); |
| 69 | } |
| 70 | catch (const sdbusplus::exception_t& e) |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 71 | { |
| 72 | std::throw_with_nested(ActionError(*this)); |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | std::string PMBusReadSensorAction::toString() const |
| 78 | { |
| 79 | std::ostringstream ss; |
| 80 | ss << "pmbus_read_sensor: { "; |
Shawn McCarney | 2f9e14f | 2021-04-29 02:45:18 -0500 | [diff] [blame] | 81 | ss << "type: " << sensors::toString(type) << ", " << std::hex |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 82 | << 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 McCarney | 5b819f4 | 2021-03-16 14:41:15 -0500 | [diff] [blame] | 97 | int8_t PMBusReadSensorAction::getExponentValue(ActionEnvironment& environment, |
| 98 | i2c::I2CInterface& interface) |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 99 | { |
| 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 McCarney | 5b819f4 | 2021-03-16 14:41:15 -0500 | [diff] [blame] | 118 | throw PMBusError("VOUT_MODE contains unsupported data format", |
| 119 | environment.getDeviceID(), |
| 120 | environment.getDevice().getFRU()); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Return parameter value; it contains the exponent when format is linear |
| 124 | return parameter; |
| 125 | } |
| 126 | |
| 127 | } // namespace phosphor::power::regulators |