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