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