]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/hwmon/ds1621.c
Merge branch 'master' of git://www.denx.de/git/u-boot
[people/ms/u-boot.git] / drivers / hwmon / ds1621.c
CommitLineData
c609719b
WD
1/*
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
c609719b 30#if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \
8bde7f77 31 (CFG_EEPROM_PAGE_WRITE_BITS < 1)
c609719b
WD
32# 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"
33#endif
34#include <i2c.h>
35#include <dtt.h>
36
37/*
38 * Device code
39 */
40#define DTT_I2C_DEV_CODE 0x48 /* Dallas Semi's DS1621 */
6ecbb45b
MS
41#define DTT_READ_TEMP 0xAA
42#define DTT_READ_COUNTER 0xA8
43#define DTT_READ_SLOPE 0xA9
44#define DTT_WRITE_START_CONV 0xEE
45#define DTT_WRITE_STOP_CONV 0x22
46#define DTT_TEMP_HIGH 0xA1
47#define DTT_TEMP_LOW 0xA2
48#define DTT_CONFIG 0xAC
c609719b
WD
49
50int dtt_read(int sensor, int reg)
51{
52 int dlen;
53 uchar data[2];
54
55 /*
56 * Calculate sensor address and command.
57 *
58 */
59 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* Calculate addr of ds1621*/
60
61 /*
62 * Prepare to handle 2 byte result.
63 */
64 if ((reg == DTT_READ_TEMP) ||
65 (reg == DTT_TEMP_HIGH) || (reg == DTT_TEMP_LOW))
66 dlen = 2;
67 else
68 dlen = 1;
69
70 /*
71 * Now try to read the register.
72 */
73 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
74 return 1;
75
76 /*
77 * Handle 2 byte result.
78 */
79 if (dlen == 2)
80 return ((int)((short)data[1] + (((short)data[0]) << 8)));
81
82 return (int)data[0];
83} /* dtt_read() */
84
85
86int dtt_write(int sensor, int reg, int val)
87{
88 int dlen;
89 uchar data[2];
90
91 /*
92 * Calculate sensor address and register.
93 *
94 */
8564acf9 95 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
c609719b
WD
96
97 /*
98 * Handle various data sizes.
99 */
100 if ((reg == DTT_READ_TEMP) ||
101 (reg == DTT_TEMP_HIGH) || (reg == DTT_TEMP_LOW)) {
102 dlen = 2;
103 data[0] = (char)((val >> 8) & 0xff); /* MSB first */
104 data[1] = (char)(val & 0xff);
105 }
106 else if ((reg == DTT_WRITE_START_CONV) || (reg == DTT_WRITE_STOP_CONV)) {
107 dlen = 0;
108 data[0] = (char)0;
109 data[1] = (char)0;
110 }
111 else {
112 dlen = 1;
113 data[0] = (char)(val & 0xff);
114 }
115
116 /*
117 * Write value to device.
118 */
119 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
120 return 1;
121
122 return 0;
123} /* dtt_write() */
124
125
126static int _dtt_init(int sensor)
127{
128 int val;
129
130 /*
131 * Setup High Temp.
132 */
133 val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80;
134 if (dtt_write(sensor, DTT_TEMP_HIGH, val) != 0)
135 return 1;
136 udelay(50000); /* Max 50ms */
137
138 /*
139 * Setup Low Temp - hysteresis.
140 */
141 val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
142 if (dtt_write(sensor, DTT_TEMP_LOW, val) != 0)
143 return 1;
144 udelay(50000); /* Max 50ms */
145
146 /*
147 * Setup configuraton register
148 *
149 * Clear THF & TLF, Reserved = 1, Polarity = Active Low, One Shot = YES
150 *
151 * We run in polled mode, since there isn't any way to know if this
152 * lousy device is ready to provide temperature readings on power up.
153 */
154 val = 0x9;
155 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
156 return 1;
157 udelay(50000); /* Max 50ms */
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
168 for (i = 0; i < sizeof(sensors); i++) {
169 if (_dtt_init(sensors[i]) != 0)
170 printf("DTT%d: FAILED\n", i+1);
171 else
172 printf("DTT%d: %i C\n", i+1, dtt_get_temp(sensors[i]));
173 }
174
175 return (0);
176} /* dtt_init() */
177
178
179int dtt_get_temp(int sensor)
180{
181 int i;
182
183 /*
184 * Start a conversion, may take up to 1 second.
185 */
186 dtt_write(sensor, DTT_WRITE_START_CONV, 0);
187 for (i = 0; i <= 10; i++) {
188 udelay(100000);
189 if (dtt_read(sensor, DTT_CONFIG) & 0x80)
190 break;
191 }
192
193 return (dtt_read(sensor, DTT_READ_TEMP) / 256);
194} /* dtt_get_temp() */