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