blob: a8730d0b7f97362110163a08e1b5e06f52a2a166 [file] [log] [blame]
balrog7e7c5e42008-04-14 21:57:44 +00001/*
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 *
aurel32fad6cb12009-01-04 22:05:52 +000017 * You should have received a copy of the GNU General Public License along
Blue Swirl8167ee82009-07-16 20:47:01 +000018 * with this program; if not, see <http://www.gnu.org/licenses/>.
balrog7e7c5e42008-04-14 21:57:44 +000019 */
20
Peter Maydell0d1c9782016-01-26 18:17:17 +000021#include "qemu/osdep.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010022#include "hw/i2c/i2c.h"
Markus Armbruster64552b62019-08-12 07:23:42 +020023#include "hw/irq.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020024#include "migration/vmstate.h"
Corey Minyard5e9ae4b2021-05-18 16:08:03 -050025#include "hw/sensor/tmp105.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Andreas Färbereb60d1c2013-01-16 01:57:59 +010027#include "qapi/visitor.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020028#include "qemu/module.h"
balrog7e7c5e42008-04-14 21:57:44 +000029
Paul Brookbc24a222009-05-10 01:44:56 +010030static void tmp105_interrupt_update(TMP105State *s)
balrog7e7c5e42008-04-14 21:57:44 +000031{
32 qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1)); /* POL */
33}
34
Paul Brookbc24a222009-05-10 01:44:56 +010035static void tmp105_alarm_update(TMP105State *s)
balrog7e7c5e42008-04-14 21:57:44 +000036{
37 if ((s->config >> 0) & 1) { /* SD */
38 if ((s->config >> 7) & 1) /* OS */
39 s->config &= ~(1 << 7); /* OS */
40 else
41 return;
42 }
43
Peter Maydellab135622020-11-17 12:56:33 +000044 if (s->config >> 1 & 1) {
45 /*
46 * TM == 1 : Interrupt mode. We signal Alert when the
47 * temperature rises above T_high, and expect the guest to clear
48 * it (eg by reading a device register).
49 */
50 if (s->detect_falling) {
51 if (s->temperature < s->limit[0]) {
52 s->alarm = 1;
53 s->detect_falling = false;
54 }
55 } else {
56 if (s->temperature >= s->limit[1]) {
57 s->alarm = 1;
58 s->detect_falling = true;
59 }
60 }
balrog7e7c5e42008-04-14 21:57:44 +000061 } else {
Peter Maydellab135622020-11-17 12:56:33 +000062 /*
63 * TM == 0 : Comparator mode. We signal Alert when the temperature
64 * rises above T_high, and stop signalling it when the temperature
65 * falls below T_low.
66 */
67 if (s->detect_falling) {
68 if (s->temperature < s->limit[0]) {
69 s->alarm = 0;
70 s->detect_falling = false;
71 }
72 } else {
73 if (s->temperature >= s->limit[1]) {
74 s->alarm = 1;
75 s->detect_falling = true;
76 }
77 }
balrog7e7c5e42008-04-14 21:57:44 +000078 }
79
80 tmp105_interrupt_update(s);
81}
82
Eric Blaked7bce992016-01-29 06:48:55 -070083static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name,
84 void *opaque, Error **errp)
balrog7e7c5e42008-04-14 21:57:44 +000085{
Andreas Färbereb60d1c2013-01-16 01:57:59 +010086 TMP105State *s = TMP105(obj);
Paolo Bonziniefdf6a52014-03-31 18:26:32 +020087 int64_t value = s->temperature * 1000 / 256;
balrog7e7c5e42008-04-14 21:57:44 +000088
Eric Blake51e72bc2016-01-29 06:48:54 -070089 visit_type_int(v, name, &value, errp);
Andreas Färbereb60d1c2013-01-16 01:57:59 +010090}
91
Paolo Bonziniefdf6a52014-03-31 18:26:32 +020092/* Units are 0.001 centigrades relative to 0 C. s->temperature is 8.8
93 * fixed point, so units are 1/256 centigrades. A simple ratio will do.
94 */
Eric Blaked7bce992016-01-29 06:48:55 -070095static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name,
96 void *opaque, Error **errp)
Andreas Färbereb60d1c2013-01-16 01:57:59 +010097{
98 TMP105State *s = TMP105(obj);
99 int64_t temp;
100
Markus Armbruster668f62e2020-07-07 18:06:02 +0200101 if (!visit_type_int(v, name, &temp, errp)) {
Andreas Färbereb60d1c2013-01-16 01:57:59 +0100102 return;
103 }
balrog7e7c5e42008-04-14 21:57:44 +0000104 if (temp >= 128000 || temp < -128000) {
Eric Blake33814662018-11-20 14:36:28 -0600105 error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range",
Andreas Färbereb60d1c2013-01-16 01:57:59 +0100106 temp / 1000, temp % 1000);
107 return;
balrog7e7c5e42008-04-14 21:57:44 +0000108 }
109
Paolo Bonziniefdf6a52014-03-31 18:26:32 +0200110 s->temperature = (int16_t) (temp * 256 / 1000);
balrog7e7c5e42008-04-14 21:57:44 +0000111
112 tmp105_alarm_update(s);
113}
114
115static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
116
Paul Brookbc24a222009-05-10 01:44:56 +0100117static void tmp105_read(TMP105State *s)
balrog7e7c5e42008-04-14 21:57:44 +0000118{
119 s->len = 0;
120
121 if ((s->config >> 1) & 1) { /* TM */
122 s->alarm = 0;
123 tmp105_interrupt_update(s);
124 }
125
126 switch (s->pointer & 3) {
Alex Horn2915efb2012-12-05 12:34:06 +0000127 case TMP105_REG_TEMPERATURE:
balrog7e7c5e42008-04-14 21:57:44 +0000128 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8);
129 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) &
130 (0xf0 << ((~s->config >> 5) & 3)); /* R */
131 break;
132
Alex Horn2915efb2012-12-05 12:34:06 +0000133 case TMP105_REG_CONFIG:
balrog7e7c5e42008-04-14 21:57:44 +0000134 s->buf[s->len ++] = s->config;
135 break;
136
Alex Horn2915efb2012-12-05 12:34:06 +0000137 case TMP105_REG_T_LOW:
balrog7e7c5e42008-04-14 21:57:44 +0000138 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
139 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
140 break;
141
Alex Horn2915efb2012-12-05 12:34:06 +0000142 case TMP105_REG_T_HIGH:
balrog7e7c5e42008-04-14 21:57:44 +0000143 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8;
144 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0;
145 break;
146 }
147}
148
Paul Brookbc24a222009-05-10 01:44:56 +0100149static void tmp105_write(TMP105State *s)
balrog7e7c5e42008-04-14 21:57:44 +0000150{
151 switch (s->pointer & 3) {
Alex Horn2915efb2012-12-05 12:34:06 +0000152 case TMP105_REG_TEMPERATURE:
balrog7e7c5e42008-04-14 21:57:44 +0000153 break;
154
Alex Horn2915efb2012-12-05 12:34:06 +0000155 case TMP105_REG_CONFIG:
balrog7e7c5e42008-04-14 21:57:44 +0000156 if (s->buf[0] & ~s->config & (1 << 0)) /* SD */
Alistair Francisa89f3642017-11-08 14:56:31 -0800157 printf("%s: TMP105 shutdown\n", __func__);
balrog7e7c5e42008-04-14 21:57:44 +0000158 s->config = s->buf[0];
159 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
160 tmp105_alarm_update(s);
161 break;
162
Alex Horn2915efb2012-12-05 12:34:06 +0000163 case TMP105_REG_T_LOW:
164 case TMP105_REG_T_HIGH:
balrog7e7c5e42008-04-14 21:57:44 +0000165 if (s->len >= 3)
166 s->limit[s->pointer & 1] = (int16_t)
167 ((((uint16_t) s->buf[0]) << 8) | s->buf[1]);
168 tmp105_alarm_update(s);
169 break;
170 }
171}
172
Corey Minyard2ac4c5f2018-11-14 11:50:50 -0600173static uint8_t tmp105_rx(I2CSlave *i2c)
balrog7e7c5e42008-04-14 21:57:44 +0000174{
Andreas Färber2aad80e2013-01-16 01:57:58 +0100175 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000176
Andreas Färber2aad80e2013-01-16 01:57:58 +0100177 if (s->len < 2) {
balrog7e7c5e42008-04-14 21:57:44 +0000178 return s->buf[s->len ++];
Andreas Färber2aad80e2013-01-16 01:57:58 +0100179 } else {
balrog7e7c5e42008-04-14 21:57:44 +0000180 return 0xff;
Andreas Färber2aad80e2013-01-16 01:57:58 +0100181 }
balrog7e7c5e42008-04-14 21:57:44 +0000182}
183
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600184static int tmp105_tx(I2CSlave *i2c, uint8_t data)
balrog7e7c5e42008-04-14 21:57:44 +0000185{
Andreas Färber2aad80e2013-01-16 01:57:58 +0100186 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000187
Andreas Färbercb5ef3f2013-01-16 01:57:56 +0100188 if (s->len == 0) {
balrog7e7c5e42008-04-14 21:57:44 +0000189 s->pointer = data;
Andreas Färbercb5ef3f2013-01-16 01:57:56 +0100190 s->len++;
191 } else {
192 if (s->len <= 2) {
balrog7e7c5e42008-04-14 21:57:44 +0000193 s->buf[s->len - 1] = data;
Andreas Färbercb5ef3f2013-01-16 01:57:56 +0100194 }
195 s->len++;
balrog7e7c5e42008-04-14 21:57:44 +0000196 tmp105_write(s);
197 }
198
199 return 0;
200}
201
Corey Minyardd307c282017-01-09 11:40:20 +0000202static int tmp105_event(I2CSlave *i2c, enum i2c_event event)
balrog7e7c5e42008-04-14 21:57:44 +0000203{
Andreas Färber2aad80e2013-01-16 01:57:58 +0100204 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000205
Andreas Färber2aad80e2013-01-16 01:57:58 +0100206 if (event == I2C_START_RECV) {
balrog7e7c5e42008-04-14 21:57:44 +0000207 tmp105_read(s);
Andreas Färber2aad80e2013-01-16 01:57:58 +0100208 }
balrog7e7c5e42008-04-14 21:57:44 +0000209
210 s->len = 0;
Corey Minyardd307c282017-01-09 11:40:20 +0000211 return 0;
balrog7e7c5e42008-04-14 21:57:44 +0000212}
213
Juan Quintela371a4462009-09-29 22:48:38 +0200214static int tmp105_post_load(void *opaque, int version_id)
balrog7e7c5e42008-04-14 21:57:44 +0000215{
Juan Quintela371a4462009-09-29 22:48:38 +0200216 TMP105State *s = opaque;
balrog7e7c5e42008-04-14 21:57:44 +0000217
Andrzej Zaborowskie5d3b982010-05-15 14:31:27 +0200218 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
219
balrog7e7c5e42008-04-14 21:57:44 +0000220 tmp105_interrupt_update(s);
balrog7e7c5e42008-04-14 21:57:44 +0000221 return 0;
222}
223
Peter Maydellab135622020-11-17 12:56:33 +0000224static bool detect_falling_needed(void *opaque)
225{
226 TMP105State *s = opaque;
227
228 /*
229 * We only need to migrate the detect_falling bool if it's set;
230 * for migration from older machines we assume that it is false
231 * (ie temperature is not out of range).
232 */
233 return s->detect_falling;
234}
235
236static const VMStateDescription vmstate_tmp105_detect_falling = {
237 .name = "TMP105/detect-falling",
238 .version_id = 1,
239 .minimum_version_id = 1,
240 .needed = detect_falling_needed,
Richard Hendersonaf10fff2023-12-21 14:16:34 +1100241 .fields = (const VMStateField[]) {
Peter Maydellab135622020-11-17 12:56:33 +0000242 VMSTATE_BOOL(detect_falling, TMP105State),
243 VMSTATE_END_OF_LIST()
244 }
245};
246
Juan Quintela371a4462009-09-29 22:48:38 +0200247static const VMStateDescription vmstate_tmp105 = {
248 .name = "TMP105",
249 .version_id = 0,
250 .minimum_version_id = 0,
Juan Quintela371a4462009-09-29 22:48:38 +0200251 .post_load = tmp105_post_load,
Richard Hendersonaf10fff2023-12-21 14:16:34 +1100252 .fields = (const VMStateField[]) {
Juan Quintela371a4462009-09-29 22:48:38 +0200253 VMSTATE_UINT8(len, TMP105State),
254 VMSTATE_UINT8_ARRAY(buf, TMP105State, 2),
255 VMSTATE_UINT8(pointer, TMP105State),
256 VMSTATE_UINT8(config, TMP105State),
257 VMSTATE_INT16(temperature, TMP105State),
258 VMSTATE_INT16_ARRAY(limit, TMP105State, 2),
259 VMSTATE_UINT8(alarm, TMP105State),
260 VMSTATE_I2C_SLAVE(i2c, TMP105State),
261 VMSTATE_END_OF_LIST()
Peter Maydellab135622020-11-17 12:56:33 +0000262 },
Richard Hendersonaf10fff2023-12-21 14:16:34 +1100263 .subsections = (const VMStateDescription * const []) {
Peter Maydellab135622020-11-17 12:56:33 +0000264 &vmstate_tmp105_detect_falling,
265 NULL
Juan Quintela371a4462009-09-29 22:48:38 +0200266 }
267};
268
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600269static void tmp105_reset(I2CSlave *i2c)
balrog7e7c5e42008-04-14 21:57:44 +0000270{
Andreas Färber2aad80e2013-01-16 01:57:58 +0100271 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000272
273 s->temperature = 0;
274 s->pointer = 0;
275 s->config = 0;
276 s->faults = tmp105_faultq[(s->config >> 3) & 3];
277 s->alarm = 0;
Peter Maydellab135622020-11-17 12:56:33 +0000278 s->detect_falling = false;
balrog7e7c5e42008-04-14 21:57:44 +0000279
Peter Maydelle1919882020-11-17 12:56:33 +0000280 s->limit[0] = 0x4b00; /* T_LOW, 75 degrees C */
281 s->limit[1] = 0x5000; /* T_HIGH, 80 degrees C */
282
balrog7e7c5e42008-04-14 21:57:44 +0000283 tmp105_interrupt_update(s);
284}
285
Philippe Mathieu-Daudéc8c9e102018-05-28 16:45:07 +0200286static void tmp105_realize(DeviceState *dev, Error **errp)
balrog7e7c5e42008-04-14 21:57:44 +0000287{
Philippe Mathieu-Daudéc8c9e102018-05-28 16:45:07 +0200288 I2CSlave *i2c = I2C_SLAVE(dev);
Andreas Färber2aad80e2013-01-16 01:57:58 +0100289 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000290
Paul Brook697454e2009-05-14 22:35:08 +0100291 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
balrog7e7c5e42008-04-14 21:57:44 +0000292
293 tmp105_reset(&s->i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000294}
Paul Brook697454e2009-05-14 22:35:08 +0100295
Andreas Färbereb60d1c2013-01-16 01:57:59 +0100296static void tmp105_initfn(Object *obj)
297{
298 object_property_add(obj, "temperature", "int",
299 tmp105_get_temperature,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200300 tmp105_set_temperature, NULL, NULL);
Andreas Färbereb60d1c2013-01-16 01:57:59 +0100301}
302
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600303static void tmp105_class_init(ObjectClass *klass, void *data)
304{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600305 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600306 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
307
Philippe Mathieu-Daudéc8c9e102018-05-28 16:45:07 +0200308 dc->realize = tmp105_realize;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600309 k->event = tmp105_event;
310 k->recv = tmp105_rx;
311 k->send = tmp105_tx;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600312 dc->vmsd = &vmstate_tmp105;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600313}
314
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100315static const TypeInfo tmp105_info = {
Andreas Färber2aad80e2013-01-16 01:57:58 +0100316 .name = TYPE_TMP105,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600317 .parent = TYPE_I2C_SLAVE,
318 .instance_size = sizeof(TMP105State),
Andreas Färbereb60d1c2013-01-16 01:57:59 +0100319 .instance_init = tmp105_initfn,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600320 .class_init = tmp105_class_init,
Paul Brook697454e2009-05-14 22:35:08 +0100321};
322
Andreas Färber83f7d432012-02-09 15:20:55 +0100323static void tmp105_register_types(void)
Paul Brook697454e2009-05-14 22:35:08 +0100324{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600325 type_register_static(&tmp105_info);
Paul Brook697454e2009-05-14 22:35:08 +0100326}
327
Andreas Färber83f7d432012-02-09 15:20:55 +0100328type_init(tmp105_register_types)