patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / i2c / chips / lm80.c
1 /*
2  * lm80.c - From lm_sensors, Linux kernel modules for hardware
3  * monitoring
4  * Copyright (C) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
5  * and Philip Edelbrock <phil@netroedge.com>
6  *
7  * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/i2c-sensor.h>
30
31 /* Addresses to scan */
32 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
33 static unsigned short normal_i2c_range[] = { 0x28, 0x2f, I2C_CLIENT_END };
34 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
35 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
36
37 /* Insmod parameters */
38 SENSORS_INSMOD_1(lm80);
39
40 /* Many LM80 constants specified below */
41
42 /* The LM80 registers */
43 #define LM80_REG_IN_MAX(nr)             (0x2a + (nr) * 2)
44 #define LM80_REG_IN_MIN(nr)             (0x2b + (nr) * 2)
45 #define LM80_REG_IN(nr)                 (0x20 + (nr))
46
47 #define LM80_REG_FAN1                   0x28
48 #define LM80_REG_FAN2                   0x29
49 #define LM80_REG_FAN_MIN(nr)            (0x3b + (nr))
50
51 #define LM80_REG_TEMP                   0x27
52 #define LM80_REG_TEMP_HOT_MAX           0x38
53 #define LM80_REG_TEMP_HOT_HYST          0x39
54 #define LM80_REG_TEMP_OS_MAX            0x3a
55 #define LM80_REG_TEMP_OS_HYST           0x3b
56
57 #define LM80_REG_CONFIG                 0x00
58 #define LM80_REG_ALARM1                 0x01
59 #define LM80_REG_ALARM2                 0x02
60 #define LM80_REG_MASK1                  0x03
61 #define LM80_REG_MASK2                  0x04
62 #define LM80_REG_FANDIV                 0x05
63 #define LM80_REG_RES                    0x06
64
65
66 /* Conversions. Rounding and limit checking is only done on the TO_REG
67    variants. Note that you should be a bit careful with which arguments
68    these macros are called: arguments may be evaluated more than once.
69    Fixing this is just not worth it. */
70
71 #define IN_TO_REG(val)          (SENSORS_LIMIT(((val)+5)/10,0,255))
72 #define IN_FROM_REG(val)        ((val)*10)
73
74 static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
75 {
76         if (rpm == 0)
77                 return 255;
78         rpm = SENSORS_LIMIT(rpm, 1, 1000000);
79         return SENSORS_LIMIT((1350000 + rpm*div / 2) / (rpm*div), 1, 254);
80 }
81
82 #define FAN_FROM_REG(val,div)   ((val)==0?-1:\
83                                 (val)==255?0:1350000/((div)*(val)))
84
85 static inline long TEMP_FROM_REG(u16 temp)
86 {
87         long res;
88
89         temp >>= 4;
90         if (temp < 0x0800)
91                 res = 625 * (long) temp;
92         else
93                 res = ((long) temp - 0x01000) * 625;
94
95         return res / 10;
96 }
97
98 #define TEMP_LIMIT_FROM_REG(val)        (((val)>0x80?(val)-0x100:(val))*1000)
99
100 #define TEMP_LIMIT_TO_REG(val)          SENSORS_LIMIT((val)<0?\
101                                         ((val)-500)/1000:((val)+500)/1000,0,255)
102
103 #define ALARMS_FROM_REG(val)            (val)
104
105 #define DIV_FROM_REG(val)               (1 << (val))
106 #define DIV_TO_REG(val)                 ((val)==8?3:(val)==4?2:(val)==1?0:1)
107
108 /*
109  * Client data (each client gets its own)
110  */
111
112 struct lm80_data {
113         struct i2c_client client;
114         struct semaphore update_lock;
115         char valid;             /* !=0 if following fields are valid */
116         unsigned long last_updated;     /* In jiffies */
117
118         u8 in[7];               /* Register value */
119         u8 in_max[7];           /* Register value */
120         u8 in_min[7];           /* Register value */
121         u8 fan[2];              /* Register value */
122         u8 fan_min[2];          /* Register value */
123         u8 fan_div[2];          /* Register encoding, shifted right */
124         u16 temp;               /* Register values, shifted right */
125         u8 temp_hot_max;        /* Register value */
126         u8 temp_hot_hyst;       /* Register value */
127         u8 temp_os_max;         /* Register value */
128         u8 temp_os_hyst;        /* Register value */
129         u16 alarms;             /* Register encoding, combined */
130 };
131
132 /* 
133  * Functions declaration
134  */
135
136 static int lm80_attach_adapter(struct i2c_adapter *adapter);
137 static int lm80_detect(struct i2c_adapter *adapter, int address, int kind);
138 static void lm80_init_client(struct i2c_client *client);
139 static int lm80_detach_client(struct i2c_client *client);
140 static struct lm80_data *lm80_update_device(struct device *dev);
141 static int lm80_read_value(struct i2c_client *client, u8 reg);
142 static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
143
144 /*
145  * Internal variables
146  */
147
148 static int lm80_id = 0;
149
150 /*
151  * Driver data (common to all clients)
152  */
153
154 static struct i2c_driver lm80_driver = {
155         .owner          = THIS_MODULE,
156         .name           = "lm80",
157         .id             = I2C_DRIVERID_LM80,
158         .flags          = I2C_DF_NOTIFY,
159         .attach_adapter = lm80_attach_adapter,
160         .detach_client  = lm80_detach_client,
161 };
162
163 /*
164  * Sysfs stuff
165  */
166
167 #define show_in(suffix, value) \
168 static ssize_t show_in_##suffix(struct device *dev, char *buf) \
169 { \
170         struct lm80_data *data = lm80_update_device(dev); \
171         return sprintf(buf, "%d\n", IN_FROM_REG(data->value)); \
172 }
173 show_in(min0, in_min[0]);
174 show_in(min1, in_min[1]);
175 show_in(min2, in_min[2]);
176 show_in(min3, in_min[3]);
177 show_in(min4, in_min[4]);
178 show_in(min5, in_min[5]);
179 show_in(min6, in_min[6]);
180 show_in(max0, in_max[0]);
181 show_in(max1, in_max[1]);
182 show_in(max2, in_max[2]);
183 show_in(max3, in_max[3]);
184 show_in(max4, in_max[4]);
185 show_in(max5, in_max[5]);
186 show_in(max6, in_max[6]);
187 show_in(input0, in[0]);
188 show_in(input1, in[1]);
189 show_in(input2, in[2]);
190 show_in(input3, in[3]);
191 show_in(input4, in[4]);
192 show_in(input5, in[5]);
193 show_in(input6, in[6]);
194
195 #define set_in(suffix, value, reg) \
196 static ssize_t set_in_##suffix(struct device *dev, const char *buf, \
197         size_t count) \
198 { \
199         struct i2c_client *client = to_i2c_client(dev); \
200         struct lm80_data *data = i2c_get_clientdata(client); \
201         long val = simple_strtol(buf, NULL, 10); \
202         data->value = IN_TO_REG(val); \
203         lm80_write_value(client, reg, data->value); \
204         return count; \
205 }
206 set_in(min0, in_min[0], LM80_REG_IN_MIN(0));
207 set_in(min1, in_min[1], LM80_REG_IN_MIN(1));
208 set_in(min2, in_min[2], LM80_REG_IN_MIN(2));
209 set_in(min3, in_min[3], LM80_REG_IN_MIN(3));
210 set_in(min4, in_min[4], LM80_REG_IN_MIN(4));
211 set_in(min5, in_min[5], LM80_REG_IN_MIN(5));
212 set_in(min6, in_min[6], LM80_REG_IN_MIN(6));
213 set_in(max0, in_max[0], LM80_REG_IN_MAX(0));
214 set_in(max1, in_max[1], LM80_REG_IN_MAX(1));
215 set_in(max2, in_max[2], LM80_REG_IN_MAX(2));
216 set_in(max3, in_max[3], LM80_REG_IN_MAX(3));
217 set_in(max4, in_max[4], LM80_REG_IN_MAX(4));
218 set_in(max5, in_max[5], LM80_REG_IN_MAX(5));
219 set_in(max6, in_max[6], LM80_REG_IN_MAX(6));
220
221 #define show_fan(suffix, value, div) \
222 static ssize_t show_fan_##suffix(struct device *dev, char *buf) \
223 { \
224         struct lm80_data *data = lm80_update_device(dev); \
225         return sprintf(buf, "%d\n", FAN_FROM_REG(data->value, \
226                        DIV_FROM_REG(data->div))); \
227 }
228 show_fan(min1, fan_min[0], fan_div[0]);
229 show_fan(min2, fan_min[1], fan_div[1]);
230 show_fan(input1, fan[0], fan_div[0]);
231 show_fan(input2, fan[1], fan_div[1]);
232
233 #define show_fan_div(suffix, value) \
234 static ssize_t show_fan_div##suffix(struct device *dev, char *buf) \
235 { \
236         struct lm80_data *data = lm80_update_device(dev); \
237         return sprintf(buf, "%d\n", DIV_FROM_REG(data->value)); \
238 }
239 show_fan_div(1, fan_div[0]);
240 show_fan_div(2, fan_div[1]);
241
242 #define set_fan(suffix, value, reg, div) \
243 static ssize_t set_fan_##suffix(struct device *dev, const char *buf, \
244         size_t count) \
245 { \
246         struct i2c_client *client = to_i2c_client(dev); \
247         struct lm80_data *data = i2c_get_clientdata(client); \
248         long val = simple_strtoul(buf, NULL, 10); \
249         data->value = FAN_TO_REG(val, DIV_FROM_REG(data->div)); \
250         lm80_write_value(client, reg, data->value); \
251         return count; \
252 }
253 set_fan(min1, fan_min[0], LM80_REG_FAN_MIN(1), fan_div[0]);
254 set_fan(min2, fan_min[1], LM80_REG_FAN_MIN(2), fan_div[1]);
255
256 /* Note: we save and restore the fan minimum here, because its value is
257    determined in part by the fan divisor.  This follows the principle of
258    least suprise; the user doesn't expect the fan minimum to change just
259    because the divisor changed. */
260 static ssize_t set_fan_div(struct device *dev, const char *buf,
261         size_t count, int nr)
262 {
263         struct i2c_client *client = to_i2c_client(dev);
264         struct lm80_data *data = i2c_get_clientdata(client);
265         unsigned long min;
266         u8 reg;
267
268         /* Save fan_min */
269         min = FAN_FROM_REG(data->fan_min[nr],
270                            DIV_FROM_REG(data->fan_div[nr]));
271
272         data->fan_div[nr] = DIV_TO_REG(simple_strtoul(buf, NULL, 10));
273
274         reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
275             | (data->fan_div[nr] << (2 * (nr + 1)));
276         lm80_write_value(client, LM80_REG_FANDIV, reg);
277
278         /* Restore fan_min */
279         data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
280         lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
281
282         return count;
283 }
284
285 #define set_fan_div(number) \
286 static ssize_t set_fan_div##number(struct device *dev, const char *buf, \
287         size_t count) \
288 { \
289         return set_fan_div(dev, buf, count, number - 1); \
290 }
291 set_fan_div(1);
292 set_fan_div(2);
293
294 static ssize_t show_temp_input1(struct device *dev, char *buf)
295 {
296         struct lm80_data *data = lm80_update_device(dev);
297         return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));
298 }
299
300 #define show_temp(suffix, value) \
301 static ssize_t show_temp_##suffix(struct device *dev, char *buf) \
302 { \
303         struct lm80_data *data = lm80_update_device(dev); \
304         return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
305 }
306 show_temp(hot_max, temp_hot_max);
307 show_temp(hot_hyst, temp_hot_hyst);
308 show_temp(os_max, temp_os_max);
309 show_temp(os_hyst, temp_os_hyst);
310
311 #define set_temp(suffix, value, reg) \
312 static ssize_t set_temp_##suffix(struct device *dev, const char *buf, \
313         size_t count) \
314 { \
315         struct i2c_client *client = to_i2c_client(dev); \
316         struct lm80_data *data = i2c_get_clientdata(client); \
317         long val = simple_strtoul(buf, NULL, 10); \
318         data->value = TEMP_LIMIT_TO_REG(val); \
319         lm80_write_value(client, reg, data->value); \
320         return count; \
321 }
322 set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
323 set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
324 set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
325 set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
326
327 static ssize_t show_alarms(struct device *dev, char *buf)
328 {
329         struct lm80_data *data = lm80_update_device(dev);
330         return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->alarms));
331 }
332
333 static DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min0, set_in_min0);
334 static DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min1, set_in_min1);
335 static DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min2, set_in_min2);
336 static DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min3, set_in_min3);
337 static DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min4, set_in_min4);
338 static DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min5, set_in_min5);
339 static DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min6, set_in_min6);
340 static DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max0, set_in_max0);
341 static DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max1, set_in_max1);
342 static DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max2, set_in_max2);
343 static DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max3, set_in_max3);
344 static DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max4, set_in_max4);
345 static DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max5, set_in_max5);
346 static DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max6, set_in_max6);
347 static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
348 static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
349 static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
350 static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
351 static DEVICE_ATTR(in4_input, S_IRUGO, show_in_input4, NULL);
352 static DEVICE_ATTR(in5_input, S_IRUGO, show_in_input5, NULL);
353 static DEVICE_ATTR(in6_input, S_IRUGO, show_in_input6, NULL);
354 static DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min1,
355     set_fan_min1);
356 static DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min2,
357     set_fan_min2);
358 static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL);
359 static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL);
360 static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div1, set_fan_div1);
361 static DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div2, set_fan_div2);
362 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
363 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
364     set_temp_hot_max);
365 static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
366     set_temp_hot_hyst);
367 static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
368     set_temp_os_max);
369 static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
370     set_temp_os_hyst);
371 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
372
373 /*
374  * Real code
375  */
376
377 static int lm80_attach_adapter(struct i2c_adapter *adapter)
378 {
379         if (!(adapter->class & I2C_CLASS_HWMON))
380                 return 0;
381         return i2c_detect(adapter, &addr_data, lm80_detect);
382 }
383
384 int lm80_detect(struct i2c_adapter *adapter, int address, int kind)
385 {
386         int i, cur;
387         struct i2c_client *new_client;
388         struct lm80_data *data;
389         int err = 0;
390         const char *name;
391
392         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
393                 goto exit;
394
395         /* OK. For now, we presume we have a valid client. We now create the
396            client structure, even though we cannot fill it completely yet.
397            But it allows us to access lm80_{read,write}_value. */
398         if (!(data = kmalloc(sizeof(struct lm80_data), GFP_KERNEL))) {
399                 err = -ENOMEM;
400                 goto exit;
401         }
402         memset(data, 0, sizeof(struct lm80_data));
403
404         new_client = &data->client;
405         i2c_set_clientdata(new_client, data);
406         new_client->addr = address;
407         new_client->adapter = adapter;
408         new_client->driver = &lm80_driver;
409         new_client->flags = 0;
410
411         /* Now, we do the remaining detection. It is lousy. */
412         if (lm80_read_value(new_client, LM80_REG_ALARM2) & 0xc0)
413                 goto error_free;
414         for (i = 0x2a; i <= 0x3d; i++) {
415                 cur = i2c_smbus_read_byte_data(new_client, i);
416                 if ((i2c_smbus_read_byte_data(new_client, i + 0x40) != cur)
417                  || (i2c_smbus_read_byte_data(new_client, i + 0x80) != cur)
418                  || (i2c_smbus_read_byte_data(new_client, i + 0xc0) != cur))
419                     goto error_free;
420         }
421
422         /* Determine the chip type - only one kind supported! */
423         kind = lm80;
424         name = "lm80";
425
426         /* Fill in the remaining client fields and put it into the global list */
427         strlcpy(new_client->name, name, I2C_NAME_SIZE);
428
429         new_client->id = lm80_id++;
430         data->valid = 0;
431         init_MUTEX(&data->update_lock);
432
433         /* Tell the I2C layer a new client has arrived */
434         if ((err = i2c_attach_client(new_client)))
435                 goto error_free;
436
437         /* Initialize the LM80 chip */
438         lm80_init_client(new_client);
439
440         /* A few vars need to be filled upon startup */
441         data->fan_min[0] = lm80_read_value(new_client, LM80_REG_FAN_MIN(1));
442         data->fan_min[1] = lm80_read_value(new_client, LM80_REG_FAN_MIN(2));
443
444         /* Register sysfs hooks */
445         device_create_file(&new_client->dev, &dev_attr_in0_min);
446         device_create_file(&new_client->dev, &dev_attr_in1_min);
447         device_create_file(&new_client->dev, &dev_attr_in2_min);
448         device_create_file(&new_client->dev, &dev_attr_in3_min);
449         device_create_file(&new_client->dev, &dev_attr_in4_min);
450         device_create_file(&new_client->dev, &dev_attr_in5_min);
451         device_create_file(&new_client->dev, &dev_attr_in6_min);
452         device_create_file(&new_client->dev, &dev_attr_in0_max);
453         device_create_file(&new_client->dev, &dev_attr_in1_max);
454         device_create_file(&new_client->dev, &dev_attr_in2_max);
455         device_create_file(&new_client->dev, &dev_attr_in3_max);
456         device_create_file(&new_client->dev, &dev_attr_in4_max);
457         device_create_file(&new_client->dev, &dev_attr_in5_max);
458         device_create_file(&new_client->dev, &dev_attr_in6_max);
459         device_create_file(&new_client->dev, &dev_attr_in0_input);
460         device_create_file(&new_client->dev, &dev_attr_in1_input);
461         device_create_file(&new_client->dev, &dev_attr_in2_input);
462         device_create_file(&new_client->dev, &dev_attr_in3_input);
463         device_create_file(&new_client->dev, &dev_attr_in4_input);
464         device_create_file(&new_client->dev, &dev_attr_in5_input);
465         device_create_file(&new_client->dev, &dev_attr_in6_input);
466         device_create_file(&new_client->dev, &dev_attr_fan1_min);
467         device_create_file(&new_client->dev, &dev_attr_fan2_min);
468         device_create_file(&new_client->dev, &dev_attr_fan1_input);
469         device_create_file(&new_client->dev, &dev_attr_fan2_input);
470         device_create_file(&new_client->dev, &dev_attr_fan1_div);
471         device_create_file(&new_client->dev, &dev_attr_fan2_div);
472         device_create_file(&new_client->dev, &dev_attr_temp1_input);
473         device_create_file(&new_client->dev, &dev_attr_temp1_max);
474         device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
475         device_create_file(&new_client->dev, &dev_attr_temp1_crit);
476         device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
477         device_create_file(&new_client->dev, &dev_attr_alarms);
478
479         return 0;
480
481 error_free:
482         kfree(data);
483 exit:
484         return err;
485 }
486
487 static int lm80_detach_client(struct i2c_client *client)
488 {
489         int err;
490
491         if ((err = i2c_detach_client(client))) {
492                 dev_err(&client->dev, "Client deregistration failed, "
493                         "client not detached.\n");
494                 return err;
495         }
496
497         kfree(i2c_get_clientdata(client));
498         return 0;
499 }
500
501 static int lm80_read_value(struct i2c_client *client, u8 reg)
502 {
503         return i2c_smbus_read_byte_data(client, reg);
504 }
505
506 static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
507 {
508         return i2c_smbus_write_byte_data(client, reg, value);
509 }
510
511 /* Called when we have found a new LM80. */
512 static void lm80_init_client(struct i2c_client *client)
513 {
514         /* Reset all except Watchdog values and last conversion values
515            This sets fan-divs to 2, among others. This makes most other
516            initializations unnecessary */
517         lm80_write_value(client, LM80_REG_CONFIG, 0x80);
518         /* Set 11-bit temperature resolution */
519         lm80_write_value(client, LM80_REG_RES, 0x08);
520
521         /* Start monitoring */
522         lm80_write_value(client, LM80_REG_CONFIG, 0x01);
523 }
524
525 static struct lm80_data *lm80_update_device(struct device *dev)
526 {
527         struct i2c_client *client = to_i2c_client(dev);
528         struct lm80_data *data = i2c_get_clientdata(client);
529         int i;
530
531         down(&data->update_lock);
532
533         if ((jiffies - data->last_updated > 2 * HZ) ||
534             (jiffies < data->last_updated) || !data->valid) {
535
536                 dev_dbg(&client->dev, "Starting lm80 update\n");
537                 for (i = 0; i <= 6; i++) {
538                         data->in[i] =
539                             lm80_read_value(client, LM80_REG_IN(i));
540                         data->in_min[i] =
541                             lm80_read_value(client, LM80_REG_IN_MIN(i));
542                         data->in_max[i] =
543                             lm80_read_value(client, LM80_REG_IN_MAX(i));
544                 }
545                 data->fan[0] = lm80_read_value(client, LM80_REG_FAN1);
546                 data->fan_min[0] =
547                     lm80_read_value(client, LM80_REG_FAN_MIN(1));
548                 data->fan[1] = lm80_read_value(client, LM80_REG_FAN2);
549                 data->fan_min[1] =
550                     lm80_read_value(client, LM80_REG_FAN_MIN(2));
551
552                 data->temp =
553                     (lm80_read_value(client, LM80_REG_TEMP) << 8) |
554                     (lm80_read_value(client, LM80_REG_RES) & 0xf0);
555                 data->temp_os_max =
556                     lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
557                 data->temp_os_hyst =
558                     lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
559                 data->temp_hot_max =
560                     lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
561                 data->temp_hot_hyst =
562                     lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
563
564                 i = lm80_read_value(client, LM80_REG_FANDIV);
565                 data->fan_div[0] = (i >> 2) & 0x03;
566                 data->fan_div[1] = (i >> 4) & 0x03;
567                 data->alarms = lm80_read_value(client, LM80_REG_ALARM1) +
568                     (lm80_read_value(client, LM80_REG_ALARM2) << 8);
569                 data->last_updated = jiffies;
570                 data->valid = 1;
571         }
572
573         up(&data->update_lock);
574
575         return data;
576 }
577
578 static int __init sensors_lm80_init(void)
579 {
580         return i2c_add_driver(&lm80_driver);
581 }
582
583 static void __exit sensors_lm80_exit(void)
584 {
585         i2c_del_driver(&lm80_driver);
586 }
587
588 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
589         "Philip Edelbrock <phil@netroedge.com>");
590 MODULE_DESCRIPTION("LM80 driver");
591 MODULE_LICENSE("GPL");
592
593 module_init(sensors_lm80_init);
594 module_exit(sensors_lm80_exit);