s390/sclpconsole: prevent char layer callback during initialization
[qemu.git] / hw / tmp105.c
blob3ad2d2f04c928f276e89efc43edb44ac89db18f0
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"
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;
61 visit_type_int(v, &value, name, errp);
64 /* Units are 0.001 centigrades relative to 0 C. */
65 static void tmp105_set_temperature(Object *obj, Visitor *v, void *opaque,
66 const char *name, Error **errp)
68 TMP105State *s = TMP105(obj);
69 int64_t temp;
71 visit_type_int(v, &temp, name, errp);
72 if (error_is_set(errp)) {
73 return;
75 if (temp >= 128000 || temp < -128000) {
76 error_setg(errp, "value %" PRId64 ".%03" PRIu64 " °C is out of range",
77 temp / 1000, temp % 1000);
78 return;
81 s->temperature = ((int16_t) (temp * 0x800 / 128000)) << 4;
83 tmp105_alarm_update(s);
86 static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
88 static void tmp105_read(TMP105State *s)
90 s->len = 0;
92 if ((s->config >> 1) & 1) { /* TM */
93 s->alarm = 0;
94 tmp105_interrupt_update(s);
97 switch (s->pointer & 3) {
98 case TMP105_REG_TEMPERATURE:
99 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8);
100 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) &
101 (0xf0 << ((~s->config >> 5) & 3)); /* R */
102 break;
104 case TMP105_REG_CONFIG:
105 s->buf[s->len ++] = s->config;
106 break;
108 case TMP105_REG_T_LOW:
109 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
110 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
111 break;
113 case TMP105_REG_T_HIGH:
114 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8;
115 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0;
116 break;
120 static void tmp105_write(TMP105State *s)
122 switch (s->pointer & 3) {
123 case TMP105_REG_TEMPERATURE:
124 break;
126 case TMP105_REG_CONFIG:
127 if (s->buf[0] & ~s->config & (1 << 0)) /* SD */
128 printf("%s: TMP105 shutdown\n", __FUNCTION__);
129 s->config = s->buf[0];
130 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
131 tmp105_alarm_update(s);
132 break;
134 case TMP105_REG_T_LOW:
135 case TMP105_REG_T_HIGH:
136 if (s->len >= 3)
137 s->limit[s->pointer & 1] = (int16_t)
138 ((((uint16_t) s->buf[0]) << 8) | s->buf[1]);
139 tmp105_alarm_update(s);
140 break;
144 static int tmp105_rx(I2CSlave *i2c)
146 TMP105State *s = TMP105(i2c);
148 if (s->len < 2) {
149 return s->buf[s->len ++];
150 } else {
151 return 0xff;
155 static int tmp105_tx(I2CSlave *i2c, uint8_t data)
157 TMP105State *s = TMP105(i2c);
159 if (s->len == 0) {
160 s->pointer = data;
161 s->len++;
162 } else {
163 if (s->len <= 2) {
164 s->buf[s->len - 1] = data;
166 s->len++;
167 tmp105_write(s);
170 return 0;
173 static void tmp105_event(I2CSlave *i2c, enum i2c_event event)
175 TMP105State *s = TMP105(i2c);
177 if (event == I2C_START_RECV) {
178 tmp105_read(s);
181 s->len = 0;
184 static int tmp105_post_load(void *opaque, int version_id)
186 TMP105State *s = opaque;
188 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
190 tmp105_interrupt_update(s);
191 return 0;
194 static const VMStateDescription vmstate_tmp105 = {
195 .name = "TMP105",
196 .version_id = 0,
197 .minimum_version_id = 0,
198 .minimum_version_id_old = 0,
199 .post_load = tmp105_post_load,
200 .fields = (VMStateField []) {
201 VMSTATE_UINT8(len, TMP105State),
202 VMSTATE_UINT8_ARRAY(buf, TMP105State, 2),
203 VMSTATE_UINT8(pointer, TMP105State),
204 VMSTATE_UINT8(config, TMP105State),
205 VMSTATE_INT16(temperature, TMP105State),
206 VMSTATE_INT16_ARRAY(limit, TMP105State, 2),
207 VMSTATE_UINT8(alarm, TMP105State),
208 VMSTATE_I2C_SLAVE(i2c, TMP105State),
209 VMSTATE_END_OF_LIST()
213 static void tmp105_reset(I2CSlave *i2c)
215 TMP105State *s = TMP105(i2c);
217 s->temperature = 0;
218 s->pointer = 0;
219 s->config = 0;
220 s->faults = tmp105_faultq[(s->config >> 3) & 3];
221 s->alarm = 0;
223 tmp105_interrupt_update(s);
226 static int tmp105_init(I2CSlave *i2c)
228 TMP105State *s = TMP105(i2c);
230 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
232 tmp105_reset(&s->i2c);
234 return 0;
237 static void tmp105_initfn(Object *obj)
239 object_property_add(obj, "temperature", "int",
240 tmp105_get_temperature,
241 tmp105_set_temperature, NULL, NULL, NULL);
244 static void tmp105_class_init(ObjectClass *klass, void *data)
246 DeviceClass *dc = DEVICE_CLASS(klass);
247 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
249 k->init = tmp105_init;
250 k->event = tmp105_event;
251 k->recv = tmp105_rx;
252 k->send = tmp105_tx;
253 dc->vmsd = &vmstate_tmp105;
256 static const TypeInfo tmp105_info = {
257 .name = TYPE_TMP105,
258 .parent = TYPE_I2C_SLAVE,
259 .instance_size = sizeof(TMP105State),
260 .instance_init = tmp105_initfn,
261 .class_init = tmp105_class_init,
264 static void tmp105_register_types(void)
266 type_register_static(&tmp105_info);
269 type_init(tmp105_register_types)