blob: 5c25f641d9dd00892afe288a48d18e2270f41e84 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2001
3 * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com.
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 * Dallas Semiconductor's DS1621 Digital Thermometer and Thermostat.
26 */
27
28#include <common.h>
29
30#ifdef CONFIG_DTT_DS1621
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_DS1621"
34#endif
35#include <i2c.h>
36#include <dtt.h>
37
38/*
39 * Device code
40 */
41#define DTT_I2C_DEV_CODE 0x48 /* Dallas Semi's DS1621 */
42
43int dtt_read(int sensor, int reg)
44{
45 int dlen;
46 uchar data[2];
47
48 /*
49 * Calculate sensor address and command.
50 *
51 */
52 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* Calculate addr of ds1621*/
53
54 /*
55 * Prepare to handle 2 byte result.
56 */
57 if ((reg == DTT_READ_TEMP) ||
58 (reg == DTT_TEMP_HIGH) || (reg == DTT_TEMP_LOW))
59 dlen = 2;
60 else
61 dlen = 1;
62
63 /*
64 * Now try to read the register.
65 */
66 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
67 return 1;
68
69 /*
70 * Handle 2 byte result.
71 */
72 if (dlen == 2)
73 return ((int)((short)data[1] + (((short)data[0]) << 8)));
74
75 return (int)data[0];
76} /* dtt_read() */
77
78
79int dtt_write(int sensor, int reg, int val)
80{
81 int dlen;
82 uchar data[2];
83
84 /*
85 * Calculate sensor address and register.
86 *
87 */
88 sensor = DTT_I2C_DEV_CODE + (sensor & sensor);
89
90 /*
91 * Handle various data sizes.
92 */
93 if ((reg == DTT_READ_TEMP) ||
94 (reg == DTT_TEMP_HIGH) || (reg == DTT_TEMP_LOW)) {
95 dlen = 2;
96 data[0] = (char)((val >> 8) & 0xff); /* MSB first */
97 data[1] = (char)(val & 0xff);
98 }
99 else if ((reg == DTT_WRITE_START_CONV) || (reg == DTT_WRITE_STOP_CONV)) {
100 dlen = 0;
101 data[0] = (char)0;
102 data[1] = (char)0;
103 }
104 else {
105 dlen = 1;
106 data[0] = (char)(val & 0xff);
107 }
108
109 /*
110 * Write value to device.
111 */
112 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
113 return 1;
114
115 return 0;
116} /* dtt_write() */
117
118
119static int _dtt_init(int sensor)
120{
121 int val;
122
123 /*
124 * Setup High Temp.
125 */
126 val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80;
127 if (dtt_write(sensor, DTT_TEMP_HIGH, val) != 0)
128 return 1;
129 udelay(50000); /* Max 50ms */
130
131 /*
132 * Setup Low Temp - hysteresis.
133 */
134 val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
135 if (dtt_write(sensor, DTT_TEMP_LOW, val) != 0)
136 return 1;
137 udelay(50000); /* Max 50ms */
138
139 /*
140 * Setup configuraton register
141 *
142 * Clear THF & TLF, Reserved = 1, Polarity = Active Low, One Shot = YES
143 *
144 * We run in polled mode, since there isn't any way to know if this
145 * lousy device is ready to provide temperature readings on power up.
146 */
147 val = 0x9;
148 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
149 return 1;
150 udelay(50000); /* Max 50ms */
151
152 return 0;
153} /* _dtt_init() */
154
155
156int dtt_init (void)
157{
158 int i;
159 unsigned char sensors[] = CONFIG_DTT_SENSORS;
160
161 for (i = 0; i < sizeof(sensors); i++) {
162 if (_dtt_init(sensors[i]) != 0)
163 printf("DTT%d: FAILED\n", i+1);
164 else
165 printf("DTT%d: %i C\n", i+1, dtt_get_temp(sensors[i]));
166 }
167
168 return (0);
169} /* dtt_init() */
170
171
172int dtt_get_temp(int sensor)
173{
174 int i;
175
176 /*
177 * Start a conversion, may take up to 1 second.
178 */
179 dtt_write(sensor, DTT_WRITE_START_CONV, 0);
180 for (i = 0; i <= 10; i++) {
181 udelay(100000);
182 if (dtt_read(sensor, DTT_CONFIG) & 0x80)
183 break;
184 }
185
186 return (dtt_read(sensor, DTT_READ_TEMP) / 256);
187} /* dtt_get_temp() */
188
189
190#endif /* CONFIG_DTT_DS1621 */
191