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