2 * Texas Instruments TMP421 temperature sensor.
4 * Copyright (c) 2016 IBM Corporation.
6 * Largely inspired by :
8 * Texas Instruments TMP105 temperature sensor.
10 * Copyright (C) 2008 Nokia Corporation
11 * Written by Andrzej Zaborowski <andrew@openedhand.com>
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.
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.
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/>.
27 #include "qemu/osdep.h"
28 #include "hw/i2c/i2c.h"
29 #include "migration/vmstate.h"
30 #include "qapi/error.h"
31 #include "qapi/visitor.h"
32 #include "qemu/module.h"
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
40 typedef struct DeviceInfo
{
45 static const DeviceInfo devices
[] = {
46 { TMP421_DEVICE_ID
, "tmp421" },
47 { TMP422_DEVICE_ID
, "tmp422" },
48 { TMP423_DEVICE_ID
, "tmp423" },
51 typedef struct TMP421State
{
56 int16_t temperature
[4];
68 typedef struct TMP421Class
{
69 I2CSlaveClass parent_class
;
73 #define TYPE_TMP421 "tmp421-generic"
74 #define TMP421(obj) OBJECT_CHECK(TMP421State, (obj), TYPE_TMP421)
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)
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)
94 #define TMP421_CONVERSION_RATE_REG 0x0B
95 #define TMP421_ONE_SHOT 0x0F
97 #define TMP421_RESET 0xFC
98 #define TMP421_MANUFACTURER_ID_REG 0xFE
99 #define TMP421_DEVICE_ID_REG 0xFF
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
110 static const int32_t mins
[2] = { -40000, -55000 };
111 static const int32_t maxs
[2] = { 127000, 150000 };
113 static void tmp421_get_temperature(Object
*obj
, Visitor
*v
, const char *name
,
114 void *opaque
, Error
**errp
)
116 TMP421State
*s
= TMP421(obj
);
117 bool ext_range
= (s
->config
[0] & TMP421_CONFIG_RANGE
);
118 int offset
= ext_range
* 64 * 256;
122 if (sscanf(name
, "temperature%d", &tempid
) != 1) {
123 error_setg(errp
, "error reading %s: %m", name
);
127 if (tempid
>= 4 || tempid
< 0) {
128 error_setg(errp
, "error reading %s", name
);
132 value
= ((s
->temperature
[tempid
] - offset
) * 1000 + 128) / 256;
134 visit_type_int(v
, name
, &value
, errp
);
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.
140 static void tmp421_set_temperature(Object
*obj
, Visitor
*v
, const char *name
,
141 void *opaque
, Error
**errp
)
143 TMP421State
*s
= TMP421(obj
);
144 Error
*local_err
= NULL
;
146 bool ext_range
= (s
->config
[0] & TMP421_CONFIG_RANGE
);
147 int offset
= ext_range
* 64 * 256;
150 visit_type_int(v
, name
, &temp
, &local_err
);
152 error_propagate(errp
, local_err
);
156 if (temp
>= maxs
[ext_range
] || temp
< mins
[ext_range
]) {
157 error_setg(errp
, "value %" PRId64
".%03" PRIu64
" C is out of range",
158 temp
/ 1000, temp
% 1000);
162 if (sscanf(name
, "temperature%d", &tempid
) != 1) {
163 error_setg(errp
, "error reading %s: %m", name
);
167 if (tempid
>= 4 || tempid
< 0) {
168 error_setg(errp
, "error reading %s", name
);
172 s
->temperature
[tempid
] = (int16_t) ((temp
* 256 - 128) / 1000) + offset
;
175 static void tmp421_read(TMP421State
*s
)
177 TMP421Class
*sc
= TMP421_GET_CLASS(s
);
181 switch (s
->pointer
) {
182 case TMP421_MANUFACTURER_ID_REG
:
183 s
->buf
[s
->len
++] = TMP421_MANUFACTURER_ID
;
185 case TMP421_DEVICE_ID_REG
:
186 s
->buf
[s
->len
++] = sc
->dev
->model
;
188 case TMP421_CONFIG_REG_1
:
189 s
->buf
[s
->len
++] = s
->config
[0];
191 case TMP421_CONFIG_REG_2
:
192 s
->buf
[s
->len
++] = s
->config
[1];
194 case TMP421_CONVERSION_RATE_REG
:
195 s
->buf
[s
->len
++] = s
->rate
;
197 case TMP421_STATUS_REG
:
198 s
->buf
[s
->len
++] = s
->status
;
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;
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;
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;
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;
218 case TMP421_TEMP_LSB0
:
219 s
->buf
[s
->len
++] = (((uint16_t) s
->temperature
[0]) >> 0) & 0xf0;
221 case TMP421_TEMP_LSB1
:
222 s
->buf
[s
->len
++] = (((uint16_t) s
->temperature
[1]) >> 0) & 0xf0;
224 case TMP421_TEMP_LSB2
:
225 s
->buf
[s
->len
++] = (((uint16_t) s
->temperature
[2]) >> 0) & 0xf0;
227 case TMP421_TEMP_LSB3
:
228 s
->buf
[s
->len
++] = (((uint16_t) s
->temperature
[3]) >> 0) & 0xf0;
233 static void tmp421_reset(I2CSlave
*i2c
);
235 static void tmp421_write(TMP421State
*s
)
237 switch (s
->pointer
) {
238 case TMP421_CONVERSION_RATE_REG
:
241 case TMP421_CONFIG_REG_1
:
242 s
->config
[0] = s
->buf
[0];
244 case TMP421_CONFIG_REG_2
:
245 s
->config
[1] = s
->buf
[0];
248 tmp421_reset(I2C_SLAVE(s
));
253 static uint8_t tmp421_rx(I2CSlave
*i2c
)
255 TMP421State
*s
= TMP421(i2c
);
258 return s
->buf
[s
->len
++];
264 static int tmp421_tx(I2CSlave
*i2c
, uint8_t data
)
266 TMP421State
*s
= TMP421(i2c
);
269 /* first byte is the register pointer for a read or write
273 } else if (s
->len
== 1) {
274 /* second byte is the data to write. The device only supports
283 static int tmp421_event(I2CSlave
*i2c
, enum i2c_event event
)
285 TMP421State
*s
= TMP421(i2c
);
287 if (event
== I2C_START_RECV
) {
295 static const VMStateDescription vmstate_tmp421
= {
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()
312 static void tmp421_reset(I2CSlave
*i2c
)
314 TMP421State
*s
= TMP421(i2c
);
315 TMP421Class
*sc
= TMP421_GET_CLASS(s
);
317 memset(s
->temperature
, 0, sizeof(s
->temperature
));
320 s
->config
[0] = 0; /* TMP421_CONFIG_RANGE */
322 /* resistance correction and channel enablement */
323 switch (sc
->dev
->model
) {
324 case TMP421_DEVICE_ID
:
327 case TMP422_DEVICE_ID
:
330 case TMP423_DEVICE_ID
:
335 s
->rate
= 0x7; /* 8Hz */
339 static void tmp421_realize(DeviceState
*dev
, Error
**errp
)
341 TMP421State
*s
= TMP421(dev
);
343 tmp421_reset(&s
->i2c
);
346 static void tmp421_initfn(Object
*obj
)
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
);
362 static void tmp421_class_init(ObjectClass
*klass
, void *data
)
364 DeviceClass
*dc
= DEVICE_CLASS(klass
);
365 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
366 TMP421Class
*sc
= TMP421_CLASS(klass
);
368 dc
->realize
= tmp421_realize
;
369 k
->event
= tmp421_event
;
372 dc
->vmsd
= &vmstate_tmp421
;
373 sc
->dev
= (DeviceInfo
*) data
;
376 static const TypeInfo tmp421_info
= {
378 .parent
= TYPE_I2C_SLAVE
,
379 .instance_size
= sizeof(TMP421State
),
380 .class_size
= sizeof(TMP421Class
),
381 .instance_init
= tmp421_initfn
,
385 static void tmp421_register_types(void)
389 type_register_static(&tmp421_info
);
390 for (i
= 0; i
< ARRAY_SIZE(devices
); ++i
) {
392 .name
= devices
[i
].name
,
393 .parent
= TYPE_TMP421
,
394 .class_init
= tmp421_class_init
,
395 .class_data
= (void *) &devices
[i
],
401 type_init(tmp421_register_types
)