]>
Commit | Line | Data |
---|---|---|
7e7c5e4c AZ |
1 | /* |
2 | * Texas Instruments TMP105 temperature sensor. | |
3 | * | |
4 | * Copyright (C) 2008 Nokia Corporation | |
5 | * Written by Andrzej Zaborowski <andrew@openedhand.com> | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
fad6cb1a | 17 | * You should have received a copy of the GNU General Public License along |
8167ee88 | 18 | * with this program; if not, see <http://www.gnu.org/licenses/>. |
7e7c5e4c AZ |
19 | */ |
20 | ||
0d1c9782 | 21 | #include "qemu/osdep.h" |
0d09e41a | 22 | #include "hw/i2c/i2c.h" |
64552b6b | 23 | #include "hw/irq.h" |
d6454270 | 24 | #include "migration/vmstate.h" |
47b43a1f | 25 | #include "tmp105.h" |
da34e65c | 26 | #include "qapi/error.h" |
eb60d1c5 | 27 | #include "qapi/visitor.h" |
0b8fa32f | 28 | #include "qemu/module.h" |
7e7c5e4c | 29 | |
bc24a225 | 30 | static void tmp105_interrupt_update(TMP105State *s) |
7e7c5e4c AZ |
31 | { |
32 | qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1)); /* POL */ | |
33 | } | |
34 | ||
bc24a225 | 35 | static void tmp105_alarm_update(TMP105State *s) |
7e7c5e4c AZ |
36 | { |
37 | if ((s->config >> 0) & 1) { /* SD */ | |
38 | if ((s->config >> 7) & 1) /* OS */ | |
39 | s->config &= ~(1 << 7); /* OS */ | |
40 | else | |
41 | return; | |
42 | } | |
43 | ||
44 | if ((s->config >> 1) & 1) { /* TM */ | |
45 | if (s->temperature >= s->limit[1]) | |
46 | s->alarm = 1; | |
47 | else if (s->temperature < s->limit[0]) | |
48 | s->alarm = 1; | |
49 | } else { | |
50 | if (s->temperature >= s->limit[1]) | |
51 | s->alarm = 1; | |
52 | else if (s->temperature < s->limit[0]) | |
53 | s->alarm = 0; | |
54 | } | |
55 | ||
56 | tmp105_interrupt_update(s); | |
57 | } | |
58 | ||
d7bce999 EB |
59 | static void tmp105_get_temperature(Object *obj, Visitor *v, const char *name, |
60 | void *opaque, Error **errp) | |
eb60d1c5 AF |
61 | { |
62 | TMP105State *s = TMP105(obj); | |
efdf6a56 | 63 | int64_t value = s->temperature * 1000 / 256; |
eb60d1c5 | 64 | |
51e72bc1 | 65 | visit_type_int(v, name, &value, errp); |
eb60d1c5 AF |
66 | } |
67 | ||
efdf6a56 PB |
68 | /* Units are 0.001 centigrades relative to 0 C. s->temperature is 8.8 |
69 | * fixed point, so units are 1/256 centigrades. A simple ratio will do. | |
70 | */ | |
d7bce999 EB |
71 | static void tmp105_set_temperature(Object *obj, Visitor *v, const char *name, |
72 | void *opaque, Error **errp) | |
7e7c5e4c | 73 | { |
eb60d1c5 AF |
74 | TMP105State *s = TMP105(obj); |
75 | int64_t temp; | |
7e7c5e4c | 76 | |
668f62ec | 77 | if (!visit_type_int(v, name, &temp, errp)) { |
eb60d1c5 AF |
78 | return; |
79 | } | |
7e7c5e4c | 80 | if (temp >= 128000 || temp < -128000) { |
3381466d | 81 | error_setg(errp, "value %" PRId64 ".%03" PRIu64 " C is out of range", |
eb60d1c5 AF |
82 | temp / 1000, temp % 1000); |
83 | return; | |
7e7c5e4c AZ |
84 | } |
85 | ||
efdf6a56 | 86 | s->temperature = (int16_t) (temp * 256 / 1000); |
7e7c5e4c AZ |
87 | |
88 | tmp105_alarm_update(s); | |
89 | } | |
90 | ||
91 | static const int tmp105_faultq[4] = { 1, 2, 4, 6 }; | |
92 | ||
bc24a225 | 93 | static void tmp105_read(TMP105State *s) |
7e7c5e4c AZ |
94 | { |
95 | s->len = 0; | |
96 | ||
97 | if ((s->config >> 1) & 1) { /* TM */ | |
98 | s->alarm = 0; | |
99 | tmp105_interrupt_update(s); | |
100 | } | |
101 | ||
102 | switch (s->pointer & 3) { | |
2915efbf | 103 | case TMP105_REG_TEMPERATURE: |
7e7c5e4c AZ |
104 | s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8); |
105 | s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) & | |
106 | (0xf0 << ((~s->config >> 5) & 3)); /* R */ | |
107 | break; | |
108 | ||
2915efbf | 109 | case TMP105_REG_CONFIG: |
7e7c5e4c AZ |
110 | s->buf[s->len ++] = s->config; |
111 | break; | |
112 | ||
2915efbf | 113 | case TMP105_REG_T_LOW: |
7e7c5e4c AZ |
114 | s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8; |
115 | s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0; | |
116 | break; | |
117 | ||
2915efbf | 118 | case TMP105_REG_T_HIGH: |
7e7c5e4c AZ |
119 | s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8; |
120 | s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0; | |
121 | break; | |
122 | } | |
123 | } | |
124 | ||
bc24a225 | 125 | static void tmp105_write(TMP105State *s) |
7e7c5e4c AZ |
126 | { |
127 | switch (s->pointer & 3) { | |
2915efbf | 128 | case TMP105_REG_TEMPERATURE: |
7e7c5e4c AZ |
129 | break; |
130 | ||
2915efbf | 131 | case TMP105_REG_CONFIG: |
7e7c5e4c | 132 | if (s->buf[0] & ~s->config & (1 << 0)) /* SD */ |
a89f364a | 133 | printf("%s: TMP105 shutdown\n", __func__); |
7e7c5e4c AZ |
134 | s->config = s->buf[0]; |
135 | s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */ | |
136 | tmp105_alarm_update(s); | |
137 | break; | |
138 | ||
2915efbf AH |
139 | case TMP105_REG_T_LOW: |
140 | case TMP105_REG_T_HIGH: | |
7e7c5e4c AZ |
141 | if (s->len >= 3) |
142 | s->limit[s->pointer & 1] = (int16_t) | |
143 | ((((uint16_t) s->buf[0]) << 8) | s->buf[1]); | |
144 | tmp105_alarm_update(s); | |
145 | break; | |
146 | } | |
147 | } | |
148 | ||
2ac4c5f4 | 149 | static uint8_t tmp105_rx(I2CSlave *i2c) |
7e7c5e4c | 150 | { |
2aad80ee | 151 | TMP105State *s = TMP105(i2c); |
7e7c5e4c | 152 | |
2aad80ee | 153 | if (s->len < 2) { |
7e7c5e4c | 154 | return s->buf[s->len ++]; |
2aad80ee | 155 | } else { |
7e7c5e4c | 156 | return 0xff; |
2aad80ee | 157 | } |
7e7c5e4c AZ |
158 | } |
159 | ||
9e07bdf8 | 160 | static int tmp105_tx(I2CSlave *i2c, uint8_t data) |
7e7c5e4c | 161 | { |
2aad80ee | 162 | TMP105State *s = TMP105(i2c); |
7e7c5e4c | 163 | |
cb5ef3fa | 164 | if (s->len == 0) { |
7e7c5e4c | 165 | s->pointer = data; |
cb5ef3fa AF |
166 | s->len++; |
167 | } else { | |
168 | if (s->len <= 2) { | |
7e7c5e4c | 169 | s->buf[s->len - 1] = data; |
cb5ef3fa AF |
170 | } |
171 | s->len++; | |
7e7c5e4c AZ |
172 | tmp105_write(s); |
173 | } | |
174 | ||
175 | return 0; | |
176 | } | |
177 | ||
d307c28c | 178 | static int tmp105_event(I2CSlave *i2c, enum i2c_event event) |
7e7c5e4c | 179 | { |
2aad80ee | 180 | TMP105State *s = TMP105(i2c); |
7e7c5e4c | 181 | |
2aad80ee | 182 | if (event == I2C_START_RECV) { |
7e7c5e4c | 183 | tmp105_read(s); |
2aad80ee | 184 | } |
7e7c5e4c AZ |
185 | |
186 | s->len = 0; | |
d307c28c | 187 | return 0; |
7e7c5e4c AZ |
188 | } |
189 | ||
371a4468 | 190 | static int tmp105_post_load(void *opaque, int version_id) |
7e7c5e4c | 191 | { |
371a4468 | 192 | TMP105State *s = opaque; |
7e7c5e4c | 193 | |
e5d3b98d AZ |
194 | s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */ |
195 | ||
7e7c5e4c | 196 | tmp105_interrupt_update(s); |
7e7c5e4c AZ |
197 | return 0; |
198 | } | |
199 | ||
371a4468 JQ |
200 | static const VMStateDescription vmstate_tmp105 = { |
201 | .name = "TMP105", | |
202 | .version_id = 0, | |
203 | .minimum_version_id = 0, | |
371a4468 | 204 | .post_load = tmp105_post_load, |
8f1e884b | 205 | .fields = (VMStateField[]) { |
371a4468 JQ |
206 | VMSTATE_UINT8(len, TMP105State), |
207 | VMSTATE_UINT8_ARRAY(buf, TMP105State, 2), | |
208 | VMSTATE_UINT8(pointer, TMP105State), | |
209 | VMSTATE_UINT8(config, TMP105State), | |
210 | VMSTATE_INT16(temperature, TMP105State), | |
211 | VMSTATE_INT16_ARRAY(limit, TMP105State, 2), | |
212 | VMSTATE_UINT8(alarm, TMP105State), | |
213 | VMSTATE_I2C_SLAVE(i2c, TMP105State), | |
214 | VMSTATE_END_OF_LIST() | |
215 | } | |
216 | }; | |
217 | ||
9e07bdf8 | 218 | static void tmp105_reset(I2CSlave *i2c) |
7e7c5e4c | 219 | { |
2aad80ee | 220 | TMP105State *s = TMP105(i2c); |
7e7c5e4c AZ |
221 | |
222 | s->temperature = 0; | |
223 | s->pointer = 0; | |
224 | s->config = 0; | |
225 | s->faults = tmp105_faultq[(s->config >> 3) & 3]; | |
226 | s->alarm = 0; | |
227 | ||
e1919889 PM |
228 | s->limit[0] = 0x4b00; /* T_LOW, 75 degrees C */ |
229 | s->limit[1] = 0x5000; /* T_HIGH, 80 degrees C */ | |
230 | ||
7e7c5e4c AZ |
231 | tmp105_interrupt_update(s); |
232 | } | |
233 | ||
c8c9e103 | 234 | static void tmp105_realize(DeviceState *dev, Error **errp) |
7e7c5e4c | 235 | { |
c8c9e103 | 236 | I2CSlave *i2c = I2C_SLAVE(dev); |
2aad80ee | 237 | TMP105State *s = TMP105(i2c); |
7e7c5e4c | 238 | |
697454eb | 239 | qdev_init_gpio_out(&i2c->qdev, &s->pin, 1); |
7e7c5e4c AZ |
240 | |
241 | tmp105_reset(&s->i2c); | |
697454eb PB |
242 | } |
243 | ||
eb60d1c5 AF |
244 | static void tmp105_initfn(Object *obj) |
245 | { | |
246 | object_property_add(obj, "temperature", "int", | |
247 | tmp105_get_temperature, | |
d2623129 | 248 | tmp105_set_temperature, NULL, NULL); |
eb60d1c5 AF |
249 | } |
250 | ||
b5ea9327 AL |
251 | static void tmp105_class_init(ObjectClass *klass, void *data) |
252 | { | |
39bffca2 | 253 | DeviceClass *dc = DEVICE_CLASS(klass); |
b5ea9327 AL |
254 | I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); |
255 | ||
c8c9e103 | 256 | dc->realize = tmp105_realize; |
b5ea9327 AL |
257 | k->event = tmp105_event; |
258 | k->recv = tmp105_rx; | |
259 | k->send = tmp105_tx; | |
39bffca2 | 260 | dc->vmsd = &vmstate_tmp105; |
b5ea9327 AL |
261 | } |
262 | ||
8c43a6f0 | 263 | static const TypeInfo tmp105_info = { |
2aad80ee | 264 | .name = TYPE_TMP105, |
39bffca2 AL |
265 | .parent = TYPE_I2C_SLAVE, |
266 | .instance_size = sizeof(TMP105State), | |
eb60d1c5 | 267 | .instance_init = tmp105_initfn, |
39bffca2 | 268 | .class_init = tmp105_class_init, |
697454eb | 269 | }; |
7e7c5e4c | 270 | |
83f7d43a | 271 | static void tmp105_register_types(void) |
697454eb | 272 | { |
39bffca2 | 273 | type_register_static(&tmp105_info); |
7e7c5e4c | 274 | } |
697454eb | 275 | |
83f7d43a | 276 | type_init(tmp105_register_types) |