blob: e44cee3279c72942d1f0f927d17e0a72c162fb1d [file] [log] [blame]
John Otkend4024bb2007-07-26 17:49:11 +02001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15 * MA 02111-1307 USA
16 */
17
18/*
19 * Dallas Semiconductor's DS1775 Digital Thermometer and Thermostat
20 */
21
22#include <common.h>
23
24#ifdef CONFIG_DTT_DS1775
25#include <i2c.h>
26#include <dtt.h>
27
28#define DTT_I2C_DEV_CODE 0x49 /* Dallas Semi's DS1775 device code */
29
30int dtt_read(int sensor, int reg)
31{
32 int dlen;
33 uchar data[2];
34
35 /*
36 * Calculate sensor address and command
37 */
38 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* Calculate addr of ds1775 */
39
40 /*
41 * Prepare to handle 2 byte result
42 */
43 if ((reg == DTT_READ_TEMP) ||
44 (reg == DTT_TEMP_OS) || (reg == DTT_TEMP_HYST))
45 dlen = 2;
46 else
47 dlen = 1;
48
49 /*
50 * Now try to read the register
51 */
52 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
53 return 1;
54
55 /*
56 * Handle 2 byte result
57 */
58 if (dlen == 2)
59 return ((int)((short)data[1] + (((short)data[0]) << 8)));
60
61 return (int) data[0];
62}
63
64
65int dtt_write(int sensor, int reg, int val)
66{
67 int dlen;
68 uchar data[2];
69
70 /*
71 * Calculate sensor address and register
72 */
73 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
74
75 /*
76 * Handle various data sizes
77 */
78 if ((reg == DTT_READ_TEMP) ||
79 (reg == DTT_TEMP_OS) || (reg == DTT_TEMP_HYST)) {
80 dlen = 2;
81 data[0] = (char)((val >> 8) & 0xff); /* MSB first */
82 data[1] = (char)(val & 0xff);
83 } else {
84 dlen = 1;
85 data[0] = (char)(val & 0xff);
86 }
87
88 /*
89 * Write value to device
90 */
91 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
92 return 1;
93
94 return 0;
95}
96
97
98static int _dtt_init(int sensor)
99{
100 int val;
101
102 /*
103 * Setup High Temp
104 */
105 val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80;
106 if (dtt_write(sensor, DTT_TEMP_OS, val) != 0)
107 return 1;
108 udelay(50000); /* Max 50ms */
109
110 /*
111 * Setup Low Temp - hysteresis
112 */
113 val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
114 if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
115 return 1;
116 udelay(50000); /* Max 50ms */
117
118 /*
119 * Setup configuraton register
120 *
121 * Fault Tolerance limits 4, Thermometer resolution bits is 9,
122 * Polarity = Active Low,continuous conversion mode, Thermostat
123 * mode is interrupt mode
124 */
125 val = 0xa;
126 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
127 return 1;
128 udelay(50000); /* Max 50ms */
129
130 return 0;
131}
132
133
134int dtt_init (void)
135{
136 int i;
137 unsigned char sensors[] = CONFIG_DTT_SENSORS;
138
139 for (i = 0; i < sizeof(sensors); i++) {
140 if (_dtt_init(sensors[i]) != 0)
141 printf("DTT%d: FAILED\n", i+1);
142 else
143 printf("DTT%d: %i C\n", i+1, dtt_get_temp(sensors[i]));
144 }
145
146 return (0);
147}
148
149
150int dtt_get_temp(int sensor)
151{
152 return (dtt_read(sensor, DTT_READ_TEMP) / 256);
153}
154
155
156#endif /* CONFIG_DTT_DS1775 */