]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/hwmon/lm75.c
Merge branch 'next' of git://git.denx.de/u-boot-avr32
[karo-tx-uboot.git] / drivers / hwmon / lm75.c
1 /*
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 #if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \
31         (CFG_EEPROM_PAGE_WRITE_BITS < 1)
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_LM75"
33 #endif
34
35 #include <i2c.h>
36 #include <dtt.h>
37
38 /*
39  * Device code
40  */
41 #define DTT_I2C_DEV_CODE 0x48                   /* ON Semi's LM75 device */
42 #define DTT_READ_TEMP           0x0
43 #define DTT_CONFIG              0x1
44 #define DTT_TEMP_HYST           0x2
45 #define DTT_TEMP_SET            0x3
46
47 int dtt_read(int sensor, int reg)
48 {
49     int dlen;
50     uchar data[2];
51
52 #ifdef CONFIG_DTT_AD7414
53     /*
54      * On AD7414 the first value upon bootup is not read correctly.
55      * This is most likely because of the 800ms update time of the
56      * temp register in normal update mode. To get current values
57      * each time we issue the "dtt" command including upon powerup
58      * we switch into one-short mode.
59      *
60      * Issue one-shot mode command
61      */
62     dtt_write(sensor, DTT_CONFIG, 0x64);
63 #endif
64
65     /*
66      * Validate 'reg' param
67      */
68     if((reg < 0) || (reg > 3))
69         return -1;
70
71     /*
72      * Calculate sensor address and register.
73      */
74     sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
75
76     /*
77      * Prepare to handle 2 byte result.
78      */
79     if ((reg == DTT_READ_TEMP) ||
80         (reg == DTT_TEMP_HYST) ||
81         (reg == DTT_TEMP_SET))
82         dlen = 2;
83     else
84         dlen = 1;
85
86     /*
87      * Now try to read the register.
88      */
89     if (i2c_read(sensor, reg, 1, data, dlen) != 0)
90         return -1;
91
92     /*
93      * Handle 2 byte result.
94      */
95     if (dlen == 2)
96         return ((int)((short)data[1] + (((short)data[0]) << 8)));
97
98
99     return (int)data[0];
100 } /* dtt_read() */
101
102
103 int dtt_write(int sensor, int reg, int val)
104 {
105     int dlen;
106     uchar data[2];
107
108     /*
109      * Validate 'reg' param
110      */
111     if ((reg < 0) || (reg > 3))
112         return 1;
113
114     /*
115      * Calculate sensor address and register.
116      */
117     sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
118
119     /*
120      * Handle 2 byte values.
121      */
122     if ((reg == DTT_READ_TEMP) ||
123         (reg == DTT_TEMP_HYST) ||
124         (reg == DTT_TEMP_SET)) {
125         dlen = 2;
126         data[0] = (char)((val >> 8) & 0xff);    /* MSB first */
127         data[1] = (char)(val & 0xff);
128     } else {
129         dlen = 1;
130         data[0] = (char)(val & 0xff);
131     }
132
133     /*
134      * Write value to register.
135      */
136     if (i2c_write(sensor, reg, 1, data, dlen) != 0)
137         return 1;
138
139     return 0;
140 } /* dtt_write() */
141
142
143 static int _dtt_init(int sensor)
144 {
145     int val;
146
147     /*
148      * Setup TSET ( trip point ) register
149      */
150     val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */
151     if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
152         return 1;
153
154     /*
155      * Setup THYST ( untrip point ) register - Hysteresis
156      */
157     val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
158     if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
159         return 1;
160
161     /*
162      * Setup configuraton register
163      */
164 #ifdef CONFIG_DTT_AD7414
165     /* config = alert active low and disabled */
166     val = 0x60;
167 #else
168     /* config = 6 sample integration, int mode, active low, and enable */
169     val = 0x18;
170 #endif
171     if (dtt_write(sensor, DTT_CONFIG, val) != 0)
172         return 1;
173
174     return 0;
175 } /* _dtt_init() */
176
177
178 int dtt_init (void)
179 {
180     int i;
181     unsigned char sensors[] = CONFIG_DTT_SENSORS;
182     const char *const header = "DTT:   ";
183
184     for (i = 0; i < sizeof(sensors); i++) {
185         if (_dtt_init(sensors[i]) != 0)
186             printf("%s%d FAILED INIT\n", header, i+1);
187         else
188             printf("%s%d is %i C\n", header, i+1,
189                    dtt_get_temp(sensors[i]));
190     }
191
192     return (0);
193 } /* dtt_init() */
194
195 int dtt_get_temp(int sensor)
196 {
197     int const ret = dtt_read(sensor, DTT_READ_TEMP);
198
199     if (ret < 0) {
200         printf("DTT temperature read failed.\n");
201         return 0;
202     }
203     return (int)((int16_t) ret / 256);
204 } /* dtt_get_temp() */