3 * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
5 * SPDX-License-Identifier: GPL-2.0+
9 * On Semiconductor's LM75 Temperature Sensor
19 #if defined(CONFIG_SYS_I2C_DTT_ADDR)
20 #define DTT_I2C_DEV_CODE CONFIG_SYS_I2C_DTT_ADDR
22 #define DTT_I2C_DEV_CODE 0x48 /* ON Semi's LM75 device */
24 #define DTT_READ_TEMP 0x0
25 #define DTT_CONFIG 0x1
26 #define DTT_TEMP_HYST 0x2
27 #define DTT_TEMP_SET 0x3
29 int dtt_read(int sensor
, int reg
)
34 #ifdef CONFIG_DTT_AD7414
36 * On AD7414 the first value upon bootup is not read correctly.
37 * This is most likely because of the 800ms update time of the
38 * temp register in normal update mode. To get current values
39 * each time we issue the "dtt" command including upon powerup
40 * we switch into one-short mode.
42 * Issue one-shot mode command
44 dtt_write(sensor
, DTT_CONFIG
, 0x64);
47 /* Validate 'reg' param */
48 if((reg
< 0) || (reg
> 3))
51 /* Calculate sensor address and register. */
52 sensor
= DTT_I2C_DEV_CODE
+ (sensor
& 0x07);
54 /* Prepare to handle 2 byte result. */
55 if ((reg
== DTT_READ_TEMP
) ||
56 (reg
== DTT_TEMP_HYST
) ||
57 (reg
== DTT_TEMP_SET
))
62 /* Now try to read the register. */
63 if (i2c_read(sensor
, reg
, 1, data
, dlen
) != 0)
66 /* Handle 2 byte result. */
68 return ((int)((short)data
[1] + (((short)data
[0]) << 8)));
74 int dtt_write(int sensor
, int reg
, int val
)
79 /* Validate 'reg' param */
80 if ((reg
< 0) || (reg
> 3))
83 /* Calculate sensor address and register. */
84 sensor
= DTT_I2C_DEV_CODE
+ (sensor
& 0x07);
86 /* Handle 2 byte values. */
87 if ((reg
== DTT_READ_TEMP
) ||
88 (reg
== DTT_TEMP_HYST
) ||
89 (reg
== DTT_TEMP_SET
)) {
91 data
[0] = (char)((val
>> 8) & 0xff); /* MSB first */
92 data
[1] = (char)(val
& 0xff);
95 data
[0] = (char)(val
& 0xff);
98 /* Write value to register. */
99 if (i2c_write(sensor
, reg
, 1, data
, dlen
) != 0)
106 int dtt_init_one(int sensor
)
110 /* Setup TSET ( trip point ) register */
111 val
= ((CONFIG_SYS_DTT_MAX_TEMP
* 2) << 7) & 0xff80; /* trip */
112 if (dtt_write(sensor
, DTT_TEMP_SET
, val
) != 0)
115 /* Setup THYST ( untrip point ) register - Hysteresis */
116 val
= (((CONFIG_SYS_DTT_MAX_TEMP
- CONFIG_SYS_DTT_HYSTERESIS
) * 2) << 7) & 0xff80;
117 if (dtt_write(sensor
, DTT_TEMP_HYST
, val
) != 0)
120 /* Setup configuraton register */
121 #ifdef CONFIG_DTT_AD7414
122 /* config = alert active low and disabled */
125 /* config = 6 sample integration, int mode, active low, and enable */
128 if (dtt_write(sensor
, DTT_CONFIG
, val
) != 0)
132 } /* dtt_init_one() */
134 int dtt_get_temp(int sensor
)
136 int const ret
= dtt_read(sensor
, DTT_READ_TEMP
);
139 printf("DTT temperature read failed.\n");
142 return (int)((int16_t) ret
/ 256);
143 } /* dtt_get_temp() */