2 * SPDX-License-Identifier: GPL-2.0+
6 * Dallas Semiconductor's DS1775 Digital Thermometer and Thermostat
14 #define DTT_I2C_DEV_CODE CONFIG_SYS_I2C_DTT_ADDR /* Dallas Semi's DS1775 device code */
15 #define DTT_READ_TEMP 0x0
16 #define DTT_CONFIG 0x1
17 #define DTT_TEMP_HYST 0x2
18 #define DTT_TEMP_OS 0x3
20 int dtt_read(int sensor
, int reg
)
26 * Calculate sensor address and command
28 sensor
= DTT_I2C_DEV_CODE
+ (sensor
& 0x07); /* Calculate addr of ds1775 */
31 * Prepare to handle 2 byte result
33 if ((reg
== DTT_READ_TEMP
) ||
34 (reg
== DTT_TEMP_OS
) || (reg
== DTT_TEMP_HYST
))
40 * Now try to read the register
42 if (i2c_read(sensor
, reg
, 1, data
, dlen
) != 0)
46 * Handle 2 byte result
49 return ((int)((short)data
[1] + (((short)data
[0]) << 8)));
55 int dtt_write(int sensor
, int reg
, int val
)
61 * Calculate sensor address and register
63 sensor
= DTT_I2C_DEV_CODE
+ (sensor
& 0x07);
66 * Handle various data sizes
68 if ((reg
== DTT_READ_TEMP
) ||
69 (reg
== DTT_TEMP_OS
) || (reg
== DTT_TEMP_HYST
)) {
71 data
[0] = (char)((val
>> 8) & 0xff); /* MSB first */
72 data
[1] = (char)(val
& 0xff);
75 data
[0] = (char)(val
& 0xff);
79 * Write value to device
81 if (i2c_write(sensor
, reg
, 1, data
, dlen
) != 0)
88 int dtt_init_one(int sensor
)
95 val
= ((CONFIG_SYS_DTT_MAX_TEMP
* 2) << 7) & 0xff80;
96 if (dtt_write(sensor
, DTT_TEMP_OS
, val
) != 0)
98 udelay(50000); /* Max 50ms */
101 * Setup Low Temp - hysteresis
103 val
= (((CONFIG_SYS_DTT_MAX_TEMP
- CONFIG_SYS_DTT_HYSTERESIS
) * 2) << 7) & 0xff80;
104 if (dtt_write(sensor
, DTT_TEMP_HYST
, val
) != 0)
106 udelay(50000); /* Max 50ms */
109 * Setup configuraton register
111 * Fault Tolerance limits 4, Thermometer resolution bits is 9,
112 * Polarity = Active Low,continuous conversion mode, Thermostat
113 * mode is interrupt mode
116 if (dtt_write(sensor
, DTT_CONFIG
, val
) != 0)
118 udelay(50000); /* Max 50ms */
123 int dtt_get_temp(int sensor
)
125 return (dtt_read(sensor
, DTT_READ_TEMP
) / 256);