Skip to contentSkip to conversation

hw/misc: Introduce a temperature sensor interface

RFC PATCH In review Philippe Mathieu-Daudé 2 participants 20 messages Apr 21, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover hw/misc: Introduce a temperature sensor interface

Hi,

This series is part of a bigger one I was preparing for a GSoC project, but unfortunately no student got interested.

The idea is to allow device with temperature sensors to use a common API with temperature operations.

While guest are running, you can modify the temperatures to check the guest reactions. Some devices can trigger IRQ when temperature pass thresholds. Somehow similar to inject NMI.

Some devices could also modify their temperature regarding their workload.

The tsc2xxxx devices are interesting but can't be converted as they predate QOM/QDev.

Temperature changes rate is <1Hz (not enforced in this model) so using a QMP to get/set is acceptable.
The query-temperature-sensors command is for informative purpose, also acceptable for QMP use.

Next step might be to publish QMP events when temperature change.

The original goal is to better interface ADCs with real world data. Here using QMP is not an option. Here the idea is to use the AudioState and audio_sample API from "audio.h" but it seems not very used, see: https://bugs.launchpad.net/qemu/+bug/1861677

What is not clear yet to me is what's the best way to externally set a specific sensor temperature. The TMPxxx I2C sensors use object_property_set_int(). I could move that to the temp-sensor interface but it seems a bit overkill. Any simpler idea?

Cc Alex Bennée <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Keith Busch <[email protected]>
Cc Cédric Le Goater <[email protected]>
Cc Joel Stanley <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Joaquin de Andres <[email protected]>

Philippe Mathieu-Daudé (17):
hw/misc: Introduce the temperature sensor interface hw/misc/temp-sensor: Add 'query-temperature-sensors' QMP command hw/misc/temp-sensor: Add 'info temp' HMP command hw/misc/tmp105: Extract get_temp_mC() and set_temp_mC() helpers hw/misc/tmp105: Implement the 'temperature-sensor' qdev interface hw/misc/tmp421: Add definition for SENSORS_COUNT hw/misc/tmp421: Extract get_temp_mC() helper hw/misc/tmp421: Extract set_temp_mC() helper hw/misc/tmp421: Implement the 'temperature-sensor' qdev interface hw/misc/bcm2835_thermal: Hold the temperature in the device state hw/misc/bcm2835_thermal: Implement the 'temperature-sensor' interface hw/misc/bcm2835_property: Hold the temperature in the device state hw/misc/bcm2835_property: Implement the 'temperature-sensor' interface hw/display/ads7846: Implement the 'temperature-sensor' qdev interface hw/ide/qdev: Implement the 'temperature-sensor' qdev interface hw/misc/imx6ul_ccm: Implement the 'temperature-sensor' qdev interface tests/qtest/tmp105-test: Trivial test for TempSensorClass

 qapi/misc.json                     |  24 +++++++
 include/hw/misc/bcm2835_property.h |   1 +
 include/hw/misc/bcm2835_thermal.h  |   1 +
 include/hw/misc/temp-sensor.h      |  69 +++++++++++++++++++
 include/monitor/hmp.h              |   1 +
 hw/display/ads7846.c               |  38 +++++++++++
 hw/ide/qdev.c                      |  14 ++++
 hw/misc/bcm2835_property.c         |  39 ++++++++++-
 hw/misc/bcm2835_thermal.c          |  42 ++++++++++--
 hw/misc/imx6ul_ccm.c               |   7 ++
 hw/misc/temp-sensor.c              | 102 +++++++++++++++++++++++++++++
 hw/misc/tmp105.c                   |  51 +++++++++++----
 hw/misc/tmp421.c                   |  74 ++++++++++++++++-----
 tests/qtest/tmp105-test.c          |  18 +++++
 MAINTAINERS                        |   6 ++
 hmp-commands-info.hx               |  11 ++++
 hw/misc/Makefile.objs              |   1 +
 17 files changed, 463 insertions(+), 36 deletions(-)
 create mode 100644 include/hw/misc/temp-sensor.h
 create mode 100644 hw/misc/temp-sensor.c
Signature · 1 line
2.21.1
#1 lore raw

RFC PATCH 1/17 hw/misc: Introduce the temperature sensor interface

Introduce an interface to set and get temperature from devices.

A device can have multiple temperature sensors.
Each sensor might have a custom name.

---
 include/hw/misc/temp-sensor.h | 69 +++++++++++++++++++++++++++++++++++
 hw/misc/temp-sensor.c         | 25 +++++++++++++
 MAINTAINERS                   |  6 +++
 hw/misc/Makefile.objs         |  1 +
 4 files changed, 101 insertions(+)
 create mode 100644 include/hw/misc/temp-sensor.h
 create mode 100644 hw/misc/temp-sensor.c
