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