diff mbox series

[PULL,1/2] hw/sensor: enable setting adm1272 temperature with qmp

Message ID 20231207014148.3116889-2-minyard@acm.org (mailing list archive)
State New, archived
Headers show
Series Pull request for some I2C changes | expand

Commit Message

Corey Minyard Dec. 7, 2023, 1:41 a.m. UTC
From: Titus Rwantare <titusr@google.com>

Reviewed-by: Patrick Venture <venture@google.com>
Reviewed-by: Chris Rauer <crauer@google.com>
Reviewed-by: Hao Wu <wuhaotsh@google.com>
Signed-off-by: Titus Rwantare <titusr@google.com>
Message-Id: <20220106173814.3580141-1-venture@google.com>
Signed-off-by: Corey Minyard <cminyard@mvista.com>
---
 hw/sensor/adm1272.c        | 27 ++++++++++++++++++++++++++-
 tests/qtest/adm1272-test.c | 28 +++++++++++++++++++++++++++-
 2 files changed, 53 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/hw/sensor/adm1272.c b/hw/sensor/adm1272.c
index 8f4a1c2cd4..6e5ae6e63b 100644
--- a/hw/sensor/adm1272.c
+++ b/hw/sensor/adm1272.c
@@ -65,6 +65,7 @@ 
 #define ADM1272_VOLTAGE_COEFF_DEFAULT   1
 #define ADM1272_CURRENT_COEFF_DEFAULT   3
 #define ADM1272_PWR_COEFF_DEFAULT       7
+#define ADM1272_TEMP_COEFF_DEFAULT      8
 #define ADM1272_IOUT_OFFSET             0x5000
 #define ADM1272_IOUT_OFFSET             0x5000
 
@@ -185,6 +186,22 @@  static uint32_t adm1272_direct_to_watts(uint16_t value)
     return pmbus_direct_mode2data(c, value);
 }
 
+static uint16_t adm1272_millidegrees_to_direct(uint32_t value)
+{
+    PMBusCoefficients c = adm1272_coefficients[ADM1272_TEMP_COEFF_DEFAULT];
+    c.b = c.b * 1000;
+    c.R = c.R - 3;
+    return pmbus_data2direct_mode(c, value);
+}
+
+static uint32_t adm1272_direct_to_millidegrees(uint16_t value)
+{
+    PMBusCoefficients c = adm1272_coefficients[ADM1272_TEMP_COEFF_DEFAULT];
+    c.b = c.b * 1000;
+    c.R = c.R - 3;
+    return pmbus_direct_mode2data(c, value);
+}
+
 static void adm1272_exit_reset(Object *obj)
 {
     ADM1272State *s = ADM1272(obj);
@@ -219,7 +236,7 @@  static void adm1272_exit_reset(Object *obj)
         = adm1272_millivolts_to_direct(ADM1272_VOLT_DEFAULT);
     pmdev->pages[0].read_iout
         = adm1272_milliamps_to_direct(ADM1272_IOUT_DEFAULT);
-    pmdev->pages[0].read_temperature_1 = 0;
+    pmdev->pages[0].read_temperature_1 = adm1272_millidegrees_to_direct(30000);
     pmdev->pages[0].read_pin = adm1272_watts_to_direct(ADM1272_PWR_DEFAULT);
     pmdev->pages[0].revision = ADM1272_PMBUS_REVISION_DEFAULT;
     pmdev->pages[0].mfr_id = ADM1272_MFR_ID_DEFAULT;
@@ -422,6 +439,8 @@  static void adm1272_get(Object *obj, Visitor *v, const char *name, void *opaque,
         value = adm1272_direct_to_milliamps(*(uint16_t *)opaque);
     } else if (strcmp(name, "pin") == 0) {
         value = adm1272_direct_to_watts(*(uint16_t *)opaque);
+    } else if (strcmp(name, "temperature") == 0) {
+        value = adm1272_direct_to_millidegrees(*(uint16_t *)opaque);
     } else {
         value = *(uint16_t *)opaque;
     }
@@ -446,6 +465,8 @@  static void adm1272_set(Object *obj, Visitor *v, const char *name, void *opaque,
         *internal = adm1272_milliamps_to_direct(value);
     } else if (strcmp(name, "pin") == 0) {
         *internal = adm1272_watts_to_direct(value);
+    } else if (strcmp(name, "temperature") == 0) {
+        *internal = adm1272_millidegrees_to_direct(value);
     } else {
         *internal = value;
     }
@@ -509,6 +530,10 @@  static void adm1272_init(Object *obj)
                         adm1272_get,
                         adm1272_set, NULL, &pmdev->pages[0].read_pin);
 