include/hw/misc/temp-sensor.h new file +69 -0
new file mode 100644
index 0000000000..3067b1dd2e
@@ -0,0 +1,69 @@
+/*
+ * Generic interface for temperature sensors
+ *
+ * Copyright (c) 2020 Philippe Mathieu-Daudé
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_MISC_TEMP_SENSOR_H
+#define HW_MISC_TEMP_SENSOR_H
+
+#include "qom/object.h"
+
+#define TYPE_TEMPSENSOR_INTERFACE "temp-sensor-interface"
+#define TEMPSENSOR_INTERFACE(obj) \
+    INTERFACE_CHECK(TempSensor, (obj), TYPE_TEMPSENSOR_INTERFACE)
+#define TEMPSENSOR_INTERFACE_CLASS(class) \
+    OBJECT_CLASS_CHECK(TempSensorClass, (class), TYPE_TEMPSENSOR_INTERFACE)
+#define TEMPSENSOR_INTERFACE_GET_CLASS(class) \
+    OBJECT_GET_CLASS(TempSensorClass, (class), TYPE_TEMPSENSOR_INTERFACE)
+
+typedef struct TempSensor TempSensor;
+
+typedef struct TempSensorClass {
+    /* <private> */
+    InterfaceClass parent;
+    /* <public> */
+
+    /** Number of temperature sensors */
+    size_t sensor_count;
+
+    /**
+     * get_name:
+     * @obj: opaque pointer to Object
+     * @sensor_id: sensor index
+     *
+     * Returns: name of a temperature sensor.
+     */
+    const char *(*get_name)(TempSensor *obj, unsigned sensor_id);
+
+    /**
+     * set_temperature:
+     * @obj: opaque pointer to Object
+     * @sensor_id: sensor index
+     * @temp_C: sensor temperature, in degree Celsius
+     * @errp: pointer to a NULL-initialized error object
+     *
+     * Set a sensor temperature.
+     *
+     * If an error occurs @errp will contain the details
+     * (likely temperature out of range).
+     */
+    void (*set_temperature)(TempSensor *obj,
+                            unsigned sensor_id, float temp_C, Error **errp);
+
+    /**
+     * get_temperature:
+     * @obj: opaque pointer to Object
+     * @sensor_id: sensor index
+     *
+     * Get a sensor temperature.
+     *
+     * Returns: sensor temperature in °C.
+     */
+    float (*get_temperature)(TempSensor *obj, unsigned sensor_id);
+} TempSensorClass;
+
+#endif /* HW_MISC_TEMP_SENSOR_H */
hw/misc/temp-sensor.c new file +25 -0
new file mode 100644
index 0000000000..b7c1eb2d87
@@ -0,0 +1,25 @@
+/*
+ * Generic interface for temperature sensors
+ *
+ * Copyright (c) 2020 Philippe Mathieu-Daudé
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/misc/temp-sensor.h"
+
+static TypeInfo tempsensor_interface_type_info = {
+    .name = TYPE_TEMPSENSOR_INTERFACE,
+    .parent = TYPE_INTERFACE,
+    .class_size = sizeof(TempSensorClass),
+};
+
+static void tempsensor_register_types(void)
+{
+    type_register_static(&tempsensor_interface_type_info);
+}
+
+type_init(tempsensor_register_types)
MAINTAINERS +6 -0
index 8cbc1fac2b..5559110c56 100644
@@ -1575,6 +1575,12 @@ F: hw/sd/sd*
 F: hw/sd/ssi-sd.c
 F: tests/qtest/sd*
 
+Temperature Sensors
+M: Philippe Mathieu-Daudé <[email protected]>
+S: Odd Fixes
+F: hw/misc/temp-sensor.c
+F: include/hw/misc/temp-sensor.h
+
 USB
 M: Gerd Hoffmann <[email protected]>
 S: Maintained
hw/misc/Makefile.objs +1 -0
index 68aae2eabb..4f90efb06d 100644
@@ -90,3 +90,4 @@ common-obj-$(CONFIG_NRF51_SOC) += nrf51_rng.o
 obj-$(CONFIG_MAC_VIA) += mac_via.o
 
 common-obj-$(CONFIG_GRLIB) += grlib_ahb_apb_pnp.o
+common-obj-y += temp-sensor.o
Signature · 1 line
2.21.1
#2 lore raw

RFC PATCH 2/17 hw/misc/temp-sensor: Add 'query-temperature-sensors' QMP command

In reply to Philippe Mathieu-Daudé

Add a command to query current temperature from all sensors able to report it:

  { "execute": "query-temperature-sensors" }
  {
      "return": [
          {
              "temperature": 25,
              "name": "videocore"
          },
          {
              "temperature": 25,
              "name": "bcm2835-thermal-0"
          }
      ]
  }
---
 qapi/misc.json        | 24 ++++++++++++++++++++++
 hw/misc/temp-sensor.c | 48 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
qapi/misc.json +24 -0
index 99b90ac80b..51881931e2 100644
@@ -1550,3 +1550,27 @@
 ##
 { 'command': 'query-vm-generation-id', 'returns': 'GuidInfo' }
 
+##
+# @TemperatureSensor:
+#
+# Temperature sensor information.
+#
+# @name: the name of the sensor
+#
+# @temperature: the current temperature of the sensor (in C)
+#
+# Since: 5.1
+##
+{ 'struct': 'TemperatureSensor',
+  'data': { 'name': 'str',
+            'temperature': 'number' } }
+
+##
+# @query-temperature-sensors:
+#
+# Return a list of TemperatureSensor for devices that support
+# the TYPE_TEMPSENSOR_INTERFACE.
+#
+# Since: 5.1
+##
+{ 'command': 'query-temperature-sensors', 'returns': ['TemperatureSensor']}
hw/misc/temp-sensor.c +48 -0

54 lines not on this page · raw message

Signature · 1 line
2.21.1
#3 lore raw

Re: [RFC PATCH 02/17] hw/misc/temp-sensor: Add 'query-temperature-sensors' QMP command

Quoted 82 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:

Add a command to query current temperature from all sensors able to report it:

  { "execute": "query-temperature-sensors" }
  {
      "return": [
          {
              "temperature": 25,
              "name": "videocore"
          },
          {
              "temperature": 25,
              "name": "bcm2835-thermal-0"
          }
      ]
  }
---
 qapi/misc.json        | 24 ++++++++++++++++++++++
 hw/misc/temp-sensor.c | 48 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
qapi/misc.json +24 -0
index 99b90ac80b..51881931e2 100644
@@ -1550,3 +1550,27 @@
 ##
 { 'command': 'query-vm-generation-id', 'returns': 'GuidInfo' }
 
+##
+# @TemperatureSensor:
+#
+# Temperature sensor information.
+#
+# @name: the name of the sensor
+#
+# @temperature: the current temperature of the sensor (in C)
+#
+# Since: 5.1
+##
+{ 'struct': 'TemperatureSensor',
+  'data': { 'name': 'str',
+            'temperature': 'number' } }
+
+##
+# @query-temperature-sensors:
+#
+# Return a list of TemperatureSensor for devices that support
+# the TYPE_TEMPSENSOR_INTERFACE.
+#
+# Since: 5.1
+##
+{ 'command': 'query-temperature-sensors', 'returns': ['TemperatureSensor']}
hw/misc/temp-sensor.c +19 -0
index b7c1eb2d87..27750c533d 100644
@@ -10,6 +10,54 @@
 
 #include "qemu/osdep.h"
 #include "hw/misc/temp-sensor.h"
+#include "qapi/qapi-commands-misc.h"
+#include "qapi/error.h"
+
+static int query_temperature_sensors_foreach(Object *obj, void *opaque)
+{
+    TemperatureSensorList **list = opaque;
+    TempSensor *sensor;
+    TempSensorClass *k;
+
+    if (!object_dynamic_cast(obj, TYPE_TEMPSENSOR_INTERFACE)) {
+        return 0;
+    }
+
+    k = TEMPSENSOR_INTERFACE_GET_CLASS(obj);
+    if (!k->get_temperature) {
+        return 0;
+    }
+
+    sensor = TEMPSENSOR_INTERFACE(obj);

hw/misc/temp-sensor.c line 41

Quoted 10 lines
hw/misc/temp-sensor.c +10 -0
+    for (size_t i = 0; i < k->sensor_count; i++) {
+        TemperatureSensorList *info = g_malloc0(sizeof(*info));
+        TemperatureSensor *value = g_malloc0(sizeof(*value));
+
+        if (k->get_name) {
+            value->name = g_strdup(k->get_name(sensor, i));
+        } else {
+            value->name = g_strdup_printf("%s-%zu",
+                                          object_get_typename(obj), i);
+        }

How do the names work if you've got multiple of the same device; e.g. is the get_name() method supposed to return the device path as well as the sensor name? if I have an emulated PCI device with two sensors on and I then instantiate two of the PCI devices, do I get a name with the PCI path in?

Dave

Quoted 25 lines
hw/misc/temp-sensor.c +19 -0
+        value->temperature = k->get_temperature(sensor, i);
+
+        info->value = value;
+        info->next = *list;
+        *list = info;
+    }
+
+    return 0;
+}
+
+TemperatureSensorList *qmp_query_temperature_sensors(Error **errp)
+{
+    TemperatureSensorList *list = NULL;
+
+    object_child_foreach_recursive(object_get_root(),
+                                   query_temperature_sensors_foreach,
+                                   &list);
+    return list;
+}
 
 static TypeInfo tempsensor_interface_type_info = {
     .name = TYPE_TEMPSENSOR_INTERFACE,
Signature · 1 line
2.21.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#4 lore raw

RFC PATCH 3/17 hw/misc/temp-sensor: Add 'info temp' HMP command

In reply to Philippe Mathieu-Daudé

Add a command to display current devices temperature in the monitor:

(qemu) info temp
Temperatures (in C):
videocore 25.00
bcm2835-thermal-0 25.00

---
 include/monitor/hmp.h |  1 +
 hw/misc/temp-sensor.c | 29 +++++++++++++++++++++++++++++
 hmp-commands-info.hx  | 11 +++++++++++
 3 files changed, 41 insertions(+)
include/monitor/hmp.h +1 -0
index e33ca5a911..f023230bd1 100644
@@ -129,5 +129,6 @@ void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict);
 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict);
 void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict);
 void hmp_info_sev(Monitor *mon, const QDict *qdict);
+void hmp_info_temp(Monitor *mon, const QDict *qdict);
 
 #endif
hw/misc/temp-sensor.c +29 -0
index 27750c533d..5f591bd9c3 100644
@@ -12,6 +12,8 @@
 #include "hw/misc/temp-sensor.h"
 #include "qapi/qapi-commands-misc.h"
 #include "qapi/error.h"
+#include "monitor/monitor.h"
+#include "monitor/hmp.h"
 
 static int query_temperature_sensors_foreach(Object *obj, void *opaque)
 {
@@ -59,6 +61,33 @@ TemperatureSensorList *qmp_query_temperature_sensors(Error **errp)
     return list;
 }
 
+void hmp_info_temp(Monitor *mon, const QDict *qdict)
+{
+    TemperatureSensorList *list, *sensor;
+    Error *err = NULL;
+
+    list = qmp_query_temperature_sensors(&err);
+    if (!list) {
+        monitor_printf(mon, "No temperature sensors\n");
+        return;
+    }
+    if (err) {
+        monitor_printf(mon, "Error while getting temperatures: %s\n",
+                       error_get_pretty(err));
+        error_free(err);
+        goto out;
+    }
+
+    monitor_printf(mon, "Temperatures (in C):\n");
+    for (sensor = list; sensor; sensor = sensor->next) {
+        monitor_printf(mon, "%-33s %6.2f\n", sensor->value->name,
+                       sensor->value->temperature);
+    }
+
+out:
+    qapi_free_TemperatureSensorList(list);
+}
+
 static TypeInfo tempsensor_interface_type_info = {
     .name = TYPE_TEMPSENSOR_INTERFACE,
     .parent = TYPE_INTERFACE,
hmp-commands-info.hx +11 -0
index ca5198438d..77f1c43ce3 100644
@@ -880,4 +880,15 @@ SRST
     Show SEV information.
 ERST
 
+    {
+        .name       = "temp",
+        .args_type  = "",
+        .params     = "",
+        .help       = "show device temperatures",
+        .cmd        = hmp_info_temp,
+    },
 
+SRST
+  ``info temp``
+    Show device temperatures.
+ERST
Signature · 1 line
2.21.1
#5 lore raw

Re: [RFC PATCH 03/17] hw/misc/temp-sensor: Add 'info temp' HMP command

Quoted 8 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:

Add a command to display current devices temperature in the monitor:

(qemu) info temp
Temperatures (in C):
videocore 25.00
bcm2835-thermal-0 25.00

How do I set the temperature?

Quoted 38 lines
---
 include/monitor/hmp.h |  1 +
 hw/misc/temp-sensor.c | 29 +++++++++++++++++++++++++++++
 hmp-commands-info.hx  | 11 +++++++++++
 3 files changed, 41 insertions(+)
include/monitor/hmp.h +1 -0
index e33ca5a911..f023230bd1 100644
@@ -129,5 +129,6 @@ void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict);
 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict);
 void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict);
 void hmp_info_sev(Monitor *mon, const QDict *qdict);
+void hmp_info_temp(Monitor *mon, const QDict *qdict);
 
 #endif
hw/misc/temp-sensor.c +6 -0
index 27750c533d..5f591bd9c3 100644
@@ -12,6 +12,8 @@
 #include "hw/misc/temp-sensor.h"
 #include "qapi/qapi-commands-misc.h"
 #include "qapi/error.h"
+#include "monitor/monitor.h"
+#include "monitor/hmp.h"
 
 static int query_temperature_sensors_foreach(Object *obj, void *opaque)
 {
@@ -59,6 +61,33 @@ TemperatureSensorList *qmp_query_temperature_sensors(Error **errp)
     return list;
 }
 
+void hmp_info_temp(Monitor *mon, const QDict *qdict)
+{
+    TemperatureSensorList *list, *sensor;
+    Error *err = NULL;

hw/misc/temp-sensor.c TemperatureSensorList *qmp_query_temperature_sensors(Error **errp) · line 77

Quoted 10 lines
hw/misc/temp-sensor.c +10 -0
+
+    list = qmp_query_temperature_sensors(&err);
+    if (!list) {
+        monitor_printf(mon, "No temperature sensors\n");
+        return;
+    }
+    if (err) {
+        monitor_printf(mon, "Error while getting temperatures: %s\n",
+                       error_get_pretty(err));
+        error_free(err);

Maybe use hmp_handle_error

hw/misc/temp-sensor.c TemperatureSensorList *qmp_query_temperature_sensors(Error **errp) · line 84

Quoted 7 lines
hw/misc/temp-sensor.c +7 -0
+        goto out;
+    }
+
+    monitor_printf(mon, "Temperatures (in C):\n");
+    for (sensor = list; sensor; sensor = sensor->next) {
+        monitor_printf(mon, "%-33s %6.2f\n", sensor->value->name,
+                       sensor->value->temperature);

See my question on the earlier patch; I'm curious here whether we want device name, and then subname within that device.

Dave

Quoted 32 lines
hw/misc/temp-sensor.c +6 -0
+    }
+
+out:
+    qapi_free_TemperatureSensorList(list);
+}
+
 static TypeInfo tempsensor_interface_type_info = {
     .name = TYPE_TEMPSENSOR_INTERFACE,
     .parent = TYPE_INTERFACE,
hmp-commands-info.hx +11 -0
index ca5198438d..77f1c43ce3 100644
@@ -880,4 +880,15 @@ SRST
     Show SEV information.
 ERST
 
+    {
+        .name       = "temp",
+        .args_type  = "",
+        .params     = "",
+        .help       = "show device temperatures",
+        .cmd        = hmp_info_temp,
+    },
 
+SRST
+  ``info temp``
+    Show device temperatures.
+ERST
Signature · 1 line
2.21.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#6 lore raw

RFC PATCH 4/17 hw/misc/tmp105: Extract get_temp_mC() and set_temp_mC() helpers

In reply to Philippe Mathieu-Daudé

Since we are going to reuse this code, extract it as helpers.

---
 hw/misc/tmp105.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)
hw/misc/tmp105.c +20 -11
index 75ddad3a12..754c8a6eb4 100644
@@ -56,11 +56,28 @@ static void tmp105_alarm_update(TMP105State *s)
     tmp105_interrupt_update(s);
 }
 
+static int64_t get_temp_mC(TMP105State *s)
+{
+    return s->temperature * 1000 / 256;
+}
+
+static void set_temp_mC(TMP105State *s, int64_t temp_mC, Error **errp)
+{
+    if (temp_mC >= 128000 || temp_mC < -128000) {
+        error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range",
+                   temp_mC / 1000, temp_mC % 1000);
+        return;
+    }
+
+    s->temperature = (int16_t) (temp_mC * 256 / 1000);
+
+    tmp105_alarm_update(s);
+}
+
 static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name,
                                    void *opaque, Error **errp)
 {
-    TMP105State *s = TMP105(obj);
-    int64_t value = s->temperature * 1000 / 256;
+    int64_t value = get_temp_mC(TMP105(obj));
 
     visit_type_int(v, name, &value, errp);
 }
@@ -71,7 +88,6 @@ static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name,
 static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name,
                                    void *opaque, Error **errp)
 {
-    TMP105State *s = TMP105(obj);
     Error *local_err = NULL;
     int64_t temp;
 
@@ -80,15 +96,8 @@ static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name,
         error_propagate(errp, local_err);
         return;
     }
-    if (temp >= 128000 || temp < -128000) {
-        error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range",
-                   temp / 1000, temp % 1000);
-        return;
-    }
 
-    s->temperature = (int16_t) (temp * 256 / 1000);
-
-    tmp105_alarm_update(s);
+    set_temp_mC(TMP105(obj), temp, errp);
 }
 
 static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
Signature · 1 line
2.21.1
#7 lore raw

RFC PATCH 5/17 hw/misc/tmp105: Implement the 'temperature-sensor' qdev interface

In reply to Philippe Mathieu-Daudé

The TMP105 is an I2C temperature sensor.
Let it implement the 'temperature-sensor' interface.

---
 hw/misc/tmp105.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
hw/misc/tmp105.c +20 -0
index 754c8a6eb4..1f0cb28012 100644
@@ -26,6 +26,7 @@
 #include "qapi/error.h"
 #include "qapi/visitor.h"
 #include "qemu/module.h"
+#include "hw/misc/temp-sensor.h"
 
 static void tmp105_interrupt_update(TMP105State *s)
 {
@@ -74,6 +75,17 @@ static void set_temp_mC(TMP105State *s, int64_t temp_mC, Error **errp)
     tmp105_alarm_update(s);
 }
 
+static float tmp105_get_temp(TempSensor *obj, unsigned sensor_id)
+{
+    return get_temp_mC(TMP105(obj)) / 1000.f;
+}
+
+static void tmp105_set_temp(TempSensor *obj, unsigned sensor_id,
+                            float temp_C, Error **errp)
+{
+    set_temp_mC(TMP105(obj), temp_C * 1000.f, errp);
+}
+
 static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name,
                                    void *opaque, Error **errp)
 {
@@ -261,12 +273,16 @@ static void tmp105_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
+    TempSensorClass *tc = TEMPSENSOR_INTERFACE_CLASS(klass);
 
     dc->realize = tmp105_realize;
     k->event = tmp105_event;
     k->recv = tmp105_rx;
     k->send = tmp105_tx;
     dc->vmsd = &vmstate_tmp105;
+    tc->sensor_count = 1;
+    tc->set_temperature = tmp105_set_temp;
+    tc->get_temperature = tmp105_get_temp;
 }
 
 static const TypeInfo tmp105_info = {
@@ -275,6 +291,10 @@ static const TypeInfo tmp105_info = {
     .instance_size = sizeof(TMP105State),
     .instance_init = tmp105_initfn,
     .class_init    = tmp105_class_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_TEMPSENSOR_INTERFACE },
+        { }
+    },
 };
 
 static void tmp105_register_types(void)
Signature · 1 line
2.21.1
#8 lore raw

RFC PATCH 6/17 hw/misc/tmp421: Add definition for SENSORS_COUNT

In reply to Philippe Mathieu-Daudé

Use a definition instead of a magic value.

---
 hw/misc/tmp421.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/misc/tmp421.c +5 -3
index c0bc150bca..04c3b3ca6e 100644
@@ -37,6 +37,8 @@
 #define TMP422_DEVICE_ID                0x22
 #define TMP423_DEVICE_ID                0x23
 
+#define SENSORS_COUNT       4
+
 typedef struct DeviceInfo {
     int model;
     const char *name;
@@ -53,7 +55,7 @@ typedef struct TMP421State {
     I2CSlave i2c;
     /*< public >*/
 
