diff options
author | Corey Minyard <cminyard@mvista.com> | 2021-05-18 16:08:03 -0500 |
---|---|---|
committer | Corey Minyard <cminyard@mvista.com> | 2021-06-17 07:10:32 -0500 |
commit | 5e9ae4b1a31a17a72487372067a78b6afa68b68d (patch) | |
tree | 3411236ff04f4ab9a028910143c1d19bf21cc00f /hw/misc | |
parent | 58f3e3fe69a66a5c27675faf3e7afa52e027e621 (diff) |
sensor: Move hardware sensors from misc to a sensor directory
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>
Diffstat (limited to 'hw/misc')
-rw-r--r-- | hw/misc/Kconfig | 12 | ||||
-rw-r--r-- | hw/misc/emc141x.c | 326 | ||||
-rw-r--r-- | hw/misc/meson.build | 3 | ||||
-rw-r--r-- | hw/misc/tmp105.c | 328 | ||||
-rw-r--r-- | hw/misc/tmp105.h | 55 | ||||
-rw-r--r-- | hw/misc/tmp421.c | 391 |
6 files changed, 0 insertions, 1115 deletions
diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig index 996d45aff5..507058d8bf 100644 --- a/hw/misc/Kconfig +++ b/hw/misc/Kconfig @@ -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 index f7c53d48a4..0000000000 --- a/hw/misc/emc141x.c +++ /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) diff --git a/hw/misc/meson.build b/hw/misc/meson.build index b0a8ee8994..046c7e0c72 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -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 index d299d9b21b..0000000000 --- a/hw/misc/tmp105.c +++ /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 index 7c97071ad7..0000000000 --- a/hw/misc/tmp105.h +++ /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 index a3db57dcb5..0000000000 --- a/hw/misc/tmp421.c +++ /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) |