blob: ec1893e1d36dde232ae8dc10958ac9e81ce8c556 [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/*
Peter Tyserc3e5fa02010-09-29 13:46:17 -050025 * Dallas Semiconductor's DS1621/1631 Digital Thermometer and Thermostat.
wdenkc6097192002-11-03 00:24:07 +000026 */
27
28#include <common.h>
wdenkc6097192002-11-03 00:24:07 +000029#include <i2c.h>
30#include <dtt.h>
31
32/*
33 * Device code
34 */
35#define DTT_I2C_DEV_CODE 0x48 /* Dallas Semi's DS1621 */
Michal Simek6ecbb452008-07-11 11:50:53 +020036#define DTT_READ_TEMP 0xAA
37#define DTT_READ_COUNTER 0xA8
38#define DTT_READ_SLOPE 0xA9
39#define DTT_WRITE_START_CONV 0xEE
40#define DTT_WRITE_STOP_CONV 0x22
41#define DTT_TEMP_HIGH 0xA1
42#define DTT_TEMP_LOW 0xA2
43#define DTT_CONFIG 0xAC
wdenkc6097192002-11-03 00:24:07 +000044
Peter Tyserc3e5fa02010-09-29 13:46:17 -050045/*
46 * Config register bits
47 */
48#define DTT_CONFIG_1SHOT 0x01
49#define DTT_CONFIG_POLARITY 0x02
50#define DTT_CONFIG_R0 0x04 /* ds1631 only */
51#define DTT_CONFIG_R1 0x08 /* ds1631 only */
52#define DTT_CONFIG_NVB 0x10
53#define DTT_CONFIG_TLF 0x20
54#define DTT_CONFIG_THF 0x40
55#define DTT_CONFIG_DONE 0x80
56
57
wdenkc6097192002-11-03 00:24:07 +000058int dtt_read(int sensor, int reg)
59{
60 int dlen;
61 uchar data[2];
62
63 /*
64 * Calculate sensor address and command.
65 *
66 */
67 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* Calculate addr of ds1621*/
68
69 /*
70 * Prepare to handle 2 byte result.
71 */
72 if ((reg == DTT_READ_TEMP) ||
73 (reg == DTT_TEMP_HIGH) || (reg == DTT_TEMP_LOW))
74 dlen = 2;
75 else
76 dlen = 1;
77
78 /*
79 * Now try to read the register.
80 */
81 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
82 return 1;
83
84 /*
85 * Handle 2 byte result.
86 */
87 if (dlen == 2)
88 return ((int)((short)data[1] + (((short)data[0]) << 8)));
89
90 return (int)data[0];
91} /* dtt_read() */
92
93
94int dtt_write(int sensor, int reg, int val)
95{
96 int dlen;
97 uchar data[2];
98
99 /*
100 * Calculate sensor address and register.
101 *
102 */
wdenk8564acf2003-07-14 22:13:32 +0000103 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
wdenkc6097192002-11-03 00:24:07 +0000104
105 /*
106 * Handle various data sizes.
107 */
108 if ((reg == DTT_READ_TEMP) ||
109 (reg == DTT_TEMP_HIGH) || (reg == DTT_TEMP_LOW)) {
110 dlen = 2;
111 data[0] = (char)((val >> 8) & 0xff); /* MSB first */
112 data[1] = (char)(val & 0xff);
113 }
114 else if ((reg == DTT_WRITE_START_CONV) || (reg == DTT_WRITE_STOP_CONV)) {
115 dlen = 0;
116 data[0] = (char)0;
117 data[1] = (char)0;
118 }
119 else {
120 dlen = 1;
121 data[0] = (char)(val & 0xff);
122 }
123
124 /*
125 * Write value to device.
126 */
127 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
128 return 1;
129
Peter Tyserc3e5fa02010-09-29 13:46:17 -0500130 /*
131 * Poll NV memory busy bit in case write was to register stored in EEPROM
132 */
133 while(i2c_reg_read(sensor, DTT_CONFIG) & DTT_CONFIG_NVB)
134 ;
135
wdenkc6097192002-11-03 00:24:07 +0000136 return 0;
137} /* dtt_write() */
138
139
140static int _dtt_init(int sensor)
141{
142 int val;
143
144 /*
145 * Setup High Temp.
146 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200147 val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80;
wdenkc6097192002-11-03 00:24:07 +0000148 if (dtt_write(sensor, DTT_TEMP_HIGH, val) != 0)
149 return 1;
wdenkc6097192002-11-03 00:24:07 +0000150
151 /*
152 * Setup Low Temp - hysteresis.
153 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200154 val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
wdenkc6097192002-11-03 00:24:07 +0000155 if (dtt_write(sensor, DTT_TEMP_LOW, val) != 0)
156 return 1;
wdenkc6097192002-11-03 00:24:07 +0000157
158 /*
159 * Setup configuraton register
160 *
161 * Clear THF & TLF, Reserved = 1, Polarity = Active Low, One Shot = YES
162 *
163 * We run in polled mode, since there isn't any way to know if this
164 * lousy device is ready to provide temperature readings on power up.
165 */
166 val = 0x9;
167 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
168 return 1;
wdenkc6097192002-11-03 00:24:07 +0000169
170 return 0;
171} /* _dtt_init() */
172
173
174int dtt_init (void)
175{
176 int i;
177 unsigned char sensors[] = CONFIG_DTT_SENSORS;
178
179 for (i = 0; i < sizeof(sensors); i++) {
180 if (_dtt_init(sensors[i]) != 0)
181 printf("DTT%d: FAILED\n", i+1);
182 else
183 printf("DTT%d: %i C\n", i+1, dtt_get_temp(sensors[i]));
184 }
185
186 return (0);
187} /* dtt_init() */
188
189
190int dtt_get_temp(int sensor)
191{
192 int i;
193
194 /*
195 * Start a conversion, may take up to 1 second.
196 */
197 dtt_write(sensor, DTT_WRITE_START_CONV, 0);
198 for (i = 0; i <= 10; i++) {
199 udelay(100000);
Peter Tyserc3e5fa02010-09-29 13:46:17 -0500200 if (dtt_read(sensor, DTT_CONFIG) & DTT_CONFIG_DONE)
wdenkc6097192002-11-03 00:24:07 +0000201 break;
202 }
203
204 return (dtt_read(sensor, DTT_READ_TEMP) / 256);
205} /* dtt_get_temp() */