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 | |
| 22 | #include <exception> |
| 23 | #include <ios> |
| 24 | #include <sstream> |
| 25 | |
| 26 | namespace phosphor::power::regulators |
| 27 | { |
| 28 | |
| 29 | bool PMBusReadSensorAction::execute(ActionEnvironment& environment) |
| 30 | { |
| 31 | try |
| 32 | { |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 33 | // Get I2C interface to current device |
| 34 | i2c::I2CInterface& interface = getI2CInterface(environment); |
| 35 | |
| 36 | // Read value of the PMBus command code |
| 37 | uint16_t value; |
| 38 | interface.read(command, value); |
| 39 | |
Shawn McCarney | 5135df6 | 2021-04-28 15:53:24 -0500 | [diff] [blame] | 40 | // Convert PMBus 2-byte value to a double |
| 41 | // TODO: Store result of conversion in a double. Not doing it yet |
| 42 | // because it results in an unused variable compile error during CI. |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 43 | switch (format) |
| 44 | { |
| 45 | case pmbus_utils::SensorDataFormat::linear_11: |
| 46 | // Convert linear_11 format to a normal decimal number |
Shawn McCarney | 5135df6 | 2021-04-28 15:53:24 -0500 | [diff] [blame] | 47 | pmbus_utils::convertFromLinear(value); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 48 | break; |
| 49 | case pmbus_utils::SensorDataFormat::linear_16: |
| 50 | // Get exponent value for converting linear format to volts |
Shawn McCarney | 5b819f4 | 2021-03-16 14:41:15 -0500 | [diff] [blame] | 51 | int8_t exponentValue = getExponentValue(environment, interface); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 52 | |
| 53 | // Convert linear_16 format to a normal decimal number |
Shawn McCarney | 5135df6 | 2021-04-28 15:53:24 -0500 | [diff] [blame] | 54 | pmbus_utils::convertFromVoutLinear(value, exponentValue); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 55 | break; |
| 56 | } |
| 57 | |
Shawn McCarney | 5135df6 | 2021-04-28 15:53:24 -0500 | [diff] [blame] | 58 | // TODO: Publish sensor value using Sensors service |
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 |
| 62 | catch (const i2c::I2CException& i2cError) |
| 63 | { |
| 64 | std::throw_with_nested(ActionError(*this)); |
| 65 | } |
| 66 | catch (const PMBusError& pmbusError) |
| 67 | { |
| 68 | std::throw_with_nested(ActionError(*this)); |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | std::string PMBusReadSensorAction::toString() const |
| 74 | { |
| 75 | std::ostringstream ss; |
| 76 | ss << "pmbus_read_sensor: { "; |
Shawn McCarney | 2f9e14f | 2021-04-29 02:45:18 -0500 | [diff] [blame] | 77 | ss << "type: " << sensors::toString(type) << ", " << std::hex |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 78 | << std::uppercase; |
| 79 | ss << "command: 0x" << static_cast<uint16_t>(command) << ", " << std::dec |
| 80 | << std::nouppercase; |
| 81 | ss << "format: " << pmbus_utils::toString(format); |
| 82 | |
| 83 | if (exponent.has_value()) |
| 84 | { |
| 85 | ss << ", exponent: " << static_cast<int16_t>(exponent.value()); |
| 86 | } |
| 87 | |
| 88 | ss << " }"; |
| 89 | |
| 90 | return ss.str(); |
| 91 | } |
| 92 | |
Shawn McCarney | 5b819f4 | 2021-03-16 14:41:15 -0500 | [diff] [blame] | 93 | int8_t PMBusReadSensorAction::getExponentValue(ActionEnvironment& environment, |
| 94 | i2c::I2CInterface& interface) |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 95 | { |
| 96 | // Check if an exponent value is defined for this action |
| 97 | if (exponent.has_value()) |
| 98 | { |
| 99 | return exponent.value(); |
| 100 | } |
| 101 | |
| 102 | // Read value of the VOUT_MODE command |
| 103 | uint8_t voutModeValue; |
| 104 | interface.read(pmbus_utils::VOUT_MODE, voutModeValue); |
| 105 | |
| 106 | // Parse VOUT_MODE value to get data format and parameter value |
| 107 | pmbus_utils::VoutDataFormat format; |
| 108 | int8_t parameter; |
| 109 | pmbus_utils::parseVoutMode(voutModeValue, format, parameter); |
| 110 | |
| 111 | // Verify format is linear; other formats not currently supported |
| 112 | if (format != pmbus_utils::VoutDataFormat::linear) |
| 113 | { |
Shawn McCarney | 5b819f4 | 2021-03-16 14:41:15 -0500 | [diff] [blame] | 114 | throw PMBusError("VOUT_MODE contains unsupported data format", |
| 115 | environment.getDeviceID(), |
| 116 | environment.getDevice().getFRU()); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Return parameter value; it contains the exponent when format is linear |
| 120 | return parameter; |
| 121 | } |
| 122 | |
| 123 | } // namespace phosphor::power::regulators |