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