blob: c34851725ef75095bee305298124c3ecaf5f5eb3 [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
Stefan Roese4d91d1d2008-05-16 11:06:06 +020050#ifdef CONFIG_DTT_AD7414
51 /*
52 * On AD7414 the first value upon bootup is not read correctly.
53 * This is most likely because of the 800ms update time of the
54 * temp register in normal update mode. To get current values
55 * each time we issue the "dtt" command including upon powerup
56 * we switch into one-short mode.
57 *
58 * Issue one-shot mode command
59 */
60 dtt_write(sensor, DTT_CONFIG, 0x64);
61#endif
62
wdenkc6097192002-11-03 00:24:07 +000063 /*
64 * Validate 'reg' param
65 */
66 if((reg < 0) || (reg > 3))
67 return -1;
68
69 /*
70 * Calculate sensor address and register.
71 */
72 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
73
74 /*
75 * Prepare to handle 2 byte result.
76 */
77 if ((reg == DTT_READ_TEMP) ||
78 (reg == DTT_TEMP_HYST) ||
79 (reg == DTT_TEMP_SET))
80 dlen = 2;
81 else
82 dlen = 1;
83
84 /*
85 * Now try to read the register.
86 */
87 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
88 return -1;
89
90 /*
91 * Handle 2 byte result.
92 */
93 if (dlen == 2)
94 return ((int)((short)data[1] + (((short)data[0]) << 8)));
95
96
97 return (int)data[0];
98} /* dtt_read() */
99
100
101int dtt_write(int sensor, int reg, int val)
102{
103 int dlen;
104 uchar data[2];
105
106 /*
107 * Validate 'reg' param
108 */
109 if ((reg < 0) || (reg > 3))
110 return 1;
111
112 /*
113 * Calculate sensor address and register.
114 */
115 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
116
117 /*
118 * Handle 2 byte values.
119 */
120 if ((reg == DTT_READ_TEMP) ||
121 (reg == DTT_TEMP_HYST) ||
122 (reg == DTT_TEMP_SET)) {
123 dlen = 2;
124 data[0] = (char)((val >> 8) & 0xff); /* MSB first */
125 data[1] = (char)(val & 0xff);
126 } else {
127 dlen = 1;
128 data[0] = (char)(val & 0xff);
129 }
130
131 /*
132 * Write value to register.
133 */
134 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
135 return 1;
136
137 return 0;
138} /* dtt_write() */
139
140
141static int _dtt_init(int sensor)
142{
143 int val;
144
145 /*
146 * Setup TSET ( trip point ) register
147 */
148 val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */
149 if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
150 return 1;
151
152 /*
153 * Setup THYST ( untrip point ) register - Hysteresis
154 */
155 val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
156 if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
157 return 1;
158
159 /*
160 * Setup configuraton register
161 */
Stefan Roese887e2ec2006-09-07 11:51:23 +0200162#ifdef CONFIG_DTT_AD7414
163 /* config = alert active low and disabled */
164 val = 0x60;
165#else
wdenkc6097192002-11-03 00:24:07 +0000166 /* config = 6 sample integration, int mode, active low, and enable */
167 val = 0x18;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200168#endif
wdenkc6097192002-11-03 00:24:07 +0000169 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
170 return 1;
171
172 return 0;
173} /* _dtt_init() */
174
175
176int dtt_init (void)
177{
178 int i;
179 unsigned char sensors[] = CONFIG_DTT_SENSORS;
180 const char *const header = "DTT: ";
181
182 for (i = 0; i < sizeof(sensors); i++) {
183 if (_dtt_init(sensors[i]) != 0)
184 printf("%s%d FAILED INIT\n", header, i+1);
185 else
186 printf("%s%d is %i C\n", header, i+1,
187 dtt_get_temp(sensors[i]));
188 }
189
190 return (0);
191} /* dtt_init() */
192
193int dtt_get_temp(int sensor)
194{
Larry Johnsond01b8472008-02-21 13:58:16 -0500195 int const ret = dtt_read(sensor, DTT_READ_TEMP);
196
197 if (ret < 0) {
198 printf("DTT temperature read failed.\n");
199 return 0;
200 }
201 return (int)((int16_t) ret / 256);
wdenkc6097192002-11-03 00:24:07 +0000202} /* dtt_get_temp() */
203
204#endif /* CONFIG_DTT_LM75 */