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