target-sparc: make do_unaligned_access static
[qemu/opensuse.git] / hw / tmp105.c
blob8e8dbd94eba1876f211bdd891f9375c864c814a9
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"
24 typedef struct {
25 I2CSlave i2c;
26 uint8_t len;
27 uint8_t buf[2];
28 qemu_irq pin;
30 uint8_t pointer;
31 uint8_t config;
32 int16_t temperature;
33 int16_t limit[2];
34 int faults;
35 uint8_t alarm;
36 } TMP105State;
38 static void tmp105_interrupt_update(TMP105State *s)
40 qemu_set_irq(s->pin, s->alarm ^ ((~s->config >> 2) & 1)); /* POL */
43 static void tmp105_alarm_update(TMP105State *s)
45 if ((s->config >> 0) & 1) { /* SD */
46 if ((s->config >> 7) & 1) /* OS */
47 s->config &= ~(1 << 7); /* OS */
48 else
49 return;
52 if ((s->config >> 1) & 1) { /* TM */
53 if (s->temperature >= s->limit[1])
54 s->alarm = 1;
55 else if (s->temperature < s->limit[0])
56 s->alarm = 1;
57 } else {
58 if (s->temperature >= s->limit[1])
59 s->alarm = 1;
60 else if (s->temperature < s->limit[0])
61 s->alarm = 0;
64 tmp105_interrupt_update(s);
67 /* Units are 0.001 centigrades relative to 0 C. */
68 void tmp105_set(I2CSlave *i2c, int temp)
70 TMP105State *s = (TMP105State *) i2c;
72 if (temp >= 128000 || temp < -128000) {
73 fprintf(stderr, "%s: values is out of range (%i.%03i C)\n",
74 __FUNCTION__, temp / 1000, temp % 1000);
75 exit(-1);
78 s->temperature = ((int16_t) (temp * 0x800 / 128000)) << 4;
80 tmp105_alarm_update(s);
83 static const int tmp105_faultq[4] = { 1, 2, 4, 6 };
85 static void tmp105_read(TMP105State *s)
87 s->len = 0;
89 if ((s->config >> 1) & 1) { /* TM */
90 s->alarm = 0;
91 tmp105_interrupt_update(s);
94 switch (s->pointer & 3) {
95 case 0: /* Temperature */
96 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 8);
97 s->buf[s->len ++] = (((uint16_t) s->temperature) >> 0) &
98 (0xf0 << ((~s->config >> 5) & 3)); /* R */
99 break;
101 case 1: /* Configuration */
102 s->buf[s->len ++] = s->config;
103 break;
105 case 2: /* T_LOW */
106 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 8;
107 s->buf[s->len ++] = ((uint16_t) s->limit[0]) >> 0;
108 break;
110 case 3: /* T_HIGH */
111 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 8;
112 s->buf[s->len ++] = ((uint16_t) s->limit[1]) >> 0;
113 break;
117 static void tmp105_write(TMP105State *s)
119 switch (s->pointer & 3) {
120 case 0: /* Temperature */
121 break;
123 case 1: /* Configuration */
124 if (s->buf[0] & ~s->config & (1 << 0)) /* SD */
125 printf("%s: TMP105 shutdown\n", __FUNCTION__);
126 s->config = s->buf[0];
127 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
128 tmp105_alarm_update(s);
129 break;
131 case 2: /* T_LOW */
132 case 3: /* T_HIGH */
133 if (s->len >= 3)
134 s->limit[s->pointer & 1] = (int16_t)
135 ((((uint16_t) s->buf[0]) << 8) | s->buf[1]);
136 tmp105_alarm_update(s);
137 break;
141 static int tmp105_rx(I2CSlave *i2c)
143 TMP105State *s = (TMP105State *) i2c;
145 if (s->len < 2)
146 return s->buf[s->len ++];
147 else
148 return 0xff;
151 static int tmp105_tx(I2CSlave *i2c, uint8_t data)
153 TMP105State *s = (TMP105State *) i2c;
155 if (!s->len ++)
156 s->pointer = data;
157 else {
158 if (s->len <= 2)
159 s->buf[s->len - 1] = data;
160 tmp105_write(s);
163 return 0;
166 static void tmp105_event(I2CSlave *i2c, enum i2c_event event)
168 TMP105State *s = (TMP105State *) i2c;
170 if (event == I2C_START_RECV)
171 tmp105_read(s);
173 s->len = 0;
176 static int tmp105_post_load(void *opaque, int version_id)
178 TMP105State *s = opaque;
180 s->faults = tmp105_faultq[(s->config >> 3) & 3]; /* F */
182 tmp105_interrupt_update(s);
183 return 0;
186 static const VMStateDescription vmstate_tmp105 = {
187 .name = "TMP105",
188 .version_id = 0,
189 .minimum_version_id = 0,
190 .minimum_version_id_old = 0,
191 .post_load = tmp105_post_load,
192 .fields = (VMStateField []) {
193 VMSTATE_UINT8(len, TMP105State),
194 VMSTATE_UINT8_ARRAY(buf, TMP105State, 2),
195 VMSTATE_UINT8(pointer, TMP105State),
196 VMSTATE_UINT8(config, TMP105State),
197 VMSTATE_INT16(temperature, TMP105State),
198 VMSTATE_INT16_ARRAY(limit, TMP105State, 2),
199 VMSTATE_UINT8(alarm, TMP105State),
200 VMSTATE_I2C_SLAVE(i2c, TMP105State),
201 VMSTATE_END_OF_LIST()
205 static void tmp105_reset(I2CSlave *i2c)
207 TMP105State *s = (TMP105State *) i2c;
209 s->temperature = 0;
210 s->pointer = 0;
211 s->config = 0;
212 s->faults = tmp105_faultq[(s->config >> 3) & 3];
213 s->alarm = 0;
215 tmp105_interrupt_update(s);
218 static int tmp105_init(I2CSlave *i2c)
220 TMP105State *s = FROM_I2C_SLAVE(TMP105State, i2c);
222 qdev_init_gpio_out(&i2c->qdev, &s->pin, 1);
224 tmp105_reset(&s->i2c);
226 return 0;
229 static void tmp105_class_init(ObjectClass *klass, void *data)
231 DeviceClass *dc = DEVICE_CLASS(klass);
232 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
234 k->init = tmp105_init;
235 k->event = tmp105_event;
236 k->recv = tmp105_rx;
237 k->send = tmp105_tx;
238 dc->vmsd = &vmstate_tmp105;
241 static TypeInfo tmp105_info = {
242 .name = "tmp105",
243 .parent = TYPE_I2C_SLAVE,
244 .instance_size = sizeof(TMP105State),
245 .class_init = tmp105_class_init,
248 static void tmp105_register_types(void)
250 type_register_static(&tmp105_info);
253 type_init(tmp105_register_types)