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/>.
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(I2CSlave
*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 TMP105_REG_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 TMP105_REG_CONFIG
:
103 s
->buf
[s
->len
++] = s
->config
;
106 case TMP105_REG_T_LOW
:
107 s
->buf
[s
->len
++] = ((uint16_t) s
->limit
[0]) >> 8;
108 s
->buf
[s
->len
++] = ((uint16_t) s
->limit
[0]) >> 0;
111 case TMP105_REG_T_HIGH
:
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 TMP105_REG_TEMPERATURE
:
124 case TMP105_REG_CONFIG
:
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
);
132 case TMP105_REG_T_LOW
:
133 case TMP105_REG_T_HIGH
:
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(I2CSlave
*i2c
)
144 TMP105State
*s
= (TMP105State
*) i2c
;
147 return s
->buf
[s
->len
++];
152 static int tmp105_tx(I2CSlave
*i2c
, uint8_t data
)
154 TMP105State
*s
= (TMP105State
*) i2c
;
160 s
->buf
[s
->len
- 1] = data
;
167 static void tmp105_event(I2CSlave
*i2c
, enum i2c_event event
)
169 TMP105State
*s
= (TMP105State
*) i2c
;
171 if (event
== I2C_START_RECV
)
177 static int tmp105_post_load(void *opaque
, int version_id
)
179 TMP105State
*s
= opaque
;
181 s
->faults
= tmp105_faultq
[(s
->config
>> 3) & 3]; /* F */
183 tmp105_interrupt_update(s
);
187 static const VMStateDescription vmstate_tmp105
= {
190 .minimum_version_id
= 0,
191 .minimum_version_id_old
= 0,
192 .post_load
= tmp105_post_load
,
193 .fields
= (VMStateField
[]) {
194 VMSTATE_UINT8(len
, TMP105State
),
195 VMSTATE_UINT8_ARRAY(buf
, TMP105State
, 2),
196 VMSTATE_UINT8(pointer
, TMP105State
),
197 VMSTATE_UINT8(config
, TMP105State
),
198 VMSTATE_INT16(temperature
, TMP105State
),
199 VMSTATE_INT16_ARRAY(limit
, TMP105State
, 2),
200 VMSTATE_UINT8(alarm
, TMP105State
),
201 VMSTATE_I2C_SLAVE(i2c
, TMP105State
),
202 VMSTATE_END_OF_LIST()
206 static void tmp105_reset(I2CSlave
*i2c
)
208 TMP105State
*s
= (TMP105State
*) i2c
;
213 s
->faults
= tmp105_faultq
[(s
->config
>> 3) & 3];
216 tmp105_interrupt_update(s
);
219 static int tmp105_init(I2CSlave
*i2c
)
221 TMP105State
*s
= FROM_I2C_SLAVE(TMP105State
, i2c
);
223 qdev_init_gpio_out(&i2c
->qdev
, &s
->pin
, 1);
225 tmp105_reset(&s
->i2c
);
230 static void tmp105_class_init(ObjectClass
*klass
, void *data
)
232 DeviceClass
*dc
= DEVICE_CLASS(klass
);
233 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
235 k
->init
= tmp105_init
;
236 k
->event
= tmp105_event
;
239 dc
->vmsd
= &vmstate_tmp105
;
242 static TypeInfo tmp105_info
= {
244 .parent
= TYPE_I2C_SLAVE
,
245 .instance_size
= sizeof(TMP105State
),
246 .class_init
= tmp105_class_init
,
249 static void tmp105_register_types(void)
251 type_register_static(&tmp105_info
);
254 type_init(tmp105_register_types
)