aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hw/i2c/pmbus_device.c18
-rw-r--r--include/hw/i2c/pmbus_device.h20
2 files changed, 37 insertions, 1 deletions
diff --git a/hw/i2c/pmbus_device.c b/hw/i2c/pmbus_device.c
index 8cb9db0f80..62885fa6a1 100644
--- a/hw/i2c/pmbus_device.c
+++ b/hw/i2c/pmbus_device.c
@@ -28,6 +28,24 @@ uint32_t pmbus_direct_mode2data(PMBusCoefficients c, uint16_t value)
return x;
}
+uint16_t pmbus_data2linear_mode(uint16_t value, int exp)
+{
+ /* L = D * 2^(-e) */
+ if (exp < 0) {
+ return value << (-exp);
+ }
+ return value >> exp;
+}
+
+uint16_t pmbus_linear_mode2data(uint16_t value, int exp)
+{
+ /* D = L * 2^e */
+ if (exp < 0) {
+ return value >> (-exp);
+ }
+ return value << exp;
+}
+
void pmbus_send(PMBusDevice *pmdev, const uint8_t *data, uint16_t len)
{
if (pmdev->out_buf_len + len > SMBUS_DATA_MAX_LEN) {
diff --git a/include/hw/i2c/pmbus_device.h b/include/hw/i2c/pmbus_device.h
index bab4526734..0f4d6b3fad 100644
--- a/include/hw/i2c/pmbus_device.h
+++ b/include/hw/i2c/pmbus_device.h
@@ -448,7 +448,7 @@ typedef struct PMBusCoefficients {
*
* Y = (m * x - b) * 10^R
*
- * @return uint32_t
+ * @return uint16_t
*/
uint16_t pmbus_data2direct_mode(PMBusCoefficients c, uint32_t value);
@@ -462,6 +462,24 @@ uint16_t pmbus_data2direct_mode(PMBusCoefficients c, uint32_t value);
uint32_t pmbus_direct_mode2data(PMBusCoefficients c, uint16_t value);
/**
+ * Convert sensor values to linear mode format
+ *
+ * L = D * 2^(-e)
+ *
+ * @return uint16
+ */
+uint16_t pmbus_data2linear_mode(uint16_t value, int exp);
+
+/**
+ * Convert linear mode formatted data into sensor reading
+ *
+ * D = L * 2^e
+ *
+ * @return uint16
+ */
+uint16_t pmbus_linear_mode2data(uint16_t value, int exp);
+
+/**
* @brief Send a block of data over PMBus
* Assumes that the bytes in the block are already ordered correctly,
* also assumes the length has been prepended to the block if necessary