blob: f6d7163273ad2c975bc577673f0f2ea52533a762 [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 Bonzini83c9f4c2013-02-04 15:40:22 +010022#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010023#include "hw/i2c/i2c.h"
Paolo Bonzini47b43a12013-03-18 17:36:02 +010024#include "tmp105.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
Andreas Färbereb60d1c2013-01-16 01:57:59 +010026#include "qapi/visitor.h"
balrog7e7c5e42008-04-14 21:57:44 +000027
Paul Brookbc24a222009-05-10 01:44:56 +010028static void tmp105_interrupt_update(TMP105State *s)
balrog7e7c5e42008-04-14 21:57:44 +000029{
30 qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1)); /* POL */
31}
32
Paul Brookbc24a222009-05-10 01:44:56 +010033static void tmp105_alarm_update(TMP105State *s)
balrog7e7c5e42008-04-14 21:57:44 +000034{
35 if ((s->config >> 0) & 1) { /* SD */
36 if ((s->config >> 7) & 1) /* OS */
37 s->config &= ~(1 << 7); /* OS */
38 else
39 return;
40 }
41
42 if ((s->config >> 1) & 1) { /* TM */
43 if (s->temperature >= s->limit[1])
44 s->alarm = 1;
45 else if (s->temperature < s->limit[0])
46 s->alarm = 1;
47 } else {
48 if (s->temperature >= s->limit[1])
49 s->alarm = 1;
50 else if (s->temperature < s->limit[0])
51 s->alarm = 0;
52 }
53
54 tmp105_interrupt_update(s);
55}
56
Eric Blaked7bce992016-01-29 06:48:55 -070057static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name,
58 void *opaque, Error **errp)
balrog7e7c5e42008-04-14 21:57:44 +000059{
Andreas Färbereb60d1c2013-01-16 01:57:59 +010060 TMP105State *s = TMP105(obj);
Paolo Bonziniefdf6a52014-03-31 18:26:32 +020061 int64_t value = s->temperature * 1000 / 256;
balrog7e7c5e42008-04-14 21:57:44 +000062
Eric Blake51e72bc2016-01-29 06:48:54 -070063 visit_type_int(v, name, &value, errp);
Andreas Färbereb60d1c2013-01-16 01:57:59 +010064}
65
Paolo Bonziniefdf6a52014-03-31 18:26:32 +020066/* Units are 0.001 centigrades relative to 0 C. s->temperature is 8.8
67 * fixed point, so units are 1/256 centigrades. A simple ratio will do.
68 */
Eric Blaked7bce992016-01-29 06:48:55 -070069static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name,
70 void *opaque, Error **errp)
Andreas Färbereb60d1c2013-01-16 01:57:59 +010071{
72 TMP105State *s = TMP105(obj);
Markus Armbruster65cd9062014-04-25 12:44:22 +020073 Error *local_err = NULL;
Andreas Färbereb60d1c2013-01-16 01:57:59 +010074 int64_t temp;
75
Eric Blake51e72bc2016-01-29 06:48:54 -070076 visit_type_int(v, name, &temp, &local_err);
Markus Armbruster65cd9062014-04-25 12:44:22 +020077 if (local_err) {
78 error_propagate(errp, local_err);
Andreas Färbereb60d1c2013-01-16 01:57:59 +010079 return;
80 }
balrog7e7c5e42008-04-14 21:57:44 +000081 if (temp >= 128000 || temp < -128000) {
Eric Blake33814662018-11-20 14:36:28 -060082 error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range",
Andreas Färbereb60d1c2013-01-16 01:57:59 +010083 temp / 1000, temp % 1000);
84 return;
balrog7e7c5e42008-04-14 21:57:44 +000085 }
86
Paolo Bonziniefdf6a52014-03-31 18:26:32 +020087 s->temperature = (int16_t) (temp * 256 / 1000);
balrog7e7c5e42008-04-14 21:57:44 +000088
89 tmp105_alarm_update(s);
90}
91
92static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
93
Paul Brookbc24a222009-05-10 01:44:56 +010094static void tmp105_read(TMP105State *s)
balrog7e7c5e42008-04-14 21:57:44 +000095{
96 s->len = 0;
97
98 if ((s->config >> 1) & 1) { /* TM */
99 s->alarm = 0;
100 tmp105_interrupt_update(s);
101 }
102
103 switch (s->pointer & 3) {
Alex Horn2915efb2012-12-05 12:34:06 +0000104 case TMP105_REG_TEMPERATURE:
balrog7e7c5e42008-04-14 21:57:44 +0000105 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8);
106 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) &
107 (0xf0 << ((~s->config >> 5) & 3)); /* R */
108 break;
109
Alex Horn2915efb2012-12-05 12:34:06 +0000110 case TMP105_REG_CONFIG:
balrog7e7c5e42008-04-14 21:57:44 +0000111 s->buf[s->len ++] = s->config;
112 break;
113
Alex Horn2915efb2012-12-05 12:34:06 +0000114 case TMP105_REG_T_LOW:
balrog7e7c5e42008-04-14 21:57:44 +0000115 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
116 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
117 break;
118
Alex Horn2915efb2012-12-05 12:34:06 +0000119 case TMP105_REG_T_HIGH:
balrog7e7c5e42008-04-14 21:57:44 +0000120 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8;
121 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0;
122 break;
123 }
124}
125
Paul Brookbc24a222009-05-10 01:44:56 +0100126static void tmp105_write(TMP105State *s)
balrog7e7c5e42008-04-14 21:57:44 +0000127{
128 switch (s->pointer & 3) {
Alex Horn2915efb2012-12-05 12:34:06 +0000129 case TMP105_REG_TEMPERATURE:
balrog7e7c5e42008-04-14 21:57:44 +0000130 break;
131
Alex Horn2915efb2012-12-05 12:34:06 +0000132 case TMP105_REG_CONFIG:
balrog7e7c5e42008-04-14 21:57:44 +0000133 if (s->buf[0] & ~s->config & (1 << 0)) /* SD */
Alistair Francisa89f3642017-11-08 14:56:31 -0800134 printf("%s: TMP105 shutdown\n", __func__);
balrog7e7c5e42008-04-14 21:57:44 +0000135 s->config = s->buf[0];
136 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
137 tmp105_alarm_update(s);
138 break;
139
Alex Horn2915efb2012-12-05 12:34:06 +0000140 case TMP105_REG_T_LOW:
141 case TMP105_REG_T_HIGH:
balrog7e7c5e42008-04-14 21:57:44 +0000142 if (s->len >= 3)
143 s->limit[s->pointer & 1] = (int16_t)
144 ((((uint16_t) s->buf[0]) << 8) | s->buf[1]);
145 tmp105_alarm_update(s);
146 break;
147 }
148}
149
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600150static int tmp105_rx(I2CSlave *i2c)
balrog7e7c5e42008-04-14 21:57:44 +0000151{
Andreas Färber2aad80e2013-01-16 01:57:58 +0100152 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000153
Andreas Färber2aad80e2013-01-16 01:57:58 +0100154 if (s->len < 2) {
balrog7e7c5e42008-04-14 21:57:44 +0000155 return s->buf[s->len ++];
Andreas Färber2aad80e2013-01-16 01:57:58 +0100156 } else {
balrog7e7c5e42008-04-14 21:57:44 +0000157 return 0xff;
Andreas Färber2aad80e2013-01-16 01:57:58 +0100158 }
balrog7e7c5e42008-04-14 21:57:44 +0000159}
160
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600161static int tmp105_tx(I2CSlave *i2c, uint8_t data)
balrog7e7c5e42008-04-14 21:57:44 +0000162{
Andreas Färber2aad80e2013-01-16 01:57:58 +0100163 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000164
Andreas Färbercb5ef3f2013-01-16 01:57:56 +0100165 if (s->len == 0) {
balrog7e7c5e42008-04-14 21:57:44 +0000166 s->pointer = data;
Andreas Färbercb5ef3f2013-01-16 01:57:56 +0100167 s->len++;
168 } else {
169 if (s->len <= 2) {
balrog7e7c5e42008-04-14 21:57:44 +0000170 s->buf[s->len - 1] = data;
Andreas Färbercb5ef3f2013-01-16 01:57:56 +0100171 }
172 s->len++;
balrog7e7c5e42008-04-14 21:57:44 +0000173 tmp105_write(s);
174 }
175
176 return 0;
177}
178
Corey Minyardd307c282017-01-09 11:40:20 +0000179static int tmp105_event(I2CSlave *i2c, enum i2c_event event)
balrog7e7c5e42008-04-14 21:57:44 +0000180{
Andreas Färber2aad80e2013-01-16 01:57:58 +0100181 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000182
Andreas Färber2aad80e2013-01-16 01:57:58 +0100183 if (event == I2C_START_RECV) {
balrog7e7c5e42008-04-14 21:57:44 +0000184 tmp105_read(s);
Andreas Färber2aad80e2013-01-16 01:57:58 +0100185 }
balrog7e7c5e42008-04-14 21:57:44 +0000186
187 s->len = 0;
Corey Minyardd307c282017-01-09 11:40:20 +0000188 return 0;
balrog7e7c5e42008-04-14 21:57:44 +0000189}
190
Juan Quintela371a4462009-09-29 22:48:38 +0200191static int tmp105_post_load(void *opaque, int version_id)
balrog7e7c5e42008-04-14 21:57:44 +0000192{
Juan Quintela371a4462009-09-29 22:48:38 +0200193 TMP105State *s = opaque;
balrog7e7c5e42008-04-14 21:57:44 +0000194
Andrzej Zaborowskie5d3b982010-05-15 14:31:27 +0200195 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
196
balrog7e7c5e42008-04-14 21:57:44 +0000197 tmp105_interrupt_update(s);
balrog7e7c5e42008-04-14 21:57:44 +0000198 return 0;
199}
200
Juan Quintela371a4462009-09-29 22:48:38 +0200201static const VMStateDescription vmstate_tmp105 = {
202 .name = "TMP105",
203 .version_id = 0,
204 .minimum_version_id = 0,
Juan Quintela371a4462009-09-29 22:48:38 +0200205 .post_load = tmp105_post_load,
Juan Quintela8f1e8842014-05-13 16:09:35 +0100206 .fields = (VMStateField[]) {
Juan Quintela371a4462009-09-29 22:48:38 +0200207 VMSTATE_UINT8(len, TMP105State),
208 VMSTATE_UINT8_ARRAY(buf, TMP105State, 2),
209 VMSTATE_UINT8(pointer, TMP105State),
210 VMSTATE_UINT8(config, TMP105State),
211 VMSTATE_INT16(temperature, TMP105State),
212 VMSTATE_INT16_ARRAY(limit, TMP105State, 2),
213 VMSTATE_UINT8(alarm, TMP105State),
214 VMSTATE_I2C_SLAVE(i2c, TMP105State),
215 VMSTATE_END_OF_LIST()
216 }
217};
218
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600219static void tmp105_reset(I2CSlave *i2c)
balrog7e7c5e42008-04-14 21:57:44 +0000220{
Andreas Färber2aad80e2013-01-16 01:57:58 +0100221 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000222
223 s->temperature = 0;
224 s->pointer = 0;
225 s->config = 0;
226 s->faults = tmp105_faultq[(s->config >> 3) & 3];
227 s->alarm = 0;
228
229 tmp105_interrupt_update(s);
230}
231
Philippe Mathieu-Daudéc8c9e102018-05-28 16:45:07 +0200232static void tmp105_realize(DeviceState *dev, Error **errp)
balrog7e7c5e42008-04-14 21:57:44 +0000233{
Philippe Mathieu-Daudéc8c9e102018-05-28 16:45:07 +0200234 I2CSlave *i2c = I2C_SLAVE(dev);
Andreas Färber2aad80e2013-01-16 01:57:58 +0100235 TMP105State *s = TMP105(i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000236
Paul Brook697454e2009-05-14 22:35:08 +0100237 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
balrog7e7c5e42008-04-14 21:57:44 +0000238
239 tmp105_reset(&s->i2c);
balrog7e7c5e42008-04-14 21:57:44 +0000240}
Paul Brook697454e2009-05-14 22:35:08 +0100241
Andreas Färbereb60d1c2013-01-16 01:57:59 +0100242static void tmp105_initfn(Object *obj)
243{
244 object_property_add(obj, "temperature", "int",
245 tmp105_get_temperature,
246 tmp105_set_temperature, NULL, NULL, NULL);
247}
248
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600249static void tmp105_class_init(ObjectClass *klass, void *data)
250{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600251 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600252 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
253
Philippe Mathieu-Daudéc8c9e102018-05-28 16:45:07 +0200254 dc->realize = tmp105_realize;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600255 k->event = tmp105_event;
256 k->recv = tmp105_rx;
257 k->send = tmp105_tx;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600258 dc->vmsd = &vmstate_tmp105;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600259}
260
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100261static const TypeInfo tmp105_info = {
Andreas Färber2aad80e2013-01-16 01:57:58 +0100262 .name = TYPE_TMP105,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600263 .parent = TYPE_I2C_SLAVE,
264 .instance_size = sizeof(TMP105State),
Andreas Färbereb60d1c2013-01-16 01:57:59 +0100265 .instance_init = tmp105_initfn,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600266 .class_init = tmp105_class_init,
Paul Brook697454e2009-05-14 22:35:08 +0100267};
268
Andreas Färber83f7d432012-02-09 15:20:55 +0100269static void tmp105_register_types(void)
Paul Brook697454e2009-05-14 22:35:08 +0100270{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600271 type_register_static(&tmp105_info);
Paul Brook697454e2009-05-14 22:35:08 +0100272}
273
Andreas Färber83f7d432012-02-09 15:20:55 +0100274type_init(tmp105_register_types)