qapi/error: Check format string argument in error_*prepend()
[qemu/armbru.git] / hw / misc / tmp105.c
blobb47120492abb5b8b51eab201d37702c38144972a
1 /*
2 * Texas Instruments TMP105 temperature sensor.
4 * Copyright (C) 2008 Nokia Corporation
5 * Written by Andrzej Zaborowski <andrew@openedhand.com>
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.
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.
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/>.
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"
30 static void tmp105_interrupt_update(TMP105State *s)
32 qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1)); /* POL */
35 static void tmp105_alarm_update(TMP105State *s)
37 if ((s->config >> 0) & 1) { /* SD */
38 if ((s->config >> 7) & 1) /* OS */
39 s->config &= ~(1 << 7); /* OS */
40 else
41 return;
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;
56 tmp105_interrupt_update(s);
59 static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name,
60 void *opaque, Error **errp)
62 TMP105State *s = TMP105(obj);
63 int64_t value = s->temperature * 1000 / 256;
65 visit_type_int(v, name, &value, errp);
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.
71 static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name,
72 void *opaque, Error **errp)
74 TMP105State *s = TMP105(obj);
75 int64_t temp;
77 if (!visit_type_int(v, name, &temp, errp)) {
78 return;
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;
86 s->temperature = (int16_t) (temp * 256 / 1000);
88 tmp105_alarm_update(s);
91 static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
93 static void tmp105_read(TMP105State *s)
95 s->len = 0;
97 if ((s->config >> 1) & 1) { /* TM */
98 s->alarm = 0;
99 tmp105_interrupt_update(s);
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;
109 case TMP105_REG_CONFIG:
110 s->buf[s->len ++] = s->config;
111 break;
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;
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;
125 static void tmp105_write(TMP105State *s)
127 switch (s->pointer & 3) {
128 case TMP105_REG_TEMPERATURE:
129 break;
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;
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;
149 static uint8_t tmp105_rx(I2CSlave *i2c)
151 TMP105State *s = TMP105(i2c);
153 if (s->len < 2) {
154 return s->buf[s->len ++];
155 } else {
156 return 0xff;
160 static int tmp105_tx(I2CSlave *i2c, uint8_t data)
162 TMP105State *s = TMP105(i2c);
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;
171 s->len++;
172 tmp105_write(s);
175 return 0;
178 static int tmp105_event(I2CSlave *i2c, enum i2c_event event)
180 TMP105State *s = TMP105(i2c);
182 if (event == I2C_START_RECV) {
183 tmp105_read(s);
186 s->len = 0;
187 return 0;
190 static int tmp105_post_load(void *opaque, int version_id)
192 TMP105State *s = opaque;
194 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
196 tmp105_interrupt_update(s);
197 return 0;
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()
218 static void tmp105_reset(I2CSlave *i2c)
220 TMP105State *s = TMP105(i2c);
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;
228 tmp105_interrupt_update(s);
231 static void tmp105_realize(DeviceState *dev, Error **errp)
233 I2CSlave *i2c = I2C_SLAVE(dev);
234 TMP105State *s = TMP105(i2c);
236 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
238 tmp105_reset(&s->i2c);
241 static void tmp105_initfn(Object *obj)
243 object_property_add(obj, "temperature", "int",
244 tmp105_get_temperature,
245 tmp105_set_temperature, NULL, NULL);
248 static void tmp105_class_init(ObjectClass *klass, void *data)
250 DeviceClass *dc = DEVICE_CLASS(klass);
251 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
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;
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,
268 static void tmp105_register_types(void)
270 type_register_static(&tmp105_info);
273 type_init(tmp105_register_types)