]>
Commit | Line | Data |
---|---|---|
fe3874b6 CLG |
1 | /* |
2 | * Texas Instruments TMP421 temperature sensor. | |
3 | * | |
4 | * Copyright (c) 2016 IBM Corporation. | |
5 | * | |
6 | * Largely inspired by : | |
7 | * | |
8 | * Texas Instruments TMP105 temperature sensor. | |
9 | * | |
10 | * Copyright (C) 2008 Nokia Corporation | |
11 | * Written by Andrzej Zaborowski <[email protected]> | |
12 | * | |
13 | * This program is free software; you can redistribute it and/or | |
14 | * modify it under the terms of the GNU General Public License as | |
15 | * published by the Free Software Foundation; either version 2 or | |
16 | * (at your option) version 3 of the License. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License along | |
24 | * with this program; if not, see <http://www.gnu.org/licenses/>. | |
25 | */ | |
26 | ||
27 | #include "qemu/osdep.h" | |
fe3874b6 | 28 | #include "hw/i2c/i2c.h" |
d6454270 | 29 | #include "migration/vmstate.h" |
fe3874b6 CLG |
30 | #include "qapi/error.h" |
31 | #include "qapi/visitor.h" | |
0b8fa32f | 32 | #include "qemu/module.h" |
fe3874b6 CLG |
33 | |
34 | /* Manufacturer / Device ID's */ | |
35 | #define TMP421_MANUFACTURER_ID 0x55 | |
36 | #define TMP421_DEVICE_ID 0x21 | |
37 | #define TMP422_DEVICE_ID 0x22 | |
38 | #define TMP423_DEVICE_ID 0x23 | |
39 | ||
40 | typedef struct DeviceInfo { | |
41 | int model; | |
42 | const char *name; | |
43 | } DeviceInfo; | |
44 | ||
45 | static const DeviceInfo devices[] = { | |
46 | { TMP421_DEVICE_ID, "tmp421" }, | |
47 | { TMP422_DEVICE_ID, "tmp422" }, | |
48 | { TMP423_DEVICE_ID, "tmp423" }, | |
49 | }; | |
50 | ||
51 | typedef struct TMP421State { | |
52 | /*< private >*/ | |
53 | I2CSlave i2c; | |
54 | /*< public >*/ | |
55 | ||
56 | int16_t temperature[4]; | |
57 | ||
58 | uint8_t status; | |
59 | uint8_t config[2]; | |
60 | uint8_t rate; | |
61 | ||
62 | uint8_t len; | |
63 | uint8_t buf[2]; | |
64 | uint8_t pointer; | |
65 | ||
66 | } TMP421State; | |
67 | ||
68 | typedef struct TMP421Class { | |
69 | I2CSlaveClass parent_class; | |
70 | DeviceInfo *dev; | |
71 | } TMP421Class; | |
72 | ||
73 | #define TYPE_TMP421 "tmp421-generic" | |
74 | #define TMP421(obj) OBJECT_CHECK(TMP421State, (obj), TYPE_TMP421) | |
75 | ||
76 | #define TMP421_CLASS(klass) \ | |
77 | OBJECT_CLASS_CHECK(TMP421Class, (klass), TYPE_TMP421) | |
78 | #define TMP421_GET_CLASS(obj) \ | |
79 | OBJECT_GET_CLASS(TMP421Class, (obj), TYPE_TMP421) | |
80 | ||
81 | /* the TMP421 registers */ | |
82 | #define TMP421_STATUS_REG 0x08 | |
83 | #define TMP421_STATUS_BUSY (1 << 7) | |
84 | #define TMP421_CONFIG_REG_1 0x09 | |
85 | #define TMP421_CONFIG_RANGE (1 << 2) | |
86 | #define TMP421_CONFIG_SHUTDOWN (1 << 6) | |
87 | #define TMP421_CONFIG_REG_2 0x0A | |
88 | #define TMP421_CONFIG_RC (1 << 2) | |
89 | #define TMP421_CONFIG_LEN (1 << 3) | |
90 | #define TMP421_CONFIG_REN (1 << 4) | |
91 | #define TMP421_CONFIG_REN2 (1 << 5) | |
92 | #define TMP421_CONFIG_REN3 (1 << 6) | |
93 | ||
94 | #define TMP421_CONVERSION_RATE_REG 0x0B | |
95 | #define TMP421_ONE_SHOT 0x0F | |
96 | ||
97 | #define TMP421_RESET 0xFC | |
98 | #define TMP421_MANUFACTURER_ID_REG 0xFE | |
99 | #define TMP421_DEVICE_ID_REG 0xFF | |
100 | ||
101 | #define TMP421_TEMP_MSB0 0x00 | |
102 | #define TMP421_TEMP_MSB1 0x01 | |
103 | #define TMP421_TEMP_MSB2 0x02 | |
104 | #define TMP421_TEMP_MSB3 0x03 | |
105 | #define TMP421_TEMP_LSB0 0x10 | |
106 | #define TMP421_TEMP_LSB1 0x11 | |
107 | #define TMP421_TEMP_LSB2 0x12 | |
108 | #define TMP421_TEMP_LSB3 0x13 | |
109 | ||
110 | static const int32_t mins[2] = { -40000, -55000 }; | |
111 | static const int32_t maxs[2] = { 127000, 150000 }; | |
112 | ||
113 | static void tmp421_get_temperature(Object *obj, Visitor *v, const char *name, | |
114 | void *opaque, Error **errp) | |
115 | { | |
116 | TMP421State *s = TMP421(obj); | |
117 | bool ext_range = (s->config[0] & TMP421_CONFIG_RANGE); | |
118 | int offset = ext_range * 64 * 256; | |
119 | int64_t value; | |
120 | int tempid; | |
121 | ||
122 | if (sscanf(name, "temperature%d", &tempid) != 1) { | |
372a87a1 | 123 | error_setg(errp, "error reading %s: %s", name, g_strerror(errno)); |
fe3874b6 CLG |
124 | return; |
125 | } | |
126 | ||
127 | if (tempid >= 4 || tempid < 0) { | |
128 | error_setg(errp, "error reading %s", name); | |
129 | return; | |
130 | } | |
131 | ||
132 | value = ((s->temperature[tempid] - offset) * 1000 + 128) / 256; | |
133 | ||
134 | visit_type_int(v, name, &value, errp); | |
135 | } | |
136 | ||
137 | /* Units are 0.001 centigrades relative to 0 C. s->temperature is 8.8 | |
138 | * fixed point, so units are 1/256 centigrades. A simple ratio will do. | |
139 | */ | |
140 | static void tmp421_set_temperature(Object *obj, Visitor *v, const char *name, | |
141 | void *opaque, Error **errp) | |
142 | { | |
143 | TMP421State *s = TMP421(obj); | |
144 | Error *local_err = NULL; | |
145 | int64_t temp; | |
146 | bool ext_range = (s->config[0] & TMP421_CONFIG_RANGE); | |
147 | int offset = ext_range * 64 * 256; | |
148 | int tempid; | |
149 | ||
150 | visit_type_int(v, name, &temp, &local_err); | |
151 | if (local_err) { | |
152 | error_propagate(errp, local_err); | |
153 | return; | |
154 | } | |
155 | ||
156 | if (temp >= maxs[ext_range] || temp < mins[ext_range]) { | |
3381466d | 157 | error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range", |
fe3874b6 CLG |
158 | temp / 1000, temp % 1000); |
159 | return; | |
160 | } | |
161 | ||
162 | if (sscanf(name, "temperature%d", &tempid) != 1) { | |
372a87a1 | 163 | error_setg(errp, "error reading %s: %s", name, g_strerror(errno)); |
fe3874b6 CLG |
164 | return; |
165 | } | |
166 | ||
167 | if (tempid >= 4 || tempid < 0) { | |
168 | error_setg(errp, "error reading %s", name); | |
169 | return; | |
170 | } | |
171 | ||
172 | s->temperature[tempid] = (int16_t) ((temp * 256 - 128) / 1000) + offset; | |
173 | } | |
174 | ||
175 | static void tmp421_read(TMP421State *s) | |
176 | { | |
177 | TMP421Class *sc = TMP421_GET_CLASS(s); | |
178 | ||
179 | s->len = 0; | |
180 | ||
181 | switch (s->pointer) { | |
182 | case TMP421_MANUFACTURER_ID_REG: | |
183 | s->buf[s->len++] = TMP421_MANUFACTURER_ID; | |
184 | break; | |
185 | case TMP421_DEVICE_ID_REG: | |
186 | s->buf[s->len++] = sc->dev->model; | |
187 | break; | |
188 | case TMP421_CONFIG_REG_1: | |
189 | s->buf[s->len++] = s->config[0]; | |
190 | break; | |
191 | case TMP421_CONFIG_REG_2: | |
192 | s->buf[s->len++] = s->config[1]; | |
193 | break; | |
194 | case TMP421_CONVERSION_RATE_REG: | |
195 | s->buf[s->len++] = s->rate; | |
196 | break; | |
197 | case TMP421_STATUS_REG: | |
198 | s->buf[s->len++] = s->status; | |
199 | break; | |
200 | ||
201 | /* FIXME: check for channel enablement in config registers */ | |
202 | case TMP421_TEMP_MSB0: | |
203 | s->buf[s->len++] = (((uint16_t) s->temperature[0]) >> 8); | |
204 | s->buf[s->len++] = (((uint16_t) s->temperature[0]) >> 0) & 0xf0; | |
205 | break; | |
206 | case TMP421_TEMP_MSB1: | |
207 | s->buf[s->len++] = (((uint16_t) s->temperature[1]) >> 8); | |
208 | s->buf[s->len++] = (((uint16_t) s->temperature[1]) >> 0) & 0xf0; | |
209 | break; | |
210 | case TMP421_TEMP_MSB2: | |
211 | s->buf[s->len++] = (((uint16_t) s->temperature[2]) >> 8); | |
212 | s->buf[s->len++] = (((uint16_t) s->temperature[2]) >> 0) & 0xf0; | |
213 | break; | |
214 | case TMP421_TEMP_MSB3: | |
215 | s->buf[s->len++] = (((uint16_t) s->temperature[3]) >> 8); | |
216 | s->buf[s->len++] = (((uint16_t) s->temperature[3]) >> 0) & 0xf0; | |
217 | break; | |
218 | case TMP421_TEMP_LSB0: | |
219 | s->buf[s->len++] = (((uint16_t) s->temperature[0]) >> 0) & 0xf0; | |
220 | break; | |
221 | case TMP421_TEMP_LSB1: | |
222 | s->buf[s->len++] = (((uint16_t) s->temperature[1]) >> 0) & 0xf0; | |
223 | break; | |
224 | case TMP421_TEMP_LSB2: | |
225 | s->buf[s->len++] = (((uint16_t) s->temperature[2]) >> 0) & 0xf0; | |
226 | break; | |
227 | case TMP421_TEMP_LSB3: | |
228 | s->buf[s->len++] = (((uint16_t) s->temperature[3]) >> 0) & 0xf0; | |
229 | break; | |
230 | } | |
231 | } | |
232 | ||
233 | static void tmp421_reset(I2CSlave *i2c); | |
234 | ||
235 | static void tmp421_write(TMP421State *s) | |
236 | { | |
237 | switch (s->pointer) { | |
238 | case TMP421_CONVERSION_RATE_REG: | |
239 | s->rate = s->buf[0]; | |
240 | break; | |
241 | case TMP421_CONFIG_REG_1: | |
242 | s->config[0] = s->buf[0]; | |
243 | break; | |
244 | case TMP421_CONFIG_REG_2: | |
245 | s->config[1] = s->buf[0]; | |
246 | break; | |
247 | case TMP421_RESET: | |
248 | tmp421_reset(I2C_SLAVE(s)); | |
249 | break; | |
250 | } | |
251 | } | |
252 | ||
2ac4c5f4 | 253 | static uint8_t tmp421_rx(I2CSlave *i2c) |
fe3874b6 CLG |
254 | { |
255 | TMP421State *s = TMP421(i2c); | |
256 | ||
257 | if (s->len < 2) { | |
258 | return s->buf[s->len++]; | |
259 | } else { | |
260 | return 0xff; | |
261 | } | |
262 | } | |
263 | ||
264 | static int tmp421_tx(I2CSlave *i2c, uint8_t data) | |
265 | { | |
266 | TMP421State *s = TMP421(i2c); | |
267 | ||
268 | if (s->len == 0) { | |
269 | /* first byte is the register pointer for a read or write | |
270 | * operation */ | |
271 | s->pointer = data; | |
272 | s->len++; | |
273 | } else if (s->len == 1) { | |
274 | /* second byte is the data to write. The device only supports | |
275 | * one byte writes */ | |
276 | s->buf[0] = data; | |
277 | tmp421_write(s); | |
278 | } | |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
283 | static int tmp421_event(I2CSlave *i2c, enum i2c_event event) | |
284 | { | |
285 | TMP421State *s = TMP421(i2c); | |
286 | ||
287 | if (event == I2C_START_RECV) { | |
288 | tmp421_read(s); | |
289 | } | |
290 | ||
291 | s->len = 0; | |
292 | return 0; | |
293 | } | |
294 | ||
295 | static const VMStateDescription vmstate_tmp421 = { | |
296 | .name = "TMP421", | |
297 | .version_id = 0, | |
298 | .minimum_version_id = 0, | |
299 | .fields = (VMStateField[]) { | |
300 | VMSTATE_UINT8(len, TMP421State), | |
301 | VMSTATE_UINT8_ARRAY(buf, TMP421State, 2), | |
302 | VMSTATE_UINT8(pointer, TMP421State), | |
303 | VMSTATE_UINT8_ARRAY(config, TMP421State, 2), | |
304 | VMSTATE_UINT8(status, TMP421State), | |
305 | VMSTATE_UINT8(rate, TMP421State), | |
306 | VMSTATE_INT16_ARRAY(temperature, TMP421State, 4), | |
307 | VMSTATE_I2C_SLAVE(i2c, TMP421State), | |
308 | VMSTATE_END_OF_LIST() | |
309 | } | |
310 | }; | |
311 | ||
312 | static void tmp421_reset(I2CSlave *i2c) | |
313 | { | |
314 | TMP421State *s = TMP421(i2c); | |
315 | TMP421Class *sc = TMP421_GET_CLASS(s); | |
316 | ||
317 | memset(s->temperature, 0, sizeof(s->temperature)); | |
318 | s->pointer = 0; | |
319 | ||
320 | s->config[0] = 0; /* TMP421_CONFIG_RANGE */ | |
321 | ||
322 | /* resistance correction and channel enablement */ | |
323 | switch (sc->dev->model) { | |
324 | case TMP421_DEVICE_ID: | |
325 | s->config[1] = 0x1c; | |
326 | break; | |
327 | case TMP422_DEVICE_ID: | |
328 | s->config[1] = 0x3c; | |
329 | break; | |
330 | case TMP423_DEVICE_ID: | |
331 | s->config[1] = 0x7c; | |
332 | break; | |
333 | } | |
334 | ||
335 | s->rate = 0x7; /* 8Hz */ | |
336 | s->status = 0; | |
337 | } | |
338 | ||
c8c9e103 | 339 | static void tmp421_realize(DeviceState *dev, Error **errp) |
fe3874b6 | 340 | { |
c8c9e103 | 341 | TMP421State *s = TMP421(dev); |
fe3874b6 CLG |
342 | |
343 | tmp421_reset(&s->i2c); | |
fe3874b6 CLG |
344 | } |
345 | ||
346 | static void tmp421_initfn(Object *obj) | |
347 | { | |
348 | object_property_add(obj, "temperature0", "int", | |
349 | tmp421_get_temperature, | |
350 | tmp421_set_temperature, NULL, NULL, NULL); | |
351 | object_property_add(obj, "temperature1", "int", | |
352 | tmp421_get_temperature, | |
353 | tmp421_set_temperature, NULL, NULL, NULL); | |
354 | object_property_add(obj, "temperature2", "int", | |
355 | tmp421_get_temperature, | |
356 | tmp421_set_temperature, NULL, NULL, NULL); | |
357 | object_property_add(obj, "temperature3", "int", | |
358 | tmp421_get_temperature, | |
359 | tmp421_set_temperature, NULL, NULL, NULL); | |
360 | } | |
361 | ||
362 | static void tmp421_class_init(ObjectClass *klass, void *data) | |
363 | { | |
364 | DeviceClass *dc = DEVICE_CLASS(klass); | |
365 | I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); | |
366 | TMP421Class *sc = TMP421_CLASS(klass); | |
367 | ||
c8c9e103 | 368 | dc->realize = tmp421_realize; |
fe3874b6 CLG |
369 | k->event = tmp421_event; |
370 | k->recv = tmp421_rx; | |
371 | k->send = tmp421_tx; | |
372 | dc->vmsd = &vmstate_tmp421; | |
373 | sc->dev = (DeviceInfo *) data; | |
374 | } | |
375 | ||
376 | static const TypeInfo tmp421_info = { | |
377 | .name = TYPE_TMP421, | |
378 | .parent = TYPE_I2C_SLAVE, | |
379 | .instance_size = sizeof(TMP421State), | |
380 | .class_size = sizeof(TMP421Class), | |
381 | .instance_init = tmp421_initfn, | |
382 | .abstract = true, | |
383 | }; | |
384 | ||
385 | static void tmp421_register_types(void) | |
386 | { | |
387 | int i; | |
388 | ||
389 | type_register_static(&tmp421_info); | |
390 | for (i = 0; i < ARRAY_SIZE(devices); ++i) { | |
391 | TypeInfo ti = { | |
392 | .name = devices[i].name, | |
393 | .parent = TYPE_TMP421, | |
394 | .class_init = tmp421_class_init, | |
395 | .class_data = (void *) &devices[i], | |
396 | }; | |
397 | type_register(&ti); | |
398 | } | |
399 | } | |
400 | ||
401 | type_init(tmp421_register_types) |