-    int16_t temperature[4];
+    int16_t temperature[SENSORS_COUNT];
 
     uint8_t status;
     uint8_t config[2];
@@ -124,7 +126,7 @@ static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    if (tempid >= 4 || tempid < 0) {
+    if (tempid >= SENSORS_COUNT || tempid < 0) {
         error_setg(errp, "error reading %s", name);
         return;
     }
@@ -164,7 +166,7 @@ static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    if (tempid >= 4 || tempid < 0) {
+    if (tempid >= SENSORS_COUNT || tempid < 0) {
         error_setg(errp, "error reading %s", name);
         return;
     }
Signature · 1 line
2.21.1
#9 lore raw

RFC PATCH 7/17 hw/misc/tmp421: Extract get_temp_mC() helper

In reply to Philippe Mathieu-Daudé

Since we are going to reuse this code, extract it first.

---
 hw/misc/tmp421.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
hw/misc/tmp421.c +11 -4
index 04c3b3ca6e..8003356307 100644
@@ -112,12 +112,19 @@ typedef struct TMP421Class {
 static const int32_t mins[2] = { -40000, -55000 };
 static const int32_t maxs[2] = { 127000, 150000 };
 
+static int64_t get_temp_mC(TMP421State *s, unsigned int id)
+{
+    bool ext_range = (s->config[0] & TMP421_CONFIG_RANGE);
+    int offset = ext_range * 64 * 256;
+
+    assert(id < SENSORS_COUNT);
+
+    return ((s->temperature[id] - offset) * 1000 + 128) / 256;
+}
+
 static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
                                    void *opaque, Error **errp)
 {
-    TMP421State *s = TMP421(obj);
-    bool ext_range = (s->config[0] & TMP421_CONFIG_RANGE);
-    int offset = ext_range * 64 * 256;
     int64_t value;
     int tempid;
 
@@ -131,7 +138,7 @@ static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    value = ((s->temperature[tempid] - offset) * 1000 + 128) / 256;
+    value = get_temp_mC(TMP421(obj), tempid);
 
     visit_type_int(v, name, &value, errp);
 }
Signature · 1 line
2.21.1
#10 lore raw

RFC PATCH 8/17 hw/misc/tmp421: Extract set_temp_mC() helper

In reply to Philippe Mathieu-Daudé

Since we are going to reuse this code, extract it first.

---
 hw/misc/tmp421.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
hw/misc/tmp421.c +17 -10
index 8003356307..270e7d5510 100644
@@ -122,6 +122,22 @@ static int64_t get_temp_mC(TMP421State *s, unsigned int id)
     return ((s->temperature[id] - offset) * 1000 + 128) / 256;
 }
 
+static void set_temp_mC(TMP421State *s, unsigned int id,
+                        int64_t temp, Error **errp)
+{
+    bool ext_range = (s->config[0] & TMP421_CONFIG_RANGE);
+    int offset = ext_range * 64 * 256;
+
+    assert(id < SENSORS_COUNT);
+    if (temp >= maxs[ext_range] || temp < mins[ext_range]) {
+        error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range",
+                   temp / 1000, temp % 1000);
+        return;
+    }
+
+    s->temperature[id] = (int16_t) ((temp * 256 - 128) / 1000) + offset;
+}
+
 static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
                                    void *opaque, Error **errp)
 {
@@ -149,11 +165,8 @@ static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
 static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
                                    void *opaque, Error **errp)
 {
-    TMP421State *s = TMP421(obj);
     Error *local_err = NULL;
     int64_t temp;
-    bool ext_range = (s->config[0] & TMP421_CONFIG_RANGE);
-    int offset = ext_range * 64 * 256;
     int tempid;
 
     visit_type_int(v, name, &temp, &local_err);
@@ -162,12 +175,6 @@ static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    if (temp >= maxs[ext_range] || temp < mins[ext_range]) {
-        error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range",
-                   temp / 1000, temp % 1000);
-        return;
-    }
-
     if (sscanf(name, "temperature%d", &tempid) != 1) {
         error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
         return;
@@ -178,7 +185,7 @@ static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    s->temperature[tempid] = (int16_t) ((temp * 256 - 128) / 1000) + offset;
+    set_temp_mC(TMP421(obj), tempid, temp, errp);
 }
 
 static void tmp421_read(TMP421State *s)
