15e9ae4b1SCorey Minyard /* 25e9ae4b1SCorey Minyard * Texas Instruments TMP105 temperature sensor. 35e9ae4b1SCorey Minyard * 45e9ae4b1SCorey Minyard * Copyright (C) 2008 Nokia Corporation 55e9ae4b1SCorey Minyard * Written by Andrzej Zaborowski <andrew@openedhand.com> 65e9ae4b1SCorey Minyard * 75e9ae4b1SCorey Minyard * This program is free software; you can redistribute it and/or 85e9ae4b1SCorey Minyard * modify it under the terms of the GNU General Public License as 95e9ae4b1SCorey Minyard * published by the Free Software Foundation; either version 2 or 105e9ae4b1SCorey Minyard * (at your option) version 3 of the License. 115e9ae4b1SCorey Minyard * 125e9ae4b1SCorey Minyard * This program is distributed in the hope that it will be useful, 135e9ae4b1SCorey Minyard * but WITHOUT ANY WARRANTY; without even the implied warranty of 145e9ae4b1SCorey Minyard * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 155e9ae4b1SCorey Minyard * GNU General Public License for more details. 165e9ae4b1SCorey Minyard * 175e9ae4b1SCorey Minyard * You should have received a copy of the GNU General Public License along 185e9ae4b1SCorey Minyard * with this program; if not, see <http://www.gnu.org/licenses/>. 195e9ae4b1SCorey Minyard */ 205e9ae4b1SCorey Minyard 215e9ae4b1SCorey Minyard #include "qemu/osdep.h" 225e9ae4b1SCorey Minyard #include "hw/i2c/i2c.h" 235e9ae4b1SCorey Minyard #include "hw/irq.h" 245e9ae4b1SCorey Minyard #include "migration/vmstate.h" 255e9ae4b1SCorey Minyard #include "hw/sensor/tmp105.h" 265e9ae4b1SCorey Minyard #include "qapi/error.h" 275e9ae4b1SCorey Minyard #include "qapi/visitor.h" 285e9ae4b1SCorey Minyard #include "qemu/module.h" 295e9ae4b1SCorey Minyard 305e9ae4b1SCorey Minyard static void tmp105_interrupt_update(TMP105State *s) 315e9ae4b1SCorey Minyard { 325e9ae4b1SCorey Minyard qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1)); /* POL */ 335e9ae4b1SCorey Minyard } 345e9ae4b1SCorey Minyard 355e9ae4b1SCorey Minyard static void tmp105_alarm_update(TMP105State *s) 365e9ae4b1SCorey Minyard { 375e9ae4b1SCorey Minyard if ((s->config >> 0) & 1) { /* SD */ 385e9ae4b1SCorey Minyard if ((s->config >> 7) & 1) /* OS */ 395e9ae4b1SCorey Minyard s->config &= ~(1 << 7); /* OS */ 405e9ae4b1SCorey Minyard else 415e9ae4b1SCorey Minyard return; 425e9ae4b1SCorey Minyard } 435e9ae4b1SCorey Minyard 445e9ae4b1SCorey Minyard if (s->config >> 1 & 1) { 455e9ae4b1SCorey Minyard /* 465e9ae4b1SCorey Minyard * TM == 1 : Interrupt mode. We signal Alert when the 475e9ae4b1SCorey Minyard * temperature rises above T_high, and expect the guest to clear 485e9ae4b1SCorey Minyard * it (eg by reading a device register). 495e9ae4b1SCorey Minyard */ 505e9ae4b1SCorey Minyard if (s->detect_falling) { 515e9ae4b1SCorey Minyard if (s->temperature < s->limit[0]) { 525e9ae4b1SCorey Minyard s->alarm = 1; 535e9ae4b1SCorey Minyard s->detect_falling = false; 545e9ae4b1SCorey Minyard } 555e9ae4b1SCorey Minyard } else { 565e9ae4b1SCorey Minyard if (s->temperature >= s->limit[1]) { 575e9ae4b1SCorey Minyard s->alarm = 1; 585e9ae4b1SCorey Minyard s->detect_falling = true; 595e9ae4b1SCorey Minyard } 605e9ae4b1SCorey Minyard } 615e9ae4b1SCorey Minyard } else { 625e9ae4b1SCorey Minyard /* 635e9ae4b1SCorey Minyard * TM == 0 : Comparator mode. We signal Alert when the temperature 645e9ae4b1SCorey Minyard * rises above T_high, and stop signalling it when the temperature 655e9ae4b1SCorey Minyard * falls below T_low. 665e9ae4b1SCorey Minyard */ 675e9ae4b1SCorey Minyard if (s->detect_falling) { 685e9ae4b1SCorey Minyard if (s->temperature < s->limit[0]) { 695e9ae4b1SCorey Minyard s->alarm = 0; 705e9ae4b1SCorey Minyard s->detect_falling = false; 715e9ae4b1SCorey Minyard } 725e9ae4b1SCorey Minyard } else { 735e9ae4b1SCorey Minyard if (s->temperature >= s->limit[1]) { 745e9ae4b1SCorey Minyard s->alarm = 1; 755e9ae4b1SCorey Minyard s->detect_falling = true; 765e9ae4b1SCorey Minyard } 775e9ae4b1SCorey Minyard } 785e9ae4b1SCorey Minyard } 795e9ae4b1SCorey Minyard 805e9ae4b1SCorey Minyard tmp105_interrupt_update(s); 815e9ae4b1SCorey Minyard } 825e9ae4b1SCorey Minyard 835e9ae4b1SCorey Minyard static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name, 845e9ae4b1SCorey Minyard void *opaque, Error **errp) 855e9ae4b1SCorey Minyard { 865e9ae4b1SCorey Minyard TMP105State *s = TMP105(obj); 875e9ae4b1SCorey Minyard int64_t value = s->temperature * 1000 / 256; 885e9ae4b1SCorey Minyard 895e9ae4b1SCorey Minyard visit_type_int(v, name, &value, errp); 905e9ae4b1SCorey Minyard } 915e9ae4b1SCorey Minyard 925e9ae4b1SCorey Minyard /* Units are 0.001 centigrades relative to 0 C. s->temperature is 8.8 935e9ae4b1SCorey Minyard * fixed point, so units are 1/256 centigrades. A simple ratio will do. 945e9ae4b1SCorey Minyard */ 955e9ae4b1SCorey Minyard static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name, 965e9ae4b1SCorey Minyard void *opaque, Error **errp) 975e9ae4b1SCorey Minyard { 985e9ae4b1SCorey Minyard TMP105State *s = TMP105(obj); 995e9ae4b1SCorey Minyard int64_t temp; 1005e9ae4b1SCorey Minyard 1015e9ae4b1SCorey Minyard if (!visit_type_int(v, name, &temp, errp)) { 1025e9ae4b1SCorey Minyard return; 1035e9ae4b1SCorey Minyard } 1045e9ae4b1SCorey Minyard if (temp >= 128000 || temp < -128000) { 1055e9ae4b1SCorey Minyard error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range", 1065e9ae4b1SCorey Minyard temp / 1000, temp % 1000); 1075e9ae4b1SCorey Minyard return; 1085e9ae4b1SCorey Minyard } 1095e9ae4b1SCorey Minyard 1105e9ae4b1SCorey Minyard s->temperature = (int16_t) (temp * 256 / 1000); 1115e9ae4b1SCorey Minyard 1125e9ae4b1SCorey Minyard tmp105_alarm_update(s); 1135e9ae4b1SCorey Minyard } 1145e9ae4b1SCorey Minyard 1155e9ae4b1SCorey Minyard static const int tmp105_faultq[4] = { 1, 2, 4, 6 }; 1165e9ae4b1SCorey Minyard 1175e9ae4b1SCorey Minyard static void tmp105_read(TMP105State *s) 1185e9ae4b1SCorey Minyard { 1195e9ae4b1SCorey Minyard s->len = 0; 1205e9ae4b1SCorey Minyard 1215e9ae4b1SCorey Minyard if ((s->config >> 1) & 1) { /* TM */ 1225e9ae4b1SCorey Minyard s->alarm = 0; 1235e9ae4b1SCorey Minyard tmp105_interrupt_update(s); 1245e9ae4b1SCorey Minyard } 1255e9ae4b1SCorey Minyard 1265e9ae4b1SCorey Minyard switch (s->pointer & 3) { 1275e9ae4b1SCorey Minyard case TMP105_REG_TEMPERATURE: 1285e9ae4b1SCorey Minyard s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8); 1295e9ae4b1SCorey Minyard s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) & 1305e9ae4b1SCorey Minyard (0xf0 << ((~s->config >> 5) & 3)); /* R */ 1315e9ae4b1SCorey Minyard break; 1325e9ae4b1SCorey Minyard 1335e9ae4b1SCorey Minyard case TMP105_REG_CONFIG: 1345e9ae4b1SCorey Minyard s->buf[s->len ++] = s->config; 1355e9ae4b1SCorey Minyard break; 1365e9ae4b1SCorey Minyard 1375e9ae4b1SCorey Minyard case TMP105_REG_T_LOW: 1385e9ae4b1SCorey Minyard s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8; 1395e9ae4b1SCorey Minyard s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0; 1405e9ae4b1SCorey Minyard break; 1415e9ae4b1SCorey Minyard 1425e9ae4b1SCorey Minyard case TMP105_REG_T_HIGH: 1435e9ae4b1SCorey Minyard s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8; 1445e9ae4b1SCorey Minyard s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0; 1455e9ae4b1SCorey Minyard break; 1465e9ae4b1SCorey Minyard } 1475e9ae4b1SCorey Minyard } 1485e9ae4b1SCorey Minyard 1495e9ae4b1SCorey Minyard static void tmp105_write(TMP105State *s) 1505e9ae4b1SCorey Minyard { 1515e9ae4b1SCorey Minyard switch (s->pointer & 3) { 1525e9ae4b1SCorey Minyard case TMP105_REG_TEMPERATURE: 1535e9ae4b1SCorey Minyard break; 1545e9ae4b1SCorey Minyard 1555e9ae4b1SCorey Minyard case TMP105_REG_CONFIG: 1565e9ae4b1SCorey Minyard if (s->buf[0] & ~s->config & (1 << 0)) /* SD */ 1575e9ae4b1SCorey Minyard printf("%s: TMP105 shutdown\n", __func__); 1585e9ae4b1SCorey Minyard s->config = s->buf[0]; 1595e9ae4b1SCorey Minyard s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */ 1605e9ae4b1SCorey Minyard tmp105_alarm_update(s); 1615e9ae4b1SCorey Minyard break; 1625e9ae4b1SCorey Minyard 1635e9ae4b1SCorey Minyard case TMP105_REG_T_LOW: 1645e9ae4b1SCorey Minyard case TMP105_REG_T_HIGH: 1655e9ae4b1SCorey Minyard if (s->len >= 3) 1665e9ae4b1SCorey Minyard s->limit[s->pointer & 1] = (int16_t) 1675e9ae4b1SCorey Minyard ((((uint16_t) s->buf[0]) << 8) | s->buf[1]); 1685e9ae4b1SCorey Minyard tmp105_alarm_update(s); 1695e9ae4b1SCorey Minyard break; 1705e9ae4b1SCorey Minyard } 1715e9ae4b1SCorey Minyard } 1725e9ae4b1SCorey Minyard 1735e9ae4b1SCorey Minyard static uint8_t tmp105_rx(I2CSlave *i2c) 1745e9ae4b1SCorey Minyard { 1755e9ae4b1SCorey Minyard TMP105State *s = TMP105(i2c); 1765e9ae4b1SCorey Minyard 1775e9ae4b1SCorey Minyard if (s->len < 2) { 1785e9ae4b1SCorey Minyard return s->buf[s->len ++]; 1795e9ae4b1SCorey Minyard } else { 1805e9ae4b1SCorey Minyard return 0xff; 1815e9ae4b1SCorey Minyard } 1825e9ae4b1SCorey Minyard } 1835e9ae4b1SCorey Minyard 1845e9ae4b1SCorey Minyard static int tmp105_tx(I2CSlave *i2c, uint8_t data) 1855e9ae4b1SCorey Minyard { 1865e9ae4b1SCorey Minyard TMP105State *s = TMP105(i2c); 1875e9ae4b1SCorey Minyard 1885e9ae4b1SCorey Minyard if (s->len == 0) { 1895e9ae4b1SCorey Minyard s->pointer = data; 1905e9ae4b1SCorey Minyard s->len++; 1915e9ae4b1SCorey Minyard } else { 1925e9ae4b1SCorey Minyard if (s->len <= 2) { 1935e9ae4b1SCorey Minyard s->buf[s->len - 1] = data; 1945e9ae4b1SCorey Minyard } 1955e9ae4b1SCorey Minyard s->len++; 1965e9ae4b1SCorey Minyard tmp105_write(s); 1975e9ae4b1SCorey Minyard } 1985e9ae4b1SCorey Minyard 1995e9ae4b1SCorey Minyard return 0; 2005e9ae4b1SCorey Minyard } 2015e9ae4b1SCorey Minyard 2025e9ae4b1SCorey Minyard static int tmp105_event(I2CSlave *i2c, enum i2c_event event) 2035e9ae4b1SCorey Minyard { 2045e9ae4b1SCorey Minyard TMP105State *s = TMP105(i2c); 2055e9ae4b1SCorey Minyard 2065e9ae4b1SCorey Minyard if (event == I2C_START_RECV) { 2075e9ae4b1SCorey Minyard tmp105_read(s); 2085e9ae4b1SCorey Minyard } 2095e9ae4b1SCorey Minyard 2105e9ae4b1SCorey Minyard s->len = 0; 2115e9ae4b1SCorey Minyard return 0; 2125e9ae4b1SCorey Minyard } 2135e9ae4b1SCorey Minyard 2145e9ae4b1SCorey Minyard static int tmp105_post_load(void *opaque, int version_id) 2155e9ae4b1SCorey Minyard { 2165e9ae4b1SCorey Minyard TMP105State *s = opaque; 2175e9ae4b1SCorey Minyard 2185e9ae4b1SCorey Minyard s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */ 2195e9ae4b1SCorey Minyard 2205e9ae4b1SCorey Minyard tmp105_interrupt_update(s); 2215e9ae4b1SCorey Minyard return 0; 2225e9ae4b1SCorey Minyard } 2235e9ae4b1SCorey Minyard 2245e9ae4b1SCorey Minyard static bool detect_falling_needed(void *opaque) 2255e9ae4b1SCorey Minyard { 2265e9ae4b1SCorey Minyard TMP105State *s = opaque; 2275e9ae4b1SCorey Minyard 2285e9ae4b1SCorey Minyard /* 2295e9ae4b1SCorey Minyard * We only need to migrate the detect_falling bool if it's set; 2305e9ae4b1SCorey Minyard * for migration from older machines we assume that it is false 2315e9ae4b1SCorey Minyard * (ie temperature is not out of range). 2325e9ae4b1SCorey Minyard */ 2335e9ae4b1SCorey Minyard return s->detect_falling; 2345e9ae4b1SCorey Minyard } 2355e9ae4b1SCorey Minyard 2365e9ae4b1SCorey Minyard static const VMStateDescription vmstate_tmp105_detect_falling = { 2375e9ae4b1SCorey Minyard .name = "TMP105/detect-falling", 2385e9ae4b1SCorey Minyard .version_id = 1, 2395e9ae4b1SCorey Minyard .minimum_version_id = 1, 2405e9ae4b1SCorey Minyard .needed = detect_falling_needed, 241*af10fff2SRichard Henderson .fields = (const VMStateField[]) { 2425e9ae4b1SCorey Minyard VMSTATE_BOOL(detect_falling, TMP105State), 2435e9ae4b1SCorey Minyard VMSTATE_END_OF_LIST() 2445e9ae4b1SCorey Minyard } 2455e9ae4b1SCorey Minyard }; 2465e9ae4b1SCorey Minyard 2475e9ae4b1SCorey Minyard static const VMStateDescription vmstate_tmp105 = { 2485e9ae4b1SCorey Minyard .name = "TMP105", 2495e9ae4b1SCorey Minyard .version_id = 0, 2505e9ae4b1SCorey Minyard .minimum_version_id = 0, 2515e9ae4b1SCorey Minyard .post_load = tmp105_post_load, 252*af10fff2SRichard Henderson .fields = (const VMStateField[]) { 2535e9ae4b1SCorey Minyard VMSTATE_UINT8(len, TMP105State), 2545e9ae4b1SCorey Minyard VMSTATE_UINT8_ARRAY(buf, TMP105State, 2), 2555e9ae4b1SCorey Minyard VMSTATE_UINT8(pointer, TMP105State), 2565e9ae4b1SCorey Minyard VMSTATE_UINT8(config, TMP105State), 2575e9ae4b1SCorey Minyard VMSTATE_INT16(temperature, TMP105State), 2585e9ae4b1SCorey Minyard VMSTATE_INT16_ARRAY(limit, TMP105State, 2), 2595e9ae4b1SCorey Minyard VMSTATE_UINT8(alarm, TMP105State), 2605e9ae4b1SCorey Minyard VMSTATE_I2C_SLAVE(i2c, TMP105State), 2615e9ae4b1SCorey Minyard VMSTATE_END_OF_LIST() 2625e9ae4b1SCorey Minyard }, 263*af10fff2SRichard Henderson .subsections = (const VMStateDescription * const []) { 2645e9ae4b1SCorey Minyard &vmstate_tmp105_detect_falling, 2655e9ae4b1SCorey Minyard NULL 2665e9ae4b1SCorey Minyard } 2675e9ae4b1SCorey Minyard }; 2685e9ae4b1SCorey Minyard 2695e9ae4b1SCorey Minyard static void tmp105_reset(I2CSlave *i2c) 2705e9ae4b1SCorey Minyard { 2715e9ae4b1SCorey Minyard TMP105State *s = TMP105(i2c); 2725e9ae4b1SCorey Minyard 2735e9ae4b1SCorey Minyard s->temperature = 0; 2745e9ae4b1SCorey Minyard s->pointer = 0; 2755e9ae4b1SCorey Minyard s->config = 0; 2765e9ae4b1SCorey Minyard s->faults = tmp105_faultq[(s->config >> 3) & 3]; 2775e9ae4b1SCorey Minyard s->alarm = 0; 2785e9ae4b1SCorey Minyard s->detect_falling = false; 2795e9ae4b1SCorey Minyard 2805e9ae4b1SCorey Minyard s->limit[0] = 0x4b00; /* T_LOW, 75 degrees C */ 2815e9ae4b1SCorey Minyard s->limit[1] = 0x5000; /* T_HIGH, 80 degrees C */ 2825e9ae4b1SCorey Minyard 2835e9ae4b1SCorey Minyard tmp105_interrupt_update(s); 2845e9ae4b1SCorey Minyard } 2855e9ae4b1SCorey Minyard 2865e9ae4b1SCorey Minyard static void tmp105_realize(DeviceState *dev, Error **errp) 2875e9ae4b1SCorey Minyard { 2885e9ae4b1SCorey Minyard I2CSlave *i2c = I2C_SLAVE(dev); 2895e9ae4b1SCorey Minyard TMP105State *s = TMP105(i2c); 2905e9ae4b1SCorey Minyard 2915e9ae4b1SCorey Minyard qdev_init_gpio_out(&i2c->qdev, &s->pin, 1); 2925e9ae4b1SCorey Minyard 2935e9ae4b1SCorey Minyard tmp105_reset(&s->i2c); 2945e9ae4b1SCorey Minyard } 2955e9ae4b1SCorey Minyard 2965e9ae4b1SCorey Minyard static void tmp105_initfn(Object *obj) 2975e9ae4b1SCorey Minyard { 2985e9ae4b1SCorey Minyard object_property_add(obj, "temperature", "int", 2995e9ae4b1SCorey Minyard tmp105_get_temperature, 3005e9ae4b1SCorey Minyard tmp105_set_temperature, NULL, NULL); 3015e9ae4b1SCorey Minyard } 3025e9ae4b1SCorey Minyard 3035e9ae4b1SCorey Minyard static void tmp105_class_init(ObjectClass *klass, void *data) 3045e9ae4b1SCorey Minyard { 3055e9ae4b1SCorey Minyard DeviceClass *dc = DEVICE_CLASS(klass); 3065e9ae4b1SCorey Minyard I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); 3075e9ae4b1SCorey Minyard 3085e9ae4b1SCorey Minyard dc->realize = tmp105_realize; 3095e9ae4b1SCorey Minyard k->event = tmp105_event; 3105e9ae4b1SCorey Minyard k->recv = tmp105_rx; 3115e9ae4b1SCorey Minyard k->send = tmp105_tx; 3125e9ae4b1SCorey Minyard dc->vmsd = &vmstate_tmp105; 3135e9ae4b1SCorey Minyard } 3145e9ae4b1SCorey Minyard 3155e9ae4b1SCorey Minyard static const TypeInfo tmp105_info = { 3165e9ae4b1SCorey Minyard .name = TYPE_TMP105, 3175e9ae4b1SCorey Minyard .parent = TYPE_I2C_SLAVE, 3185e9ae4b1SCorey Minyard .instance_size = sizeof(TMP105State), 3195e9ae4b1SCorey Minyard .instance_init = tmp105_initfn, 3205e9ae4b1SCorey Minyard .class_init = tmp105_class_init, 3215e9ae4b1SCorey Minyard }; 3225e9ae4b1SCorey Minyard 3235e9ae4b1SCorey Minyard static void tmp105_register_types(void) 3245e9ae4b1SCorey Minyard { 3255e9ae4b1SCorey Minyard type_register_static(&tmp105_info); 3265e9ae4b1SCorey Minyard } 3275e9ae4b1SCorey Minyard 3285e9ae4b1SCorey Minyard type_init(tmp105_register_types) 329