qemu-ga: qmp_guest_network_get_interfaces(): get rid of snprintf() + error_set()
[qemu/ar7.git] / hw / tmp105.c
blob9c67e644dd4bfd7241378ace043da7b91839134f
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 "hw.h"
22 #include "i2c.h"
23 #include "tmp105.h"
25 typedef struct {
26 I2CSlave i2c;
27 uint8_t len;
28 uint8_t buf[2];
29 qemu_irq pin;
31 uint8_t pointer;
32 uint8_t config;
33 int16_t temperature;
34 int16_t limit[2];
35 int faults;
36 uint8_t alarm;
37 } TMP105State;
39 static void tmp105_interrupt_update(TMP105State *s)
41 qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1)); /* POL */
44 static void tmp105_alarm_update(TMP105State *s)
46 if ((s->config >> 0) & 1) { /* SD */
47 if ((s->config >> 7) & 1) /* OS */
48 s->config &= ~(1 << 7); /* OS */
49 else
50 return;
53 if ((s->config >> 1) & 1) { /* TM */
54 if (s->temperature >= s->limit[1])
55 s->alarm = 1;
56 else if (s->temperature < s->limit[0])
57 s->alarm = 1;
58 } else {
59 if (s->temperature >= s->limit[1])
60 s->alarm = 1;
61 else if (s->temperature < s->limit[0])
62 s->alarm = 0;
65 tmp105_interrupt_update(s);
68 /* Units are 0.001 centigrades relative to 0 C. */
69 void tmp105_set(I2CSlave *i2c, int temp)
71 TMP105State *s = (TMP105State *) i2c;
73 if (temp >= 128000 || temp < -128000) {
74 fprintf(stderr, "%s: values is out of range (%i.%03i C)\n",
75 __FUNCTION__, temp / 1000, temp % 1000);
76 exit(-1);
79 s->temperature = ((int16_t) (temp * 0x800 / 128000)) << 4;
81 tmp105_alarm_update(s);
84 static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
86 static void tmp105_read(TMP105State *s)
88 s->len = 0;
90 if ((s->config >> 1) & 1) { /* TM */
91 s->alarm = 0;
92 tmp105_interrupt_update(s);
95 switch (s->pointer & 3) {
96 case TMP105_REG_TEMPERATURE:
97 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8);
98 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) &
99 (0xf0 << ((~s->config >> 5) & 3)); /* R */
100 break;
102 case TMP105_REG_CONFIG:
103 s->buf[s->len ++] = s->config;
104 break;
106 case TMP105_REG_T_LOW:
107 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
108 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
109 break;
111 case TMP105_REG_T_HIGH:
112 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8;
113 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0;
114 break;
118 static void tmp105_write(TMP105State *s)
120 switch (s->pointer & 3) {
121 case TMP105_REG_TEMPERATURE:
122 break;
124 case TMP105_REG_CONFIG:
125 if (s->buf[0] & ~s->config & (1 << 0)) /* SD */
126 printf("%s: TMP105 shutdown\n", __FUNCTION__);
127 s->config = s->buf[0];
128 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
129 tmp105_alarm_update(s);
130 break;
132 case TMP105_REG_T_LOW:
133 case TMP105_REG_T_HIGH:
134 if (s->len >= 3)
135 s->limit[s->pointer & 1] = (int16_t)
136 ((((uint16_t) s->buf[0]) << 8) | s->buf[1]);
137 tmp105_alarm_update(s);
138 break;
142 static int tmp105_rx(I2CSlave *i2c)
144 TMP105State *s = (TMP105State *) i2c;
146 if (s->len < 2)
147 return s->buf[s->len ++];
148 else
149 return 0xff;
152 static int tmp105_tx(I2CSlave *i2c, uint8_t data)
154 TMP105State *s = (TMP105State *) i2c;
156 if (!s->len ++)
157 s->pointer = data;
158 else {
159 if (s->len <= 2)
160 s->buf[s->len - 1] = data;
161 tmp105_write(s);
164 return 0;
167 static void tmp105_event(I2CSlave *i2c, enum i2c_event event)
169 TMP105State *s = (TMP105State *) i2c;
171 if (event == I2C_START_RECV)
172 tmp105_read(s);
174 s->len = 0;
177 static int tmp105_post_load(void *opaque, int version_id)
179 TMP105State *s = opaque;
181 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
183 tmp105_interrupt_update(s);
184 return 0;
187 static const VMStateDescription vmstate_tmp105 = {
188 .name = "TMP105",
189 .version_id = 0,
190 .minimum_version_id = 0,
191 .minimum_version_id_old = 0,
192 .post_load = tmp105_post_load,
193 .fields = (VMStateField []) {
194 VMSTATE_UINT8(len, TMP105State),
195 VMSTATE_UINT8_ARRAY(buf, TMP105State, 2),
196 VMSTATE_UINT8(pointer, TMP105State),
197 VMSTATE_UINT8(config, TMP105State),
198 VMSTATE_INT16(temperature, TMP105State),
199 VMSTATE_INT16_ARRAY(limit, TMP105State, 2),
200 VMSTATE_UINT8(alarm, TMP105State),
201 VMSTATE_I2C_SLAVE(i2c, TMP105State),
202 VMSTATE_END_OF_LIST()
206 static void tmp105_reset(I2CSlave *i2c)
208 TMP105State *s = (TMP105State *) i2c;
210 s->temperature = 0;
211 s->pointer = 0;
212 s->config = 0;
213 s->faults = tmp105_faultq[(s->config >> 3) & 3];
214 s->alarm = 0;
216 tmp105_interrupt_update(s);
219 static int tmp105_init(I2CSlave *i2c)
221 TMP105State *s = FROM_I2C_SLAVE(TMP105State, i2c);
223 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
225 tmp105_reset(&s->i2c);
227 return 0;
230 static void tmp105_class_init(ObjectClass *klass, void *data)
232 DeviceClass *dc = DEVICE_CLASS(klass);
233 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
235 k->init = tmp105_init;
236 k->event = tmp105_event;
237 k->recv = tmp105_rx;
238 k->send = tmp105_tx;
239 dc->vmsd = &vmstate_tmp105;
242 static TypeInfo tmp105_info = {
243 .name = "tmp105",
244 .parent = TYPE_I2C_SLAVE,
245 .instance_size = sizeof(TMP105State),
246 .class_init = tmp105_class_init,
249 static void tmp105_register_types(void)
251 type_register_static(&tmp105_info);
254 type_init(tmp105_register_types)