1 /* 2 * Texas Instruments TMP105 temperature sensor. 3 * 4 * Copyright (C) 2008 Nokia Corporation 5 * Written by Andrzej Zaborowski <andrew@openedhand.com> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation; either version 2 or 10 * (at your option) version 3 of the License. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #include "qemu/osdep.h" 22 #include "hw/i2c/i2c.h" 23 #include "hw/irq.h" 24 #include "migration/vmstate.h" 25 #include "hw/sensor/tmp105.h" 26 #include "qapi/error.h" 27 #include "qapi/visitor.h" 28 #include "qemu/module.h" 29 #include "hw/registerfields.h" 30 31 FIELD(CONFIG, SHUTDOWN_MODE, 0, 1) 32 FIELD(CONFIG, THERMOSTAT_MODE, 1, 1) 33 FIELD(CONFIG, POLARITY, 2, 1) 34 FIELD(CONFIG, FAULT_QUEUE, 3, 2) 35 FIELD(CONFIG, CONVERTER_RESOLUTION, 5, 2) 36 FIELD(CONFIG, ONE_SHOT, 7, 1) 37 38 static void tmp105_interrupt_update(TMP105State *s) 39 { 40 qemu_set_irq(s->pin, s->alarm ^ FIELD_EX8(~s->config, CONFIG, POLARITY)); 41 } 42 43 static void tmp105_alarm_update(TMP105State *s, bool one_shot) 44 { 45 if (FIELD_EX8(s->config, CONFIG, SHUTDOWN_MODE) && !one_shot) { 46 return; 47 } 48 49 if (FIELD_EX8(s->config, CONFIG, THERMOSTAT_MODE)) { 50 /* 51 * TM == 1 : Interrupt mode. We signal Alert when the 52 * temperature rises above T_high, and expect the guest to clear 53 * it (eg by reading a device register). 54 */ 55 if (s->detect_falling) { 56 if (s->temperature < s->limit[0]) { 57 s->alarm = 1; 58 s->detect_falling = false; 59 } 60 } else { 61 if (s->temperature >= s->limit[1]) { 62 s->alarm = 1; 63 s->detect_falling = true; 64 } 65 } 66 } else { 67 /* 68 * TM == 0 : Comparator mode. We signal Alert when the temperature 69 * rises above T_high, and stop signalling it when the temperature 70 * falls below T_low. 71 */ 72 if (s->detect_falling) { 73 if (s->temperature < s->limit[0]) { 74 s->alarm = 0; 75 s->detect_falling = false; 76 } 77 } else { 78 if (s->temperature >= s->limit[1]) { 79 s->alarm = 1; 80 s->detect_falling = true; 81 } 82 } 83 } 84 85 tmp105_interrupt_update(s); 86 } 87 88 static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name, 89 void *opaque, Error **errp) 90 { 91 TMP105State *s = TMP105(obj); 92 int64_t value = s->temperature * 1000 / 256; 93 94 visit_type_int(v, name, &value, errp); 95 } 96 97 /* 98 * Units are 0.001 centigrades relative to 0 C. s->temperature is 8.8 99 * fixed point, so units are 1/256 centigrades. A simple ratio will do. 100 */ 101 static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name, 102 void *opaque, Error **errp) 103 { 104 TMP105State *s = TMP105(obj); 105 int64_t temp; 106 107 if (!visit_type_int(v, name, &temp, errp)) { 108 return; 109 } 110 if (temp >= 128000 || temp < -128000) { 111 error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range", 112 temp / 1000, temp % 1000); 113 return; 114 } 115 116 s->temperature = (int16_t) (temp * 256 / 1000); 117 118 tmp105_alarm_update(s, false); 119 } 120 121 static const int tmp105_faultq[4] = { 1, 2, 4, 6 }; 122 123 static void tmp105_read(TMP105State *s) 124 { 125 s->len = 0; 126 127 if (FIELD_EX8(s->config, CONFIG, THERMOSTAT_MODE)) { 128 s->alarm = 0; 129 tmp105_interrupt_update(s); 130 } 131 132 switch (s->pointer & 3) { 133 case TMP105_REG_TEMPERATURE: 134 s->buf[s->len++] = (((uint16_t) s->temperature) >> 8); 135 s->buf[s->len++] = (((uint16_t) s->temperature) >> 0) & 136 (0xf0 << (FIELD_EX8(~s->config, CONFIG, CONVERTER_RESOLUTION))); 137 break; 138 139 case TMP105_REG_CONFIG: 140 s->buf[s->len++] = s->config; 141 break; 142 143 case TMP105_REG_T_LOW: 144 s->buf[s->len++] = ((uint16_t) s->limit[0]) >> 8; 145 s->buf[s->len++] = ((uint16_t) s->limit[0]) >> 0; 146 break; 147 148 case TMP105_REG_T_HIGH: 149 s->buf[s->len++] = ((uint16_t) s->limit[1]) >> 8; 150 s->buf[s->len++] = ((uint16_t) s->limit[1]) >> 0; 151 break; 152 } 153 } 154 155 static void tmp105_write(TMP105State *s) 156 { 157 switch (s->pointer & 3) { 158 case TMP105_REG_TEMPERATURE: 159 break; 160 161 case TMP105_REG_CONFIG: 162 if (FIELD_EX8(s->buf[0] & ~s->config, CONFIG, SHUTDOWN_MODE)) { 163 printf("%s: TMP105 shutdown\n", __func__); 164 } 165 s->config = FIELD_DP8(s->buf[0], CONFIG, ONE_SHOT, 0); 166 s->faults = tmp105_faultq[FIELD_EX8(s->config, CONFIG, FAULT_QUEUE)]; 167 tmp105_alarm_update(s, FIELD_EX8(s->buf[0], CONFIG, ONE_SHOT)); 168 break; 169 170 case TMP105_REG_T_LOW: 171 case TMP105_REG_T_HIGH: 172 if (s->len >= 3) { 173 s->limit[s->pointer & 1] = (int16_t) 174 ((((uint16_t) s->buf[0]) << 8) | (s->buf[1] & 0xf0)); 175 } 176 tmp105_alarm_update(s, false); 177 break; 178 } 179 } 180 181 static uint8_t tmp105_rx(I2CSlave *i2c) 182 { 183 TMP105State *s = TMP105(i2c); 184 185 if (s->len < 2) { 186 return s->buf[s->len++]; 187 } else { 188 return 0xff; 189 } 190 } 191 192 static int tmp105_tx(I2CSlave *i2c, uint8_t data) 193 { 194 TMP105State *s = TMP105(i2c); 195 196 if (s->len == 0) { 197 s->pointer = data; 198 s->len++; 199 } else { 200 if (s->len <= 2) { 201 s->buf[s->len - 1] = data; 202 } 203 s->len++; 204 tmp105_write(s); 205 } 206 207 return 0; 208 } 209 210 static int tmp105_event(I2CSlave *i2c, enum i2c_event event) 211 { 212 TMP105State *s = TMP105(i2c); 213 214 if (event == I2C_START_RECV) { 215 tmp105_read(s); 216 } 217 218 s->len = 0; 219 return 0; 220 } 221 222 static int tmp105_post_load(void *opaque, int version_id) 223 { 224 TMP105State *s = opaque; 225 226 s->faults = tmp105_faultq[FIELD_EX8(s->config, CONFIG, FAULT_QUEUE)]; 227 228 tmp105_interrupt_update(s); 229 return 0; 230 } 231 232 static bool detect_falling_needed(void *opaque) 233 { 234 TMP105State *s = opaque; 235 236 /* 237 * We only need to migrate the detect_falling bool if it's set; 238 * for migration from older machines we assume that it is false 239 * (ie temperature is not out of range). 240 */ 241 return s->detect_falling; 242 } 243 244 static const VMStateDescription vmstate_tmp105_detect_falling = { 245 .name = "TMP105/detect-falling", 246 .version_id = 1, 247 .minimum_version_id = 1, 248 .needed = detect_falling_needed, 249 .fields = (const VMStateField[]) { 250 VMSTATE_BOOL(detect_falling, TMP105State), 251 VMSTATE_END_OF_LIST() 252 } 253 }; 254 255 static const VMStateDescription vmstate_tmp105 = { 256 .name = "TMP105", 257 .version_id = 0, 258 .minimum_version_id = 0, 259 .post_load = tmp105_post_load, 260 .fields = (const VMStateField[]) { 261 VMSTATE_UINT8(len, TMP105State), 262 VMSTATE_UINT8_ARRAY(buf, TMP105State, 2), 263 VMSTATE_UINT8(pointer, TMP105State), 264 VMSTATE_UINT8(config, TMP105State), 265 VMSTATE_INT16(temperature, TMP105State), 266 VMSTATE_INT16_ARRAY(limit, TMP105State, 2), 267 VMSTATE_UINT8(alarm, TMP105State), 268 VMSTATE_I2C_SLAVE(i2c, TMP105State), 269 VMSTATE_END_OF_LIST() 270 }, 271 .subsections = (const VMStateDescription * const []) { 272 &vmstate_tmp105_detect_falling, 273 NULL 274 } 275 }; 276 277 static void tmp105_reset(I2CSlave *i2c) 278 { 279 TMP105State *s = TMP105(i2c); 280 281 s->temperature = 0; 282 s->pointer = 0; 283 s->config = 0; 284 s->faults = tmp105_faultq[FIELD_EX8(s->config, CONFIG, FAULT_QUEUE)]; 285 s->alarm = 0; 286 s->detect_falling = false; 287 288 s->limit[0] = 0x4b00; /* T_LOW, 75 degrees C */ 289 s->limit[1] = 0x5000; /* T_HIGH, 80 degrees C */ 290 291 tmp105_interrupt_update(s); 292 } 293 294 static void tmp105_realize(DeviceState *dev, Error **errp) 295 { 296 I2CSlave *i2c = I2C_SLAVE(dev); 297 TMP105State *s = TMP105(i2c); 298 299 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1); 300 301 tmp105_reset(&s->i2c); 302 } 303 304 static void tmp105_initfn(Object *obj) 305 { 306 object_property_add(obj, "temperature", "int", 307 tmp105_get_temperature, 308 tmp105_set_temperature, NULL, NULL); 309 } 310 311 static void tmp105_class_init(ObjectClass *klass, void *data) 312 { 313 DeviceClass *dc = DEVICE_CLASS(klass); 314 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); 315 316 dc->realize = tmp105_realize; 317 k->event = tmp105_event; 318 k->recv = tmp105_rx; 319 k->send = tmp105_tx; 320 dc->vmsd = &vmstate_tmp105; 321 } 322 323 static const TypeInfo tmp105_info = { 324 .name = TYPE_TMP105, 325 .parent = TYPE_I2C_SLAVE, 326 .instance_size = sizeof(TMP105State), 327 .instance_init = tmp105_initfn, 328 .class_init = tmp105_class_init, 329 }; 330 331 static void tmp105_register_types(void) 332 { 333 type_register_static(&tmp105_info); 334 } 335 336 type_init(tmp105_register_types) 337