Signature · 1 line
2.21.1
#11 lore raw

RFC PATCH 9/17 hw/misc/tmp421: Implement the 'temperature-sensor' qdev interface

In reply to Philippe Mathieu-Daudé

The TMP421 is an I2C device with multiple temperature sensors. Let it implement the 'temperature-sensor' interface.

---
 hw/misc/tmp421.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
hw/misc/tmp421.c +24 -0
index 270e7d5510..31bfbecb1d 100644
@@ -30,6 +30,7 @@
 #include "qapi/error.h"
 #include "qapi/visitor.h"
 #include "qemu/module.h"
+#include "hw/misc/temp-sensor.h"
 
 /* Manufacturer / Device ID's */
 #define TMP421_MANUFACTURER_ID          0x55
@@ -138,6 +139,21 @@ static void set_temp_mC(TMP421State *s, unsigned int id,
     s->temperature[id] = (int16_t) ((temp * 256 - 128) / 1000) + offset;
 }
 
+static float tmp421_get_temp(TempSensor *obj, unsigned sensor_id)
+{
+    TMP421State *s = TMP421(obj);
+
+    return get_temp_mC(s, sensor_id) / 1000.f;
+}
+
+static void tmp421_set_temp(TempSensor *obj, unsigned sensor_id,
+                            float temp_C, Error **errp)
+{
+    TMP421State *s = TMP421(obj);
+
+    set_temp_mC(s, sensor_id, temp_C * 1000.f, errp);
+}
+
 static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name,
                                    void *opaque, Error **errp)
 {
@@ -380,6 +396,7 @@ static void tmp421_class_init(ObjectClass *klass, void *data)
     DeviceClass *dc = DEVICE_CLASS(klass);
     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
     TMP421Class *sc = TMP421_CLASS(klass);
+    TempSensorClass *tc = TEMPSENSOR_INTERFACE_CLASS(klass);
 
     dc->realize = tmp421_realize;
     k->event = tmp421_event;
@@ -387,6 +404,9 @@ static void tmp421_class_init(ObjectClass *klass, void *data)
     k->send = tmp421_tx;
     dc->vmsd = &vmstate_tmp421;
     sc->dev = (DeviceInfo *) data;
+    tc->sensor_count = SENSORS_COUNT;
+    tc->set_temperature = tmp421_set_temp;
+    tc->get_temperature = tmp421_get_temp;
 }
 
 static const TypeInfo tmp421_info = {
@@ -396,6 +416,10 @@ static const TypeInfo tmp421_info = {
     .class_size    = sizeof(TMP421Class),
     .instance_init = tmp421_initfn,
     .abstract      = true,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_TEMPSENSOR_INTERFACE },
+        { }
+    },
 };
 
 static void tmp421_register_types(void)
