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