MAINTAINERS/s390x: add terminal3270.c
[qemu.git] / hw / misc / tmp105.c
blob04e83787d40bf96092461928ed4ff08fe104e3bd
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/hw.h"
23 #include "hw/i2c/i2c.h"
24 #include "tmp105.h"
25 #include "qapi/error.h"
26 #include "qapi/visitor.h"
28 static void tmp105_interrupt_update(TMP105State *s)
30 qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1)); /* POL */
33 static void tmp105_alarm_update(TMP105State *s)
35 if ((s->config >> 0) & 1) { /* SD */
36 if ((s->config >> 7) & 1) /* OS */
37 s->config &= ~(1 << 7); /* OS */
38 else
39 return;
42 if ((s->config >> 1) & 1) { /* TM */
43 if (s->temperature >= s->limit[1])
44 s->alarm = 1;
45 else if (s->temperature < s->limit[0])
46 s->alarm = 1;
47 } else {
48 if (s->temperature >= s->limit[1])
49 s->alarm = 1;
50 else if (s->temperature < s->limit[0])
51 s->alarm = 0;
54 tmp105_interrupt_update(s);
57 static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name,
58 void *opaque, Error **errp)
60 TMP105State *s = TMP105(obj);
61 int64_t value = s->temperature * 1000 / 256;
63 visit_type_int(v, name, &value, errp);
66 /* Units are 0.001 centigrades relative to 0 C. s->temperature is 8.8
67 * fixed point, so units are 1/256 centigrades. A simple ratio will do.
69 static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name,
70 void *opaque, Error **errp)
72 TMP105State *s = TMP105(obj);
73 Error *local_err = NULL;
74 int64_t temp;
76 visit_type_int(v, name, &temp, &local_err);
77 if (local_err) {
78 error_propagate(errp, local_err);
79 return;
81 if (temp >= 128000 || temp < -128000) {
82 error_setg(errp, "value %" PRId64 ".%03" PRIu64 " °C is out of range",
83 temp / 1000, temp % 1000);
84 return;
87 s->temperature = (int16_t) (temp * 256 / 1000);
89 tmp105_alarm_update(s);
92 static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
94 static void tmp105_read(TMP105State *s)
96 s->len = 0;
98 if ((s->config >> 1) & 1) { /* TM */
99 s->alarm = 0;
100 tmp105_interrupt_update(s);
103 switch (s->pointer & 3) {
104 case TMP105_REG_TEMPERATURE:
105 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8);
106 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) &
107 (0xf0 << ((~s->config >> 5) & 3)); /* R */
108 break;
110 case TMP105_REG_CONFIG:
111 s->buf[s->len ++] = s->config;
112 break;
114 case TMP105_REG_T_LOW:
115 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
116 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
117 break;
119 case TMP105_REG_T_HIGH:
120 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8;
121 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0;
122 break;
126 static void tmp105_write(TMP105State *s)
128 switch (s->pointer & 3) {
129 case TMP105_REG_TEMPERATURE:
130 break;
132 case TMP105_REG_CONFIG:
133 if (s->buf[0] & ~s->config & (1 << 0)) /* SD */
134 printf("%s: TMP105 shutdown\n", __FUNCTION__);
135 s->config = s->buf[0];
136 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
137 tmp105_alarm_update(s);
138 break;
140 case TMP105_REG_T_LOW:
141 case TMP105_REG_T_HIGH:
142 if (s->len >= 3)
143 s->limit[s->pointer & 1] = (int16_t)
144 ((((uint16_t) s->buf[0]) << 8) | s->buf[1]);
145 tmp105_alarm_update(s);
146 break;
150 static int tmp105_rx(I2CSlave *i2c)
152 TMP105State *s = TMP105(i2c);
154 if (s->len < 2) {
155 return s->buf[s->len ++];
156 } else {
157 return 0xff;
161 static int tmp105_tx(I2CSlave *i2c, uint8_t data)
163 TMP105State *s = TMP105(i2c);
165 if (s->len == 0) {
166 s->pointer = data;
167 s->len++;
168 } else {
169 if (s->len <= 2) {
170 s->buf[s->len - 1] = data;
172 s->len++;
173 tmp105_write(s);
176 return 0;
179 static int tmp105_event(I2CSlave *i2c, enum i2c_event event)
181 TMP105State *s = TMP105(i2c);
183 if (event == I2C_START_RECV) {
184 tmp105_read(s);
187 s->len = 0;
188 return 0;
191 static int tmp105_post_load(void *opaque, int version_id)
193 TMP105State *s = opaque;
195 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
197 tmp105_interrupt_update(s);
198 return 0;
201 static const VMStateDescription vmstate_tmp105 = {
202 .name = "TMP105",
203 .version_id = 0,
204 .minimum_version_id = 0,
205 .post_load = tmp105_post_load,
206 .fields = (VMStateField[]) {
207 VMSTATE_UINT8(len, TMP105State),
208 VMSTATE_UINT8_ARRAY(buf, TMP105State, 2),
209 VMSTATE_UINT8(pointer, TMP105State),
210 VMSTATE_UINT8(config, TMP105State),
211 VMSTATE_INT16(temperature, TMP105State),
212 VMSTATE_INT16_ARRAY(limit, TMP105State, 2),
213 VMSTATE_UINT8(alarm, TMP105State),
214 VMSTATE_I2C_SLAVE(i2c, TMP105State),
215 VMSTATE_END_OF_LIST()
219 static void tmp105_reset(I2CSlave *i2c)
221 TMP105State *s = TMP105(i2c);
223 s->temperature = 0;
224 s->pointer = 0;
225 s->config = 0;
226 s->faults = tmp105_faultq[(s->config >> 3) & 3];
227 s->alarm = 0;
229 tmp105_interrupt_update(s);
232 static int tmp105_init(I2CSlave *i2c)
234 TMP105State *s = TMP105(i2c);
236 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
238 tmp105_reset(&s->i2c);
240 return 0;
243 static void tmp105_initfn(Object *obj)
245 object_property_add(obj, "temperature", "int",
246 tmp105_get_temperature,
247 tmp105_set_temperature, NULL, NULL, NULL);
250 static void tmp105_class_init(ObjectClass *klass, void *data)
252 DeviceClass *dc = DEVICE_CLASS(klass);
253 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
255 k->init = tmp105_init;
256 k->event = tmp105_event;
257 k->recv = tmp105_rx;
258 k->send = tmp105_tx;
259 dc->vmsd = &vmstate_tmp105;
262 static const TypeInfo tmp105_info = {
263 .name = TYPE_TMP105,
264 .parent = TYPE_I2C_SLAVE,
265 .instance_size = sizeof(TMP105State),
266 .instance_init = tmp105_initfn,
267 .class_init = tmp105_class_init,
270 static void tmp105_register_types(void)
272 type_register_static(&tmp105_info);
275 type_init(tmp105_register_types)