3 * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com.
5 * SPDX-License-Identifier: GPL-2.0+
9 * Dallas Semiconductor's DS1621/1631 Digital Thermometer and Thermostat.
19 #define DTT_I2C_DEV_CODE 0x48 /* Dallas Semi's DS1621 */
20 #define DTT_READ_TEMP 0xAA
21 #define DTT_READ_COUNTER 0xA8
22 #define DTT_READ_SLOPE 0xA9
23 #define DTT_WRITE_START_CONV 0xEE
24 #define DTT_WRITE_STOP_CONV 0x22
25 #define DTT_TEMP_HIGH 0xA1
26 #define DTT_TEMP_LOW 0xA2
27 #define DTT_CONFIG 0xAC
30 * Config register bits
32 #define DTT_CONFIG_1SHOT 0x01
33 #define DTT_CONFIG_POLARITY 0x02
34 #define DTT_CONFIG_R0 0x04 /* ds1631 only */
35 #define DTT_CONFIG_R1 0x08 /* ds1631 only */
36 #define DTT_CONFIG_NVB 0x10
37 #define DTT_CONFIG_TLF 0x20
38 #define DTT_CONFIG_THF 0x40
39 #define DTT_CONFIG_DONE 0x80
42 int dtt_read(int sensor
, int reg
)
47 /* Calculate sensor address and command */
48 sensor
= DTT_I2C_DEV_CODE
+ (sensor
& 0x07); /* Calculate addr of ds1621*/
50 /* Prepare to handle 2 byte result */
61 /* Now try to read the register */
62 if (i2c_read(sensor
, reg
, 1, data
, dlen
) != 0)
65 /* Handle 2 byte result */
67 return (short)((data
[0] << 8) | data
[1]);
73 int dtt_write(int sensor
, int reg
, int val
)
78 /* Calculate sensor address and register */
79 sensor
= DTT_I2C_DEV_CODE
+ (sensor
& 0x07);
81 /* Handle various data sizes. */
87 data
[0] = (char)((val
>> 8) & 0xff); /* MSB first */
88 data
[1] = (char)(val
& 0xff);
90 case DTT_WRITE_START_CONV
:
91 case DTT_WRITE_STOP_CONV
:
98 data
[0] = (char)(val
& 0xff);
101 /* Write value to device */
102 if (i2c_write(sensor
, reg
, 1, data
, dlen
) != 0)
105 /* Poll NV memory busy bit in case write was to register stored in EEPROM */
106 while(i2c_reg_read(sensor
, DTT_CONFIG
) & DTT_CONFIG_NVB
)
113 int dtt_init_one(int sensor
)
117 /* Setup High Temp */
118 val
= ((CONFIG_SYS_DTT_MAX_TEMP
* 2) << 7) & 0xff80;
119 if (dtt_write(sensor
, DTT_TEMP_HIGH
, val
) != 0)
122 /* Setup Low Temp - hysteresis */
123 val
= (((CONFIG_SYS_DTT_MAX_TEMP
- CONFIG_SYS_DTT_HYSTERESIS
) * 2) << 7) & 0xff80;
124 if (dtt_write(sensor
, DTT_TEMP_LOW
, val
) != 0)
128 * Setup configuraton register
130 * Clear THF & TLF, Reserved = 1, Polarity = Active Low, One Shot = YES
132 * We run in polled mode, since there isn't any way to know if this
133 * lousy device is ready to provide temperature readings on power up.
136 if (dtt_write(sensor
, DTT_CONFIG
, val
) != 0)
142 int dtt_get_temp(int sensor
)
146 /* Start a conversion, may take up to 1 second. */
147 dtt_write(sensor
, DTT_WRITE_START_CONV
, 0);
148 for (i
= 0; i
<= 10; i
++) {
150 if (dtt_read(sensor
, DTT_CONFIG
) & DTT_CONFIG_DONE
)
154 return (dtt_read(sensor
, DTT_READ_TEMP
) / 256);