Signature · 1 line
2.21.1
#12 lore raw

RFC PATCH 10/17 hw/misc/bcm2835_thermal: Hold the temperature in the device state

In reply to Philippe Mathieu-Daudé

We are going to modify this variable, move it to the device state.

---
 include/hw/misc/bcm2835_thermal.h |  1 +
 hw/misc/bcm2835_thermal.c         | 18 +++++++++++++-----
 2 files changed, 14 insertions(+), 5 deletions(-)
include/hw/misc/bcm2835_thermal.h +1 -0
index c3651b27ec..df6a7b86c3 100644
@@ -22,6 +22,7 @@ typedef struct {
     /*< public >*/
     MemoryRegion iomem;
     uint32_t ctl;
+    float64 temp;
 } Bcm2835ThermalState;
 
 #endif
hw/misc/bcm2835_thermal.c +13 -5
index c6f3b1ad60..6ee149de20 100644
@@ -32,7 +32,7 @@ FIELD(STAT, INTERRUPT, 11, 1)
 #define THERMAL_OFFSET_C 412
 #define THERMAL_COEFF  (-0.538f)
 
-static uint16_t bcm2835_thermal_temp2adc(int temp_C)
+static uint16_t bcm2835_thermal_temp2adc(float64 temp_C)
 {
     return (temp_C - THERMAL_OFFSET_C) / THERMAL_COEFF;
 }
