meson: convert hw/vfio
[qemu.git] / hw / misc / tmp105.c
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 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "qemu/osdep.h"
22 #include "hw/i2c/i2c.h"
23 #include "hw/irq.h"
24 #include "migration/vmstate.h"
25 #include "tmp105.h"
26 #include "qapi/error.h"
27 #include "qapi/visitor.h"
28 #include "qemu/module.h"
29
30 static void tmp105_interrupt_update(TMP105State *s)
31 {
32 qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1)); /* POL */
33 }
34
35 static void tmp105_alarm_update(TMP105State *s)
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
59 static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name,
60 void *opaque, Error **errp)
61 {
62 TMP105State *s = TMP105(obj);
63 int64_t value = s->temperature * 1000 / 256;
64
65 visit_type_int(v, name, &value, errp);
66 }
67
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 */
71 static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name,
72 void *opaque, Error **errp)
73 {
74 TMP105State *s = TMP105(obj);
75 int64_t temp;
76
77 if (!visit_type_int(v, name, &temp, errp)) {
78 return;
79 }
80 if (temp >= 128000 || temp < -128000) {
81 error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range",
82 temp / 1000, temp % 1000);
83 return;
84 }
85
86 s->temperature = (int16_t) (temp * 256 / 1000);
87
88 tmp105_alarm_update(s);
89 }
90
91 static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
92
93 static void tmp105_read(TMP105State *s)
94 {
95 s->len = 0;
96
97 if ((s->config >> 1) & 1) { /* TM */
98 s->alarm = 0;
99 tmp105_interrupt_update(s);
100 }
101
102 switch (s->pointer & 3) {
103 case TMP105_REG_TEMPERATURE:
104 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8);
105 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) &
106 (0xf0 << ((~s->config >> 5) & 3)); /* R */
107 break;
108
109 case TMP105_REG_CONFIG:
110 s->buf[s->len ++] = s->config;
111 break;
112
113 case TMP105_REG_T_LOW:
114 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
115 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
116 break;
117
118 case TMP105_REG_T_HIGH:
119 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8;
120 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0;
121 break;
122 }
123 }
124
125 static void tmp105_write(TMP105State *s)
126 {
127 switch (s->pointer & 3) {
128 case TMP105_REG_TEMPERATURE:
129 break;
130
131 case TMP105_REG_CONFIG:
132 if (s->buf[0] & ~s->config & (1 << 0)) /* SD */
133 printf("%s: TMP105 shutdown\n", __func__);
134 s->config = s->buf[0];
135 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
136 tmp105_alarm_update(s);
137 break;
138
139 case TMP105_REG_T_LOW:
140 case TMP105_REG_T_HIGH:
141 if (s->len >= 3)
142 s->limit[s->pointer & 1] = (int16_t)
143 ((((uint16_t) s->buf[0]) << 8) | s->buf[1]);
144 tmp105_alarm_update(s);
145 break;
146 }
147 }
148
149 static uint8_t tmp105_rx(I2CSlave *i2c)
150 {
151 TMP105State *s = TMP105(i2c);
152
153 if (s->len < 2) {
154 return s->buf[s->len ++];
155 } else {
156 return 0xff;
157 }
158 }
159
160 static int tmp105_tx(I2CSlave *i2c, uint8_t data)
161 {
162 TMP105State *s = TMP105(i2c);
163
164 if (s->len == 0) {
165 s->pointer = data;
166 s->len++;
167 } else {
168 if (s->len <= 2) {
169 s->buf[s->len - 1] = data;
170 }
171 s->len++;
172 tmp105_write(s);
173 }
174
175 return 0;
176 }
177
178 static int tmp105_event(I2CSlave *i2c, enum i2c_event event)
179 {
180 TMP105State *s = TMP105(i2c);
181
182 if (event == I2C_START_RECV) {
183 tmp105_read(s);
184 }
185
186 s->len = 0;
187 return 0;
188 }
189
190 static int tmp105_post_load(void *opaque, int version_id)
191 {
192 TMP105State *s = opaque;
193
194 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
195
196 tmp105_interrupt_update(s);
197 return 0;
198 }
199
200 static const VMStateDescription vmstate_tmp105 = {
201 .name = "TMP105",
202 .version_id = 0,
203 .minimum_version_id = 0,
204 .post_load = tmp105_post_load,
205 .fields = (VMStateField[]) {
206 VMSTATE_UINT8(len, TMP105State),
207 VMSTATE_UINT8_ARRAY(buf, TMP105State, 2),
208 VMSTATE_UINT8(pointer, TMP105State),
209 VMSTATE_UINT8(config, TMP105State),
210 VMSTATE_INT16(temperature, TMP105State),
211 VMSTATE_INT16_ARRAY(limit, TMP105State, 2),
212 VMSTATE_UINT8(alarm, TMP105State),
213 VMSTATE_I2C_SLAVE(i2c, TMP105State),
214 VMSTATE_END_OF_LIST()
215 }
216 };
217
218 static void tmp105_reset(I2CSlave *i2c)
219 {
220 TMP105State *s = TMP105(i2c);
221
222 s->temperature = 0;
223 s->pointer = 0;
224 s->config = 0;
225 s->faults = tmp105_faultq[(s->config >> 3) & 3];
226 s->alarm = 0;
227
228 tmp105_interrupt_update(s);
229 }
230
231 static void tmp105_realize(DeviceState *dev, Error **errp)
232 {
233 I2CSlave *i2c = I2C_SLAVE(dev);
234 TMP105State *s = TMP105(i2c);
235
236 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
237
238 tmp105_reset(&s->i2c);
239 }
240
241 static void tmp105_initfn(Object *obj)
242 {
243 object_property_add(obj, "temperature", "int",
244 tmp105_get_temperature,
245 tmp105_set_temperature, NULL, NULL);
246 }
247
248 static void tmp105_class_init(ObjectClass *klass, void *data)
249 {
250 DeviceClass *dc = DEVICE_CLASS(klass);
251 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
252
253 dc->realize = tmp105_realize;
254 k->event = tmp105_event;
255 k->recv = tmp105_rx;
256 k->send = tmp105_tx;
257 dc->vmsd = &vmstate_tmp105;
258 }
259
260 static const TypeInfo tmp105_info = {
261 .name = TYPE_TMP105,
262 .parent = TYPE_I2C_SLAVE,
263 .instance_size = sizeof(TMP105State),
264 .instance_init = tmp105_initfn,
265 .class_init = tmp105_class_init,
266 };
267
268 static void tmp105_register_types(void)
269 {
270 type_register_static(&tmp105_info);
271 }
272
273 type_init(tmp105_register_types)