]> Git Repo - qemu.git/blame - hw/misc/tmp105.c
migration: Move the VMStateDescription typedef to typedefs.h
[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 <[email protected]>
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"
83c9f4ca 22#include "hw/hw.h"
0d09e41a 23#include "hw/i2c/i2c.h"
64552b6b 24#include "hw/irq.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
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
d7bce999
EB
59static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name,
60 void *opaque, Error **errp)
eb60d1c5
AF
61{
62 TMP105State *s = TMP105(obj);
efdf6a56 63 int64_t value = s->temperature * 1000 / 256;
eb60d1c5 64
51e72bc1 65 visit_type_int(v, name, &value, errp);
eb60d1c5
AF
66}
67
efdf6a56
PB
68/* 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 */
d7bce999
EB
71static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name,
72 void *opaque, Error **errp)
7e7c5e4c 73{
eb60d1c5 74 TMP105State *s = TMP105(obj);
65cd9064 75 Error *local_err = NULL;
eb60d1c5 76 int64_t temp;
7e7c5e4c 77
51e72bc1 78 visit_type_int(v, name, &temp, &local_err);
65cd9064
MA
79 if (local_err) {
80 error_propagate(errp, local_err);
eb60d1c5
AF
81 return;
82 }
7e7c5e4c 83 if (temp >= 128000 || temp < -128000) {
3381466d 84 error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range",
eb60d1c5
AF
85 temp / 1000, temp % 1000);
86 return;
7e7c5e4c
AZ
87 }
88
efdf6a56 89 s->temperature = (int16_t) (temp * 256 / 1000);
7e7c5e4c
AZ
90
91 tmp105_alarm_update(s);
92}
93
94static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
95
bc24a225 96static void tmp105_read(TMP105State *s)
7e7c5e4c
AZ
97{
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) {
2915efbf 106 case TMP105_REG_TEMPERATURE:
7e7c5e4c
AZ
107 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
2915efbf 112 case TMP105_REG_CONFIG:
7e7c5e4c
AZ
113 s->buf[s->len ++] = s->config;
114 break;
115
2915efbf 116 case TMP105_REG_T_LOW:
7e7c5e4c
AZ
117 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
118 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
119 break;
120
2915efbf 121 case TMP105_REG_T_HIGH:
7e7c5e4c
AZ
122 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
bc24a225 128static void tmp105_write(TMP105State *s)
7e7c5e4c
AZ
129{
130 switch (s->pointer & 3) {
2915efbf 131 case TMP105_REG_TEMPERATURE:
7e7c5e4c
AZ
132 break;
133
2915efbf 134 case TMP105_REG_CONFIG:
7e7c5e4c 135 if (s->buf[0] & ~s->config & (1 << 0)) /* SD */
a89f364a 136 printf("%s: TMP105 shutdown\n", __func__);
7e7c5e4c
AZ
137 s->config = s->buf[0];
138 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
139 tmp105_alarm_update(s);
140 break;
141
2915efbf
AH
142 case TMP105_REG_T_LOW:
143 case TMP105_REG_T_HIGH:
7e7c5e4c
AZ
144 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
2ac4c5f4 152static uint8_t tmp105_rx(I2CSlave *i2c)
7e7c5e4c 153{
2aad80ee 154 TMP105State *s = TMP105(i2c);
7e7c5e4c 155
2aad80ee 156 if (s->len < 2) {
7e7c5e4c 157 return s->buf[s->len ++];
2aad80ee 158 } else {
7e7c5e4c 159 return 0xff;
2aad80ee 160 }
7e7c5e4c
AZ
161}
162
9e07bdf8 163static int tmp105_tx(I2CSlave *i2c, uint8_t data)
7e7c5e4c 164{
2aad80ee 165 TMP105State *s = TMP105(i2c);
7e7c5e4c 166
cb5ef3fa 167 if (s->len == 0) {
7e7c5e4c 168 s->pointer = data;
cb5ef3fa
AF
169 s->len++;
170 } else {
171 if (s->len <= 2) {
7e7c5e4c 172 s->buf[s->len - 1] = data;
cb5ef3fa
AF
173 }
174 s->len++;
7e7c5e4c
AZ
175 tmp105_write(s);
176 }
177
178 return 0;
179}
180
d307c28c 181static int tmp105_event(I2CSlave *i2c, enum i2c_event event)
7e7c5e4c 182{
2aad80ee 183 TMP105State *s = TMP105(i2c);
7e7c5e4c 184
2aad80ee 185 if (event == I2C_START_RECV) {
7e7c5e4c 186 tmp105_read(s);
2aad80ee 187 }
7e7c5e4c
AZ
188
189 s->len = 0;
d307c28c 190 return 0;
7e7c5e4c
AZ
191}
192
371a4468 193static int tmp105_post_load(void *opaque, int version_id)
7e7c5e4c 194{
371a4468 195 TMP105State *s = opaque;
7e7c5e4c 196
e5d3b98d
AZ
197 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
198
7e7c5e4c 199 tmp105_interrupt_update(s);
7e7c5e4c
AZ
200 return 0;
201}
202
371a4468
JQ
203static const VMStateDescription vmstate_tmp105 = {
204 .name = "TMP105",
205 .version_id = 0,
206 .minimum_version_id = 0,
371a4468 207 .post_load = tmp105_post_load,
8f1e884b 208 .fields = (VMStateField[]) {
371a4468
JQ
209 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
9e07bdf8 221static void tmp105_reset(I2CSlave *i2c)
7e7c5e4c 222{
2aad80ee 223 TMP105State *s = TMP105(i2c);
7e7c5e4c
AZ
224
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
c8c9e103 234static void tmp105_realize(DeviceState *dev, Error **errp)
7e7c5e4c 235{
c8c9e103 236 I2CSlave *i2c = I2C_SLAVE(dev);
2aad80ee 237 TMP105State *s = TMP105(i2c);
7e7c5e4c 238
697454eb 239 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
7e7c5e4c
AZ
240
241 tmp105_reset(&s->i2c);
697454eb
PB
242}
243
eb60d1c5
AF
244static void tmp105_initfn(Object *obj)
245{
246 object_property_add(obj, "temperature", "int",
247 tmp105_get_temperature,
248 tmp105_set_temperature, NULL, NULL, NULL);
249}
250
b5ea9327
AL
251static void tmp105_class_init(ObjectClass *klass, void *data)
252{
39bffca2 253 DeviceClass *dc = DEVICE_CLASS(klass);
b5ea9327
AL
254 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
255
c8c9e103 256 dc->realize = tmp105_realize;
b5ea9327
AL
257 k->event = tmp105_event;
258 k->recv = tmp105_rx;
259 k->send = tmp105_tx;
39bffca2 260 dc->vmsd = &vmstate_tmp105;
b5ea9327
AL
261}
262
8c43a6f0 263static const TypeInfo tmp105_info = {
2aad80ee 264 .name = TYPE_TMP105,
39bffca2
AL
265 .parent = TYPE_I2C_SLAVE,
266 .instance_size = sizeof(TMP105State),
eb60d1c5 267 .instance_init = tmp105_initfn,
39bffca2 268 .class_init = tmp105_class_init,
697454eb 269};
7e7c5e4c 270
83f7d43a 271static void tmp105_register_types(void)
697454eb 272{
39bffca2 273 type_register_static(&tmp105_info);
7e7c5e4c 274}
697454eb 275
83f7d43a 276type_init(tmp105_register_types)
This page took 1.014255 seconds and 4 git commands to generate.