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