sensor: Move hardware sensors from misc to a sensor directory
authorCorey Minyard <cminyard@mvista.com>
Tue, 18 May 2021 21:08:03 +0000 (16:08 -0500)
committerCorey Minyard <cminyard@mvista.com>
Thu, 17 Jun 2021 12:10:32 +0000 (07:10 -0500)
Lots of this are expected to be coming in, create a directory for them.

Also move the tmp105.h file into the include directory where it
should be.

Cc: Cédric Le Goater <clg@kaod.org>
Cc: Peter Maydell <peter.maydell@linaro.org>
Cc: Andrew Jeffery <andrew@aj.id.au>
Cc: Joel Stanley <joel@jms.id.au>
Cc: Andrzej Zaborowski <balrogg@gmail.com>
Cc: qemu-arm@nongnu.org
Signed-off-by: Corey Minyard <cminyard@mvista.com>
Acked-by: Cédric Le Goater <clg@kaod.org>
23 files changed:
hw/Kconfig
hw/arm/aspeed.c
hw/arm/nseries.c
hw/meson.build
hw/misc/Kconfig
hw/misc/emc141x.c [deleted file]
hw/misc/meson.build
hw/misc/tmp105.c [deleted file]
hw/misc/tmp105.h [deleted file]
hw/misc/tmp421.c [deleted file]
hw/sensor/Kconfig [new file with mode: 0644]
hw/sensor/emc141x.c [new file with mode: 0644]
hw/sensor/meson.build [new file with mode: 0644]
hw/sensor/tmp105.c [new file with mode: 0644]
hw/sensor/tmp421.c [new file with mode: 0644]
include/hw/misc/emc141x_regs.h [deleted file]
include/hw/misc/tmp105_regs.h [deleted file]
include/hw/sensor/emc141x_regs.h [new file with mode: 0644]
include/hw/sensor/tmp105.h [new file with mode: 0644]
include/hw/sensor/tmp105_regs.h [new file with mode: 0644]
tests/qtest/emc141x-test.c
tests/qtest/npcm7xx_smbus-test.c
tests/qtest/tmp105-test.c

index 805860f564516cc96c209b66e91d395af8fe79cc..8cb7664d70585cf21564c4bda7b1668ca26fdeb5 100644 (file)
@@ -32,6 +32,7 @@ source remote/Kconfig
 source rtc/Kconfig
 source scsi/Kconfig
 source sd/Kconfig
+source sensor/Kconfig
 source smbios/Kconfig
 source ssi/Kconfig
 source timer/Kconfig
index 1301e8fdffb28011c528f40bf8daef74d9c7f334..9d43e26c51bb24a0948e8d8f6e4406a21cfa894c 100644 (file)
@@ -17,7 +17,7 @@
 #include "hw/i2c/i2c_mux_pca954x.h"
 #include "hw/i2c/smbus_eeprom.h"
 #include "hw/misc/pca9552.h"
-#include "hw/misc/tmp105.h"
+#include "hw/sensor/tmp105.h"
 #include "hw/misc/led.h"
 #include "hw/qdev-properties.h"
 #include "sysemu/block-backend.h"
index 0aefa5d0f3ed31e838f026b240432b6b58f4c512..a10d8f53b50e6d7f51ab8d77e3f096592e51f0c9 100644 (file)
@@ -36,7 +36,7 @@
 #include "hw/display/blizzard.h"
 #include "hw/input/tsc2xxx.h"
 #include "hw/misc/cbus.h"
-#include "hw/misc/tmp105.h"
+#include "hw/sensor/tmp105.h"
 #include "hw/qdev-properties.h"
 #include "hw/block/flash.h"
 #include "hw/hw.h"
index ba0601e36edf55050c56376ce8f8c69ca3fd29f5..b3366c888ef61b3093091c260d23926c0524ca6e 100644 (file)
@@ -31,6 +31,7 @@ subdir('rdma')
 subdir('rtc')
 subdir('scsi')
 subdir('sd')
+subdir('sensor')
 subdir('smbios')
 subdir('ssi')
 subdir('timer')
index 996d45aff5ff741b57263f57dd4cd83583d09ffb..507058d8bffbf92f2abc1c7a2bf7bb4a77a955fd 100644 (file)
@@ -11,18 +11,6 @@ config ARMSSE_MHU
 config ARMSSE_CPU_PWRCTRL
     bool
 
-config TMP105
-    bool
-    depends on I2C
-
-config TMP421
-    bool
-    depends on I2C
-
-config EMC141X
-    bool
-    depends on I2C
-
 config ISA_DEBUG
     bool
     depends on ISA_BUS
