This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / i2c / chips / pc87360.c
1 /*
2  *  pc87360.c - Part of lm_sensors, Linux kernel modules
3  *              for hardware monitoring
4  *  Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
5  *
6  *  Copied from smsc47m1.c:
7  *  Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
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  *  Supports the following chips:
24  *
25  *  Chip        #vin    #fan    #pwm    #temp   devid
26  *  PC87360     -       2       2       -       0xE1
27  *  PC87363     -       2       2       -       0xE8
28  *  PC87364     -       3       3       -       0xE4
29  *  PC87365     11      3       3       2       0xE5
30  *  PC87366     11      3       3       3-4     0xE9
31  *
32  *  This driver assumes that no more than one chip is present, and one of
33  *  the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
34  */
35
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/slab.h>
40 #include <linux/i2c.h>
41 #include <linux/i2c-sensor.h>
42 #include <linux/i2c-vid.h>
43 #include <asm/io.h>
44
45 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
46 static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
47 static struct i2c_force_data forces[] = {{ NULL }};
48 static u8 devid;
49 static unsigned int extra_isa[3];
50 static u8 confreg[4];
51
52 enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
53 static struct i2c_address_data addr_data = {
54         .normal_i2c             = normal_i2c,
55         .normal_isa             = normal_isa,
56         .forces                 = forces,
57 };
58
59 static int init = 1;
60 module_param(init, int, 0);
61 MODULE_PARM_DESC(init,
62  "Chip initialization level:\n"
63  " 0: None\n"
64  "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
65  " 2: Forcibly enable all voltage and temperature channels, except in9\n"
66  " 3: Forcibly enable all voltage and temperature channels, including in9");
67
68 /*
69  * Super-I/O registers and operations
70  */
71
72 #define DEV     0x07    /* Register: Logical device select */
73 #define DEVID   0x20    /* Register: Device ID */
74 #define ACT     0x30    /* Register: Device activation */
75 #define BASE    0x60    /* Register: Base address */
76
77 #define FSCM    0x09    /* Logical device: fans */
78 #define VLM     0x0d    /* Logical device: voltages */
79 #define TMS     0x0e    /* Logical device: temperatures */
80 static const u8 logdev[3] = { FSCM, VLM, TMS };
81
82 #define LD_FAN          0
83 #define LD_IN           1
84 #define LD_TEMP         2
85
86 static inline void superio_outb(int sioaddr, int reg, int val)
87 {
88         outb(reg, sioaddr);
89         outb(val, sioaddr+1);
90 }
91
92 static inline int superio_inb(int sioaddr, int reg)
93 {
94         outb(reg, sioaddr);
95         return inb(sioaddr+1);
96 }
97
98 static inline void superio_exit(int sioaddr)
99 {
100         outb(0x02, sioaddr);
101         outb(0x02, sioaddr+1);
102 }
103
104 /*
105  * Logical devices
106  */
107
108 #define PC87360_EXTENT          0x10
109 #define PC87365_REG_BANK        0x09
110 #define NO_BANK                 0xff
111
112 /*
113  * Fan registers and conversions
114  */
115
116 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
117 #define PC87360_REG_PRESCALE(nr)        (0x00 + 2 * (nr))
118 #define PC87360_REG_PWM(nr)             (0x01 + 2 * (nr))
119 #define PC87360_REG_FAN_MIN(nr)         (0x06 + 3 * (nr))
120 #define PC87360_REG_FAN(nr)             (0x07 + 3 * (nr))
121 #define PC87360_REG_FAN_STATUS(nr)      (0x08 + 3 * (nr))
122
123 #define FAN_FROM_REG(val,div)           ((val) == 0 ? 0: \
124                                          480000 / ((val)*(div)))
125 #define FAN_TO_REG(val,div)             ((val) <= 100 ? 0 : \
126                                          480000 / ((val)*(div)))
127 #define FAN_DIV_FROM_REG(val)           (1 << ((val >> 5) & 0x03))
128 #define FAN_STATUS_FROM_REG(val)        ((val) & 0x07)
129
130 #define FAN_CONFIG_MONITOR(val,nr)      (((val) >> (2 + nr * 3)) & 1)
131 #define FAN_CONFIG_CONTROL(val,nr)      (((val) >> (3 + nr * 3)) & 1)
132 #define FAN_CONFIG_INVERT(val,nr)       (((val) >> (4 + nr * 3)) & 1)
133
134 #define PWM_FROM_REG(val,inv)           ((inv) ? 255 - (val) : (val))
135 static inline u8 PWM_TO_REG(int val, int inv)
136 {
137         if (inv)
138                 val = 255 - val;
139         if (val < 0)
140                 return 0;
141         if (val > 255)
142                 return 255;
143         return val;
144 }
145
146 /*
147  * Voltage registers and conversions
148  */
149
150 #define PC87365_REG_IN_CONVRATE         0x07
151 #define PC87365_REG_IN_CONFIG           0x08
152 #define PC87365_REG_IN                  0x0B
153 #define PC87365_REG_IN_MIN              0x0D
154 #define PC87365_REG_IN_MAX              0x0C
155 #define PC87365_REG_IN_STATUS           0x0A
156 #define PC87365_REG_IN_ALARMS1          0x00
157 #define PC87365_REG_IN_ALARMS2          0x01
158 #define PC87365_REG_VID                 0x06
159
160 #define IN_FROM_REG(val,ref)            (((val) * (ref) + 128) / 256)
161 #define IN_TO_REG(val,ref)              ((val) < 0 ? 0 : \
162                                          (val)*256 >= (ref)*255 ? 255: \
163                                          ((val) * 256 + (ref)/2) / (ref))
164
165 /*
166  * Temperature registers and conversions
167  */
168
169 #define PC87365_REG_TEMP_CONFIG         0x08
170 #define PC87365_REG_TEMP                0x0B
171 #define PC87365_REG_TEMP_MIN            0x0D
172 #define PC87365_REG_TEMP_MAX            0x0C
173 #define PC87365_REG_TEMP_CRIT           0x0E
174 #define PC87365_REG_TEMP_STATUS         0x0A
175 #define PC87365_REG_TEMP_ALARMS         0x00
176
177 #define TEMP_FROM_REG(val)              ((val) * 1000)
178 #define TEMP_TO_REG(val)                ((val) < -55000 ? -55 : \
179                                          (val) > 127000 ? 127 : \
180                                          (val) < 0 ? ((val) - 500) / 1000 : \
181                                          ((val) + 500) / 1000)
182
183 /*
184  * Client data (each client gets its own)
185  */
186
187 struct pc87360_data {
188         struct i2c_client client;
189         struct semaphore lock;
190         struct semaphore update_lock;
191         char valid;             /* !=0 if following fields are valid */
192         unsigned long last_updated;     /* In jiffies */
193
194         int address[3];
195
196         u8 fannr, innr, tempnr;
197
198         u8 fan[3];              /* Register value */
199         u8 fan_min[3];          /* Register value */
200         u8 fan_status[3];       /* Register value */
201         u8 pwm[3];              /* Register value */
202         u16 fan_conf;           /* Configuration register values, combined */
203
204         u16 in_vref;            /* 1 mV/bit */
205         u8 in[14];              /* Register value */
206         u8 in_min[14];          /* Register value */
207         u8 in_max[14];          /* Register value */
208         u8 in_crit[3];          /* Register value */
209         u8 in_status[14];       /* Register value */
210         u16 in_alarms;          /* Register values, combined, masked */
211         u8 vid_conf;            /* Configuration register value */
212         u8 vrm;
213         u8 vid;                 /* Register value */
214
215         s8 temp[3];             /* Register value */
216         s8 temp_min[3];         /* Register value */
217         s8 temp_max[3];         /* Register value */
218         s8 temp_crit[3];        /* Register value */
219         u8 temp_status[3];      /* Register value */
220         u8 temp_alarms;         /* Register value, masked */
221 };
222
223 /*
224  * Functions declaration
225  */
226
227 static int pc87360_attach_adapter(struct i2c_adapter *adapter);
228 static int pc87360_detect(struct i2c_adapter *adapter, int address, int kind);
229 static int pc87360_detach_client(struct i2c_client *client);
230
231 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
232                               u8 reg);
233 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
234                                 u8 reg, u8 value);
235 static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
236 static struct pc87360_data *pc87360_update_device(struct device *dev);
237
238 /*
239  * Driver data (common to all clients)
240  */
241
242 static struct i2c_driver pc87360_driver = {
243         .owner          = THIS_MODULE,
244         .name           = "pc87360",
245         .flags          = I2C_DF_NOTIFY,
246         .attach_adapter = pc87360_attach_adapter,
247         .detach_client  = pc87360_detach_client,
248 };
249
250 /*
251  * Sysfs stuff
252  */
253
254 static ssize_t set_fan_min(struct device *dev, const char *buf,
255         size_t count, int nr)
256 {
257         struct i2c_client *client = to_i2c_client(dev);
258         struct pc87360_data *data = i2c_get_clientdata(client);
259         long fan_min = simple_strtol(buf, NULL, 10);
260
261         fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[nr]));
262
263         /* If it wouldn't fit, change clock divisor */
264         while (fan_min > 255
265             && (data->fan_status[nr] & 0x60) != 0x60) {
266                 fan_min >>= 1;
267                 data->fan[nr] >>= 1;
268                 data->fan_status[nr] += 0x20;
269         }
270         data->fan_min[nr] = fan_min > 255 ? 255 : fan_min;
271         pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(nr),
272                             data->fan_min[nr]);
273
274         /* Write new divider, preserve alarm bits */
275         pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(nr),
276                             data->fan_status[nr] & 0xF9);
277
278         return count;
279 }
280
281 #define show_and_set_fan(offset) \
282 static ssize_t show_fan##offset##_input(struct device *dev, char *buf) \
283 { \
284         struct pc87360_data *data = pc87360_update_device(dev); \
285         return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[offset-1], \
286                        FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
287 } \
288 static ssize_t show_fan##offset##_min(struct device *dev, char *buf) \
289 { \
290         struct pc87360_data *data = pc87360_update_device(dev); \
291         return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[offset-1], \
292                        FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
293 } \
294 static ssize_t show_fan##offset##_div(struct device *dev, char *buf) \
295 { \
296         struct pc87360_data *data = pc87360_update_device(dev); \
297         return sprintf(buf, "%u\n", \
298                        FAN_DIV_FROM_REG(data->fan_status[offset-1])); \
299 } \
300 static ssize_t show_fan##offset##_status(struct device *dev, char *buf) \
301 { \
302         struct pc87360_data *data = pc87360_update_device(dev); \
303         return sprintf(buf, "%u\n", \
304                        FAN_STATUS_FROM_REG(data->fan_status[offset-1])); \
305 } \
306 static ssize_t set_fan##offset##_min(struct device *dev, const char *buf, \
307         size_t count) \
308 { \
309         return set_fan_min(dev, buf, count, offset-1); \
310 } \
311 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
312         show_fan##offset##_input, NULL); \
313 static DEVICE_ATTR(fan##offset##_min, S_IWUSR | S_IRUGO, \
314         show_fan##offset##_min, set_fan##offset##_min); \
315 static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
316         show_fan##offset##_div, NULL); \
317 static DEVICE_ATTR(fan##offset##_status, S_IRUGO, \
318         show_fan##offset##_status, NULL);
319 show_and_set_fan(1)
320 show_and_set_fan(2)
321 show_and_set_fan(3)
322
323 #define show_and_set_pwm(offset) \
324 static ssize_t show_pwm##offset(struct device *dev, char *buf) \
325 { \
326         struct pc87360_data *data = pc87360_update_device(dev); \
327         return sprintf(buf, "%u\n", \
328                        PWM_FROM_REG(data->pwm[offset-1], \
329                                     FAN_CONFIG_INVERT(data->fan_conf, \
330                                                       offset-1))); \
331 } \
332 static ssize_t set_pwm##offset(struct device *dev, const char *buf, \
333         size_t count) \
334 { \
335         struct i2c_client *client = to_i2c_client(dev); \
336         struct pc87360_data *data = i2c_get_clientdata(client); \
337         long val = simple_strtol(buf, NULL, 10); \
338         data->pwm[offset-1] = PWM_TO_REG(val, \
339                               FAN_CONFIG_INVERT(data->fan_conf, offset-1)); \
340         pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(offset-1), \
341                             data->pwm[offset-1]); \
342         return count; \
343 } \
344 static DEVICE_ATTR(pwm##offset, S_IWUSR | S_IRUGO, \
345         show_pwm##offset, set_pwm##offset);
346 show_and_set_pwm(1)
347 show_and_set_pwm(2)
348 show_and_set_pwm(3)
349
350 #define show_and_set_in(offset) \
351 static ssize_t show_in##offset##_input(struct device *dev, char *buf) \
352 { \
353         struct pc87360_data *data = pc87360_update_device(dev); \
354         return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
355                        data->in_vref)); \
356 } \
357 static ssize_t show_in##offset##_min(struct device *dev, char *buf) \
358 { \
359         struct pc87360_data *data = pc87360_update_device(dev); \
360         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
361                        data->in_vref)); \
362 } \
363 static ssize_t show_in##offset##_max(struct device *dev, char *buf) \
364 { \
365         struct pc87360_data *data = pc87360_update_device(dev); \
366         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
367                        data->in_vref)); \
368 } \
369 static ssize_t show_in##offset##_status(struct device *dev, char *buf) \
370 { \
371         struct pc87360_data *data = pc87360_update_device(dev); \
372         return sprintf(buf, "%u\n", data->in_status[offset]); \
373 } \
374 static ssize_t set_in##offset##_min(struct device *dev, const char *buf, \
375         size_t count) \
376 { \
377         struct i2c_client *client = to_i2c_client(dev); \
378         struct pc87360_data *data = i2c_get_clientdata(client); \
379         long val = simple_strtol(buf, NULL, 10); \
380         data->in_min[offset] = IN_TO_REG(val, data->in_vref); \
381         pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MIN, \
382                             data->in_min[offset]); \
383         return count; \
384 } \
385 static ssize_t set_in##offset##_max(struct device *dev, const char *buf, \
386         size_t count) \
387 { \
388         struct i2c_client *client = to_i2c_client(dev); \
389         struct pc87360_data *data = i2c_get_clientdata(client); \
390         long val = simple_strtol(buf, NULL, 10); \
391         data->in_max[offset] = IN_TO_REG(val, \
392                                data->in_vref); \
393         pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MAX, \
394                             data->in_max[offset]); \
395         return count; \
396 } \
397 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
398         show_in##offset##_input, NULL); \
399 static DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
400         show_in##offset##_min, set_in##offset##_min); \
401 static DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
402         show_in##offset##_max, set_in##offset##_max); \
403 static DEVICE_ATTR(in##offset##_status, S_IRUGO, \
404         show_in##offset##_status, NULL);
405 show_and_set_in(0)
406 show_and_set_in(1)
407 show_and_set_in(2)
408 show_and_set_in(3)
409 show_and_set_in(4)
410 show_and_set_in(5)
411 show_and_set_in(6)
412 show_and_set_in(7)
413 show_and_set_in(8)
414 show_and_set_in(9)
415 show_and_set_in(10)
416
417 #define show_and_set_therm(offset) \
418 static ssize_t show_temp##offset##_input(struct device *dev, char *buf) \
419 { \
420         struct pc87360_data *data = pc87360_update_device(dev); \
421         return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset+7], \
422                        data->in_vref)); \
423 } \
424 static ssize_t show_temp##offset##_min(struct device *dev, char *buf) \
425 { \
426         struct pc87360_data *data = pc87360_update_device(dev); \
427         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset+7], \
428                        data->in_vref)); \
429 } \
430 static ssize_t show_temp##offset##_max(struct device *dev, char *buf) \
431 { \
432         struct pc87360_data *data = pc87360_update_device(dev); \
433         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset+7], \
434                        data->in_vref)); \
435 } \
436 static ssize_t show_temp##offset##_crit(struct device *dev, char *buf) \
437 { \
438         struct pc87360_data *data = pc87360_update_device(dev); \
439         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[offset-4], \
440                        data->in_vref)); \
441 } \
442 static ssize_t show_temp##offset##_status(struct device *dev, char *buf) \
443 { \
444         struct pc87360_data *data = pc87360_update_device(dev); \
445         return sprintf(buf, "%u\n", data->in_status[offset+7]); \
446 } \
447 static ssize_t set_temp##offset##_min(struct device *dev, const char *buf, \
448         size_t count) \
449 { \
450         struct i2c_client *client = to_i2c_client(dev); \
451         struct pc87360_data *data = i2c_get_clientdata(client); \
452         long val = simple_strtol(buf, NULL, 10); \
453         data->in_min[offset+7] = IN_TO_REG(val, data->in_vref); \
454         pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MIN, \
455                             data->in_min[offset+7]); \
456         return count; \
457 } \
458 static ssize_t set_temp##offset##_max(struct device *dev, const char *buf, \
459         size_t count) \
460 { \
461         struct i2c_client *client = to_i2c_client(dev); \
462         struct pc87360_data *data = i2c_get_clientdata(client); \
463         long val = simple_strtol(buf, NULL, 10); \
464         data->in_max[offset+7] = IN_TO_REG(val, data->in_vref); \
465         pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MAX, \
466                             data->in_max[offset+7]); \
467         return count; \
468 } \
469 static ssize_t set_temp##offset##_crit(struct device *dev, const char *buf, \
470         size_t count) \
471 { \
472         struct i2c_client *client = to_i2c_client(dev); \
473         struct pc87360_data *data = i2c_get_clientdata(client); \
474         long val = simple_strtol(buf, NULL, 10); \
475         data->in_crit[offset-4] = IN_TO_REG(val, data->in_vref); \
476         pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_CRIT, \
477                             data->in_crit[offset-4]); \
478         return count; \
479 } \
480 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
481         show_temp##offset##_input, NULL); \
482 static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
483         show_temp##offset##_min, set_temp##offset##_min); \
484 static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
485         show_temp##offset##_max, set_temp##offset##_max); \
486 static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
487         show_temp##offset##_crit, set_temp##offset##_crit); \
488 static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
489         show_temp##offset##_status, NULL);
490 show_and_set_therm(4)
491 show_and_set_therm(5)
492 show_and_set_therm(6)
493
494 static ssize_t show_vid(struct device *dev, char *buf)
495 {
496         struct pc87360_data *data = pc87360_update_device(dev);
497         return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
498 }
499 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
500
501 static ssize_t show_vrm(struct device *dev, char *buf)
502 {
503         struct pc87360_data *data = pc87360_update_device(dev);
504         return sprintf(buf, "%u\n", data->vrm);
505 }
506 static ssize_t set_vrm(struct device *dev, const char *buf, size_t count)
507 {
508         struct i2c_client *client = to_i2c_client(dev);
509         struct pc87360_data *data = i2c_get_clientdata(client);
510         data->vrm = simple_strtoul(buf, NULL, 10);
511         return count;
512 }
513 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
514
515 static ssize_t show_in_alarms(struct device *dev, char *buf)
516 {
517         struct pc87360_data *data = pc87360_update_device(dev);
518         return sprintf(buf, "%u\n", data->in_alarms);
519 }
520 static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
521
522 #define show_and_set_temp(offset) \
523 static ssize_t show_temp##offset##_input(struct device *dev, char *buf) \
524 { \
525         struct pc87360_data *data = pc87360_update_device(dev); \
526         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
527 } \
528 static ssize_t show_temp##offset##_min(struct device *dev, char *buf) \
529 { \
530         struct pc87360_data *data = pc87360_update_device(dev); \
531         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
532 } \
533 static ssize_t show_temp##offset##_max(struct device *dev, char *buf) \
534 { \
535         struct pc87360_data *data = pc87360_update_device(dev); \
536         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
537 }\
538 static ssize_t show_temp##offset##_crit(struct device *dev, char *buf) \
539 { \
540         struct pc87360_data *data = pc87360_update_device(dev); \
541         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[offset-1])); \
542 }\
543 static ssize_t show_temp##offset##_status(struct device *dev, char *buf) \
544 { \
545         struct pc87360_data *data = pc87360_update_device(dev); \
546         return sprintf(buf, "%d\n", data->temp_status[offset-1]); \
547 }\
548 static ssize_t set_temp##offset##_min(struct device *dev, const char *buf, \
549         size_t count) \
550 { \
551         struct i2c_client *client = to_i2c_client(dev); \
552         struct pc87360_data *data = i2c_get_clientdata(client); \
553         long val = simple_strtol(buf, NULL, 10); \
554         data->temp_min[offset-1] = TEMP_TO_REG(val); \
555         pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MIN, \
556                             data->temp_min[offset-1]); \
557         return count; \
558 } \
559 static ssize_t set_temp##offset##_max(struct device *dev, const char *buf, \
560         size_t count) \
561 { \
562         struct i2c_client *client = to_i2c_client(dev); \
563         struct pc87360_data *data = i2c_get_clientdata(client); \
564         long val = simple_strtol(buf, NULL, 10); \
565         data->temp_max[offset-1] = TEMP_TO_REG(val); \
566         pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MAX, \
567                             data->temp_max[offset-1]); \
568         return count; \
569 } \
570 static ssize_t set_temp##offset##_crit(struct device *dev, const char *buf, \
571         size_t count) \
572 { \
573         struct i2c_client *client = to_i2c_client(dev); \
574         struct pc87360_data *data = i2c_get_clientdata(client); \
575         long val = simple_strtol(buf, NULL, 10); \
576         data->temp_crit[offset-1] = TEMP_TO_REG(val); \
577         pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_CRIT, \
578                             data->temp_crit[offset-1]); \
579         return count; \
580 } \
581 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
582         show_temp##offset##_input, NULL); \
583 static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
584         show_temp##offset##_min, set_temp##offset##_min); \
585 static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
586         show_temp##offset##_max, set_temp##offset##_max); \
587 static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
588         show_temp##offset##_crit, set_temp##offset##_crit); \
589 static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
590         show_temp##offset##_status, NULL);
591 show_and_set_temp(1)
592 show_and_set_temp(2)
593 show_and_set_temp(3)
594
595 static ssize_t show_temp_alarms(struct device *dev, char *buf)
596 {
597         struct pc87360_data *data = pc87360_update_device(dev);
598         return sprintf(buf, "%u\n", data->temp_alarms);
599 }
600 static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
601
602 /*
603  * Device detection, registration and update
604  */
605
606 static int pc87360_attach_adapter(struct i2c_adapter *adapter)
607 {
608         return i2c_detect(adapter, &addr_data, pc87360_detect);
609 }
610
611 static int pc87360_find(int sioaddr, u8 *devid, int *address)
612 {
613         u16 val;
614         int i;
615         int nrdev; /* logical device count */
616
617         /* No superio_enter */
618
619         /* Identify device */
620         val = superio_inb(sioaddr, DEVID);
621         switch (val) {
622         case 0xE1: /* PC87360 */
623         case 0xE8: /* PC87363 */
624         case 0xE4: /* PC87364 */
625                 nrdev = 1;
626                 break;
627         case 0xE5: /* PC87365 */
628         case 0xE9: /* PC87366 */
629                 nrdev = 3;
630                 break;
631         default:
632                 superio_exit(sioaddr);
633                 return -ENODEV;
634         }
635         /* Remember the device id */
636         *devid = val;
637
638         for (i = 0; i < nrdev; i++) {
639                 /* select logical device */
640                 superio_outb(sioaddr, DEV, logdev[i]);
641
642                 val = superio_inb(sioaddr, ACT);
643                 if (!(val & 0x01)) {
644                         printk(KERN_INFO "pc87360: Device 0x%02x not "
645                                "activated\n", logdev[i]);
646                         continue;
647                 }
648
649                 val = (superio_inb(sioaddr, BASE) << 8)
650                     | superio_inb(sioaddr, BASE + 1);
651                 if (!val) {
652                         printk(KERN_INFO "pc87360: Base address not set for "
653                                "device 0x%02x\n", logdev[i]);
654                         continue;
655                 }
656
657                 address[i] = val;
658
659                 if (i==0) { /* Fans */
660                         confreg[0] = superio_inb(sioaddr, 0xF0);
661                         confreg[1] = superio_inb(sioaddr, 0xF1);
662
663 #ifdef DEBUG
664                         printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
665                                "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
666                                (confreg[0]>>3)&1, (confreg[0]>>4)&1);
667                         printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
668                                "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
669                                (confreg[0]>>6)&1, (confreg[0]>>7)&1);
670                         printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
671                                "ctrl=%d inv=%d\n", confreg[1]&1,
672                                (confreg[1]>>1)&1, (confreg[1]>>2)&1);
673 #endif
674                 } else if (i==1) { /* Voltages */
675                         /* Are we using thermistors? */
676                         if (*devid == 0xE9) { /* PC87366 */
677                                 /* These registers are not logical-device
678                                    specific, just that we won't need them if
679                                    we don't use the VLM device */
680                                 confreg[2] = superio_inb(sioaddr, 0x2B);
681                                 confreg[3] = superio_inb(sioaddr, 0x25);
682
683                                 if (confreg[2] & 0x40) {
684                                         printk(KERN_INFO "pc87360: Using "
685                                                "thermistors for temperature "
686                                                "monitoring\n");
687                                 }
688                                 if (confreg[3] & 0xE0) {
689                                         printk(KERN_INFO "pc87360: VID "
690                                                "inputs routed (mode %u)\n",
691                                                confreg[3] >> 5);
692                                 }
693                         }
694                 }
695         }
696
697         superio_exit(sioaddr);
698         return 0;
699 }
700
701 /* We don't really care about the address.
702    Read from extra_isa instead. */
703 int pc87360_detect(struct i2c_adapter *adapter, int address, int kind)
704 {
705         int i;
706         struct i2c_client *new_client;
707         struct pc87360_data *data;
708         int err = 0;
709         const char *name = "pc87360";
710         int use_thermistors = 0;
711
712         if (!i2c_is_isa_adapter(adapter))
713                 return -ENODEV;
714
715         if (!(data = kmalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
716                 return -ENOMEM;
717         memset(data, 0x00, sizeof(struct pc87360_data));
718
719         new_client = &data->client;
720         i2c_set_clientdata(new_client, data);
721         new_client->addr = address;
722         init_MUTEX(&data->lock);
723         new_client->adapter = adapter;
724         new_client->driver = &pc87360_driver;
725         new_client->flags = 0;
726
727         data->fannr = 2;
728         data->innr = 0;
729         data->tempnr = 0;
730
731         switch (devid) {
732         case 0xe8:
733                 name = "pc87363";
734                 break;
735         case 0xe4:
736                 name = "pc87364";
737                 data->fannr = 3;
738                 break;
739         case 0xe5:
740                 name = "pc87365";
741                 data->fannr = extra_isa[0] ? 3 : 0;
742                 data->innr = extra_isa[1] ? 11 : 0;
743                 data->tempnr = extra_isa[2] ? 2 : 0;
744                 break;
745         case 0xe9:
746                 name = "pc87366";
747                 data->fannr = extra_isa[0] ? 3 : 0;
748                 data->innr = extra_isa[1] ? 14 : 0;
749                 data->tempnr = extra_isa[2] ? 3 : 0;
750                 break;
751         }
752
753         strcpy(new_client->name, name);
754         data->valid = 0;
755         init_MUTEX(&data->update_lock);
756
757         for (i = 0; i < 3; i++) {
758                 if (((data->address[i] = extra_isa[i]))
759                  && !request_region(extra_isa[i], PC87360_EXTENT, "pc87360")) {
760                         dev_err(&new_client->dev, "Region 0x%x-0x%x already "
761                                 "in use!\n", extra_isa[i],
762                                 extra_isa[i]+PC87360_EXTENT-1);
763                         for (i--; i >= 0; i--)
764                                 release_region(extra_isa[i], PC87360_EXTENT);
765                         err = -EBUSY;
766                         goto ERROR1;
767                 }
768         }
769
770         /* Retrieve the fans configuration from Super-I/O space */
771         if (data->fannr)
772                 data->fan_conf = confreg[0] | (confreg[1] << 8);
773
774         if ((err = i2c_attach_client(new_client)))
775                 goto ERROR2;
776
777         /* Use the correct reference voltage
778            Unless both the VLM and the TMS logical devices agree to
779            use an external Vref, the internal one is used. */
780         if (data->innr) {
781                 i = pc87360_read_value(data, LD_IN, NO_BANK,
782                                        PC87365_REG_IN_CONFIG);
783                 if (data->tempnr) {
784                         i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
785                                                 PC87365_REG_TEMP_CONFIG);
786                 }
787                 data->in_vref = (i&0x02) ? 3025 : 2966;
788                 dev_dbg(&new_client->dev, "Using %s reference voltage\n",
789                         (i&0x02) ? "external" : "internal");
790
791                 data->vid_conf = confreg[3];
792                 data->vrm = 90;
793         }
794
795         /* Fan clock dividers may be needed before any data is read */
796         for (i = 0; i < data->fannr; i++) {
797                 data->fan_status[i] = pc87360_read_value(data, LD_FAN,
798                                       NO_BANK, PC87360_REG_FAN_STATUS(i));
799         }
800
801         if (init > 0) {
802                 if (devid == 0xe9 && data->address[1]) /* PC87366 */
803                         use_thermistors = confreg[2] & 0x40;
804
805                 pc87360_init_client(new_client, use_thermistors);
806         }
807
808         /* Register sysfs hooks */
809         if (data->innr) {
810                 device_create_file(&new_client->dev, &dev_attr_in0_input);
811                 device_create_file(&new_client->dev, &dev_attr_in1_input);
812                 device_create_file(&new_client->dev, &dev_attr_in2_input);
813                 device_create_file(&new_client->dev, &dev_attr_in3_input);
814                 device_create_file(&new_client->dev, &dev_attr_in4_input);
815                 device_create_file(&new_client->dev, &dev_attr_in5_input);
816                 device_create_file(&new_client->dev, &dev_attr_in6_input);
817                 device_create_file(&new_client->dev, &dev_attr_in7_input);
818                 device_create_file(&new_client->dev, &dev_attr_in8_input);
819                 device_create_file(&new_client->dev, &dev_attr_in9_input);
820                 device_create_file(&new_client->dev, &dev_attr_in10_input);
821                 device_create_file(&new_client->dev, &dev_attr_in0_min);
822                 device_create_file(&new_client->dev, &dev_attr_in1_min);
823                 device_create_file(&new_client->dev, &dev_attr_in2_min);
824                 device_create_file(&new_client->dev, &dev_attr_in3_min);
825                 device_create_file(&new_client->dev, &dev_attr_in4_min);
826                 device_create_file(&new_client->dev, &dev_attr_in5_min);
827                 device_create_file(&new_client->dev, &dev_attr_in6_min);
828                 device_create_file(&new_client->dev, &dev_attr_in7_min);
829                 device_create_file(&new_client->dev, &dev_attr_in8_min);
830                 device_create_file(&new_client->dev, &dev_attr_in9_min);
831                 device_create_file(&new_client->dev, &dev_attr_in10_min);
832                 device_create_file(&new_client->dev, &dev_attr_in0_max);
833                 device_create_file(&new_client->dev, &dev_attr_in1_max);
834                 device_create_file(&new_client->dev, &dev_attr_in2_max);
835                 device_create_file(&new_client->dev, &dev_attr_in3_max);
836                 device_create_file(&new_client->dev, &dev_attr_in4_max);
837                 device_create_file(&new_client->dev, &dev_attr_in5_max);
838                 device_create_file(&new_client->dev, &dev_attr_in6_max);
839                 device_create_file(&new_client->dev, &dev_attr_in7_max);
840                 device_create_file(&new_client->dev, &dev_attr_in8_max);
841                 device_create_file(&new_client->dev, &dev_attr_in9_max);
842                 device_create_file(&new_client->dev, &dev_attr_in10_max);
843                 device_create_file(&new_client->dev, &dev_attr_in0_status);
844                 device_create_file(&new_client->dev, &dev_attr_in1_status);
845                 device_create_file(&new_client->dev, &dev_attr_in2_status);
846                 device_create_file(&new_client->dev, &dev_attr_in3_status);
847                 device_create_file(&new_client->dev, &dev_attr_in4_status);
848                 device_create_file(&new_client->dev, &dev_attr_in5_status);
849                 device_create_file(&new_client->dev, &dev_attr_in6_status);
850                 device_create_file(&new_client->dev, &dev_attr_in7_status);
851                 device_create_file(&new_client->dev, &dev_attr_in8_status);
852                 device_create_file(&new_client->dev, &dev_attr_in9_status);
853                 device_create_file(&new_client->dev, &dev_attr_in10_status);
854
855                 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
856                 device_create_file(&new_client->dev, &dev_attr_vrm);
857                 device_create_file(&new_client->dev, &dev_attr_alarms_in);
858         }
859
860         if (data->tempnr) {
861                 device_create_file(&new_client->dev, &dev_attr_temp1_input);
862                 device_create_file(&new_client->dev, &dev_attr_temp2_input);
863                 device_create_file(&new_client->dev, &dev_attr_temp1_min);
864                 device_create_file(&new_client->dev, &dev_attr_temp2_min);
865                 device_create_file(&new_client->dev, &dev_attr_temp1_max);
866                 device_create_file(&new_client->dev, &dev_attr_temp2_max);
867                 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
868                 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
869                 device_create_file(&new_client->dev, &dev_attr_temp1_status);
870                 device_create_file(&new_client->dev, &dev_attr_temp2_status);
871
872                 device_create_file(&new_client->dev, &dev_attr_alarms_temp);
873         }
874         if (data->tempnr == 3) {
875                 device_create_file(&new_client->dev, &dev_attr_temp3_input);
876                 device_create_file(&new_client->dev, &dev_attr_temp3_min);
877                 device_create_file(&new_client->dev, &dev_attr_temp3_max);
878                 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
879                 device_create_file(&new_client->dev, &dev_attr_temp3_status);
880         }
881         if (data->innr == 14) {
882                 device_create_file(&new_client->dev, &dev_attr_temp4_input);
883                 device_create_file(&new_client->dev, &dev_attr_temp5_input);
884                 device_create_file(&new_client->dev, &dev_attr_temp6_input);
885                 device_create_file(&new_client->dev, &dev_attr_temp4_min);
886                 device_create_file(&new_client->dev, &dev_attr_temp5_min);
887                 device_create_file(&new_client->dev, &dev_attr_temp6_min);
888                 device_create_file(&new_client->dev, &dev_attr_temp4_max);
889                 device_create_file(&new_client->dev, &dev_attr_temp5_max);
890                 device_create_file(&new_client->dev, &dev_attr_temp6_max);
891                 device_create_file(&new_client->dev, &dev_attr_temp4_crit);
892                 device_create_file(&new_client->dev, &dev_attr_temp5_crit);
893                 device_create_file(&new_client->dev, &dev_attr_temp6_crit);
894                 device_create_file(&new_client->dev, &dev_attr_temp4_status);
895                 device_create_file(&new_client->dev, &dev_attr_temp5_status);
896                 device_create_file(&new_client->dev, &dev_attr_temp6_status);
897         }
898
899         if (data->fannr) {
900                 device_create_file(&new_client->dev, &dev_attr_fan1_input);
901                 device_create_file(&new_client->dev, &dev_attr_fan2_input);
902                 device_create_file(&new_client->dev, &dev_attr_fan1_min);
903                 device_create_file(&new_client->dev, &dev_attr_fan2_min);
904                 device_create_file(&new_client->dev, &dev_attr_fan1_div);
905                 device_create_file(&new_client->dev, &dev_attr_fan2_div);
906                 device_create_file(&new_client->dev, &dev_attr_fan1_status);
907                 device_create_file(&new_client->dev, &dev_attr_fan2_status);
908
909                 if (FAN_CONFIG_CONTROL(data->fan_conf, 0))
910                         device_create_file(&new_client->dev, &dev_attr_pwm1);
911                 if (FAN_CONFIG_CONTROL(data->fan_conf, 1))
912                         device_create_file(&new_client->dev, &dev_attr_pwm2);
913         }
914         if (data->fannr == 3) {
915                 device_create_file(&new_client->dev, &dev_attr_fan3_input);
916                 device_create_file(&new_client->dev, &dev_attr_fan3_min);
917                 device_create_file(&new_client->dev, &dev_attr_fan3_div);
918                 device_create_file(&new_client->dev, &dev_attr_fan3_status);
919
920                 if (FAN_CONFIG_CONTROL(data->fan_conf, 2))
921                         device_create_file(&new_client->dev, &dev_attr_pwm3);
922         }
923
924         return 0;
925
926 ERROR2:
927         for (i = 0; i < 3; i++) {
928                 if (data->address[i]) {
929                         release_region(data->address[i], PC87360_EXTENT);
930                 }
931         }
932 ERROR1:
933         kfree(data);
934         return err;
935 }
936
937 static int pc87360_detach_client(struct i2c_client *client)
938 {
939         struct pc87360_data *data = i2c_get_clientdata(client);
940         int i;
941
942         if ((i = i2c_detach_client(client))) {
943                 dev_err(&client->dev, "Client deregistration failed, "
944                         "client not detached.\n");
945                 return i;
946         }
947
948         for (i = 0; i < 3; i++) {
949                 if (data->address[i]) {
950                         release_region(data->address[i], PC87360_EXTENT);
951                 }
952         }
953         kfree(data);
954
955         return 0;
956 }
957
958 /* ldi is the logical device index
959    bank is for voltages and temperatures only */
960 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
961                               u8 reg)
962 {
963         int res;
964
965         down(&(data->lock));
966         if (bank != NO_BANK)
967                 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
968         res = inb_p(data->address[ldi] + reg);
969         up(&(data->lock));
970
971         return res;
972 }
973
974 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
975                                 u8 reg, u8 value)
976 {
977         down(&(data->lock));
978         if (bank != NO_BANK)
979                 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
980         outb_p(value, data->address[ldi] + reg);
981         up(&(data->lock));
982 }
983
984 static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
985 {
986         struct pc87360_data *data = i2c_get_clientdata(client);
987         int i, nr;
988         const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
989         const u8 init_temp[3] = { 2, 2, 1 };
990         u8 reg;
991
992         if (init >= 2 && data->innr) {
993                 reg = pc87360_read_value(data, LD_IN, NO_BANK,
994                                          PC87365_REG_IN_CONVRATE);
995                 dev_info(&client->dev, "VLM conversion set to"
996                          "1s period, 160us delay\n");
997                 pc87360_write_value(data, LD_IN, NO_BANK,
998                                     PC87365_REG_IN_CONVRATE,
999                                     (reg & 0xC0) | 0x11);
1000         }
1001
1002         nr = data->innr < 11 ? data->innr : 11;
1003         for (i=0; i<nr; i++) {
1004                 if (init >= init_in[i]) {
1005                         /* Forcibly enable voltage channel */
1006                         reg = pc87360_read_value(data, LD_IN, i,
1007                                                  PC87365_REG_IN_STATUS);
1008                         if (!(reg & 0x01)) {
1009                                 dev_dbg(&client->dev, "Forcibly "
1010                                         "enabling in%d\n", i);
1011                                 pc87360_write_value(data, LD_IN, i,
1012                                                     PC87365_REG_IN_STATUS,
1013                                                     (reg & 0x68) | 0x87);
1014                         }
1015                 }
1016         }
1017
1018         /* We can't blindly trust the Super-I/O space configuration bit,
1019            most BIOS won't set it properly */
1020         for (i=11; i<data->innr; i++) {
1021                 reg = pc87360_read_value(data, LD_IN, i,
1022                                          PC87365_REG_TEMP_STATUS);
1023                 use_thermistors = use_thermistors || (reg & 0x01);
1024         }
1025
1026         i = use_thermistors ? 2 : 0;
1027         for (; i<data->tempnr; i++) {
1028                 if (init >= init_temp[i]) {
1029                         /* Forcibly enable temperature channel */
1030                         reg = pc87360_read_value(data, LD_TEMP, i,
1031                                                  PC87365_REG_TEMP_STATUS);
1032                         if (!(reg & 0x01)) {
1033                                 dev_dbg(&client->dev, "Forcibly "
1034                                         "enabling temp%d\n", i+1);
1035                                 pc87360_write_value(data, LD_TEMP, i,
1036                                                     PC87365_REG_TEMP_STATUS,
1037                                                     0xCF);
1038                         }
1039                 }
1040         }
1041
1042         if (use_thermistors) {
1043                 for (i=11; i<data->innr; i++) {
1044                         if (init >= init_in[i]) {
1045                                 /* The pin may already be used by thermal
1046                                    diodes */
1047                                 reg = pc87360_read_value(data, LD_TEMP,
1048                                       (i-11)/2, PC87365_REG_TEMP_STATUS);
1049                                 if (reg & 0x01) {
1050                                         dev_dbg(&client->dev, "Skipping "
1051                                                 "temp%d, pin already in use "
1052                                                 "by temp%d\n", i-7, (i-11)/2);
1053                                         continue;
1054                                 }
1055
1056                                 /* Forcibly enable thermistor channel */
1057                                 reg = pc87360_read_value(data, LD_IN, i,
1058                                                          PC87365_REG_IN_STATUS);
1059                                 if (!(reg & 0x01)) {
1060                                         dev_dbg(&client->dev, "Forcibly "
1061                                                 "enabling temp%d\n", i-7);
1062                                         pc87360_write_value(data, LD_IN, i,
1063                                                 PC87365_REG_TEMP_STATUS,
1064                                                 (reg & 0x60) | 0x8F);
1065                                 }
1066                         }
1067                 }
1068         }
1069
1070         if (data->innr) {
1071                 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1072                                          PC87365_REG_IN_CONFIG);
1073                 if (reg & 0x01) {
1074                         dev_dbg(&client->dev, "Forcibly "
1075                                 "enabling monitoring (VLM)\n");
1076                         pc87360_write_value(data, LD_IN, NO_BANK,
1077                                             PC87365_REG_IN_CONFIG,
1078                                             reg & 0xFE);
1079                 }
1080         }
1081
1082         if (data->tempnr) {
1083                 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1084                                          PC87365_REG_TEMP_CONFIG);
1085                 if (reg & 0x01) {
1086                         dev_dbg(&client->dev, "Forcibly enabling "
1087                                 "monitoring (TMS)\n");
1088                         pc87360_write_value(data, LD_TEMP, NO_BANK,
1089                                             PC87365_REG_TEMP_CONFIG,
1090                                             reg & 0xFE);
1091                 }
1092
1093                 if (init >= 2) {
1094                         /* Chip config as documented by National Semi. */
1095                         pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1096                         /* We voluntarily omit the bank here, in case the
1097                            sequence itself matters. It shouldn't be a problem,
1098                            since nobody else is supposed to access the
1099                            device at that point. */
1100                         pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1101                         pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1102                         pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1103                         pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1104                 }
1105         }
1106 }
1107
1108 static void pc87360_autodiv(struct i2c_client *client, int nr)
1109 {
1110         struct pc87360_data *data = i2c_get_clientdata(client);
1111         u8 old_min = data->fan_min[nr];
1112
1113         /* Increase clock divider if needed and possible */
1114         if ((data->fan_status[nr] & 0x04) /* overflow flag */
1115          || (data->fan[nr] >= 224)) { /* next to overflow */
1116                 if ((data->fan_status[nr] & 0x60) != 0x60) {
1117                         data->fan_status[nr] += 0x20;
1118                         data->fan_min[nr] >>= 1;
1119                         data->fan[nr] >>= 1;
1120                         dev_dbg(&client->dev, "Increasing "
1121                                 "clock divider to %d for fan %d\n",
1122                                 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1123                 }
1124         } else {
1125                 /* Decrease clock divider if possible */
1126                 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1127                  && data->fan[nr] < 85 /* bad accuracy */
1128                  && (data->fan_status[nr] & 0x60) != 0x00) {
1129                         data->fan_status[nr] -= 0x20;
1130                         data->fan_min[nr] <<= 1;
1131                         data->fan[nr] <<= 1;
1132                         dev_dbg(&client->dev, "Decreasing "
1133                                 "clock divider to %d for fan %d\n",
1134                                 FAN_DIV_FROM_REG(data->fan_status[nr]),
1135                                 nr+1);
1136                 }
1137         }
1138
1139         /* Write new fan min if it changed */
1140         if (old_min != data->fan_min[nr]) {
1141                 pc87360_write_value(data, LD_FAN, NO_BANK,
1142                                     PC87360_REG_FAN_MIN(nr),
1143                                     data->fan_min[nr]);
1144         }
1145 }
1146
1147 static struct pc87360_data *pc87360_update_device(struct device *dev)
1148 {
1149         struct i2c_client *client = to_i2c_client(dev);
1150         struct pc87360_data *data = i2c_get_clientdata(client);
1151         u8 i;
1152
1153         down(&data->update_lock);
1154
1155         if ((jiffies - data->last_updated > HZ * 2)
1156          || (jiffies < data->last_updated) || !data->valid) {
1157                 dev_dbg(&client->dev, "Data update\n");
1158
1159                 /* Fans */
1160                 for (i = 0; i < data->fannr; i++) {
1161                         if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1162                                 data->fan_status[i] =
1163                                         pc87360_read_value(data, LD_FAN,
1164                                         NO_BANK, PC87360_REG_FAN_STATUS(i));
1165                                 data->fan[i] = pc87360_read_value(data, LD_FAN,
1166                                                NO_BANK, PC87360_REG_FAN(i));
1167                                 data->fan_min[i] = pc87360_read_value(data,
1168                                                    LD_FAN, NO_BANK,
1169                                                    PC87360_REG_FAN_MIN(i));
1170                                 /* Change clock divider if needed */
1171                                 pc87360_autodiv(client, i);
1172                                 /* Clear bits and write new divider */
1173                                 pc87360_write_value(data, LD_FAN, NO_BANK,
1174                                                     PC87360_REG_FAN_STATUS(i),
1175                                                     data->fan_status[i]);
1176                         }
1177                         if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1178                                 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1179                                                NO_BANK, PC87360_REG_PWM(i));
1180                 }
1181
1182                 /* Voltages */
1183                 for (i = 0; i < data->innr; i++) {
1184                         data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1185                                              PC87365_REG_IN_STATUS);
1186                         /* Clear bits */
1187                         pc87360_write_value(data, LD_IN, i,
1188                                             PC87365_REG_IN_STATUS,
1189                                             data->in_status[i]);
1190                         if ((data->in_status[i] & 0x81) == 0x81) {
1191                                 data->in[i] = pc87360_read_value(data, LD_IN,
1192                                               i, PC87365_REG_IN);
1193                         }
1194                         if (data->in_status[i] & 0x01) {
1195                                 data->in_min[i] = pc87360_read_value(data,
1196                                                   LD_IN, i,
1197                                                   PC87365_REG_IN_MIN);
1198                                 data->in_max[i] = pc87360_read_value(data,
1199                                                   LD_IN, i,
1200                                                   PC87365_REG_IN_MAX);
1201                                 if (i >= 11)
1202                                         data->in_crit[i-11] =
1203                                                 pc87360_read_value(data, LD_IN,
1204                                                 i, PC87365_REG_TEMP_CRIT);
1205                         }
1206                 }
1207                 if (data->innr) {
1208                         data->in_alarms = pc87360_read_value(data, LD_IN,
1209                                           NO_BANK, PC87365_REG_IN_ALARMS1)
1210                                         | ((pc87360_read_value(data, LD_IN,
1211                                             NO_BANK, PC87365_REG_IN_ALARMS2)
1212                                             & 0x07) << 8);
1213                         data->vid = (data->vid_conf & 0xE0) ?
1214                                     pc87360_read_value(data, LD_IN,
1215                                     NO_BANK, PC87365_REG_VID) : 0x1F;
1216                 }
1217
1218                 /* Temperatures */
1219                 for (i = 0; i < data->tempnr; i++) {
1220                         data->temp_status[i] = pc87360_read_value(data,
1221                                                LD_TEMP, i,
1222                                                PC87365_REG_TEMP_STATUS);
1223                         /* Clear bits */
1224                         pc87360_write_value(data, LD_TEMP, i,
1225                                             PC87365_REG_TEMP_STATUS,
1226                                             data->temp_status[i]);
1227                         if ((data->temp_status[i] & 0x81) == 0x81) {
1228                                 data->temp[i] = pc87360_read_value(data,
1229                                                 LD_TEMP, i,
1230                                                 PC87365_REG_TEMP);
1231                         }
1232                         if (data->temp_status[i] & 0x01) {
1233                                 data->temp_min[i] = pc87360_read_value(data,
1234                                                     LD_TEMP, i,
1235                                                     PC87365_REG_TEMP_MIN);
1236                                 data->temp_max[i] = pc87360_read_value(data,
1237                                                     LD_TEMP, i,
1238                                                     PC87365_REG_TEMP_MAX);
1239                                 data->temp_crit[i] = pc87360_read_value(data,
1240                                                      LD_TEMP, i,
1241                                                      PC87365_REG_TEMP_CRIT);
1242                         }
1243                 }
1244                 if (data->tempnr) {
1245                         data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1246                                             NO_BANK, PC87365_REG_TEMP_ALARMS)
1247                                             & 0x3F;
1248                 }
1249
1250                 data->last_updated = jiffies;
1251                 data->valid = 1;
1252         }
1253
1254         up(&data->update_lock);
1255
1256         return data;
1257 }
1258
1259 static int __init pc87360_init(void)
1260 {
1261         int i;
1262
1263         if (pc87360_find(0x2e, &devid, extra_isa)
1264          && pc87360_find(0x4e, &devid, extra_isa)) {
1265                 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1266                        "module not inserted.\n");
1267                 return -ENODEV;
1268         }
1269
1270         /* Arbitrarily pick one of the addresses */
1271         for (i = 0; i < 3; i++) {
1272                 if (extra_isa[i] != 0x0000) {
1273                         normal_isa[0] = extra_isa[i];
1274                         break;
1275                 }
1276         }
1277
1278         if (normal_isa[0] == 0x0000) {
1279                 printk(KERN_WARNING "pc87360: No active logical device, "
1280                        "module not inserted.\n");
1281                 return -ENODEV;
1282         }
1283
1284         return i2c_add_driver(&pc87360_driver);
1285 }
1286
1287 static void __exit pc87360_exit(void)
1288 {
1289         i2c_del_driver(&pc87360_driver);
1290 }
1291
1292
1293 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1294 MODULE_DESCRIPTION("PC8736x hardware monitor");
1295 MODULE_LICENSE("GPL");
1296
1297 module_init(pc87360_init);
1298 module_exit(pc87360_exit);