+    object_property_add(obj, "temperature", "uint16",
+                        adm1272_get,
+                        adm1272_set, NULL, &pmdev->pages[0].read_temperature_1);
+
 }
 
 static void adm1272_class_init(ObjectClass *klass, void *data)
diff --git a/tests/qtest/adm1272-test.c b/tests/qtest/adm1272-test.c
index 63f8514801..98134aabd2 100644
--- a/tests/qtest/adm1272-test.c
+++ b/tests/qtest/adm1272-test.c
@@ -65,6 +65,7 @@ 
 #define ADM1272_VOLTAGE_COEFF_DEFAULT   1
 #define ADM1272_CURRENT_COEFF_DEFAULT   3
 #define ADM1272_PWR_COEFF_DEFAULT       7
+#define ADM1272_TEMP_COEFF_DEFAULT      8
 #define ADM1272_IOUT_OFFSET             0x5000
 #define ADM1272_IOUT_OFFSET             0x5000
 
@@ -144,6 +145,22 @@  static uint32_t adm1272_direct_to_watts(uint16_t value)
     return pmbus_direct_mode2data(c, value);
 }
 
+static uint16_t adm1272_millidegrees_to_direct(uint32_t value)
+{
+    PMBusCoefficients c = adm1272_coefficients[ADM1272_TEMP_COEFF_DEFAULT];
+    c.b = c.b * 1000;
+    c.R = c.R - 3;
+    return pmbus_data2direct_mode(c, value);
+}
+
+static uint32_t adm1272_direct_to_millidegrees(uint16_t value)
+{
+    PMBusCoefficients c = adm1272_coefficients[ADM1272_TEMP_COEFF_DEFAULT];
+    c.b = c.b * 1000;
+    c.R = c.R - 3;
+    return pmbus_direct_mode2data(c, value);
+}
+
 static uint16_t qmp_adm1272_get(const char *id, const char *property)
 {
     QDict *response;
@@ -248,7 +265,7 @@  static void test_defaults(void *obj, void *data, QGuestAllocator *alloc)
 /* test qmp access */
 static void test_tx_rx(void *obj, void *data, QGuestAllocator *alloc)
 {
-    uint16_t i2c_value, value, i2c_voltage, i2c_pwr, lossy_value;
+    uint16_t i2c_value, value, i2c_voltage, i2c_pwr, i2c_temp, lossy_value;
     QI2CDevice *i2cdev = (QI2CDevice *)obj;
 
     /* converting to direct mode is lossy - we generate the same loss here */
@@ -287,6 +304,15 @@  static void test_tx_rx(void *obj, void *data, QGuestAllocator *alloc)
     i2c_pwr = adm1272_direct_to_watts(i2c_value);
     g_assert_cmphex(value, ==, i2c_pwr);
     g_assert_cmphex(i2c_pwr, ==, lossy_value);
+
+    lossy_value =
+        adm1272_direct_to_millidegrees(adm1272_millidegrees_to_direct(25000));
+    qmp_adm1272_set(TEST_ID, "temperature", 25000);
+    value = qmp_adm1272_get(TEST_ID, "temperature");
+    i2c_value = adm1272_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_1);
+    i2c_temp = adm1272_direct_to_millidegrees(i2c_value);
+    g_assert_cmphex(value, ==, i2c_temp);
+    g_assert_cmphex(i2c_temp, ==, lossy_value);
 }
 
 /* test r/w registers */