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