blob: 58dbebca905622ea958b0c8f53f4d32c7fe76669 [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"
Paolo Bonzini47b43a12013-03-18 17:36:02 +010025#include "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
44 if ((s->config >> 1) & 1) { /* TM */
45 if (s->temperature >= s->limit[1])
46 s->alarm = 1;
47 else if (s->temperature < s->limit[0])
48 s->alarm = 1;
49 } else {
50 if (s->temperature >= s->limit[1])
51 s->alarm = 1;
52 else if (s->temperature < s->limit[0])
53 s->alarm = 0;
54 }
55
56 tmp105_interrupt_update(s);
57}
58
Eric Blaked7bce992016-01-29 06:48:55 -070059static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name,
60 void *opaque, Error **errp)
balrog7e7c5e42008-04-14 21:57:44 +000061{
Andreas Färbereb60d1c2013-01-16 01:57:59 +010062 TMP105State *s = TMP105(obj);
Paolo Bonziniefdf6a52014-03-31 18:26:32 +020063 int64_t value = s->temperature * 1000 / 256;
balrog7e7c5e42008-04-14 21:57:44 +000064
Eric Blake51e72bc2016-01-29 06:48:54 -070065 visit_type_int(v, name, &value, errp);
Andreas Färbereb60d1c2013-01-16 01:57:59 +010066}
67
Paolo Bonziniefdf6a52014-03-31 18:26:32 +020068/* Units are 0.001 centigrades relative to 0 C. s->temperature is 8.8
69 * fixed point, so units are 1/256 centigrades. A simple ratio will do.
70 */
Eric Blaked7bce992016-01-29 06:48:55 -070071static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name,
72 void *opaque, Error **errp)
Andreas Färbereb60d1c2013-01-16 01:57:59 +010073{
74 TMP105State *s = TMP105(obj);
Markus Armbruster65cd9062014-04-25 12:44:22 +020075 Error *local_err = NULL;
Andreas Färbereb60d1c2013-01-16 01:57:59 +010076 int64_t temp;
77
Eric Blake51e72bc2016-01-29 06:48:54 -070078 visit_type_int(v, name, &temp, &local_err);
Markus Armbruster65cd9062014-04-25 12:44:22 +020079 if (local_err) {
80 error_propagate(errp, local_err);
Andreas Färbereb60d1c2013-01-16 01:57:59 +010081 return;
82 }
balrog7e7c5e42008-04-14 21:57:44 +000083 if (temp >= 128000 || temp < -128000) {
Eric Blake33814662018-11-20 14:36:28 -060084 error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range",
Andreas Färbereb60d1c2013-01-16 01:57:59 +010085 temp / 1000, temp % 1000);
86 return;
balrog7e7c5e42008-04-14 21:57:44 +000087 }
88
Paolo Bonziniefdf6a52014-03-31 18:26:32 +020089 s->temperature = (int16_t) (temp * 256 / 1000);
balrog7e7c5e42008-04-14 21:57:44 +000090
91 tmp105_alarm_update(s);
92}
93
94static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
95
Paul Brookbc24a222009-05-10 01:44:56 +010096static void tmp105_read(TMP105State *s)
balrog7e7c5e42008-04-14 21:57:44 +000097{
98 s->len = 0;
99
100 if ((s->config >> 1) & 1) { /* TM */
101 s->alarm = 0;
102 tmp105_interrupt_update(s);
103 }
104
105 switch (s->pointer & 3) {
Alex Horn2915efb2012-12-05 12:34:06 +0000106 case TMP105_REG_TEMPERATURE:
balrog7e7c5e42008-04-14 21:57:44 +0000107 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8);
108 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) &
109 (0xf0 << ((~s->config >> 5) & 3)); /* R */
110 break;
111
Alex Horn2915efb2012-12-05 12:34:06 +0000112 case TMP105_REG_CONFIG:
balrog7e7c5e42008-04-14 21:57:44 +0000113 s->buf[s->len ++] = s->config;
114 break;
115
Alex Horn2915efb2012-12-05 12:34:06 +0000116 case TMP105_REG_T_LOW:
balrog7e7c5e42008-04-14 21:57:44 +0000117 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
118 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
119 break;
120
Alex Horn2915efb2012-12-05 12:34:06 +0000121 case TMP105_REG_T_HIGH:
balrog7e7c5e42008-04-14 21:57:44 +0000122 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8;
123 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0;
124 break;
125 }
126}
127
Paul Brookbc24a222009-05-10 01:44:56 +0100128static void tmp105_write(TMP105State *s)
balrog7e7c5e42008-04-14 21:57:44 +0000129{
130 switch (s->pointer & 3) {
Alex Horn2915efb2012-12-05 12:34:06 +0000131 case TMP105_REG_TEMPERATURE:
balrog7e7c5e42008-04-14 21:57:44 +0000132 break;
133
Alex Horn2915efb2012-12-05 12:34:06 +0000134 case TMP105_REG_CONFIG:
balrog7e7c5e42008-04-14 21:57:44 +0000135 if (s->buf[0] & ~s->config & (1 << 0)) /* SD */
Alistair Francisa89f3642017-11-08 14:56:31 -0800136 printf("%s: TMP105 shutdown\n", __func__);
balrog7e7c5e42008-04-14 21:57:44 +0000137 s->config = s->buf[0];
138 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
139 tmp105_alarm_update(s);
140 break;
141
Alex Horn2915efb2012-12-05 12:34:06 +0000142 case TMP105_REG_T_LOW:
143 case TMP105_REG_T_HIGH:
balrog7e7c5e42008-04-14 21:57:44 +0000144 if (s->len >= 3)
145 s->limit[s->pointer & 1] = (int16_t)
146 ((((uint16_t) s->buf[0]) << 8) | s->buf[1]);
147 tmp105_alarm_update(s);
148 break;
149 }
150}
151
Corey Minyard2ac4c5f2018-11-14 11:50:50 -0600152static uint8_t tmp105_rx(I2CSlave *i2c)
balrog7e7c5e42008-04-14 21:57:44 +0000153{
Andreas Färber2aad80e2013-01-16 01:57:58 +0100154 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000155
Andreas Färber2aad80e2013-01-16 01:57:58 +0100156 if (s->len < 2) {
balrog7e7c5e42008-04-14 21:57:44 +0000157 return s->buf[s->len ++];
Andreas Färber2aad80e2013-01-16 01:57:58 +0100158 } else {
balrog7e7c5e42008-04-14 21:57:44 +0000159 return 0xff;
Andreas Färber2aad80e2013-01-16 01:57:58 +0100160 }
balrog7e7c5e42008-04-14 21:57:44 +0000161}
162
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600163static int tmp105_tx(I2CSlave *i2c, uint8_t data)
balrog7e7c5e42008-04-14 21:57:44 +0000164{
Andreas Färber2aad80e2013-01-16 01:57:58 +0100165 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000166
Andreas Färbercb5ef3f2013-01-16 01:57:56 +0100167 if (s->len == 0) {
balrog7e7c5e42008-04-14 21:57:44 +0000168 s->pointer = data;
Andreas Färbercb5ef3f2013-01-16 01:57:56 +0100169 s->len++;
170 } else {
171 if (s->len <= 2) {
balrog7e7c5e42008-04-14 21:57:44 +0000172 s->buf[s->len - 1] = data;
Andreas Färbercb5ef3f2013-01-16 01:57:56 +0100173 }
174 s->len++;
balrog7e7c5e42008-04-14 21:57:44 +0000175 tmp105_write(s);
176 }
177
178 return 0;
179}
180
Corey Minyardd307c282017-01-09 11:40:20 +0000181static int tmp105_event(I2CSlave *i2c, enum i2c_event event)
balrog7e7c5e42008-04-14 21:57:44 +0000182{
Andreas Färber2aad80e2013-01-16 01:57:58 +0100183 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000184
Andreas Färber2aad80e2013-01-16 01:57:58 +0100185 if (event == I2C_START_RECV) {
balrog7e7c5e42008-04-14 21:57:44 +0000186 tmp105_read(s);
Andreas Färber2aad80e2013-01-16 01:57:58 +0100187 }
balrog7e7c5e42008-04-14 21:57:44 +0000188
189 s->len = 0;
Corey Minyardd307c282017-01-09 11:40:20 +0000190 return 0;
balrog7e7c5e42008-04-14 21:57:44 +0000191}
192
Juan Quintela371a4462009-09-29 22:48:38 +0200193static int tmp105_post_load(void *opaque, int version_id)
balrog7e7c5e42008-04-14 21:57:44 +0000194{
Juan Quintela371a4462009-09-29 22:48:38 +0200195 TMP105State *s = opaque;
balrog7e7c5e42008-04-14 21:57:44 +0000196
Andrzej Zaborowskie5d3b982010-05-15 14:31:27 +0200197 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
198
balrog7e7c5e42008-04-14 21:57:44 +0000199 tmp105_interrupt_update(s);
balrog7e7c5e42008-04-14 21:57:44 +0000200 return 0;
201}
202
Juan Quintela371a4462009-09-29 22:48:38 +0200203static const VMStateDescription vmstate_tmp105 = {
204 .name = "TMP105",
205 .version_id = 0,
206 .minimum_version_id = 0,
Juan Quintela371a4462009-09-29 22:48:38 +0200207 .post_load = tmp105_post_load,
Juan Quintela8f1e8842014-05-13 16:09:35 +0100208 .fields = (VMStateField[]) {
Juan Quintela371a4462009-09-29 22:48:38 +0200209 VMSTATE_UINT8(len, TMP105State),
210 VMSTATE_UINT8_ARRAY(buf, TMP105State, 2),
211 VMSTATE_UINT8(pointer, TMP105State),
212 VMSTATE_UINT8(config, TMP105State),
213 VMSTATE_INT16(temperature, TMP105State),
214 VMSTATE_INT16_ARRAY(limit, TMP105State, 2),
215 VMSTATE_UINT8(alarm, TMP105State),
216 VMSTATE_I2C_SLAVE(i2c, TMP105State),
217 VMSTATE_END_OF_LIST()
218 }
219};
220
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600221static void tmp105_reset(I2CSlave *i2c)
balrog7e7c5e42008-04-14 21:57:44 +0000222{
Andreas Färber2aad80e2013-01-16 01:57:58 +0100223 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000224
225 s->temperature = 0;
226 s->pointer = 0;
227 s->config = 0;
228 s->faults = tmp105_faultq[(s->config >> 3) & 3];
229 s->alarm = 0;
230
231 tmp105_interrupt_update(s);
232}
233
Philippe Mathieu-Daudéc8c9e102018-05-28 16:45:07 +0200234static void tmp105_realize(DeviceState *dev, Error **errp)
balrog7e7c5e42008-04-14 21:57:44 +0000235{
Philippe Mathieu-Daudéc8c9e102018-05-28 16:45:07 +0200236 I2CSlave *i2c = I2C_SLAVE(dev);
Andreas Färber2aad80e2013-01-16 01:57:58 +0100237 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000238
Paul Brook697454e2009-05-14 22:35:08 +0100239 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
balrog7e7c5e42008-04-14 21:57:44 +0000240
241 tmp105_reset(&s->i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000242}
Paul Brook697454e2009-05-14 22:35:08 +0100243
Andreas Färbereb60d1c2013-01-16 01:57:59 +0100244static void tmp105_initfn(Object *obj)
245{
246 object_property_add(obj, "temperature", "int",
247 tmp105_get_temperature,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200248 tmp105_set_temperature, NULL, NULL);
Andreas Färbereb60d1c2013-01-16 01:57:59 +0100249}
250
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600251static void tmp105_class_init(ObjectClass *klass, void *data)
252{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600253 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600254 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
255
Philippe Mathieu-Daudéc8c9e102018-05-28 16:45:07 +0200256 dc->realize = tmp105_realize;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600257 k->event = tmp105_event;
258 k->recv = tmp105_rx;
259 k->send = tmp105_tx;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600260 dc->vmsd = &vmstate_tmp105;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600261}
262
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100263static const TypeInfo tmp105_info = {
Andreas Färber2aad80e2013-01-16 01:57:58 +0100264 .name = TYPE_TMP105,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600265 .parent = TYPE_I2C_SLAVE,
266 .instance_size = sizeof(TMP105State),
Andreas Färbereb60d1c2013-01-16 01:57:59 +0100267 .instance_init = tmp105_initfn,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600268 .class_init = tmp105_class_init,
Paul Brook697454e2009-05-14 22:35:08 +0100269};
270
Andreas Färber83f7d432012-02-09 15:20:55 +0100271static void tmp105_register_types(void)
Paul Brook697454e2009-05-14 22:35:08 +0100272{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600273 type_register_static(&tmp105_info);
Paul Brook697454e2009-05-14 22:35:08 +0100274}
275
Andreas Färber83f7d432012-02-09 15:20:55 +0100276type_init(tmp105_register_types)