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, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
39 static void tmp105_interrupt_update(TMP105State
*s
)
41 qemu_set_irq(s
->pin
, s
->alarm
^ ((~s
->config
>> 2) & 1)); /* POL */
44 static void tmp105_alarm_update(TMP105State
*s
)
46 if ((s
->config
>> 0) & 1) { /* SD */
47 if ((s
->config
>> 7) & 1) /* OS */
48 s
->config
&= ~(1 << 7); /* OS */
53 if ((s
->config
>> 1) & 1) { /* TM */
54 if (s
->temperature
>= s
->limit
[1])
56 else if (s
->temperature
< s
->limit
[0])
59 if (s
->temperature
>= s
->limit
[1])
61 else if (s
->temperature
< s
->limit
[0])
65 tmp105_interrupt_update(s
);
68 /* Units are 0.001 centigrades relative to 0 C. */
69 void tmp105_set(i2c_slave
*i2c
, int temp
)
71 TMP105State
*s
= (TMP105State
*) i2c
;
73 if (temp
>= 128000 || temp
< -128000) {
74 fprintf(stderr
, "%s: values is out of range (%i.%03i C)\n",
75 __FUNCTION__
, temp
/ 1000, temp
% 1000);
79 s
->temperature
= ((int16_t) (temp
* 0x800 / 128000)) << 4;
81 tmp105_alarm_update(s
);
84 static const int tmp105_faultq
[4] = { 1, 2, 4, 6 };
86 static void tmp105_read(TMP105State
*s
)
90 if ((s
->config
>> 1) & 1) { /* TM */
92 tmp105_interrupt_update(s
);
95 switch (s
->pointer
& 3) {
96 case 0: /* Temperature */
97 s
->buf
[s
->len
++] = (((uint16_t) s
->temperature
) >> 8);
98 s
->buf
[s
->len
++] = (((uint16_t) s
->temperature
) >> 0) &
99 (0xf0 << ((~s
->config
>> 5) & 3)); /* R */
102 case 1: /* Configuration */
103 s
->buf
[s
->len
++] = s
->config
;
107 s
->buf
[s
->len
++] = ((uint16_t) s
->limit
[0]) >> 8;
108 s
->buf
[s
->len
++] = ((uint16_t) s
->limit
[0]) >> 0;
112 s
->buf
[s
->len
++] = ((uint16_t) s
->limit
[1]) >> 8;
113 s
->buf
[s
->len
++] = ((uint16_t) s
->limit
[1]) >> 0;
118 static void tmp105_write(TMP105State
*s
)
120 switch (s
->pointer
& 3) {
121 case 0: /* Temperature */
124 case 1: /* Configuration */
125 if (s
->buf
[0] & ~s
->config
& (1 << 0)) /* SD */
126 printf("%s: TMP105 shutdown\n", __FUNCTION__
);
127 s
->config
= s
->buf
[0];
128 s
->faults
= tmp105_faultq
[(s
->config
>> 3) & 3]; /* F */
129 tmp105_alarm_update(s
);
135 s
->limit
[s
->pointer
& 1] = (int16_t)
136 ((((uint16_t) s
->buf
[0]) << 8) | s
->buf
[1]);
137 tmp105_alarm_update(s
);
142 static int tmp105_rx(i2c_slave
*i2c
)
144 TMP105State
*s
= (TMP105State
*) i2c
;
147 return s
->buf
[s
->len
++];
152 static int tmp105_tx(i2c_slave
*i2c
, uint8_t data
)
154 TMP105State
*s
= (TMP105State
*) i2c
;
160 s
->buf
[s
->len
- 1] = data
;
167 static void tmp105_event(i2c_slave
*i2c
, enum i2c_event event
)
169 TMP105State
*s
= (TMP105State
*) i2c
;
171 if (event
== I2C_START_RECV
)
177 static void tmp105_save(QEMUFile
*f
, void *opaque
)
179 TMP105State
*s
= (TMP105State
*) opaque
;
181 qemu_put_byte(f
, s
->len
);
182 qemu_put_8s(f
, &s
->buf
[0]);
183 qemu_put_8s(f
, &s
->buf
[1]);
185 qemu_put_8s(f
, &s
->pointer
);
186 qemu_put_8s(f
, &s
->config
);
187 qemu_put_sbe16s(f
, &s
->temperature
);
188 qemu_put_sbe16s(f
, &s
->limit
[0]);
189 qemu_put_sbe16s(f
, &s
->limit
[1]);
190 qemu_put_byte(f
, s
->alarm
);
191 s
->faults
= tmp105_faultq
[(s
->config
>> 3) & 3]; /* F */
193 i2c_slave_save(f
, &s
->i2c
);
196 static int tmp105_load(QEMUFile
*f
, void *opaque
, int version_id
)
198 TMP105State
*s
= (TMP105State
*) opaque
;
200 s
->len
= qemu_get_byte(f
);
201 qemu_get_8s(f
, &s
->buf
[0]);
202 qemu_get_8s(f
, &s
->buf
[1]);
204 qemu_get_8s(f
, &s
->pointer
);
205 qemu_get_8s(f
, &s
->config
);
206 qemu_get_sbe16s(f
, &s
->temperature
);
207 qemu_get_sbe16s(f
, &s
->limit
[0]);
208 qemu_get_sbe16s(f
, &s
->limit
[1]);
209 s
->alarm
= qemu_get_byte(f
);
211 tmp105_interrupt_update(s
);
213 i2c_slave_load(f
, &s
->i2c
);
217 static void tmp105_reset(i2c_slave
*i2c
)
219 TMP105State
*s
= (TMP105State
*) i2c
;
224 s
->faults
= tmp105_faultq
[(s
->config
>> 3) & 3];
227 tmp105_interrupt_update(s
);
230 static void tmp105_init(i2c_slave
*i2c
)
232 TMP105State
*s
= FROM_I2C_SLAVE(TMP105State
, i2c
);
234 qdev_init_gpio_out(&i2c
->qdev
, &s
->pin
, 1);
236 tmp105_reset(&s
->i2c
);
238 register_savevm("TMP105", -1, 0, tmp105_save
, tmp105_load
, s
);
241 static I2CSlaveInfo tmp105_info
= {
242 .qdev
.name
= "tmp105",
243 .qdev
.size
= sizeof(TMP105State
),
245 .event
= tmp105_event
,
250 static void tmp105_register_devices(void)
252 i2c_register_slave(&tmp105_info
);
255 device_init(tmp105_register_devices
)