blob: 59daa453976ccfd6cbeac3e5be190d1d1c865768 [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
50 /*
51 * Validate 'reg' param
52 */
53 if((reg < 0) || (reg > 3))
54 return -1;
55
56 /*
57 * Calculate sensor address and register.
58 */
59 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
60
61 /*
62 * Prepare to handle 2 byte result.
63 */
64 if ((reg == DTT_READ_TEMP) ||
65 (reg == DTT_TEMP_HYST) ||
66 (reg == DTT_TEMP_SET))
67 dlen = 2;
68 else
69 dlen = 1;
70
71 /*
72 * Now try to read the register.
73 */
74 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
75 return -1;
76
77 /*
78 * Handle 2 byte result.
79 */
80 if (dlen == 2)
81 return ((int)((short)data[1] + (((short)data[0]) << 8)));
82
83
84 return (int)data[0];
85} /* dtt_read() */
86
87
88int dtt_write(int sensor, int reg, int val)
89{
90 int dlen;
91 uchar data[2];
92
93 /*
94 * Validate 'reg' param
95 */
96 if ((reg < 0) || (reg > 3))
97 return 1;
98
99 /*
100 * Calculate sensor address and register.
101 */
102 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
103
104 /*
105 * Handle 2 byte values.
106 */
107 if ((reg == DTT_READ_TEMP) ||
108 (reg == DTT_TEMP_HYST) ||
109 (reg == DTT_TEMP_SET)) {
110 dlen = 2;
111 data[0] = (char)((val >> 8) & 0xff); /* MSB first */
112 data[1] = (char)(val & 0xff);
113 } else {
114 dlen = 1;
115 data[0] = (char)(val & 0xff);
116 }
117
118 /*
119 * Write value to register.
120 */
121 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
122 return 1;
123
124 return 0;
125} /* dtt_write() */
126
127
128static int _dtt_init(int sensor)
129{
130 int val;
131
132 /*
133 * Setup TSET ( trip point ) register
134 */
135 val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */
136 if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
137 return 1;
138
139 /*
140 * Setup THYST ( untrip point ) register - Hysteresis
141 */
142 val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
143 if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
144 return 1;
145
146 /*
147 * Setup configuraton register
148 */
149 /* config = 6 sample integration, int mode, active low, and enable */
150 val = 0x18;
151 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
152 return 1;
153
154 return 0;
155} /* _dtt_init() */
156
157
158int dtt_init (void)
159{
160 int i;
161 unsigned char sensors[] = CONFIG_DTT_SENSORS;
162 const char *const header = "DTT: ";
163
164 for (i = 0; i < sizeof(sensors); i++) {
165 if (_dtt_init(sensors[i]) != 0)
166 printf("%s%d FAILED INIT\n", header, i+1);
167 else
168 printf("%s%d is %i C\n", header, i+1,
169 dtt_get_temp(sensors[i]));
170 }
171
172 return (0);
173} /* dtt_init() */
174
175int dtt_get_temp(int sensor)
176{
177 return (dtt_read(sensor, DTT_READ_TEMP) / 256);
178} /* dtt_get_temp() */
179
180#endif /* CONFIG_DTT_LM75 */