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