Add driver for National Semiconductor LM73 temperature sensor
[oweals/u-boot.git] / drivers / hwmon / lm73.c
1 /*
2  * (C) Copyright 2007
3  * Larry Johnson, lrj@acm.org
4  *
5  * based on dtt/lm75.c which is ...
6  *
7  * (C) Copyright 2001
8  * Bill Hunter,  Wave 7 Optics, williamhunter@mediaone.net
9  *
10  * See file CREDITS for list of people who contributed to this
11  * project.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of
16  * the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26  * MA 02111-1307 USA
27  */
28
29 /*
30  * National Semiconductor LM73 Temperature Sensor
31  */
32
33 #include <common.h>
34
35 #ifdef CONFIG_DTT_LM73
36 #if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \
37         (CFG_EEPROM_PAGE_WRITE_BITS < 1)
38 # error "CFG_EEPROM_PAGE_WRITE_ENABLE must be defined and CFG_EEPROM_PAGE_WRITE_BITS must be greater than  1 to use CONFIG_DTT_LM73"
39 #endif
40
41 #include <i2c.h>
42 #include <dtt.h>
43
44 /*
45  * Device code
46  */
47 #define DTT_I2C_DEV_CODE 0x48   /* National Semi's LM73 device */
48
49 int dtt_read(int sensor, int reg)
50 {
51         int dlen;
52         uchar data[2];
53
54         /*
55          * Validate 'reg' param and get register size.
56          */
57         switch (reg) {
58         case DTT_CONFIG:
59         case DTT_CONTROL:
60                 dlen = 1;
61                 break;
62         case DTT_READ_TEMP:
63         case DTT_TEMP_HIGH:
64         case DTT_TEMP_LOW:
65         case DTT_ID:
66                 dlen = 2;
67                 break;
68         default:
69                 return -1;
70         }
71         /*
72          * Calculate sensor address and register.
73          */
74         sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);    /* calculate LM73 addr */
75         /*
76          * Now try to read the register.
77          */
78         if (i2c_read(sensor, reg, 1, data, dlen) != 0)
79                 return -1;
80         /*
81          * Handle 2 byte result.
82          */
83         if (2 == dlen)
84                 return ((int)((short)data[1] + (((short)data[0]) << 8)));
85
86         return (int)data[0];
87 } /* dtt_read() */
88
89 int dtt_write(int sensor, int reg, int val)
90 {
91         int dlen;
92         uchar data[2];
93
94         /*
95          * Validate 'reg' param and handle register size
96          */
97         switch (reg) {
98         case DTT_CONFIG:
99         case DTT_CONTROL:
100                 dlen = 1;
101                 data[0] = (char)(val & 0xff);
102                 break;
103         case DTT_TEMP_HIGH:
104         case DTT_TEMP_LOW:
105                 dlen = 2;
106                 data[0] = (char)((val >> 8) & 0xff);    /* MSB first */
107                 data[1] = (char)(val & 0xff);
108                 break;
109         default:
110                 return -1;
111         }
112         /*
113          * Calculate sensor address and register.
114          */
115         sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);    /* calculate LM73 addr */
116         /*
117          * Write value to register.
118          */
119         return i2c_write(sensor, reg, 1, data, dlen) != 0;
120 } /* dtt_write() */
121
122 static int _dtt_init(int sensor)
123 {
124         int val;
125
126         /*
127          * Validate the Identification register
128          */
129         if (0x0190 != dtt_read(sensor, DTT_ID))
130                 return 1;
131         /*
132          * Setup THIGH (upper-limit) and TLOW (lower-limit) registers
133          */
134         val = CFG_DTT_MAX_TEMP << 7;
135         if (dtt_write(sensor, DTT_TEMP_HIGH, val))
136                 return 1;
137
138         val = CFG_DTT_MIN_TEMP << 7;
139         if (dtt_write(sensor, DTT_TEMP_LOW, val))
140                 return 1;
141         /*
142          * Setup configuraton register
143          */
144         /* config = alert active low, disabled, and reset */
145         val = 0x64;
146         if (dtt_write(sensor, DTT_CONFIG, val))
147                 return 1;
148         /*
149          * Setup control/status register
150          */
151         /* control = temp resolution 0.25C */
152         val = 0x00;
153         if (dtt_write(sensor, DTT_CONTROL, val))
154                 return 1;
155
156         dtt_read(sensor, DTT_CONTROL);  /* clear temperature flags */
157         return 0;
158 } /* _dtt_init() */
159
160 int dtt_init(void)
161 {
162         int i;
163         unsigned char sensors[] = CONFIG_DTT_SENSORS;
164         const char *const header = "DTT:   ";
165
166         for (i = 0; i < sizeof(sensors); i++) {
167                 if (_dtt_init(sensors[i]) != 0)
168                         printf("%s%d FAILED INIT\n", header, i + 1);
169                 else
170                         printf("%s%d is %i C\n", header, i + 1,
171                                dtt_get_temp(sensors[i]));
172         }
173         return 0;
174 } /* dtt_init() */
175
176 int dtt_get_temp(int sensor)
177 {
178         return (dtt_read(sensor, DTT_READ_TEMP) + 0x0040) >> 7;
179 } /* dtt_get_temp() */
180
181 #endif /* CONFIG_DTT_LM73 */