diff --git a/hw/misc/emc141x.c b/hw/misc/emc141x.c
deleted file mode 100644 (file)
index f7c53d4..0000000
+++ /dev/null
@@ -1,326 +0,0 @@
-/*
- * SMSC EMC141X temperature sensor.
- *
- * Copyright (c) 2020 Bytedance Corporation
- * Written by John Wang <wangzhiqiang.bj@bytedance.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 or
- * (at your option) version 3 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "qemu/osdep.h"
-#include "hw/i2c/i2c.h"
-#include "migration/vmstate.h"
-#include "qapi/error.h"
-#include "qapi/visitor.h"
-#include "qemu/module.h"
-#include "qom/object.h"
-#include "hw/misc/emc141x_regs.h"
-
-#define SENSORS_COUNT_MAX    4
-
-struct EMC141XState {
-    I2CSlave parent_obj;
-    struct {
-        uint8_t raw_temp_min;
-        uint8_t raw_temp_current;
-        uint8_t raw_temp_max;
-    } sensor[SENSORS_COUNT_MAX];
-    uint8_t len;
-    uint8_t data;
-    uint8_t pointer;
-};
-
-struct EMC141XClass {
-    I2CSlaveClass parent_class;
-    uint8_t model;
-    unsigned sensors_count;
-};
-
-#define TYPE_EMC141X "emc141x"
-OBJECT_DECLARE_TYPE(EMC141XState, EMC141XClass, EMC141X)
-
-static void emc141x_get_temperature(Object *obj, Visitor *v, const char *name,
-                                    void *opaque, Error **errp)
-{
-    EMC141XState *s = EMC141X(obj);
-    EMC141XClass *sc = EMC141X_GET_CLASS(s);
-    int64_t value;
-    unsigned tempid;
-
-    if (sscanf(name, "temperature%u", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
-        return;
-    }
-
-    if (tempid >= sc->sensors_count) {
-        error_setg(errp, "error reading %s", name);
-        return;
-    }
-
-    value = s->sensor[tempid].raw_temp_current * 1000;
-
-    visit_type_int(v, name, &value, errp);
-}
-
-static void emc141x_set_temperature(Object *obj, Visitor *v, const char *name,
-                                    void *opaque, Error **errp)
-{
-    EMC141XState *s = EMC141X(obj);
-    EMC141XClass *sc = EMC141X_GET_CLASS(s);
-    int64_t temp;
-    unsigned tempid;
-
-    if (!visit_type_int(v, name, &temp, errp)) {
-        return;
-    }
-
-    if (sscanf(name, "temperature%u", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
-        return;
-    }
-
-    if (tempid >= sc->sensors_count) {
-        error_setg(errp, "error reading %s", name);
-        return;
-    }
-
-    s->sensor[tempid].raw_temp_current = temp / 1000;
-}
-
-static void emc141x_read(EMC141XState *s)
-{
-    EMC141XClass *sc = EMC141X_GET_CLASS(s);
-    switch (s->pointer) {
-    case EMC141X_DEVICE_ID:
-        s->data = sc->model;
-        break;
-    case EMC141X_MANUFACTURER_ID:
-        s->data = MANUFACTURER_ID;
-        break;
-    case EMC141X_REVISION:
-        s->data = REVISION;
-        break;
-    case EMC141X_TEMP_HIGH0:
-        s->data = s->sensor[0].raw_temp_current;
-        break;
-    case EMC141X_TEMP_HIGH1:
-        s->data = s->sensor[1].raw_temp_current;
-        break;
-    case EMC141X_TEMP_HIGH2:
-        s->data = s->sensor[2].raw_temp_current;
-        break;
-    case EMC141X_TEMP_HIGH3:
-        s->data = s->sensor[3].raw_temp_current;
-        break;
-    case EMC141X_TEMP_MAX_HIGH0:
-        s->data = s->sensor[0].raw_temp_max;
-        break;
-    case EMC141X_TEMP_MAX_HIGH1:
-        s->data = s->sensor[1].raw_temp_max;
-        break;
-    case EMC141X_TEMP_MAX_HIGH2:
-        s->data = s->sensor[2].raw_temp_max;
-        break;
-    case EMC141X_TEMP_MAX_HIGH3:
-        s->data = s->sensor[3].raw_temp_max;
-        break;
-    case EMC141X_TEMP_MIN_HIGH0:
-        s->data = s->sensor[0].raw_temp_min;
-        break;
-    case EMC141X_TEMP_MIN_HIGH1:
-        s->data = s->sensor[1].raw_temp_min;
-        break;
-    case EMC141X_TEMP_MIN_HIGH2:
-        s->data = s->sensor[2].raw_temp_min;
-        break;
-    case EMC141X_TEMP_MIN_HIGH3:
-        s->data = s->sensor[3].raw_temp_min;
-        break;
-    default:
-        s->data = 0;
-    }
-}
-
-static void emc141x_write(EMC141XState *s)
-{
-    switch (s->pointer) {
-    case EMC141X_TEMP_MAX_HIGH0:
-        s->sensor[0].raw_temp_max = s->data;
-        break;
-    case EMC141X_TEMP_MAX_HIGH1:
-        s->sensor[1].raw_temp_max = s->data;
-        break;
-    case EMC141X_TEMP_MAX_HIGH2:
-        s->sensor[2].raw_temp_max = s->data;
-        break;
-    case EMC141X_TEMP_MAX_HIGH3:
-        s->sensor[3].raw_temp_max = s->data;
-        break;
-    case EMC141X_TEMP_MIN_HIGH0:
-        s->sensor[0].raw_temp_min = s->data;
-        break;
-    case EMC141X_TEMP_MIN_HIGH1:
-        s->sensor[1].raw_temp_min = s->data;
-        break;
-    case EMC141X_TEMP_MIN_HIGH2:
-        s->sensor[2].raw_temp_min = s->data;
-        break;
-    case EMC141X_TEMP_MIN_HIGH3:
-        s->sensor[3].raw_temp_min = s->data;
-        break;
-    default:
-        s->data = 0;
-    }
-}
-
-static uint8_t emc141x_rx(I2CSlave *i2c)
-{
-    EMC141XState *s = EMC141X(i2c);
-
-    if (s->len == 0) {
-        s->len++;
-        return s->data;
-    } else {
-        return 0xff;
-    }
-}
-
-static int emc141x_tx(I2CSlave *i2c, uint8_t data)
-{
-    EMC141XState *s = EMC141X(i2c);
-
-    if (s->len == 0) {
-        /* first byte is the reg pointer */
-        s->pointer = data;
-        s->len++;
-    } else if (s->len == 1) {
-        s->data = data;
-        emc141x_write(s);
-    }
-
-    return 0;
-}
-
-static int emc141x_event(I2CSlave *i2c, enum i2c_event event)
-{
-    EMC141XState *s = EMC141X(i2c);
-
-    if (event == I2C_START_RECV) {
-        emc141x_read(s);
-    }
-
-    s->len = 0;
-    return 0;
-}
-
-static const VMStateDescription vmstate_emc141x = {
-    .name = "EMC141X",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .fields = (VMStateField[]) {
-        VMSTATE_UINT8(len, EMC141XState),
-        VMSTATE_UINT8(data, EMC141XState),
-        VMSTATE_UINT8(pointer, EMC141XState),
-        VMSTATE_I2C_SLAVE(parent_obj, EMC141XState),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
-static void emc141x_reset(DeviceState *dev)
-{
-    EMC141XState *s = EMC141X(dev);
-    int i;
-
-    for (i = 0; i < SENSORS_COUNT_MAX; i++) {
-        s->sensor[i].raw_temp_max = 0x55;
-    }
-    s->pointer = 0;
-    s->len = 0;
-}
-
-static void emc141x_initfn(Object *obj)
-{
-    object_property_add(obj, "temperature0", "int",
-                        emc141x_get_temperature,
-                        emc141x_set_temperature, NULL, NULL);
-    object_property_add(obj, "temperature1", "int",
-                        emc141x_get_temperature,
-                        emc141x_set_temperature, NULL, NULL);
-    object_property_add(obj, "temperature2", "int",
-                        emc141x_get_temperature,
-                        emc141x_set_temperature, NULL, NULL);
-    object_property_add(obj, "temperature3", "int",
-                        emc141x_get_temperature,
-                        emc141x_set_temperature, NULL, NULL);
-}
-
-static void emc141x_class_init(ObjectClass *klass, void *data)
-{
-    DeviceClass *dc = DEVICE_CLASS(klass);
-    I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
-
-    dc->reset = emc141x_reset;
-    k->event = emc141x_event;
-    k->recv = emc141x_rx;
-    k->send = emc141x_tx;
-    dc->vmsd = &vmstate_emc141x;
-}
-
-static void emc1413_class_init(ObjectClass *klass, void *data)
-{
-    EMC141XClass *ec = EMC141X_CLASS(klass);
-
-    emc141x_class_init(klass, data);
-    ec->model = EMC1413_DEVICE_ID;
-    ec->sensors_count = 3;
-}
-
-static void emc1414_class_init(ObjectClass *klass, void *data)
-{
-    EMC141XClass *ec = EMC141X_CLASS(klass);
-
-    emc141x_class_init(klass, data);
-    ec->model = EMC1414_DEVICE_ID;
-    ec->sensors_count = 4;
-}
-
-static const TypeInfo emc141x_info = {
-    .name          = TYPE_EMC141X,
-    .parent        = TYPE_I2C_SLAVE,
-    .instance_size = sizeof(EMC141XState),
-    .class_size    = sizeof(EMC141XClass),
-    .instance_init = emc141x_initfn,
-    .abstract      = true,
-};
-
-static const TypeInfo emc1413_info = {
-    .name          = "emc1413",
-    .parent        = TYPE_EMC141X,
-    .class_init    = emc1413_class_init,
-};
-
-static const TypeInfo emc1414_info = {
-    .name          = "emc1414",
-    .parent        = TYPE_EMC141X,
-    .class_init    = emc1414_class_init,
-};
-
-static void emc141x_register_types(void)
-{
-    type_register_static(&emc141x_info);
-    type_register_static(&emc1413_info);
-    type_register_static(&emc1414_info);
-}
-
-type_init(emc141x_register_types)
index b0a8ee8994f2bdf44d88bf0f928b71846fc53331..046c7e0c723dc11345a8f991717467a28f2de21a 100644 (file)
@@ -6,9 +6,6 @@ softmmu_ss.add(when: 'CONFIG_ISA_TESTDEV', if_true: files('pc-testdev.c'))
 softmmu_ss.add(when: 'CONFIG_PCA9552', if_true: files('pca9552.c'))
 softmmu_ss.add(when: 'CONFIG_PCI_TESTDEV', if_true: files('pci-testdev.c'))
 softmmu_ss.add(when: 'CONFIG_SGA', if_true: files('sga.c'))
-softmmu_ss.add(when: 'CONFIG_TMP105', if_true: files('tmp105.c'))
-softmmu_ss.add(when: 'CONFIG_TMP421', if_true: files('tmp421.c'))
-softmmu_ss.add(when: 'CONFIG_EMC141X', if_true: files('emc141x.c'))
 softmmu_ss.add(when: 'CONFIG_UNIMP', if_true: files('unimp.c'))
 softmmu_ss.add(when: 'CONFIG_EMPTY_SLOT', if_true: files('empty_slot.c'))
 softmmu_ss.add(when: 'CONFIG_LED', if_true: files('led.c'))
diff --git a/hw/misc/tmp105.c b/hw/misc/tmp105.c
deleted file mode 100644 (file)
index d299d9b..0000000
+++ /dev/null
@@ -1,328 +0,0 @@
-/*
- * Texas Instruments TMP105 temperature sensor.
- *
- * Copyright (C) 2008 Nokia Corporation
- * Written by Andrzej Zaborowski <andrew@openedhand.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 or
- * (at your option) version 3 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "qemu/osdep.h"
-#include "hw/i2c/i2c.h"
-#include "hw/irq.h"
-#include "migration/vmstate.h"
-#include "tmp105.h"
-#include "qapi/error.h"
-#include "qapi/visitor.h"
-#include "qemu/module.h"
-
-static void tmp105_interrupt_update(TMP105State *s)
-{
-    qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1));  /* POL */
-}
-
-static void tmp105_alarm_update(TMP105State *s)
-{
-    if ((s->config >> 0) & 1) {                                        /* SD */
-        if ((s->config >> 7) & 1)                              /* OS */
-            s->config &= ~(1 << 7);                            /* OS */
-        else
-            return;
-    }
-
-    if (s->config >> 1 & 1) {
-        /*
-         * TM == 1 : Interrupt mode. We signal Alert when the
-         * temperature rises above T_high, and expect the guest to clear
-         * it (eg by reading a device register).
-         */
-        if (s->detect_falling) {
-            if (s->temperature < s->limit[0]) {
-                s->alarm = 1;
-                s->detect_falling = false;
-            }
-        } else {
-            if (s->temperature >= s->limit[1]) {
-                s->alarm = 1;
-                s->detect_falling = true;
-            }
-        }
-    } else {
-        /*
-         * TM == 0 : Comparator mode. We signal Alert when the temperature
-         * rises above T_high, and stop signalling it when the temperature
-         * falls below T_low.
-         */
-        if (s->detect_falling) {
-            if (s->temperature < s->limit[0]) {
-                s->alarm = 0;
-                s->detect_falling = false;
-            }
-        } else {
-            if (s->temperature >= s->limit[1]) {
-                s->alarm = 1;
-                s->detect_falling = true;
-            }
-        }
-    }
-
-    tmp105_interrupt_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;
-
-    visit_type_int(v, name, &value, errp);
-}
-
-/* Units are 0.001 centigrades relative to 0 C.  s->temperature is 8.8
- * fixed point, so units are 1/256 centigrades.  A simple ratio will do.
- */
-static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name,
-                                   void *opaque, Error **errp)
-{
-    TMP105State *s = TMP105(obj);
-    int64_t temp;
-
-    if (!visit_type_int(v, name, &temp, errp)) {
-        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);
-}
-
-static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
-
-static void tmp105_read(TMP105State *s)
-{
-    s->len = 0;
-
-    if ((s->config >> 1) & 1) {                                        /* TM */
-        s->alarm = 0;
-        tmp105_interrupt_update(s);
-    }
-
-    switch (s->pointer & 3) {
-    case TMP105_REG_TEMPERATURE:
-        s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8);
-        s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) &
-                (0xf0 << ((~s->config >> 5) & 3));             /* R */
-        break;
-
-    case TMP105_REG_CONFIG:
-        s->buf[s->len ++] = s->config;
-        break;
-
-    case TMP105_REG_T_LOW:
-        s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
-        s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
-        break;
-
-    case TMP105_REG_T_HIGH:
-        s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8;
-        s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0;
-        break;
-    }
-}
-
-static void tmp105_write(TMP105State *s)
-{
-    switch (s->pointer & 3) {
-    case TMP105_REG_TEMPERATURE:
-        break;
-
-    case TMP105_REG_CONFIG:
-        if (s->buf[0] & ~s->config & (1 << 0))                 /* SD */
-            printf("%s: TMP105 shutdown\n", __func__);
-        s->config = s->buf[0];
-        s->faults = tmp105_faultq[(s->config >> 3) & 3];       /* F */
-        tmp105_alarm_update(s);
-        break;
-
-    case TMP105_REG_T_LOW:
-    case TMP105_REG_T_HIGH:
-        if (s->len >= 3)
-            s->limit[s->pointer & 1] = (int16_t)
-                    ((((uint16_t) s->buf[0]) << 8) | s->buf[1]);
-        tmp105_alarm_update(s);
-        break;
-    }
-}
-
-static uint8_t tmp105_rx(I2CSlave *i2c)
-{
-    TMP105State *s = TMP105(i2c);
-
-    if (s->len < 2) {
-        return s->buf[s->len ++];
-    } else {
-        return 0xff;
-    }
-}
-
-static int tmp105_tx(I2CSlave *i2c, uint8_t data)
-{
-    TMP105State *s = TMP105(i2c);
-
-    if (s->len == 0) {
-        s->pointer = data;
-        s->len++;
-    } else {
-        if (s->len <= 2) {
-            s->buf[s->len - 1] = data;
-        }
-        s->len++;
-        tmp105_write(s);
-    }
-
-    return 0;
-}
-
-static int tmp105_event(I2CSlave *i2c, enum i2c_event event)
-{
-    TMP105State *s = TMP105(i2c);
-
-    if (event == I2C_START_RECV) {
-        tmp105_read(s);
-    }
-
-    s->len = 0;
-    return 0;
-}
-
-static int tmp105_post_load(void *opaque, int version_id)
-{
-    TMP105State *s = opaque;
-
-    s->faults = tmp105_faultq[(s->config >> 3) & 3];           /* F */
-
-    tmp105_interrupt_update(s);
-    return 0;
-}
-
-static bool detect_falling_needed(void *opaque)
-{
-    TMP105State *s = opaque;
-
-    /*
-     * We only need to migrate the detect_falling bool if it's set;
-     * for migration from older machines we assume that it is false
-     * (ie temperature is not out of range).
-     */
-    return s->detect_falling;
-}
-
-static const VMStateDescription vmstate_tmp105_detect_falling = {
-    .name = "TMP105/detect-falling",
-    .version_id = 1,
-    .minimum_version_id = 1,
-    .needed = detect_falling_needed,
-    .fields = (VMStateField[]) {
-        VMSTATE_BOOL(detect_falling, TMP105State),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
-static const VMStateDescription vmstate_tmp105 = {
-    .name = "TMP105",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .post_load = tmp105_post_load,
-    .fields = (VMStateField[]) {
-        VMSTATE_UINT8(len, TMP105State),
-        VMSTATE_UINT8_ARRAY(buf, TMP105State, 2),
-        VMSTATE_UINT8(pointer, TMP105State),
-        VMSTATE_UINT8(config, TMP105State),
-        VMSTATE_INT16(temperature, TMP105State),
-        VMSTATE_INT16_ARRAY(limit, TMP105State, 2),
-        VMSTATE_UINT8(alarm, TMP105State),
-        VMSTATE_I2C_SLAVE(i2c, TMP105State),
-        VMSTATE_END_OF_LIST()
-    },
-    .subsections = (const VMStateDescription*[]) {
-        &vmstate_tmp105_detect_falling,
-        NULL
-    }
-};
-
-static void tmp105_reset(I2CSlave *i2c)
-{
-    TMP105State *s = TMP105(i2c);
-
-    s->temperature = 0;
-    s->pointer = 0;
-    s->config = 0;
-    s->faults = tmp105_faultq[(s->config >> 3) & 3];
-    s->alarm = 0;
-    s->detect_falling = false;
-
-    s->limit[0] = 0x4b00; /* T_LOW, 75 degrees C */
-    s->limit[1] = 0x5000; /* T_HIGH, 80 degrees C */
-
-    tmp105_interrupt_update(s);
-}
-
-static void tmp105_realize(DeviceState *dev, Error **errp)
-{
-    I2CSlave *i2c = I2C_SLAVE(dev);
-    TMP105State *s = TMP105(i2c);
-
-    qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
-
-    tmp105_reset(&s->i2c);
-}
-
-static void tmp105_initfn(Object *obj)
-{
-    object_property_add(obj, "temperature", "int",
-                        tmp105_get_temperature,
-                        tmp105_set_temperature, NULL, NULL);
-}
-
-static void tmp105_class_init(ObjectClass *klass, void *data)
-{
-    DeviceClass *dc = DEVICE_CLASS(klass);
-    I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
-
-    dc->realize = tmp105_realize;
-    k->event = tmp105_event;
-    k->recv = tmp105_rx;
-    k->send = tmp105_tx;
-    dc->vmsd = &vmstate_tmp105;
-}
-
-static const TypeInfo tmp105_info = {
-    .name          = TYPE_TMP105,
-    .parent        = TYPE_I2C_SLAVE,
-    .instance_size = sizeof(TMP105State),
-    .instance_init = tmp105_initfn,
-    .class_init    = tmp105_class_init,
-};
-
-static void tmp105_register_types(void)
-{
-    type_register_static(&tmp105_info);
-}
-
-type_init(tmp105_register_types)
diff --git a/hw/misc/tmp105.h b/hw/misc/tmp105.h
deleted file mode 100644 (file)
index 7c97071..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Texas Instruments TMP105 Temperature Sensor
- *
- * Browse the data sheet:
- *
- *    http://www.ti.com/lit/gpn/tmp105
- *
- * Copyright (C) 2012 Alex Horn <alex.horn@cs.ox.ac.uk>
- * Copyright (C) 2008-2012 Andrzej Zaborowski <balrogg@gmail.com>
- *
- * This work is licensed under the terms of the GNU GPL, version 2 or
- * later. See the COPYING file in the top-level directory.
- */
-#ifndef QEMU_TMP105_H
-#define QEMU_TMP105_H
-
-#include "hw/i2c/i2c.h"
-#include "hw/misc/tmp105_regs.h"
-#include "qom/object.h"
-
-#define TYPE_TMP105 "tmp105"
-OBJECT_DECLARE_SIMPLE_TYPE(TMP105State, TMP105)
-
-/**
- * TMP105State:
- * @config: Bits 5 and 6 (value 32 and 64) determine the precision of the
- * temperature. See Table 8 in the data sheet.
- *
- * @see_also: http://www.ti.com/lit/gpn/tmp105
- */
-struct TMP105State {
-    /*< private >*/
-    I2CSlave i2c;
-    /*< public >*/
-
-    uint8_t len;
-    uint8_t buf[2];
-    qemu_irq pin;
-
-    uint8_t pointer;
-    uint8_t config;
-    int16_t temperature;
-    int16_t limit[2];
-    int faults;
-    uint8_t alarm;
-    /*
-     * The TMP105 initially looks for a temperature rising above T_high;
-     * once this is detected, the condition it looks for next is the
-     * temperature falling below T_low. This flag is false when initially
-     * looking for T_high, true when looking for T_low.
-     */
-    bool detect_falling;
-};
-
-#endif
diff --git a/hw/misc/tmp421.c b/hw/misc/tmp421.c
deleted file mode 100644 (file)
index a3db57d..0000000
+++ /dev/null
@@ -1,391 +0,0 @@
-/*
- * Texas Instruments TMP421 temperature sensor.
- *
- * Copyright (c) 2016 IBM Corporation.
- *
- * Largely inspired by :
- *
- * Texas Instruments TMP105 temperature sensor.
- *
- * Copyright (C) 2008 Nokia Corporation
- * Written by Andrzej Zaborowski <andrew@openedhand.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 or
- * (at your option) version 3 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "qemu/osdep.h"
-#include "hw/i2c/i2c.h"
-#include "migration/vmstate.h"
-#include "qapi/error.h"
-#include "qapi/visitor.h"
-#include "qemu/module.h"
-#include "qom/object.h"
-
-/* Manufacturer / Device ID's */
-#define TMP421_MANUFACTURER_ID          0x55
-#define TMP421_DEVICE_ID                0x21
-#define TMP422_DEVICE_ID                0x22
-#define TMP423_DEVICE_ID                0x23
-
-typedef struct DeviceInfo {
-    int model;
-    const char *name;
-} DeviceInfo;
-
-static const DeviceInfo devices[] = {
-    { TMP421_DEVICE_ID, "tmp421" },
-    { TMP422_DEVICE_ID, "tmp422" },
-    { TMP423_DEVICE_ID, "tmp423" },
-};
-
-struct TMP421State {
-    /*< private >*/
-    I2CSlave i2c;
-    /*< public >*/
-
-    int16_t temperature[4];
-
-    uint8_t status;
-    uint8_t config[2];
-    uint8_t rate;
-
-    uint8_t len;
-    uint8_t buf[2];
-    uint8_t pointer;
-
-};
-
-struct TMP421Class {
-    I2CSlaveClass parent_class;
-    DeviceInfo *dev;
-};
-
-#define TYPE_TMP421 "tmp421-generic"
-OBJECT_DECLARE_TYPE(TMP421State, TMP421Class, TMP421)
-
-
-/* the TMP421 registers */
-#define TMP421_STATUS_REG               0x08
-#define    TMP421_STATUS_BUSY             (1 << 7)
-#define TMP421_CONFIG_REG_1             0x09
-#define    TMP421_CONFIG_RANGE            (1 << 2)
-#define    TMP421_CONFIG_SHUTDOWN         (1 << 6)
-#define TMP421_CONFIG_REG_2             0x0A
-#define    TMP421_CONFIG_RC               (1 << 2)
-#define    TMP421_CONFIG_LEN              (1 << 3)
-#define    TMP421_CONFIG_REN              (1 << 4)
-#define    TMP421_CONFIG_REN2             (1 << 5)
-#define    TMP421_CONFIG_REN3             (1 << 6)
-
-#define TMP421_CONVERSION_RATE_REG      0x0B
-#define TMP421_ONE_SHOT                 0x0F
-
-#define TMP421_RESET                    0xFC
-#define TMP421_MANUFACTURER_ID_REG      0xFE
-#define TMP421_DEVICE_ID_REG            0xFF
-
-#define TMP421_TEMP_MSB0                0x00
-#define TMP421_TEMP_MSB1                0x01
-#define TMP421_TEMP_MSB2                0x02
-#define TMP421_TEMP_MSB3                0x03
-#define TMP421_TEMP_LSB0                0x10
-#define TMP421_TEMP_LSB1                0x11
-#define TMP421_TEMP_LSB2                0x12
-#define TMP421_TEMP_LSB3                0x13
-
-static const int32_t mins[2] = { -40000, -55000 };
-static const int32_t maxs[2] = { 127000, 150000 };
-
-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;
-
-    if (sscanf(name, "temperature%d", &tempid) != 1) {
-        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
-        return;
-    }
-
-    if (tempid >= 4 || tempid < 0) {
-        error_setg(errp, "error reading %s", name);
-        return;
-    }
-
-    value = ((s->temperature[tempid] - offset) * 1000 + 128) / 256;
-
-    visit_type_int(v, name, &value, errp);
-}
-
-/* Units are 0.001 centigrades relative to 0 C.  s->temperature is 8.8
- * fixed point, so units are 1/256 centigrades.  A simple ratio will do.
- */
-static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
-                                   void *opaque, Error **errp)
-{
-    TMP421State *s = TMP421(obj);
-    int64_t temp;
-    bool ext_range = (s->config[0] & TMP421_CONFIG_RANGE);
-    int offset = ext_range * 64 * 256;
-    int tempid;
-
-    if (!visit_type_int(v, name, &temp, errp)) {
-        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;
-    }
-
-    if (tempid >= 4 || tempid < 0) {
-        error_setg(errp, "error reading %s", name);
-        return;
-    }
-
-    s->temperature[tempid] = (int16_t) ((temp * 256 - 128) / 1000) + offset;
-}
-
-static void tmp421_read(TMP421State *s)
-{
-    TMP421Class *sc = TMP421_GET_CLASS(s);
-
-    s->len = 0;
-
-    switch (s->pointer) {
-    case TMP421_MANUFACTURER_ID_REG:
-        s->buf[s->len++] = TMP421_MANUFACTURER_ID;
-        break;
-    case TMP421_DEVICE_ID_REG:
-        s->buf[s->len++] = sc->dev->model;
-        break;
-    case TMP421_CONFIG_REG_1:
-        s->buf[s->len++] = s->config[0];
-        break;
-    case TMP421_CONFIG_REG_2:
-        s->buf[s->len++] = s->config[1];
-        break;
-    case TMP421_CONVERSION_RATE_REG:
-        s->buf[s->len++] = s->rate;
-        break;
-    case TMP421_STATUS_REG:
-        s->buf[s->len++] = s->status;
-        break;
-
-        /* FIXME: check for channel enablement in config registers */
-    case TMP421_TEMP_MSB0:
-        s->buf[s->len++] = (((uint16_t) s->temperature[0]) >> 8);
-        s->buf[s->len++] = (((uint16_t) s->temperature[0]) >> 0) & 0xf0;
-        break;
-    case TMP421_TEMP_MSB1:
-        s->buf[s->len++] = (((uint16_t) s->temperature[1]) >> 8);
-        s->buf[s->len++] = (((uint16_t) s->temperature[1]) >> 0) & 0xf0;
-        break;
-    case TMP421_TEMP_MSB2:
-        s->buf[s->len++] = (((uint16_t) s->temperature[2]) >> 8);
-        s->buf[s->len++] = (((uint16_t) s->temperature[2]) >> 0) & 0xf0;
-        break;
-    case TMP421_TEMP_MSB3:
-        s->buf[s->len++] = (((uint16_t) s->temperature[3]) >> 8);
-        s->buf[s->len++] = (((uint16_t) s->temperature[3]) >> 0) & 0xf0;
-        break;
-    case TMP421_TEMP_LSB0:
-        s->buf[s->len++] = (((uint16_t) s->temperature[0]) >> 0) & 0xf0;
-        break;
-    case TMP421_TEMP_LSB1:
-        s->buf[s->len++] = (((uint16_t) s->temperature[1]) >> 0) & 0xf0;
-        break;
-    case TMP421_TEMP_LSB2:
-        s->buf[s->len++] = (((uint16_t) s->temperature[2]) >> 0) & 0xf0;
-        break;
-    case TMP421_TEMP_LSB3:
-        s->buf[s->len++] = (((uint16_t) s->temperature[3]) >> 0) & 0xf0;
-        break;
-    }
-}
-
-static void tmp421_reset(I2CSlave *i2c);
-
-static void tmp421_write(TMP421State *s)
-{
-    switch (s->pointer) {
-    case TMP421_CONVERSION_RATE_REG:
-        s->rate = s->buf[0];
-        break;
-    case TMP421_CONFIG_REG_1:
-        s->config[0] = s->buf[0];
-        break;
-    case TMP421_CONFIG_REG_2:
-        s->config[1] = s->buf[0];
-        break;
-    case TMP421_RESET:
-        tmp421_reset(I2C_SLAVE(s));
-        break;
-    }
-}
-
-static uint8_t tmp421_rx(I2CSlave *i2c)
-{
-    TMP421State *s = TMP421(i2c);
-
-    if (s->len < 2) {
-        return s->buf[s->len++];
-    } else {
-        return 0xff;
-    }
-}
-
-static int tmp421_tx(I2CSlave *i2c, uint8_t data)
-{
-    TMP421State *s = TMP421(i2c);
-
-    if (s->len == 0) {
-        /* first byte is the register pointer for a read or write
-         * operation */
-        s->pointer = data;
-        s->len++;
-    } else if (s->len == 1) {
-        /* second byte is the data to write. The device only supports
-         * one byte writes */
-        s->buf[0] = data;
-        tmp421_write(s);
-    }
-
-    return 0;
-}
-
-static int tmp421_event(I2CSlave *i2c, enum i2c_event event)
-{
-    TMP421State *s = TMP421(i2c);
-
-    if (event == I2C_START_RECV) {
-        tmp421_read(s);
-    }
-
-    s->len = 0;
-    return 0;
-}
-
-static const VMStateDescription vmstate_tmp421 = {
-    .name = "TMP421",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .fields = (VMStateField[]) {
-        VMSTATE_UINT8(len, TMP421State),
-        VMSTATE_UINT8_ARRAY(buf, TMP421State, 2),
-        VMSTATE_UINT8(pointer, TMP421State),
-        VMSTATE_UINT8_ARRAY(config, TMP421State, 2),
-        VMSTATE_UINT8(status, TMP421State),
-        VMSTATE_UINT8(rate, TMP421State),
-        VMSTATE_INT16_ARRAY(temperature, TMP421State, 4),
-        VMSTATE_I2C_SLAVE(i2c, TMP421State),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
-static void tmp421_reset(I2CSlave *i2c)
-{
-    TMP421State *s = TMP421(i2c);
-    TMP421Class *sc = TMP421_GET_CLASS(s);
-
-    memset(s->temperature, 0, sizeof(s->temperature));
-    s->pointer = 0;
-
-    s->config[0] = 0; /* TMP421_CONFIG_RANGE */
-
-     /* resistance correction and channel enablement */
-    switch (sc->dev->model) {
-    case TMP421_DEVICE_ID:
-        s->config[1] = 0x1c;
-        break;
-    case TMP422_DEVICE_ID:
-        s->config[1] = 0x3c;
-        break;
-    case TMP423_DEVICE_ID:
-        s->config[1] = 0x7c;
-        break;
-    }
-
-    s->rate = 0x7;       /* 8Hz */
-    s->status = 0;
-}
-
-static void tmp421_realize(DeviceState *dev, Error **errp)
-{
-    TMP421State *s = TMP421(dev);
-
-    tmp421_reset(&s->i2c);
-}
-
-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);
-
-    dc->realize = tmp421_realize;
-    k->event = tmp421_event;
-    k->recv = tmp421_rx;
-    k->send = tmp421_tx;
-    dc->vmsd = &vmstate_tmp421;
-    sc->dev = (DeviceInfo *) data;
-
-    object_class_property_add(klass, "temperature0", "int",
-                              tmp421_get_temperature,
-                              tmp421_set_temperature, NULL, NULL);
-    object_class_property_add(klass, "temperature1", "int",
-                              tmp421_get_temperature,
-                              tmp421_set_temperature, NULL, NULL);
-    object_class_property_add(klass, "temperature2", "int",
-                              tmp421_get_temperature,
-                              tmp421_set_temperature, NULL, NULL);
-    object_class_property_add(klass, "temperature3", "int",
-                              tmp421_get_temperature,
-                              tmp421_set_temperature, NULL, NULL);
-}
-
-static const TypeInfo tmp421_info = {
-    .name          = TYPE_TMP421,
-    .parent        = TYPE_I2C_SLAVE,
-    .instance_size = sizeof(TMP421State),
-    .class_size    = sizeof(TMP421Class),
-    .abstract      = true,
-};
-
-static void tmp421_register_types(void)
-{
-    int i;
-
-    type_register_static(&tmp421_info);
-    for (i = 0; i < ARRAY_SIZE(devices); ++i) {
-        TypeInfo ti = {
-            .name       = devices[i].name,
-            .parent     = TYPE_TMP421,
-            .class_init = tmp421_class_init,
-            .class_data = (void *) &devices[i],
-        };
-        type_register(&ti);
-    }
-}
-
-type_init(tmp421_register_types)
diff --git a/hw/sensor/Kconfig b/hw/sensor/Kconfig
new file mode 100644 (file)
index 0000000..097cb8f
--- /dev/null
@@ -0,0 +1,11 @@
+config TMP105
+    bool
+    depends on I2C
+
+config TMP421
+    bool
+    depends on I2C
+
+config EMC141X
+    bool
+    depends on I2C
diff --git a/hw/sensor/emc141x.c b/hw/sensor/emc141x.c
new file mode 100644 (file)
index 0000000..7ce8f4e
--- /dev/null
@@ -0,0 +1,326 @@
+/*
+ * SMSC EMC141X temperature sensor.
+ *
+ * Copyright (c) 2020 Bytedance Corporation
+ * Written by John Wang <wangzhiqiang.bj@bytedance.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 or
+ * (at your option) version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "qemu/osdep.h"
+#include "hw/i2c/i2c.h"
+#include "migration/vmstate.h"
+#include "qapi/error.h"
+#include "qapi/visitor.h"
+#include "qemu/module.h"
+#include "qom/object.h"
+#include "hw/sensor/emc141x_regs.h"
+
+#define SENSORS_COUNT_MAX    4
+
+struct EMC141XState {
+    I2CSlave parent_obj;
+    struct {
+        uint8_t raw_temp_min;
+        uint8_t raw_temp_current;
+        uint8_t raw_temp_max;
+    } sensor[SENSORS_COUNT_MAX];
+    uint8_t len;
+    uint8_t data;
+    uint8_t pointer;
+};
+
+struct EMC141XClass {
+    I2CSlaveClass parent_class;
+    uint8_t model;
+    unsigned sensors_count;
+};
+
+#define TYPE_EMC141X "emc141x"
+OBJECT_DECLARE_TYPE(EMC141XState, EMC141XClass, EMC141X)
+
+static void emc141x_get_temperature(Object *obj, Visitor *v, const char *name,
+                                    void *opaque, Error **errp)
+{
+    EMC141XState *s = EMC141X(obj);
+    EMC141XClass *sc = EMC141X_GET_CLASS(s);
+    int64_t value;
+    unsigned tempid;
+
+    if (sscanf(name, "temperature%u", &tempid) != 1) {
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
+        return;
+    }
+
+    if (tempid >= sc->sensors_count) {
+        error_setg(errp, "error reading %s", name);
+        return;
+    }
+
+    value = s->sensor[tempid].raw_temp_current * 1000;
+
+    visit_type_int(v, name, &value, errp);
+}
+
+static void emc141x_set_temperature(Object *obj, Visitor *v, const char *name,
+                                    void *opaque, Error **errp)
+{
+    EMC141XState *s = EMC141X(obj);
+    EMC141XClass *sc = EMC141X_GET_CLASS(s);
+    int64_t temp;
+    unsigned tempid;
+
+    if (!visit_type_int(v, name, &temp, errp)) {
+        return;
+    }
+
+    if (sscanf(name, "temperature%u", &tempid) != 1) {
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
+        return;
+    }
+
+    if (tempid >= sc->sensors_count) {
+        error_setg(errp, "error reading %s", name);
+        return;
+    }
+
+    s->sensor[tempid].raw_temp_current = temp / 1000;
+}
+
+static void emc141x_read(EMC141XState *s)
+{
+    EMC141XClass *sc = EMC141X_GET_CLASS(s);
+    switch (s->pointer) {
+    case EMC141X_DEVICE_ID:
+        s->data = sc->model;
+        break;
+    case EMC141X_MANUFACTURER_ID:
+        s->data = MANUFACTURER_ID;
+        break;
+    case EMC141X_REVISION:
+        s->data = REVISION;
+        break;
+    case EMC141X_TEMP_HIGH0:
+        s->data = s->sensor[0].raw_temp_current;
+        break;
+    case EMC141X_TEMP_HIGH1:
+        s->data = s->sensor[1].raw_temp_current;
+        break;
+    case EMC141X_TEMP_HIGH2:
+        s->data = s->sensor[2].raw_temp_current;
+        break;
+    case EMC141X_TEMP_HIGH3:
+        s->data = s->sensor[3].raw_temp_current;
+        break;
+    case EMC141X_TEMP_MAX_HIGH0:
+        s->data = s->sensor[0].raw_temp_max;
+        break;
+    case EMC141X_TEMP_MAX_HIGH1:
+        s->data = s->sensor[1].raw_temp_max;
+        break;
+    case EMC141X_TEMP_MAX_HIGH2:
+        s->data = s->sensor[2].raw_temp_max;
+        break;
+    case EMC141X_TEMP_MAX_HIGH3:
+        s->data = s->sensor[3].raw_temp_max;
+        break;
+    case EMC141X_TEMP_MIN_HIGH0:
+        s->data = s->sensor[0].raw_temp_min;
+        break;
+    case EMC141X_TEMP_MIN_HIGH1:
+        s->data = s->sensor[1].raw_temp_min;
+        break;
+    case EMC141X_TEMP_MIN_HIGH2:
+        s->data = s->sensor[2].raw_temp_min;
+        break;
+    case EMC141X_TEMP_MIN_HIGH3:
+        s->data = s->sensor[3].raw_temp_min;
+        break;
+    default:
+        s->data = 0;
+    }
+}
+
+static void emc141x_write(EMC141XState *s)
+{
+    switch (s->pointer) {
+    case EMC141X_TEMP_MAX_HIGH0:
+        s->sensor[0].raw_temp_max = s->data;
+        break;
+    case EMC141X_TEMP_MAX_HIGH1:
+        s->sensor[1].raw_temp_max = s->data;
+        break;
+    case EMC141X_TEMP_MAX_HIGH2:
+        s->sensor[2].raw_temp_max = s->data;
+        break;
+    case EMC141X_TEMP_MAX_HIGH3:
+        s->sensor[3].raw_temp_max = s->data;
+        break;
+    case EMC141X_TEMP_MIN_HIGH0:
+        s->sensor[0].raw_temp_min = s->data;
+        break;
+    case EMC141X_TEMP_MIN_HIGH1:
+        s->sensor[1].raw_temp_min = s->data;
+        break;
+    case EMC141X_TEMP_MIN_HIGH2:
+        s->sensor[2].raw_temp_min = s->data;
+        break;
+    case EMC141X_TEMP_MIN_HIGH3:
+        s->sensor[3].raw_temp_min = s->data;
+        break;
+    default:
+        s->data = 0;
+    }
+}
+
+static uint8_t emc141x_rx(I2CSlave *i2c)
+{
+    EMC141XState *s = EMC141X(i2c);
+
+    if (s->len == 0) {
+        s->len++;
+        return s->data;
+    } else {
+        return 0xff;
+    }
+}
+
+static int emc141x_tx(I2CSlave *i2c, uint8_t data)
+{
+    EMC141XState *s = EMC141X(i2c);
+
+    if (s->len == 0) {
+        /* first byte is the reg pointer */
+        s->pointer = data;
+        s->len++;
+    } else if (s->len == 1) {
+        s->data = data;
+        emc141x_write(s);
+    }
+
+    return 0;
+}
+
+static int emc141x_event(I2CSlave *i2c, enum i2c_event event)
+{
+    EMC141XState *s = EMC141X(i2c);
+
+    if (event == I2C_START_RECV) {
+        emc141x_read(s);
+    }
+
+    s->len = 0;
+    return 0;
+}
+
+static const VMStateDescription vmstate_emc141x = {
+    .name = "EMC141X",
+    .version_id = 0,
+    .minimum_version_id = 0,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT8(len, EMC141XState),
+        VMSTATE_UINT8(data, EMC141XState),
+        VMSTATE_UINT8(pointer, EMC141XState),
+        VMSTATE_I2C_SLAVE(parent_obj, EMC141XState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static void emc141x_reset(DeviceState *dev)
+{
+    EMC141XState *s = EMC141X(dev);
+    int i;
+
+    for (i = 0; i < SENSORS_COUNT_MAX; i++) {
+        s->sensor[i].raw_temp_max = 0x55;
+    }
+    s->pointer = 0;
+    s->len = 0;
+}
+
+static void emc141x_initfn(Object *obj)
+{
+    object_property_add(obj, "temperature0", "int",
+                        emc141x_get_temperature,
+                        emc141x_set_temperature, NULL, NULL);
+    object_property_add(obj, "temperature1", "int",
+                        emc141x_get_temperature,
+                        emc141x_set_temperature, NULL, NULL);
+    object_property_add(obj, "temperature2", "int",
+                        emc141x_get_temperature,
+                        emc141x_set_temperature, NULL, NULL);
+    object_property_add(obj, "temperature3", "int",
+                        emc141x_get_temperature,
+                        emc141x_set_temperature, NULL, NULL);
+}
+
+static void emc141x_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
+
+    dc->reset = emc141x_reset;
+    k->event = emc141x_event;
+    k->recv = emc141x_rx;
+    k->send = emc141x_tx;
+    dc->vmsd = &vmstate_emc141x;
+}
+
+static void emc1413_class_init(ObjectClass *klass, void *data)
+{
+    EMC141XClass *ec = EMC141X_CLASS(klass);
+
+    emc141x_class_init(klass, data);
+    ec->model = EMC1413_DEVICE_ID;
+    ec->sensors_count = 3;
+}
+
+static void emc1414_class_init(ObjectClass *klass, void *data)
+{
+    EMC141XClass *ec = EMC141X_CLASS(klass);
+
+    emc141x_class_init(klass, data);
+    ec->model = EMC1414_DEVICE_ID;
+    ec->sensors_count = 4;
+}
+
+static const TypeInfo emc141x_info = {
+    .name          = TYPE_EMC141X,
+    .parent        = TYPE_I2C_SLAVE,
+    .instance_size = sizeof(EMC141XState),
+    .class_size    = sizeof(EMC141XClass),
+    .instance_init = emc141x_initfn,
+    .abstract      = true,
+};
+
+static const TypeInfo emc1413_info = {
+    .name          = "emc1413",
+    .parent        = TYPE_EMC141X,
+    .class_init    = emc1413_class_init,
+};
+
+static const TypeInfo emc1414_info = {
+    .name          = "emc1414",
+    .parent        = TYPE_EMC141X,
+    .class_init    = emc1414_class_init,
+};
+
+static void emc141x_register_types(void)
+{
+    type_register_static(&emc141x_info);
+    type_register_static(&emc1413_info);
+    type_register_static(&emc1414_info);
+}
+
+type_init(emc141x_register_types)
diff --git a/hw/sensor/meson.build b/hw/sensor/meson.build
new file mode 100644 (file)
index 0000000..9e0f3ab
--- /dev/null
@@ -0,0 +1,3 @@
+softmmu_ss.add(when: 'CONFIG_TMP105', if_true: files('tmp105.c'))
+softmmu_ss.add(when: 'CONFIG_TMP421', if_true: files('tmp421.c'))
+softmmu_ss.add(when: 'CONFIG_EMC141X', if_true: files('emc141x.c'))
diff --git a/hw/sensor/tmp105.c b/hw/sensor/tmp105.c
new file mode 100644 (file)
index 0000000..2056449
--- /dev/null
@@ -0,0 +1,328 @@
+/*
+ * Texas Instruments TMP105 temperature sensor.
+ *
+ * Copyright (C) 2008 Nokia Corporation
+ * Written by Andrzej Zaborowski <andrew@openedhand.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 or
+ * (at your option) version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "qemu/osdep.h"
+#include "hw/i2c/i2c.h"
+#include "hw/irq.h"
+#include "migration/vmstate.h"
+#include "hw/sensor/tmp105.h"
+#include "qapi/error.h"
+#include "qapi/visitor.h"
+#include "qemu/module.h"
+
+static void tmp105_interrupt_update(TMP105State *s)
+{
+    qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1));  /* POL */
+}
+
+static void tmp105_alarm_update(TMP105State *s)
+{
+    if ((s->config >> 0) & 1) {                                        /* SD */
+        if ((s->config >> 7) & 1)                              /* OS */
+            s->config &= ~(1 << 7);                            /* OS */
+        else
+            return;
+    }
+
+    if (s->config >> 1 & 1) {
+        /*
+         * TM == 1 : Interrupt mode. We signal Alert when the
+         * temperature rises above T_high, and expect the guest to clear
+         * it (eg by reading a device register).
+         */
+        if (s->detect_falling) {
+            if (s->temperature < s->limit[0]) {
+                s->alarm = 1;
+                s->detect_falling = false;
+            }
+        } else {
+            if (s->temperature >= s->limit[1]) {
+                s->alarm = 1;
+                s->detect_falling = true;
+            }
+        }
+    } else {
+        /*
+         * TM == 0 : Comparator mode. We signal Alert when the temperature
+         * rises above T_high, and stop signalling it when the temperature
+         * falls below T_low.
+         */
+        if (s->detect_falling) {
+            if (s->temperature < s->limit[0]) {
+                s->alarm = 0;
+                s->detect_falling = false;
+            }
+        } else {
+            if (s->temperature >= s->limit[1]) {
+                s->alarm = 1;
+                s->detect_falling = true;
+            }
+        }
+    }
+
+    tmp105_interrupt_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;
+
+    visit_type_int(v, name, &value, errp);
+}
+
+/* Units are 0.001 centigrades relative to 0 C.  s->temperature is 8.8
+ * fixed point, so units are 1/256 centigrades.  A simple ratio will do.
+ */
+static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name,
+                                   void *opaque, Error **errp)
+{
+    TMP105State *s = TMP105(obj);
+    int64_t temp;
+
+    if (!visit_type_int(v, name, &temp, errp)) {
+        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);
+}
+
+static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
+
+static void tmp105_read(TMP105State *s)
+{
+    s->len = 0;
+
+    if ((s->config >> 1) & 1) {                                        /* TM */
+        s->alarm = 0;
+        tmp105_interrupt_update(s);
+    }
+
+    switch (s->pointer & 3) {
+    case TMP105_REG_TEMPERATURE:
+        s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8);
+        s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) &
+                (0xf0 << ((~s->config >> 5) & 3));             /* R */
+        break;
+
+    case TMP105_REG_CONFIG:
+        s->buf[s->len ++] = s->config;
+        break;
+
+    case TMP105_REG_T_LOW:
+        s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
+        s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
+        break;
+
+    case TMP105_REG_T_HIGH:
+        s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8;
+        s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0;
+        break;
+    }
+}
+
+static void tmp105_write(TMP105State *s)
+{
+    switch (s->pointer & 3) {
+    case TMP105_REG_TEMPERATURE:
+        break;
+
+    case TMP105_REG_CONFIG:
+        if (s->buf[0] & ~s->config & (1 << 0))                 /* SD */
+            printf("%s: TMP105 shutdown\n", __func__);
+        s->config = s->buf[0];
+        s->faults = tmp105_faultq[(s->config >> 3) & 3];       /* F */
+        tmp105_alarm_update(s);
+        break;
+
+    case TMP105_REG_T_LOW:
+    case TMP105_REG_T_HIGH:
+        if (s->len >= 3)
+            s->limit[s->pointer & 1] = (int16_t)
+                    ((((uint16_t) s->buf[0]) << 8) | s->buf[1]);
+        tmp105_alarm_update(s);
+        break;
+    }
+}
+
+static uint8_t tmp105_rx(I2CSlave *i2c)
+{
+    TMP105State *s = TMP105(i2c);
+
+    if (s->len < 2) {
+        return s->buf[s->len ++];
+    } else {
+        return 0xff;
+    }
+}
+
+static int tmp105_tx(I2CSlave *i2c, uint8_t data)
+{
+    TMP105State *s = TMP105(i2c);
+
+    if (s->len == 0) {
+        s->pointer = data;
+        s->len++;
+    } else {
+        if (s->len <= 2) {
+            s->buf[s->len - 1] = data;
+        }
+        s->len++;
+        tmp105_write(s);
+    }
+
+    return 0;
+}
+
+static int tmp105_event(I2CSlave *i2c, enum i2c_event event)
+{
+    TMP105State *s = TMP105(i2c);
+
+    if (event == I2C_START_RECV) {
+        tmp105_read(s);
+    }
+
+    s->len = 0;
+    return 0;
+}
+
+static int tmp105_post_load(void *opaque, int version_id)
+{
+    TMP105State *s = opaque;
+
+    s->faults = tmp105_faultq[(s->config >> 3) & 3];           /* F */
+
+    tmp105_interrupt_update(s);
+    return 0;
+}
+
+static bool detect_falling_needed(void *opaque)
+{
+    TMP105State *s = opaque;
+
+    /*
+     * We only need to migrate the detect_falling bool if it's set;
+     * for migration from older machines we assume that it is false
+     * (ie temperature is not out of range).
+     */
+    return s->detect_falling;
+}
+
+static const VMStateDescription vmstate_tmp105_detect_falling = {
+    .name = "TMP105/detect-falling",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = detect_falling_needed,
+    .fields = (VMStateField[]) {
+        VMSTATE_BOOL(detect_falling, TMP105State),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static const VMStateDescription vmstate_tmp105 = {
+    .name = "TMP105",
+    .version_id = 0,
+    .minimum_version_id = 0,
+    .post_load = tmp105_post_load,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT8(len, TMP105State),
+        VMSTATE_UINT8_ARRAY(buf, TMP105State, 2),
+        VMSTATE_UINT8(pointer, TMP105State),
+        VMSTATE_UINT8(config, TMP105State),
+        VMSTATE_INT16(temperature, TMP105State),
+        VMSTATE_INT16_ARRAY(limit, TMP105State, 2),
+        VMSTATE_UINT8(alarm, TMP105State),
+        VMSTATE_I2C_SLAVE(i2c, TMP105State),
+        VMSTATE_END_OF_LIST()
+    },
+    .subsections = (const VMStateDescription*[]) {
+        &vmstate_tmp105_detect_falling,
+        NULL
+    }
+};
+
+static void tmp105_reset(I2CSlave *i2c)
+{
+    TMP105State *s = TMP105(i2c);
+
+    s->temperature = 0;
+    s->pointer = 0;
+    s->config = 0;
+    s->faults = tmp105_faultq[(s->config >> 3) & 3];
+    s->alarm = 0;
+    s->detect_falling = false;
+
+    s->limit[0] = 0x4b00; /* T_LOW, 75 degrees C */
+    s->limit[1] = 0x5000; /* T_HIGH, 80 degrees C */
+
+    tmp105_interrupt_update(s);
+}
+
+static void tmp105_realize(DeviceState *dev, Error **errp)
+{
+    I2CSlave *i2c = I2C_SLAVE(dev);
+    TMP105State *s = TMP105(i2c);
+
+    qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
+
+    tmp105_reset(&s->i2c);
+}
+
+static void tmp105_initfn(Object *obj)
+{
+    object_property_add(obj, "temperature", "int",
+                        tmp105_get_temperature,
+                        tmp105_set_temperature, NULL, NULL);
+}
+
+static void tmp105_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
+
+    dc->realize = tmp105_realize;
+    k->event = tmp105_event;
+    k->recv = tmp105_rx;
+    k->send = tmp105_tx;
+    dc->vmsd = &vmstate_tmp105;
+}
+
+static const TypeInfo tmp105_info = {
+    .name          = TYPE_TMP105,
+    .parent        = TYPE_I2C_SLAVE,
+    .instance_size = sizeof(TMP105State),
+    .instance_init = tmp105_initfn,
+    .class_init    = tmp105_class_init,
+};
+
+static void tmp105_register_types(void)
+{
+    type_register_static(&tmp105_info);
+}
+
+type_init(tmp105_register_types)
diff --git a/hw/sensor/tmp421.c b/hw/sensor/tmp421.c
new file mode 100644 (file)
index 0000000..a3db57d
--- /dev/null
@@ -0,0 +1,391 @@
+/*
+ * Texas Instruments TMP421 temperature sensor.
+ *
+ * Copyright (c) 2016 IBM Corporation.
+ *
+ * Largely inspired by :
+ *
+ * Texas Instruments TMP105 temperature sensor.
+ *
+ * Copyright (C) 2008 Nokia Corporation
+ * Written by Andrzej Zaborowski <andrew@openedhand.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 or
+ * (at your option) version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "qemu/osdep.h"
+#include "hw/i2c/i2c.h"
+#include "migration/vmstate.h"
+#include "qapi/error.h"
+#include "qapi/visitor.h"
+#include "qemu/module.h"
+#include "qom/object.h"
+
+/* Manufacturer / Device ID's */
+#define TMP421_MANUFACTURER_ID          0x55
+#define TMP421_DEVICE_ID                0x21
+#define TMP422_DEVICE_ID                0x22
+#define TMP423_DEVICE_ID                0x23
+
+typedef struct DeviceInfo {
+    int model;
+    const char *name;
+} DeviceInfo;
+
+static const DeviceInfo devices[] = {
+    { TMP421_DEVICE_ID, "tmp421" },
+    { TMP422_DEVICE_ID, "tmp422" },
+    { TMP423_DEVICE_ID, "tmp423" },
+};
+
+struct TMP421State {
+    /*< private >*/
+    I2CSlave i2c;
+    /*< public >*/
+
+    int16_t temperature[4];
+
+    uint8_t status;
+    uint8_t config[2];
+    uint8_t rate;
+
+    uint8_t len;
+    uint8_t buf[2];
+    uint8_t pointer;
+
+};
+
+struct TMP421Class {
+    I2CSlaveClass parent_class;
+    DeviceInfo *dev;
+};
+
+#define TYPE_TMP421 "tmp421-generic"
+OBJECT_DECLARE_TYPE(TMP421State, TMP421Class, TMP421)
+
+
+/* the TMP421 registers */
+#define TMP421_STATUS_REG               0x08
+#define    TMP421_STATUS_BUSY             (1 << 7)
+#define TMP421_CONFIG_REG_1             0x09
+#define    TMP421_CONFIG_RANGE            (1 << 2)
+#define    TMP421_CONFIG_SHUTDOWN         (1 << 6)
+#define TMP421_CONFIG_REG_2             0x0A
+#define    TMP421_CONFIG_RC               (1 << 2)
+#define    TMP421_CONFIG_LEN              (1 << 3)
+#define    TMP421_CONFIG_REN              (1 << 4)
+#define    TMP421_CONFIG_REN2             (1 << 5)
+#define    TMP421_CONFIG_REN3             (1 << 6)
+
+#define TMP421_CONVERSION_RATE_REG      0x0B
+#define TMP421_ONE_SHOT                 0x0F
+
+#define TMP421_RESET                    0xFC
+#define TMP421_MANUFACTURER_ID_REG      0xFE
+#define TMP421_DEVICE_ID_REG            0xFF
+
+#define TMP421_TEMP_MSB0                0x00
+#define TMP421_TEMP_MSB1                0x01
+#define TMP421_TEMP_MSB2                0x02
+#define TMP421_TEMP_MSB3                0x03
+#define TMP421_TEMP_LSB0                0x10
+#define TMP421_TEMP_LSB1                0x11
+#define TMP421_TEMP_LSB2                0x12
+#define TMP421_TEMP_LSB3                0x13
+
+static const int32_t mins[2] = { -40000, -55000 };
+static const int32_t maxs[2] = { 127000, 150000 };
+
+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;
+
+    if (sscanf(name, "temperature%d", &tempid) != 1) {
+        error_setg(errp, "error reading %s: %s", name, g_strerror(errno));
+        return;
+    }
+
+    if (tempid >= 4 || tempid < 0) {
+        error_setg(errp, "error reading %s", name);
+        return;
+    }
+
+    value = ((s->temperature[tempid] - offset) * 1000 + 128) / 256;
+
+    visit_type_int(v, name, &value, errp);
+}
+
+/* Units are 0.001 centigrades relative to 0 C.  s->temperature is 8.8
+ * fixed point, so units are 1/256 centigrades.  A simple ratio will do.
+ */
+static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name,
+                                   void *opaque, Error **errp)
+{
+    TMP421State *s = TMP421(obj);
+    int64_t temp;
+    bool ext_range = (s->config[0] & TMP421_CONFIG_RANGE);
+    int offset = ext_range * 64 * 256;
+    int tempid;
+
+    if (!visit_type_int(v, name, &temp, errp)) {
+        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;
+    }
+
+    if (tempid >= 4 || tempid < 0) {
+        error_setg(errp, "error reading %s", name);
+        return;
+    }
+
+    s->temperature[tempid] = (int16_t) ((temp * 256 - 128) / 1000) + offset;
+}
+
+static void tmp421_read(TMP421State *s)
+{
+    TMP421Class *sc = TMP421_GET_CLASS(s);
+
+    s->len = 0;
+
+    switch (s->pointer) {
+    case TMP421_MANUFACTURER_ID_REG:
+        s->buf[s->len++] = TMP421_MANUFACTURER_ID;
+        break;
+    case TMP421_DEVICE_ID_REG:
+        s->buf[s->len++] = sc->dev->model;
+        break;
+    case TMP421_CONFIG_REG_1:
+        s->buf[s->len++] = s->config[0];
+        break;
+    case TMP421_CONFIG_REG_2:
+        s->buf[s->len++] = s->config[1];
+        break;
+    case TMP421_CONVERSION_RATE_REG:
+        s->buf[s->len++] = s->rate;
+        break;
+    case TMP421_STATUS_REG:
+        s->buf[s->len++] = s->status;
+        break;
+
+        /* FIXME: check for channel enablement in config registers */
+    case TMP421_TEMP_MSB0:
+        s->buf[s->len++] = (((uint16_t) s->temperature[0]) >> 8);
+        s->buf[s->len++] = (((uint16_t) s->temperature[0]) >> 0) & 0xf0;
+        break;
+    case TMP421_TEMP_MSB1:
+        s->buf[s->len++] = (((uint16_t) s->temperature[1]) >> 8);
+        s->buf[s->len++] = (((uint16_t) s->temperature[1]) >> 0) & 0xf0;
+        break;
+    case TMP421_TEMP_MSB2:
+        s->buf[s->len++] = (((uint16_t) s->temperature[2]) >> 8);
+        s->buf[s->len++] = (((uint16_t) s->temperature[2]) >> 0) & 0xf0;
+        break;
+    case TMP421_TEMP_MSB3:
+        s->buf[s->len++] = (((uint16_t) s->temperature[3]) >> 8);
+        s->buf[s->len++] = (((uint16_t) s->temperature[3]) >> 0) & 0xf0;
+        break;
+    case TMP421_TEMP_LSB0:
+        s->buf[s->len++] = (((uint16_t) s->temperature[0]) >> 0) & 0xf0;
+        break;
+    case TMP421_TEMP_LSB1:
+        s->buf[s->len++] = (((uint16_t) s->temperature[1]) >> 0) & 0xf0;
+        break;
+    case TMP421_TEMP_LSB2:
+        s->buf[s->len++] = (((uint16_t) s->temperature[2]) >> 0) & 0xf0;
+        break;
+    case TMP421_TEMP_LSB3:
+        s->buf[s->len++] = (((uint16_t) s->temperature[3]) >> 0) & 0xf0;
+        break;
+    }
+}
+
+static void tmp421_reset(I2CSlave *i2c);
+
+static void tmp421_write(TMP421State *s)
+{
+    switch (s->pointer) {
+    case TMP421_CONVERSION_RATE_REG:
+        s->rate = s->buf[0];
+        break;
+    case TMP421_CONFIG_REG_1:
+        s->config[0] = s->buf[0];
+        break;
+    case TMP421_CONFIG_REG_2:
+        s->config[1] = s->buf[0];
+        break;
+    case TMP421_RESET:
+        tmp421_reset(I2C_SLAVE(s));
+        break;
+    }
+}
+
+static uint8_t tmp421_rx(I2CSlave *i2c)
+{
+    TMP421State *s = TMP421(i2c);
+
+    if (s->len < 2) {
+        return s->buf[s->len++];
+    } else {
+        return 0xff;
+    }
+}
+
+static int tmp421_tx(I2CSlave *i2c, uint8_t data)
+{
+    TMP421State *s = TMP421(i2c);
+
+    if (s->len == 0) {
+        /* first byte is the register pointer for a read or write
+         * operation */
+        s->pointer = data;
+        s->len++;
+    } else if (s->len == 1) {
+        /* second byte is the data to write. The device only supports
+         * one byte writes */
+        s->buf[0] = data;
+        tmp421_write(s);
+    }
+
+    return 0;
+}
+
+static int tmp421_event(I2CSlave *i2c, enum i2c_event event)
+{
+    TMP421State *s = TMP421(i2c);
+
+    if (event == I2C_START_RECV) {
+        tmp421_read(s);
+    }
+
+    s->len = 0;
+    return 0;
+}
+
+static const VMStateDescription vmstate_tmp421 = {
+    .name = "TMP421",
+    .version_id = 0,
+    .minimum_version_id = 0,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT8(len, TMP421State),
+        VMSTATE_UINT8_ARRAY(buf, TMP421State, 2),
+        VMSTATE_UINT8(pointer, TMP421State),
+        VMSTATE_UINT8_ARRAY(config, TMP421State, 2),
+        VMSTATE_UINT8(status, TMP421State),
+        VMSTATE_UINT8(rate, TMP421State),
+        VMSTATE_INT16_ARRAY(temperature, TMP421State, 4),
+        VMSTATE_I2C_SLAVE(i2c, TMP421State),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static void tmp421_reset(I2CSlave *i2c)
+{
+    TMP421State *s = TMP421(i2c);
+    TMP421Class *sc = TMP421_GET_CLASS(s);
+
+    memset(s->temperature, 0, sizeof(s->temperature));
+    s->pointer = 0;
+
+    s->config[0] = 0; /* TMP421_CONFIG_RANGE */
+
+     /* resistance correction and channel enablement */
+    switch (sc->dev->model) {
+    case TMP421_DEVICE_ID:
+        s->config[1] = 0x1c;
+        break;
+    case TMP422_DEVICE_ID:
+        s->config[1] = 0x3c;
+        break;
+    case TMP423_DEVICE_ID:
+        s->config[1] = 0x7c;
+        break;
+    }
+
+    s->rate = 0x7;       /* 8Hz */
+    s->status = 0;
+}
+
+static void tmp421_realize(DeviceState *dev, Error **errp)
+{
+    TMP421State *s = TMP421(dev);
+
+    tmp421_reset(&s->i2c);
+}
+
+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);
+
+    dc->realize = tmp421_realize;
+    k->event = tmp421_event;
+    k->recv = tmp421_rx;
+    k->send = tmp421_tx;
+    dc->vmsd = &vmstate_tmp421;
+    sc->dev = (DeviceInfo *) data;
+
+    object_class_property_add(klass, "temperature0", "int",
+                              tmp421_get_temperature,
+                              tmp421_set_temperature, NULL, NULL);
+    object_class_property_add(klass, "temperature1", "int",
+                              tmp421_get_temperature,
+                              tmp421_set_temperature, NULL, NULL);
+    object_class_property_add(klass, "temperature2", "int",
+                              tmp421_get_temperature,
+                              tmp421_set_temperature, NULL, NULL);
+    object_class_property_add(klass, "temperature3", "int",
+                              tmp421_get_temperature,
+                              tmp421_set_temperature, NULL, NULL);
+}
+
+static const TypeInfo tmp421_info = {
+    .name          = TYPE_TMP421,
+    .parent        = TYPE_I2C_SLAVE,
+    .instance_size = sizeof(TMP421State),
+    .class_size    = sizeof(TMP421Class),
+    .abstract      = true,
+};
+
+static void tmp421_register_types(void)
+{
+    int i;
+
+    type_register_static(&tmp421_info);
+    for (i = 0; i < ARRAY_SIZE(devices); ++i) {
+        TypeInfo ti = {
+            .name       = devices[i].name,
+            .parent     = TYPE_TMP421,
+            .class_init = tmp421_class_init,
+            .class_data = (void *) &devices[i],
+        };
+        type_register(&ti);
+    }
+}
+
+type_init(tmp421_register_types)
diff --git a/include/hw/misc/emc141x_regs.h b/include/hw/misc/emc141x_regs.h
deleted file mode 100644 (file)
index 0560fb7..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * SMSC EMC141X temperature sensor.
- *
- * Browse the data sheet:
- *
- *    http://ww1.microchip.com/downloads/en/DeviceDoc/20005274A.pdf
- *
- * This work is licensed under the terms of the GNU GPL, version 2 or
- * later. See the COPYING file in the top-level directory.
- */
-
-#ifndef TMP105_REGS_H
-#define TMP105_REGS_H
-
-#define EMC1413_DEVICE_ID                0x21
-#define EMC1414_DEVICE_ID                0x25
-#define MANUFACTURER_ID                  0x5d
-#define REVISION                         0x04
-
-/* the EMC141X registers */
-#define EMC141X_TEMP_HIGH0               0x00
-#define EMC141X_TEMP_HIGH1               0x01
-#define EMC141X_TEMP_HIGH2               0x23
-#define EMC141X_TEMP_HIGH3               0x2a
-#define EMC141X_TEMP_MAX_HIGH0           0x05
-#define EMC141X_TEMP_MIN_HIGH0           0x06
-#define EMC141X_TEMP_MAX_HIGH1           0x07
-#define EMC141X_TEMP_MIN_HIGH1           0x08
-#define EMC141X_TEMP_MAX_HIGH2           0x15
-#define EMC141X_TEMP_MIN_HIGH2           0x16
-#define EMC141X_TEMP_MAX_HIGH3           0x2c
-#define EMC141X_TEMP_MIN_HIGH3           0x2d
-#define EMC141X_DEVICE_ID                0xfd
-#define EMC141X_MANUFACTURER_ID          0xfe
-#define EMC141X_REVISION                 0xff
-
-#endif
diff --git a/include/hw/misc/tmp105_regs.h b/include/hw/misc/tmp105_regs.h
deleted file mode 100644 (file)
index ef015ee..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Texas Instruments TMP105 Temperature Sensor I2C messages
- *
- * Browse the data sheet:
- *
- *    http://www.ti.com/lit/gpn/tmp105
- *
- * Copyright (C) 2012 Alex Horn <alex.horn@cs.ox.ac.uk>
- * Copyright (C) 2008-2012 Andrzej Zaborowski <balrogg@gmail.com>
- *
- * This work is licensed under the terms of the GNU GPL, version 2 or
- * later. See the COPYING file in the top-level directory.
- */
-
-#ifndef TMP105_REGS_H
-#define TMP105_REGS_H
-
-/**
- * TMP105Reg:
- * @TMP105_REG_TEMPERATURE: Temperature register
- * @TMP105_REG_CONFIG: Configuration register
- * @TMP105_REG_T_LOW: Low temperature register (also known as T_hyst)
- * @TMP105_REG_T_HIGH: High temperature register (also known as T_OS)
- *
- * The following temperature sensors are
- * compatible with the TMP105 registers:
- * - adt75
- * - ds1775
- * - ds75
- * - lm75
- * - lm75a
- * - max6625
- * - max6626
- * - mcp980x
- * - stds75
- * - tcn75
- * - tmp100
- * - tmp101
- * - tmp105
- * - tmp175
- * - tmp275
- * - tmp75
- **/
-typedef enum TMP105Reg {
-    TMP105_REG_TEMPERATURE = 0,
-    TMP105_REG_CONFIG,
-    TMP105_REG_T_LOW,
-    TMP105_REG_T_HIGH,
-} TMP105Reg;
-
-#endif
diff --git a/include/hw/sensor/emc141x_regs.h b/include/hw/sensor/emc141x_regs.h
new file mode 100644 (file)
index 0000000..0560fb7
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * SMSC EMC141X temperature sensor.
+ *
+ * Browse the data sheet:
+ *
+ *    http://ww1.microchip.com/downloads/en/DeviceDoc/20005274A.pdf
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later. See the COPYING file in the top-level directory.
+ */
+
+#ifndef TMP105_REGS_H
+#define TMP105_REGS_H
+
+#define EMC1413_DEVICE_ID                0x21
+#define EMC1414_DEVICE_ID                0x25
+#define MANUFACTURER_ID                  0x5d
+#define REVISION                         0x04
+
+/* the EMC141X registers */
+#define EMC141X_TEMP_HIGH0               0x00
+#define EMC141X_TEMP_HIGH1               0x01
+#define EMC141X_TEMP_HIGH2               0x23
+#define EMC141X_TEMP_HIGH3               0x2a
+#define EMC141X_TEMP_MAX_HIGH0           0x05
+#define EMC141X_TEMP_MIN_HIGH0           0x06
+#define EMC141X_TEMP_MAX_HIGH1           0x07
+#define EMC141X_TEMP_MIN_HIGH1           0x08
+#define EMC141X_TEMP_MAX_HIGH2           0x15
+#define EMC141X_TEMP_MIN_HIGH2           0x16
+#define EMC141X_TEMP_MAX_HIGH3           0x2c
+#define EMC141X_TEMP_MIN_HIGH3           0x2d
+#define EMC141X_DEVICE_ID                0xfd
+#define EMC141X_MANUFACTURER_ID          0xfe
+#define EMC141X_REVISION                 0xff
+
+#endif
diff --git a/include/hw/sensor/tmp105.h b/include/hw/sensor/tmp105.h
new file mode 100644 (file)
index 0000000..244e298
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Texas Instruments TMP105 Temperature Sensor
+ *
+ * Browse the data sheet:
+ *
+ *    http://www.ti.com/lit/gpn/tmp105
+ *
+ * Copyright (C) 2012 Alex Horn <alex.horn@cs.ox.ac.uk>
+ * Copyright (C) 2008-2012 Andrzej Zaborowski <balrogg@gmail.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later. See the COPYING file in the top-level directory.
+ */
+#ifndef QEMU_TMP105_H
+#define QEMU_TMP105_H
+
+#include "hw/i2c/i2c.h"
+#include "hw/sensor/tmp105_regs.h"
+#include "qom/object.h"
+
+#define TYPE_TMP105 "tmp105"
+OBJECT_DECLARE_SIMPLE_TYPE(TMP105State, TMP105)
+
+/**
+ * TMP105State:
+ * @config: Bits 5 and 6 (value 32 and 64) determine the precision of the
+ * temperature. See Table 8 in the data sheet.
+ *
+ * @see_also: http://www.ti.com/lit/gpn/tmp105
+ */
+struct TMP105State {
+    /*< private >*/
+    I2CSlave i2c;
+    /*< public >*/
+
+    uint8_t len;
+    uint8_t buf[2];
+    qemu_irq pin;
+
+    uint8_t pointer;
+    uint8_t config;
+    int16_t temperature;
+    int16_t limit[2];
+    int faults;
+    uint8_t alarm;
+    /*
+     * The TMP105 initially looks for a temperature rising above T_high;
+     * once this is detected, the condition it looks for next is the
+     * temperature falling below T_low. This flag is false when initially
+     * looking for T_high, true when looking for T_low.
+     */
+    bool detect_falling;
+};
+
+#endif
diff --git a/include/hw/sensor/tmp105_regs.h b/include/hw/sensor/tmp105_regs.h
new file mode 100644 (file)
index 0000000..ef015ee
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Texas Instruments TMP105 Temperature Sensor I2C messages
+ *
+ * Browse the data sheet:
+ *
+ *    http://www.ti.com/lit/gpn/tmp105
+ *
+ * Copyright (C) 2012 Alex Horn <alex.horn@cs.ox.ac.uk>
+ * Copyright (C) 2008-2012 Andrzej Zaborowski <balrogg@gmail.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later. See the COPYING file in the top-level directory.
+ */
+
+#ifndef TMP105_REGS_H
+#define TMP105_REGS_H
+
+/**
+ * TMP105Reg:
+ * @TMP105_REG_TEMPERATURE: Temperature register
+ * @TMP105_REG_CONFIG: Configuration register
+ * @TMP105_REG_T_LOW: Low temperature register (also known as T_hyst)
+ * @TMP105_REG_T_HIGH: High temperature register (also known as T_OS)
+ *
+ * The following temperature sensors are
+ * compatible with the TMP105 registers:
+ * - adt75
+ * - ds1775
+ * - ds75
+ * - lm75
+ * - lm75a
+ * - max6625
+ * - max6626
+ * - mcp980x
+ * - stds75
+ * - tcn75
+ * - tmp100
+ * - tmp101
+ * - tmp105
+ * - tmp175
+ * - tmp275
+ * - tmp75
+ **/
+typedef enum TMP105Reg {
+    TMP105_REG_TEMPERATURE = 0,
+    TMP105_REG_CONFIG,
+    TMP105_REG_T_LOW,
+    TMP105_REG_T_HIGH,
+} TMP105Reg;
+
+#endif
index 714058806a71a756abb97b1b02d714043586f2b7..8c86694091657c4cef5086ab6e168432ae68f7e7 100644 (file)
@@ -11,7 +11,7 @@
 #include "libqos/qgraph.h"
 #include "libqos/i2c.h"
 #include "qapi/qmp/qdict.h"