@@ -47,8 +47,7 @@ static uint64_t bcm2835_thermal_read(void *opaque, hwaddr addr, unsigned size)
         val = s->ctl;
         break;
     case A_STAT:
-        /* Temperature is constantly 25°C. */
-        val = FIELD_DP32(bcm2835_thermal_temp2adc(25), STAT, VALID, true);
+        val = FIELD_DP32(bcm2835_thermal_temp2adc(s->temp), STAT, VALID, true);
         break;
     default:
         /* MemoryRegionOps are aligned, so this can not happen. */
@@ -85,6 +84,13 @@ static const MemoryRegionOps bcm2835_thermal_ops = {
     .endianness = DEVICE_NATIVE_ENDIAN,
 };
 
+static void bcm2835_thermal_init(Object *obj)
+{
+    Bcm2835ThermalState *s = BCM2835_THERMAL(obj);
+
+    s->temp = 25.0;
+}
+
 static void bcm2835_thermal_reset(DeviceState *dev)
 {
     Bcm2835ThermalState *s = BCM2835_THERMAL(dev);
@@ -103,10 +109,11 @@ static void bcm2835_thermal_realize(DeviceState *dev, Error **errp)
 
 static const VMStateDescription bcm2835_thermal_vmstate = {
     .name = "bcm2835_thermal",
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .fields = (VMStateField[]) {
         VMSTATE_UINT32(ctl, Bcm2835ThermalState),
+        VMSTATE_FLOAT64(temp, Bcm2835ThermalState),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -124,6 +131,7 @@ static const TypeInfo bcm2835_thermal_info = {
     .name = TYPE_BCM2835_THERMAL,
     .parent = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(Bcm2835ThermalState),
+    .instance_init = bcm2835_thermal_init,
     .class_init = bcm2835_thermal_class_init,
 };
 
Signature · 1 line
2.21.1
#13 lore raw

RFC PATCH 11/17 hw/misc/bcm2835_thermal: Implement the 'temperature-sensor' interface

In reply to Philippe Mathieu-Daudé

The BCM2835 has a block that reports its temperature. Let it implement the 'temperature-sensor' interface.

---
 hw/misc/bcm2835_thermal.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
hw/misc/bcm2835_thermal.c +24 -0
index 6ee149de20..6b85cf9705 100644
@@ -12,6 +12,7 @@
 #include "hw/misc/bcm2835_thermal.h"
 #include "hw/registerfields.h"
 #include "migration/vmstate.h"
+#include "hw/misc/temp-sensor.h"
 
 REG32(CTL, 0)
 FIELD(CTL, POWER_DOWN, 0, 1)
@@ -32,6 +33,21 @@ FIELD(STAT, INTERRUPT, 11, 1)
 #define THERMAL_OFFSET_C 412
 #define THERMAL_COEFF  (-0.538f)
 
+static void bcm2835_set_temperature(TempSensor *obj, unsigned int sensor_id,
+                                    float temp, Error **errp)
+{
+    Bcm2835ThermalState *s = BCM2835_THERMAL(obj);
+
+    s->temp = temp;
+}
+
+static float bcm2835_get_temperature(TempSensor *obj, unsigned int sensor_id)
+{
+    Bcm2835ThermalState *s = BCM2835_THERMAL(obj);
+
+    return s->temp;
+}
+
 static uint16_t bcm2835_thermal_temp2adc(float64 temp_C)
 {
     return (temp_C - THERMAL_OFFSET_C) / THERMAL_COEFF;
@@ -121,10 +137,14 @@ static const VMStateDescription bcm2835_thermal_vmstate = {
 static void bcm2835_thermal_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
+    TempSensorClass *tc = TEMPSENSOR_INTERFACE_CLASS(klass);
 
     dc->realize = bcm2835_thermal_realize;
     dc->reset = bcm2835_thermal_reset;
     dc->vmsd = &bcm2835_thermal_vmstate;
+    tc->sensor_count = 1;
+    tc->set_temperature = bcm2835_set_temperature;
+    tc->get_temperature = bcm2835_get_temperature;
 }
 
 static const TypeInfo bcm2835_thermal_info = {
@@ -133,6 +153,10 @@ static const TypeInfo bcm2835_thermal_info = {
     .instance_size = sizeof(Bcm2835ThermalState),
     .instance_init = bcm2835_thermal_init,
     .class_init = bcm2835_thermal_class_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_TEMPSENSOR_INTERFACE },
+        { }
+    },
 };
 
 static void bcm2835_thermal_register_types(void)
Signature · 1 line
2.21.1
#14 lore raw

RFC PATCH 12/17 hw/misc/bcm2835_property: Hold the temperature in the device state

In reply to Philippe Mathieu-Daudé

We are going to modify this variable, move it to the device state.

---
 include/hw/misc/bcm2835_property.h | 1 +
 hw/misc/bcm2835_property.c         | 8 +++++---
 2 files changed, 6 insertions(+), 3 deletions(-)
include/hw/misc/bcm2835_property.h +1 -0
index b321f22499..010ad39ed0 100644
@@ -31,6 +31,7 @@ typedef struct {
     uint32_t board_rev;
     uint32_t addr;
     bool pending;
+    float64 temp_mC;
 } BCM2835PropertyState;
 
 #endif
hw/misc/bcm2835_property.c +5 -3
index 3e228ca0ae..bbe80affd4 100644
@@ -143,7 +143,7 @@ static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value)
         /* Temperature */
 
         case 0x00030006: /* Get temperature */
-            stl_le_phys(&s->dma_as, value + 16, 25000);
+            stl_le_phys(&s->dma_as, value + 16, s->temp_mC);
             resplen = 8;
             break;
 
@@ -361,12 +361,13 @@ static const MemoryRegionOps bcm2835_property_ops = {
 
 static const VMStateDescription vmstate_bcm2835_property = {
     .name = TYPE_BCM2835_PROPERTY,
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .fields      = (VMStateField[]) {
         VMSTATE_MACADDR(macaddr, BCM2835PropertyState),
         VMSTATE_UINT32(addr, BCM2835PropertyState),
         VMSTATE_BOOL(pending, BCM2835PropertyState),
+        VMSTATE_FLOAT64(temp_mC, BCM2835PropertyState),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -375,6 +376,7 @@ static void bcm2835_property_init(Object *obj)
 {
     BCM2835PropertyState *s = BCM2835_PROPERTY(obj);
 
+    s->temp_mC = 25e3;
     memory_region_init_io(&s->iomem, OBJECT(s), &bcm2835_property_ops, s,
                           TYPE_BCM2835_PROPERTY, 0x10);
     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
Signature · 1 line
2.21.1
#15 lore raw

RFC PATCH 13/17 hw/misc/bcm2835_property: Implement the 'temperature-sensor' interface

In reply to Philippe Mathieu-Daudé

The BCM2835 'property' QEMU device models the serialized responses from the VideoCore firmware. As we can query the VC for its current temperatyre, let this model implement the 'temperature-sensor' interface.

---
 hw/misc/bcm2835_property.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
hw/misc/bcm2835_property.c +31 -0
index bbe80affd4..dd1e9a2e03 100644
@@ -12,6 +12,7 @@
 #include "migration/vmstate.h"
 #include "hw/irq.h"
 #include "hw/misc/bcm2835_mbox_defs.h"
+#include "hw/misc/temp-sensor.h"
 #include "sysemu/dma.h"
 #include "qemu/log.h"
 #include "qemu/module.h"
@@ -372,6 +373,27 @@ static const VMStateDescription vmstate_bcm2835_property = {
     }
 };
 
+static const char *bcm2835_property_get_temp_sensor_name(TempSensor *obj,
+                                                         unsigned sensor_id)
+{
+    return "videocore";
+}
+
+static float bcm2835_property_get_temp(TempSensor *obj, unsigned sensor_id)
+{
+    BCM2835PropertyState *s = BCM2835_PROPERTY(obj);
+
+    return s->temp_mC / 1000;
+}
+
+static void bcm2835_property_set_temp(TempSensor *obj, unsigned sensor_id,
+                                      float temp_C, Error **errp)
+{
+    BCM2835PropertyState *s = BCM2835_PROPERTY(obj);
+
+    s->temp_mC = temp_C * 1000;
+}
+
 static void bcm2835_property_init(Object *obj)
 {
     BCM2835PropertyState *s = BCM2835_PROPERTY(obj);
@@ -429,10 +451,15 @@ static Property bcm2835_property_props[] = {
 static void bcm2835_property_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
+    TempSensorClass *tc = TEMPSENSOR_INTERFACE_CLASS(klass);
 
     device_class_set_props(dc, bcm2835_property_props);
     dc->realize = bcm2835_property_realize;
     dc->vmsd = &vmstate_bcm2835_property;
+    tc->sensor_count = 1;
+    tc->get_name = bcm2835_property_get_temp_sensor_name;
+    tc->set_temperature = bcm2835_property_set_temp;
+    tc->get_temperature = bcm2835_property_get_temp;
 }
 
 static TypeInfo bcm2835_property_info = {
@@ -441,6 +468,10 @@ static TypeInfo bcm2835_property_info = {
     .instance_size = sizeof(BCM2835PropertyState),
     .class_init    = bcm2835_property_class_init,
     .instance_init = bcm2835_property_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_TEMPSENSOR_INTERFACE },
+        { }
+    },
 };
 
 static void bcm2835_property_register_types(void)
Signature · 1 line
2.21.1
#16 lore raw

RFC PATCH 14/17 hw/display/ads7846: Implement the 'temperature-sensor' qdev interface

In reply to Philippe Mathieu-Daudé

The 4-wire ADS7846 Touch Screen Controller is able to report a pair of temperatures. Let it implement the 'temperature-sensor' interface.

---
 hw/display/ads7846.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
hw/display/ads7846.c +38 -0
index 9228b40b1a..e37e99f5ec 100644
@@ -16,6 +16,7 @@
 #include "migration/vmstate.h"
 #include "qemu/module.h"
 #include "ui/console.h"
+#include "hw/misc/temp-sensor.h"
 
 typedef struct {
     SSISlave ssidev;
@@ -53,6 +54,12 @@ typedef struct {
 #define ADS_Z1POS(x, y)	600
 #define ADS_Z2POS(x, y)	(600 + 6000 / ADS_XPOS(x, y))
 
+/* TI Datasheet SBAS125H "TEMP DIODE VOLTAGE vs TEMPERATURE (2.7V SUPPLY)" */
+#define ADS_TEMP0_OFS_MILLIV    666.f
+#define ADS_TEMP1_OFS_MILLIV    761.f
+#define ADS_TEMP0_COEFF         2.08f
+#define ADS_TEMP1_COEFF         1.60f
+
 static void ads7846_int_update(ADS7846State *s)
 {
     if (s->interrupt)
@@ -157,12 +164,39 @@ static void ads7846_realize(SSISlave *d, Error **errp)
     vmstate_register(NULL, VMSTATE_INSTANCE_ID_ANY, &vmstate_ads7846, s);
 }
 
+static float ads7846_get_temp(TempSensor *obj, unsigned sensor_id)
+{
+    ADS7846State *s = (ADS7846State *)obj;
+
+    if (sensor_id) {
+        return (ADS_TEMP1_OFS_MILLIV - s->input[7]) / ADS_TEMP1_COEFF;
+    } else {
+        return (ADS_TEMP0_OFS_MILLIV - s->input[0]) / ADS_TEMP0_COEFF;
+    }
+}
+
+static void ads7846_set_temp(TempSensor *obj, unsigned sensor_id,
+                             float temp_C, Error **errp)
+{
+    ADS7846State *s = (ADS7846State *)obj;
+
+    if (sensor_id) {
+        s->input[7] = ADS_TEMP1_OFS_MILLIV - ADS_TEMP1_COEFF * temp_C;
+    } else {
+        s->input[0] = ADS_TEMP0_OFS_MILLIV - ADS_TEMP0_COEFF * temp_C;
+    }
+}
+
 static void ads7846_class_init(ObjectClass *klass, void *data)
 {
     SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
+    TempSensorClass *tc = TEMPSENSOR_INTERFACE_CLASS(klass);
 
     k->realize = ads7846_realize;
     k->transfer = ads7846_transfer;
+    tc->sensor_count = 2;
+    tc->set_temperature = ads7846_set_temp;
+    tc->get_temperature = ads7846_get_temp;
 }
 
 static const TypeInfo ads7846_info = {
@@ -170,6 +204,10 @@ static const TypeInfo ads7846_info = {
     .parent        = TYPE_SSI_SLAVE,
     .instance_size = sizeof(ADS7846State),
     .class_init    = ads7846_class_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_TEMPSENSOR_INTERFACE },
+        { }
+    },
 };
 
 static void ads7846_register_types(void)
Signature · 1 line
2.21.1
#17 lore raw

RFC PATCH 15/17 hw/ide/qdev: Implement the 'temperature-sensor' qdev interface

In reply to Philippe Mathieu-Daudé

IDE drives exposing a SMART interface report the air flow temperature. Let them implement the 'temperature-sensor' interface.

---
 hw/ide/qdev.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/ide/qdev.c +14 -0
index c22afdb8ee..144d6ab533 100644
@@ -32,6 +32,7 @@
 #include "sysemu/sysemu.h"
 #include "sysemu/runstate.h"
 #include "qapi/visitor.h"
+#include "hw/misc/temp-sensor.h"
 
 /* --------------------------------- */
 
@@ -270,6 +271,12 @@ static void ide_dev_instance_init(Object *obj)
     object_property_set_int(obj, -1, "bootindex", NULL);
 }
 
+static float ide_hd_get_temp(TempSensor *obj, unsigned sensor_id)
+{
+    /* See airflow-temperature-celsius in smart_attributes[] */
+    return 100.f - 0x45;
+}
+
 static void ide_hd_realize(IDEDevice *dev, Error **errp)
 {
     ide_dev_initfn(dev, IDE_HD, errp);
@@ -315,11 +322,14 @@ static void ide_hd_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     IDEDeviceClass *k = IDE_DEVICE_CLASS(klass);
+    TempSensorClass *tc = TEMPSENSOR_INTERFACE_CLASS(klass);
 
     k->realize  = ide_hd_realize;
     dc->fw_name = "drive";
     dc->desc    = "virtual IDE disk";
     device_class_set_props(dc, ide_hd_properties);
+    tc->sensor_count = 1;
+    tc->get_temperature = ide_hd_get_temp;
 }
 
 static const TypeInfo ide_hd_info = {
@@ -327,6 +337,10 @@ static const TypeInfo ide_hd_info = {
     .parent        = TYPE_IDE_DEVICE,
     .instance_size = sizeof(IDEDrive),
     .class_init    = ide_hd_class_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_TEMPSENSOR_INTERFACE },
+        { }
+    },
 };
 
 static Property ide_cd_properties[] = {
Signature · 1 line
2.21.1
#18 lore raw

RFC PATCH 16/17 hw/misc/imx6ul_ccm: Implement the 'temperature-sensor' qdev interface

In reply to Philippe Mathieu-Daudé

The IMX6UL Clock Control Module can report few temperatures. While the model doesn't implement it, we can still let it implement the 'temperature-sensor' interface.

---
 hw/misc/imx6ul_ccm.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/misc/imx6ul_ccm.c +7 -0
index a2fc1d0364..2711cdaf09 100644
@@ -16,6 +16,7 @@
 #include "hw/misc/imx6ul_ccm.h"
 #include "qemu/log.h"
 #include "qemu/module.h"
+#include "hw/misc/temp-sensor.h"
 
 #include "trace.h"
 
@@ -864,12 +865,14 @@ static void imx6ul_ccm_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     IMXCCMClass *ccm = IMX_CCM_CLASS(klass);
+    TempSensorClass *tc = TEMPSENSOR_INTERFACE_CLASS(klass);
 
     dc->reset = imx6ul_ccm_reset;
     dc->vmsd = &vmstate_imx6ul_ccm;
     dc->desc = "i.MX6UL Clock Control Module";
 
     ccm->get_clock_frequency = imx6ul_ccm_get_clock_frequency;
+    tc->sensor_count = 3;
 }
 
 static const TypeInfo imx6ul_ccm_info = {
@@ -878,6 +881,10 @@ static const TypeInfo imx6ul_ccm_info = {
     .instance_size = sizeof(IMX6ULCCMState),
     .instance_init = imx6ul_ccm_init,
     .class_init    = imx6ul_ccm_class_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_TEMPSENSOR_INTERFACE },
+        { }
+    },
 };
 
 static void imx6ul_ccm_register_types(void)
Signature · 1 line
2.21.1
#19 lore raw

RFC PATCH 17/17 tests/qtest/tmp105-test: Trivial test for TempSensorClass

In reply to Philippe Mathieu-Daudé

The qmp_tmp105_set_temperature() call will trigger TempSensorClass::set_temperature(), then hmp_info_temperature() triggers TempSensorClass::get_temperature().

---
 tests/qtest/tmp105-test.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tests/qtest/tmp105-test.c +18 -0
index f930a96b83..db7e505cbe 100644
@@ -41,11 +41,26 @@ static void qmp_tmp105_set_temperature(const char *id, int value)
     qobject_unref(response);
 }
 
+static char *hmp_info_temperature(const char *id)
+{
+    QDict *response;
+    char *output;
+
+    response = qmp("{'execute': 'human-monitor-command',"
+                   " 'arguments': {'command-line': 'info temp'}}");
+    g_assert(qdict_haskey(response, "return"));
+    output = g_strdup(qdict_get_try_str(response, "return"));
+    qobject_unref(response);
+
+    return output;
+}
+
 #define TMP105_PRECISION (1000/16)
 static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
 {
     uint16_t value;
     QI2CDevice *i2cdev = (QI2CDevice *)obj;
+    g_autofree char *s = NULL;
 
     value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
     g_assert_cmpuint(value, ==, 0);
@@ -56,6 +71,9 @@ static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
     qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
     value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
     g_assert_cmpuint(value, ==, 20000);
+    /* Test TempSensorClass via 'info temp' */
+    s = hmp_info_temperature(TMP105_TEST_ID);
+    g_assert(strstr(s, "tmp105-0                           20.00"));
 
     value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
     g_assert_cmphex(value, ==, 0x1400);
Signature · 1 line
2.21.1