]> git.proxmox.com Git - mirror_qemu.git/blame - hw/misc/tmp105.c
Merge remote-tracking branch 'remotes/ericb/tags/pull-nbd-2021-05-11' into staging
[mirror_qemu.git] / hw / misc / tmp105.c
CommitLineData
7e7c5e4c
AZ
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 *
fad6cb1a 17 * You should have received a copy of the GNU General Public License along
8167ee88 18 * with this program; if not, see <http://www.gnu.org/licenses/>.
7e7c5e4c
AZ
19 */
20
0d1c9782 21#include "qemu/osdep.h"
0d09e41a 22#include "hw/i2c/i2c.h"
64552b6b 23#include "hw/irq.h"
d6454270 24#include "migration/vmstate.h"
47b43a1f 25#include "tmp105.h"
da34e65c 26#include "qapi/error.h"
eb60d1c5 27#include "qapi/visitor.h"
0b8fa32f 28#include "qemu/module.h"
7e7c5e4c 29
bc24a225 30static void tmp105_interrupt_update(TMP105State *s)
7e7c5e4c
AZ
31{
32 qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1)); /* POL */
33}
34
bc24a225 35static void tmp105_alarm_update(TMP105State *s)
7e7c5e4c
AZ
36{
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
ab135622
PM
44 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 }
7e7c5e4c 61 } else {
ab135622
PM
62 /*
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 }
7e7c5e4c
AZ
78 }
79
80 tmp105_interrupt_update(s);
81}
82
d7bce999
EB
83static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name,
84 void *opaque, Error **errp)
eb60d1c5
AF
85{
86 TMP105State *s = TMP105(obj);
efdf6a56 87 int64_t value = s->temperature * 1000 / 256;
eb60d1c5 88
51e72bc1 89 visit_type_int(v, name, &value, errp);
eb60d1c5
AF
90}
91
efdf6a56
PB
92/* 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 */
d7bce999
EB
95static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name,
96 void *opaque, Error **errp)
7e7c5e4c 97{
eb60d1c5
AF
98 TMP105State *s = TMP105(obj);
99 int64_t temp;
7e7c5e4c 100
668f62ec 101 if (!visit_type_int(v, name, &temp, errp)) {
eb60d1c5
AF
102 return;
103 }
7e7c5e4c 104 if (temp >= 128000 || temp < -128000) {
3381466d 105 error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range",
eb60d1c5
AF
106 temp / 1000, temp % 1000);
107 return;
7e7c5e4c
AZ
108 }
109
efdf6a56 110 s->temperature = (int16_t) (temp * 256 / 1000);
7e7c5e4c
AZ
111
112 tmp105_alarm_update(s);
113}
114
115static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
116
bc24a225 117static void tmp105_read(TMP105State *s)
7e7c5e4c
AZ
118{
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) {
2915efbf 127 case TMP105_REG_TEMPERATURE:
7e7c5e4c
AZ
128 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
2915efbf 133 case TMP105_REG_CONFIG:
7e7c5e4c
AZ
134 s->buf[s->len ++] = s->config;
135 break;
136
2915efbf 137 case TMP105_REG_T_LOW:
7e7c5e4c
AZ
138 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
139 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
140 break;
141
2915efbf 142 case TMP105_REG_T_HIGH:
7e7c5e4c
AZ
143 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
bc24a225 149static void tmp105_write(TMP105State *s)
7e7c5e4c
AZ
150{
151 switch (s->pointer & 3) {
2915efbf 152 case TMP105_REG_TEMPERATURE:
7e7c5e4c
AZ
153 break;
154
2915efbf 155 case TMP105_REG_CONFIG:
7e7c5e4c 156 if (s->buf[0] & ~s->config & (1 << 0)) /* SD */
a89f364a 157 printf("%s: TMP105 shutdown\n", __func__);
7e7c5e4c
AZ
158 s->config = s->buf[0];
159 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
160 tmp105_alarm_update(s);
161 break;
162
2915efbf
AH
163 case TMP105_REG_T_LOW:
164 case TMP105_REG_T_HIGH:
7e7c5e4c
AZ
165 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
2ac4c5f4 173static uint8_t tmp105_rx(I2CSlave *i2c)
7e7c5e4c 174{
2aad80ee 175 TMP105State *s = TMP105(i2c);
7e7c5e4c 176
2aad80ee 177 if (s->len < 2) {
7e7c5e4c 178 return s->buf[s->len ++];
2aad80ee 179 } else {
7e7c5e4c 180 return 0xff;
2aad80ee 181 }
7e7c5e4c
AZ
182}
183
9e07bdf8 184static int tmp105_tx(I2CSlave *i2c, uint8_t data)
7e7c5e4c 185{
2aad80ee 186 TMP105State *s = TMP105(i2c);
7e7c5e4c 187
cb5ef3fa 188 if (s->len == 0) {
7e7c5e4c 189 s->pointer = data;
cb5ef3fa
AF
190 s->len++;
191 } else {
192 if (s->len <= 2) {
7e7c5e4c 193 s->buf[s->len - 1] = data;
cb5ef3fa
AF
194 }
195 s->len++;
7e7c5e4c
AZ
196 tmp105_write(s);
197 }
198
199 return 0;
200}
201
d307c28c 202static int tmp105_event(I2CSlave *i2c, enum i2c_event event)
7e7c5e4c 203{
2aad80ee 204 TMP105State *s = TMP105(i2c);
7e7c5e4c 205
2aad80ee 206 if (event == I2C_START_RECV) {
7e7c5e4c 207 tmp105_read(s);
2aad80ee 208 }
7e7c5e4c
AZ
209
210 s->len = 0;
d307c28c 211 return 0;
7e7c5e4c
AZ
212}
213
371a4468 214static int tmp105_post_load(void *opaque, int version_id)
7e7c5e4c 215{
371a4468 216 TMP105State *s = opaque;
7e7c5e4c 217
e5d3b98d
AZ
218 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
219
7e7c5e4c 220 tmp105_interrupt_update(s);
7e7c5e4c
AZ
221 return 0;
222}
223
ab135622
PM
224static 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,
241 .fields = (VMStateField[]) {
242 VMSTATE_BOOL(detect_falling, TMP105State),
243 VMSTATE_END_OF_LIST()
244 }
245};
246
371a4468
JQ
247static const VMStateDescription vmstate_tmp105 = {
248 .name = "TMP105",
249 .version_id = 0,
250 .minimum_version_id = 0,
371a4468 251 .post_load = tmp105_post_load,
8f1e884b 252 .fields = (VMStateField[]) {
371a4468
JQ
253 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()
ab135622
PM
262 },
263 .subsections = (const VMStateDescription*[]) {
264 &vmstate_tmp105_detect_falling,
265 NULL
371a4468
JQ
266 }
267};
268
9e07bdf8 269static void tmp105_reset(I2CSlave *i2c)
7e7c5e4c 270{
2aad80ee 271 TMP105State *s = TMP105(i2c);
7e7c5e4c
AZ
272
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;
ab135622 278 s->detect_falling = false;
7e7c5e4c 279
e1919889
PM
280 s->limit[0] = 0x4b00; /* T_LOW, 75 degrees C */
281 s->limit[1] = 0x5000; /* T_HIGH, 80 degrees C */
282
7e7c5e4c
AZ
283 tmp105_interrupt_update(s);
284}
285
c8c9e103 286static void tmp105_realize(DeviceState *dev, Error **errp)
7e7c5e4c 287{
c8c9e103 288 I2CSlave *i2c = I2C_SLAVE(dev);
2aad80ee 289 TMP105State *s = TMP105(i2c);
7e7c5e4c 290
697454eb 291 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
7e7c5e4c
AZ
292
293 tmp105_reset(&s->i2c);
697454eb
PB
294}
295
eb60d1c5
AF
296static void tmp105_initfn(Object *obj)
297{
298 object_property_add(obj, "temperature", "int",
299 tmp105_get_temperature,
d2623129 300 tmp105_set_temperature, NULL, NULL);
eb60d1c5
AF
301}
302
b5ea9327
AL
303static void tmp105_class_init(ObjectClass *klass, void *data)
304{
39bffca2 305 DeviceClass *dc = DEVICE_CLASS(klass);
b5ea9327
AL
306 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
307
c8c9e103 308 dc->realize = tmp105_realize;
b5ea9327
AL
309 k->event = tmp105_event;
310 k->recv = tmp105_rx;
311 k->send = tmp105_tx;
39bffca2 312 dc->vmsd = &vmstate_tmp105;
b5ea9327
AL
313}
314
8c43a6f0 315static const TypeInfo tmp105_info = {
2aad80ee 316 .name = TYPE_TMP105,
39bffca2
AL
317 .parent = TYPE_I2C_SLAVE,
318 .instance_size = sizeof(TMP105State),
eb60d1c5 319 .instance_init = tmp105_initfn,
39bffca2 320 .class_init = tmp105_class_init,
697454eb 321};
7e7c5e4c 322
83f7d43a 323static void tmp105_register_types(void)
697454eb 324{
39bffca2 325 type_register_static(&tmp105_info);
7e7c5e4c 326}
697454eb 327
83f7d43a 328type_init(tmp105_register_types)