-#include "hw/misc/emc141x_regs.h"
+#include "hw/sensor/emc141x_regs.h"
 
 #define EMC1414_TEST_ID   "emc1414-test"
 
index 4f9f493872a2986f7d9bcd7a70c220355c360508..6b3038ac596fef6b1ba553ac08e17fc9b420fe76 100644 (file)
@@ -18,7 +18,7 @@
 #include "qemu/bitops.h"
 #include "libqos/i2c.h"
 #include "libqos/libqtest.h"
-#include "hw/misc/tmp105_regs.h"
+#include "hw/sensor/tmp105_regs.h"
 
 #define NR_SMBUS_DEVICES    16
 #define SMBUS_ADDR(x)       (0xf0080000 + 0x1000 * (x))
index f930a96b83e0412081a6dba94b0cf3ab8b8e0be8..3678646df52c3faa9689c3118b721d8c7185a5b5 100644 (file)
@@ -13,7 +13,7 @@
 #include "libqos/qgraph.h"
 #include "libqos/i2c.h"
 #include "qapi/qmp/qdict.h"
-#include "hw/misc/tmp105_regs.h"
+#include "hw/sensor/tmp105_regs.h"
 
 #define TMP105_TEST_ID   "tmp105-test"
 #define TMP105_TEST_ADDR 0x49