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