regulators: Add pmbus_write_vout_command action
Implement the pmbus_write_vout_command action in the JSON config file.
See pmbus_write_vout_command.md for more information about this action.
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: Idef8da56d19df8a416efec068ffa9fb9493100cc
diff --git a/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp
new file mode 100644
index 0000000..ae5ac4d
--- /dev/null
+++ b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp
@@ -0,0 +1,167 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "pmbus_write_vout_command_action.hpp"
+
+#include "action_error.hpp"
+#include "pmbus_error.hpp"
+#include "write_verification_error.hpp"
+
+#include <exception>
+#include <ios>
+#include <sstream>
+
+namespace phosphor::power::regulators
+{
+
+bool PMBusWriteVoutCommandAction::execute(ActionEnvironment& environment)
+{
+ try
+ {
+ // Get volts value
+ double voltsValue = getVoltsValue(environment);
+
+ // Get I2C interface to current device
+ i2c::I2CInterface& interface = getI2CInterface(environment);
+
+ // Get exponent value for converting volts value to linear format
+ int8_t exponentValue = getExponentValue(interface);
+
+ // Convert volts value to linear data format
+ uint16_t linearValue =
+ pmbus_utils::convertToVoutLinear(voltsValue, exponentValue);
+
+ // Write linear format value to VOUT_COMMAND. I2CInterface method
+ // writes low-order byte first as required by PMBus.
+ interface.write(pmbus_utils::VOUT_COMMAND, linearValue);
+
+ // Verify write if necessary
+ if (isWriteVerified)
+ {
+ verifyWrite(environment, interface, linearValue);
+ }
+ }
+ // Nest the following exception types within an ActionError so the caller
+ // will have both the low level error information and the action information
+ catch (const i2c::I2CException& i2cError)
+ {
+ std::throw_with_nested(ActionError(*this));
+ }
+ catch (const PMBusError& pmbusError)
+ {
+ std::throw_with_nested(ActionError(*this));
+ }
+ catch (const WriteVerificationError& verifyError)
+ {
+ std::throw_with_nested(ActionError(*this));
+ }
+ return true;
+}
+
+std::string PMBusWriteVoutCommandAction::toString() const
+{
+ std::ostringstream ss;
+ ss << "pmbus_write_vout_command: { ";
+
+ if (volts.has_value())
+ {
+ ss << "volts: " << volts.value() << ", ";
+ }
+
+ // Only linear format is currently supported
+ ss << "format: linear";
+
+ if (exponent.has_value())
+ {
+ ss << ", exponent: " << static_cast<int16_t>(exponent.value());
+ }
+
+ ss << ", is_verified: " << std::boolalpha << isWriteVerified << " }";
+ return ss.str();
+}
+
+int8_t
+ PMBusWriteVoutCommandAction::getExponentValue(i2c::I2CInterface& interface)
+{
+ // Check if an exponent value is defined for this action
+ if (exponent.has_value())
+ {
+ return exponent.value();
+ }
+
+ // Read value of the VOUT_MODE command
+ uint8_t voutModeValue;
+ interface.read(pmbus_utils::VOUT_MODE, voutModeValue);
+
+ // Parse VOUT_MODE value to get data format and parameter value
+ pmbus_utils::VoutDataFormat format;
+ int8_t parameter;
+ pmbus_utils::parseVoutMode(voutModeValue, format, parameter);
+
+ // Verify format is linear; other formats not currently supported
+ if (format != pmbus_utils::VoutDataFormat::linear)
+ {
+ throw PMBusError("VOUT_MODE contains unsupported data format");
+ }
+
+ // Return parameter value; it contains the exponent when format is linear
+ return parameter;
+}
+
+double
+ PMBusWriteVoutCommandAction::getVoltsValue(ActionEnvironment& environment)
+{
+ double voltsValue;
+
+ if (volts.has_value())
+ {
+ // A volts value is defined for this action
+ voltsValue = volts.value();
+ }
+ else if (environment.hasVolts())
+ {
+ // A volts value is defined in the ActionEnvironment
+ voltsValue = environment.getVolts();
+ }
+ else
+ {
+ throw ActionError(*this, "No volts value defined");
+ }
+
+ return voltsValue;
+}
+
+void PMBusWriteVoutCommandAction::verifyWrite(ActionEnvironment& environment,
+ i2c::I2CInterface& interface,
+ uint16_t valueWritten)
+{
+ // Read current value of VOUT_COMMAND. I2CInterface method reads low byte
+ // first as required by PMBus.
+ uint16_t valueRead{0x00};
+ interface.read(pmbus_utils::VOUT_COMMAND, valueRead);
+
+ // Verify value read equals value written
+ if (valueRead != valueWritten)
+ {
+ std::ostringstream ss;
+ ss << "device: " << environment.getDeviceID()
+ << ", register: VOUT_COMMAND, value_written: 0x" << std::hex
+ << std::uppercase << valueWritten << ", value_read: 0x" << valueRead;
+ throw WriteVerificationError(ss.str());
+ }
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/actions/pmbus_write_vout_command_action.hpp b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.hpp
new file mode 100644
index 0000000..4ed1e10
--- /dev/null
+++ b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.hpp
@@ -0,0 +1,248 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "action_environment.hpp"
+#include "i2c_action.hpp"
+#include "i2c_interface.hpp"
+#include "pmbus_utils.hpp"
+
+#include <cstdint>
+#include <optional>
+#include <stdexcept>
+#include <string>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class PMBusWriteVoutCommandAction
+ *
+ * Writes the value of VOUT_COMMAND to set the output voltage of a PMBus
+ * regulator rail. Communicates with the device directly using the I2C
+ * interface.
+ *
+ * Implements the pmbus_write_vout_command action in the JSON config file.
+ *
+ * The volts value to write can be specified in the constructor. Otherwise, the
+ * volts value will be obtained from the ActionEnvironment.
+ *
+ * The PMBus specification defines four data formats for the value of
+ * VOUT_COMMAND:
+ * - Linear
+ * - VID
+ * - Direct
+ * - IEEE Half-Precision Floating Point
+ * Currently only the linear data format is supported. The volts value is
+ * converted into linear format before being written.
+ *
+ * The linear data format requires an exponent value. The exponent value can be
+ * specified in the constructor. Otherwise the exponent value will be obtained
+ * from the PMBus VOUT_MODE command. Note that some PMBus devices do not
+ * support the VOUT_MODE command. The exponent value for a device is often
+ * found in the device documentation (data sheet).
+ *
+ * If desired, write verification can be performed. The value of VOUT_COMMAND
+ * will be read from the device after it is written to ensure that it contains
+ * the expected value. If VOUT_COMMAND contains an unexpected value, a
+ * WriteVerificationError is thrown. To perform verification, the device must
+ * return all 16 bits of voltage data that were written to VOUT_COMMAND.
+ */
+class PMBusWriteVoutCommandAction : public I2CAction
+{
+ public:
+ // Specify which compiler-generated methods we want
+ PMBusWriteVoutCommandAction() = delete;
+ PMBusWriteVoutCommandAction(const PMBusWriteVoutCommandAction&) = delete;
+ PMBusWriteVoutCommandAction(PMBusWriteVoutCommandAction&&) = delete;
+ PMBusWriteVoutCommandAction&
+ operator=(const PMBusWriteVoutCommandAction&) = delete;
+ PMBusWriteVoutCommandAction&
+ operator=(PMBusWriteVoutCommandAction&&) = delete;
+ virtual ~PMBusWriteVoutCommandAction() = default;
+
+ /**
+ * Constructor.
+ *
+ * Throws an exception if any of the input parameters are invalid.
+ *
+ * @param volts Optional volts value to write to VOUT_COMMAND.
+ * @param format Data format of the volts value written to VOUT_COMMAND.
+ * Currently the only supported format is linear.
+ * @param exponent Optional exponent to use to convert the volts value to
+ * linear data format.
+ * @param isVerified Specifies whether the updated value of VOUT_COMMAND is
+ * verified by reading it from the device.
+ */
+ explicit PMBusWriteVoutCommandAction(std::optional<double> volts,
+ pmbus_utils::VoutDataFormat format,
+ std::optional<int8_t> exponent,
+ bool isVerified) :
+ volts{volts},
+ format{format}, exponent{exponent}, isWriteVerified{isVerified}
+ {
+ // Currently only linear format is supported
+ if (format != pmbus_utils::VoutDataFormat::linear)
+ {
+ throw std::invalid_argument{"Unsupported data format specified"};
+ }
+ }
+
+ /**
+ * Executes this action.
+ *
+ * Writes a volts value to VOUT_COMMAND using the I2C interface.
+ *
+ * If a volts value was specified in the constructor, that value will be
+ * used. Otherwise the volts value will be obtained from the
+ * ActionEnvironment.
+ *
+ * The data format is specified in the constructor. Currently only the
+ * linear format is supported.
+ *
+ * An exponent value is required to convert the volts value to linear
+ * format. If an exponent value was specified in the constructor, that
+ * value will be used. Otherwise the exponent value will be obtained from
+ * the VOUT_MODE command.
+ *
+ * Write verification will be performed if specified in the constructor.
+ *
+ * The device is obtained from the ActionEnvironment.
+ *
+ * Throws an exception if an error occurs.
+ *
+ * @param environment action execution environment
+ * @return true
+ */
+ virtual bool execute(ActionEnvironment& environment) override;
+
+ /**
+ * Returns the optional exponent value used to convert the volts value to
+ * linear data format.
+ *
+ * @return optional exponent value
+ */
+ std::optional<int8_t> getExponent() const
+ {
+ return exponent;
+ }
+
+ /**
+ * Returns the data format of the value written to VOUT_COMMAND.
+ *
+ * @return data format
+ */
+ pmbus_utils::VoutDataFormat getFormat() const
+ {
+ return format;
+ }
+
+ /**
+ * Returns the optional volts value to write to VOUT_COMMAND.
+ *
+ * @return optional volts value
+ */
+ std::optional<double> getVolts() const
+ {
+ return volts;
+ }
+
+ /**
+ * Returns whether write verification will be performed when writing to
+ * VOUT_COMMAND.
+ *
+ * @return true if write verification will be performed, false otherwise
+ */
+ bool isVerified() const
+ {
+ return isWriteVerified;
+ }
+
+ /**
+ * Returns a string description of this action.
+ *
+ * @return description of action
+ */
+ virtual std::string toString() const override;
+
+ private:
+ /**
+ * Gets the exponent value to use to convert the volts value to linear data
+ * format.
+ *
+ * If an exponent value is defined for this action, that value is returned.
+ * Otherwise VOUT_MODE is read from the current device to obtain the
+ * exponent value.
+ *
+ * Throws an exception if an error occurs.
+ *
+ * @param interface I2C interface to the current device
+ * @return exponent value
+ */
+ int8_t getExponentValue(i2c::I2CInterface& interface);
+
+ /**
+ * Gets the volts value to write to VOUT_COMMAND.
+ *
+ * If a volts value is defined for this action, that value is returned.
+ * Otherwise the volts value is obtained from the specified
+ * ActionEnvironment.
+ *
+ * Throws an exception if no volts value is defined.
+ *
+ * @param environment action execution environment
+ * @return volts value
+ */
+ double getVoltsValue(ActionEnvironment& environment);
+
+ /**
+ * Verifies the value written to VOUT_COMMAND. Reads the current value of
+ * VOUT_COMMAND and ensures that it matches the value written.
+ *
+ * Throws an exception if the values do not match or a communication error
+ * occurs.
+ *
+ * @param environment action execution environment
+ * @param interface I2C interface to the current device
+ * @param valueWritten linear format volts value written to VOUT_COMMAND
+ */
+ void verifyWrite(ActionEnvironment& environment,
+ i2c::I2CInterface& interface, uint16_t valueWritten);
+
+ /**
+ * Optional volts value to write.
+ */
+ const std::optional<double> volts{};
+
+ /**
+ * Data format of the volts value written to VOUT_COMMAND.
+ */
+ const pmbus_utils::VoutDataFormat format{};
+
+ /**
+ * Optional exponent value to use to convert the volts value to linear data
+ * format.
+ */
+ const std::optional<int8_t> exponent{};
+
+ /**
+ * Indicates whether write verification will be performed when writing to
+ * VOUT_COMMAND.
+ */
+ const bool isWriteVerified{false};
+};
+
+} // namespace phosphor::power::regulators