3 * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * On Semiconductor's LM75 Temperature Sensor
30 #ifdef CONFIG_DTT_LM75
31 #if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \
32 (CFG_EEPROM_PAGE_WRITE_BITS < 1)
33 # error "CFG_EEPROM_PAGE_WRITE_ENABLE must be defined and CFG_EEPROM_PAGE_WRITE_BITS must be greater than 1 to use CONFIG_DTT_LM75"
43 #define DTT_I2C_DEV_CODE 0x48 /* ON Semi's LM75 device */
45 int dtt_read(int sensor
, int reg
)
51 * Validate 'reg' param
53 if((reg
< 0) || (reg
> 3))
57 * Calculate sensor address and register.
59 sensor
= DTT_I2C_DEV_CODE
+ (sensor
& 0x07); /* calculate address of lm75 */
62 * Prepare to handle 2 byte result.
64 if ((reg
== DTT_READ_TEMP
) ||
65 (reg
== DTT_TEMP_HYST
) ||
66 (reg
== DTT_TEMP_SET
))
72 * Now try to read the register.
74 if (i2c_read(sensor
, reg
, 1, data
, dlen
) != 0)
78 * Handle 2 byte result.
81 return ((int)((short)data
[1] + (((short)data
[0]) << 8)));
88 int dtt_write(int sensor
, int reg
, int val
)
94 * Validate 'reg' param
96 if ((reg
< 0) || (reg
> 3))
100 * Calculate sensor address and register.
102 sensor
= DTT_I2C_DEV_CODE
+ (sensor
& 0x07); /* calculate address of lm75 */
105 * Handle 2 byte values.
107 if ((reg
== DTT_READ_TEMP
) ||
108 (reg
== DTT_TEMP_HYST
) ||
109 (reg
== DTT_TEMP_SET
)) {
111 data
[0] = (char)((val
>> 8) & 0xff); /* MSB first */
112 data
[1] = (char)(val
& 0xff);
115 data
[0] = (char)(val
& 0xff);
119 * Write value to register.
121 if (i2c_write(sensor
, reg
, 1, data
, dlen
) != 0)
128 static int _dtt_init(int sensor
)
133 * Setup TSET ( trip point ) register
135 val
= ((CFG_DTT_MAX_TEMP
* 2) << 7) & 0xff80; /* trip */
136 if (dtt_write(sensor
, DTT_TEMP_SET
, val
) != 0)
140 * Setup THYST ( untrip point ) register - Hysteresis
142 val
= (((CFG_DTT_MAX_TEMP
- CFG_DTT_HYSTERESIS
) * 2) << 7) & 0xff80;
143 if (dtt_write(sensor
, DTT_TEMP_HYST
, val
) != 0)
147 * Setup configuraton register
149 #ifdef CONFIG_DTT_AD7414
150 /* config = alert active low and disabled */
153 /* config = 6 sample integration, int mode, active low, and enable */
156 if (dtt_write(sensor
, DTT_CONFIG
, val
) != 0)
166 unsigned char sensors
[] = CONFIG_DTT_SENSORS
;
167 const char *const header
= "DTT: ";
169 for (i
= 0; i
< sizeof(sensors
); i
++) {
170 if (_dtt_init(sensors
[i
]) != 0)
171 printf("%s%d FAILED INIT\n", header
, i
+1);
173 printf("%s%d is %i C\n", header
, i
+1,
174 dtt_get_temp(sensors
[i
]));
180 int dtt_get_temp(int sensor
)
182 return (dtt_read(sensor
, DTT_READ_TEMP
) / 256);
183 } /* dtt_get_temp() */
185 #endif /* CONFIG_DTT_LM75 */