monitor: Split MonitorQAPIEventConf off MonitorQAPIEventState
[qemu/ar7.git] / hw / misc / tmp105.c
blobf3fe8b81fd77c23caec126a91f6ee30dac690fb4
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/hw.h"
22 #include "hw/i2c/i2c.h"
23 #include "tmp105.h"
24 #include "qapi/visitor.h"
26 static void tmp105_interrupt_update(TMP105State *s)
28 qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1)); /* POL */
31 static void tmp105_alarm_update(TMP105State *s)
33 if ((s->config >> 0) & 1) { /* SD */
34 if ((s->config >> 7) & 1) /* OS */
35 s->config &= ~(1 << 7); /* OS */
36 else
37 return;
40 if ((s->config >> 1) & 1) { /* TM */
41 if (s->temperature >= s->limit[1])
42 s->alarm = 1;
43 else if (s->temperature < s->limit[0])
44 s->alarm = 1;
45 } else {
46 if (s->temperature >= s->limit[1])
47 s->alarm = 1;
48 else if (s->temperature < s->limit[0])
49 s->alarm = 0;
52 tmp105_interrupt_update(s);
55 static void tmp105_get_temperature(Object *obj, Visitor *v, void *opaque,
56 const char *name, Error **errp)
58 TMP105State *s = TMP105(obj);
59 int64_t value = s->temperature * 1000 / 256;
61 visit_type_int(v, &value, name, errp);
64 /* Units are 0.001 centigrades relative to 0 C. s->temperature is 8.8
65 * fixed point, so units are 1/256 centigrades. A simple ratio will do.
67 static void tmp105_set_temperature(Object *obj, Visitor *v, void *opaque,
68 const char *name, Error **errp)
70 TMP105State *s = TMP105(obj);
71 Error *local_err = NULL;
72 int64_t temp;
74 visit_type_int(v, &temp, name, &local_err);
75 if (local_err) {
76 error_propagate(errp, local_err);
77 return;
79 if (temp >= 128000 || temp < -128000) {
80 error_setg(errp, "value %" PRId64 ".%03" PRIu64 " °C is out of range",
81 temp / 1000, temp % 1000);
82 return;
85 s->temperature = (int16_t) (temp * 256 / 1000);
87 tmp105_alarm_update(s);
90 static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
92 static void tmp105_read(TMP105State *s)
94 s->len = 0;
96 if ((s->config >> 1) & 1) { /* TM */
97 s->alarm = 0;
98 tmp105_interrupt_update(s);
101 switch (s->pointer & 3) {
102 case TMP105_REG_TEMPERATURE:
103 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8);
104 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) &
105 (0xf0 << ((~s->config >> 5) & 3)); /* R */
106 break;
108 case TMP105_REG_CONFIG:
109 s->buf[s->len ++] = s->config;
110 break;
112 case TMP105_REG_T_LOW:
113 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
114 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
115 break;
117 case TMP105_REG_T_HIGH:
118 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8;
119 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0;
120 break;
124 static void tmp105_write(TMP105State *s)
126 switch (s->pointer & 3) {
127 case TMP105_REG_TEMPERATURE:
128 break;
130 case TMP105_REG_CONFIG:
131 if (s->buf[0] & ~s->config & (1 << 0)) /* SD */
132 printf("%s: TMP105 shutdown\n", __FUNCTION__);
133 s->config = s->buf[0];
134 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
135 tmp105_alarm_update(s);
136 break;
138 case TMP105_REG_T_LOW:
139 case TMP105_REG_T_HIGH:
140 if (s->len >= 3)
141 s->limit[s->pointer & 1] = (int16_t)
142 ((((uint16_t) s->buf[0]) << 8) | s->buf[1]);
143 tmp105_alarm_update(s);
144 break;
148 static int tmp105_rx(I2CSlave *i2c)
150 TMP105State *s = TMP105(i2c);
152 if (s->len < 2) {
153 return s->buf[s->len ++];
154 } else {
155 return 0xff;
159 static int tmp105_tx(I2CSlave *i2c, uint8_t data)
161 TMP105State *s = TMP105(i2c);
163 if (s->len == 0) {
164 s->pointer = data;
165 s->len++;
166 } else {
167 if (s->len <= 2) {
168 s->buf[s->len - 1] = data;
170 s->len++;
171 tmp105_write(s);
174 return 0;
177 static void tmp105_event(I2CSlave *i2c, enum i2c_event event)
179 TMP105State *s = TMP105(i2c);
181 if (event == I2C_START_RECV) {
182 tmp105_read(s);
185 s->len = 0;
188 static int tmp105_post_load(void *opaque, int version_id)
190 TMP105State *s = opaque;
192 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
194 tmp105_interrupt_update(s);
195 return 0;
198 static const VMStateDescription vmstate_tmp105 = {
199 .name = "TMP105",
200 .version_id = 0,
201 .minimum_version_id = 0,
202 .post_load = tmp105_post_load,
203 .fields = (VMStateField[]) {
204 VMSTATE_UINT8(len, TMP105State),
205 VMSTATE_UINT8_ARRAY(buf, TMP105State, 2),
206 VMSTATE_UINT8(pointer, TMP105State),
207 VMSTATE_UINT8(config, TMP105State),
208 VMSTATE_INT16(temperature, TMP105State),
209 VMSTATE_INT16_ARRAY(limit, TMP105State, 2),
210 VMSTATE_UINT8(alarm, TMP105State),
211 VMSTATE_I2C_SLAVE(i2c, TMP105State),
212 VMSTATE_END_OF_LIST()
216 static void tmp105_reset(I2CSlave *i2c)
218 TMP105State *s = TMP105(i2c);
220 s->temperature = 0;
221 s->pointer = 0;
222 s->config = 0;
223 s->faults = tmp105_faultq[(s->config >> 3) & 3];
224 s->alarm = 0;
226 tmp105_interrupt_update(s);
229 static int tmp105_init(I2CSlave *i2c)
231 TMP105State *s = TMP105(i2c);
233 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
235 tmp105_reset(&s->i2c);
237 return 0;
240 static void tmp105_initfn(Object *obj)
242 object_property_add(obj, "temperature", "int",
243 tmp105_get_temperature,
244 tmp105_set_temperature, NULL, NULL, NULL);
247 static void tmp105_class_init(ObjectClass *klass, void *data)
249 DeviceClass *dc = DEVICE_CLASS(klass);
250 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
252 k->init = tmp105_init;
253 k->event = tmp105_event;
254 k->recv = tmp105_rx;
255 k->send = tmp105_tx;
256 dc->vmsd = &vmstate_tmp105;
259 static const TypeInfo tmp105_info = {
260 .name = TYPE_TMP105,
261 .parent = TYPE_I2C_SLAVE,
262 .instance_size = sizeof(TMP105State),
263 .instance_init = tmp105_initfn,
264 .class_init = tmp105_class_init,
267 static void tmp105_register_types(void)
269 type_register_static(&tmp105_info);
272 type_init(tmp105_register_types)