blob: e29b29440f237aa537321bc388abce9c86014822 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2001
3 * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * On Semiconductor's LM75 Temperature Sensor
26 */
27
28#include <common.h>
29
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"
34#endif
35
36#include <i2c.h>
37#include <dtt.h>
38
39
40/*
41 * Device code
42 */
43#define DTT_I2C_DEV_CODE 0x48 /* ON Semi's LM75 device */
44
45int dtt_read(int sensor, int reg)
46{
47 int dlen;
48 uchar data[2];
49
50 /*
51 * Validate 'reg' param
52 */
53 if((reg < 0) || (reg > 3))
54 return -1;
55
56 /*
57 * Calculate sensor address and register.
58 */
59 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
60
61 /*
62 * Prepare to handle 2 byte result.
63 */
64 if ((reg == DTT_READ_TEMP) ||
65 (reg == DTT_TEMP_HYST) ||
66 (reg == DTT_TEMP_SET))
67 dlen = 2;
68 else
69 dlen = 1;
70
71 /*
72 * Now try to read the register.
73 */
74 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
75 return -1;
76
77 /*
78 * Handle 2 byte result.
79 */
80 if (dlen == 2)
81 return ((int)((short)data[1] + (((short)data[0]) << 8)));
82
83
84 return (int)data[0];
85} /* dtt_read() */
86
87
88int dtt_write(int sensor, int reg, int val)
89{
90 int dlen;
91 uchar data[2];
92
93 /*
94 * Validate 'reg' param
95 */
96 if ((reg < 0) || (reg > 3))
97 return 1;
98
99 /*
100 * Calculate sensor address and register.
101 */
102 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
103
104 /*
105 * Handle 2 byte values.
106 */
107 if ((reg == DTT_READ_TEMP) ||
108 (reg == DTT_TEMP_HYST) ||
109 (reg == DTT_TEMP_SET)) {
110 dlen = 2;
111 data[0] = (char)((val >> 8) & 0xff); /* MSB first */
112 data[1] = (char)(val & 0xff);
113 } else {
114 dlen = 1;
115 data[0] = (char)(val & 0xff);
116 }
117
118 /*
119 * Write value to register.
120 */
121 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
122 return 1;
123
124 return 0;
125} /* dtt_write() */
126
127
128static int _dtt_init(int sensor)
129{
130 int val;
131
132 /*
133 * Setup TSET ( trip point ) register
134 */
135 val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */
136 if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
137 return 1;
138
139 /*
140 * Setup THYST ( untrip point ) register - Hysteresis
141 */
142 val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
143 if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
144 return 1;
145
146 /*
147 * Setup configuraton register
148 */
Stefan Roese887e2ec2006-09-07 11:51:23 +0200149#ifdef CONFIG_DTT_AD7414
150 /* config = alert active low and disabled */
151 val = 0x60;
152#else
wdenkc6097192002-11-03 00:24:07 +0000153 /* config = 6 sample integration, int mode, active low, and enable */
154 val = 0x18;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200155#endif
wdenkc6097192002-11-03 00:24:07 +0000156 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
157 return 1;
158
159 return 0;
160} /* _dtt_init() */
161
162
163int dtt_init (void)
164{
165 int i;
166 unsigned char sensors[] = CONFIG_DTT_SENSORS;
167 const char *const header = "DTT: ";
168
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);
172 else
173 printf("%s%d is %i C\n", header, i+1,
174 dtt_get_temp(sensors[i]));
175 }
176
177 return (0);
178} /* dtt_init() */
179
180int dtt_get_temp(int sensor)
181{
Larry Johnsond01b8472008-02-21 13:58:16 -0500182 int const ret = dtt_read(sensor, DTT_READ_TEMP);
183
184 if (ret < 0) {
185 printf("DTT temperature read failed.\n");
186 return 0;
187 }
188 return (int)((int16_t) ret / 256);
wdenkc6097192002-11-03 00:24:07 +0000189} /* dtt_get_temp() */
190
191#endif /* CONFIG_DTT_LM75 */