1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "hw/hw.h"
22#include "hw/i2c/i2c.h"
23#include "tmp105.h"
24#include "qapi/visitor.h"
25
26static void tmp105_interrupt_update(TMP105State *s)
27{
28 qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1));
29}
30
31static void tmp105_alarm_update(TMP105State *s)
32{
33 if ((s->config >> 0) & 1) {
34 if ((s->config >> 7) & 1)
35 s->config &= ~(1 << 7);
36 else
37 return;
38 }
39
40 if ((s->config >> 1) & 1) {
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;
50 }
51
52 tmp105_interrupt_update(s);
53}
54
55static void tmp105_get_temperature(Object *obj, Visitor *v, void *opaque,
56 const char *name, Error **errp)
57{
58 TMP105State *s = TMP105(obj);
59 int64_t value = s->temperature;
60
61 visit_type_int(v, &value, name, errp);
62}
63
64
65static void tmp105_set_temperature(Object *obj, Visitor *v, void *opaque,
66 const char *name, Error **errp)
67{
68 TMP105State *s = TMP105(obj);
69 int64_t temp;
70
71 visit_type_int(v, &temp, name, errp);
72 if (error_is_set(errp)) {
73 return;
74 }
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;
79 }
80
81 s->temperature = ((int16_t) (temp * 0x800 / 128000)) << 4;
82
83 tmp105_alarm_update(s);
84}
85
86static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
87
88static void tmp105_read(TMP105State *s)
89{
90 s->len = 0;
91
92 if ((s->config >> 1) & 1) {
93 s->alarm = 0;
94 tmp105_interrupt_update(s);
95 }
96
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));
102 break;
103
104 case TMP105_REG_CONFIG:
105 s->buf[s->len ++] = s->config;
106 break;
107
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;
112
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;
117 }
118}
119
120static void tmp105_write(TMP105State *s)
121{
122 switch (s->pointer & 3) {
123 case TMP105_REG_TEMPERATURE:
124 break;
125
126 case TMP105_REG_CONFIG:
127 if (s->buf[0] & ~s->config & (1 << 0))
128 printf("%s: TMP105 shutdown\n", __FUNCTION__);
129 s->config = s->buf[0];
130 s->faults = tmp105_faultq[(s->config >> 3) & 3];
131 tmp105_alarm_update(s);
132 break;
133
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;
141 }
142}
143
144static int tmp105_rx(I2CSlave *i2c)
145{
146 TMP105State *s = TMP105(i2c);
147
148 if (s->len < 2) {
149 return s->buf[s->len ++];
150 } else {
151 return 0xff;
152 }
153}
154
155static int tmp105_tx(I2CSlave *i2c, uint8_t data)
156{
157 TMP105State *s = TMP105(i2c);
158
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;
165 }
166 s->len++;
167 tmp105_write(s);
168 }
169
170 return 0;
171}
172
173static void tmp105_event(I2CSlave *i2c, enum i2c_event event)
174{
175 TMP105State *s = TMP105(i2c);
176
177 if (event == I2C_START_RECV) {
178 tmp105_read(s);
179 }
180
181 s->len = 0;
182}
183
184static int tmp105_post_load(void *opaque, int version_id)
185{
186 TMP105State *s = opaque;
187
188 s->faults = tmp105_faultq[(s->config >> 3) & 3];
189
190 tmp105_interrupt_update(s);
191 return 0;
192}
193
194static 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()
210 }
211};
212
213static void tmp105_reset(I2CSlave *i2c)
214{
215 TMP105State *s = TMP105(i2c);
216
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;
222
223 tmp105_interrupt_update(s);
224}
225
226static int tmp105_init(I2CSlave *i2c)
227{
228 TMP105State *s = TMP105(i2c);
229
230 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
231
232 tmp105_reset(&s->i2c);
233
234 return 0;
235}
236
237static void tmp105_initfn(Object *obj)
238{
239 object_property_add(obj, "temperature", "int",
240 tmp105_get_temperature,
241 tmp105_set_temperature, NULL, NULL, NULL);
242}
243
244static void tmp105_class_init(ObjectClass *klass, void *data)
245{
246 DeviceClass *dc = DEVICE_CLASS(klass);
247 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
248
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;
254}
255
256static 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,
262};
263
264static void tmp105_register_types(void)
265{
266 type_register_static(&tmp105_info);
267}
268
269type_init(tmp105_register_types)
270