[PATCH] I2C: Separate non-i2c hwmon drivers from i2c-core (3/9)
[safe/jmp/linux-2.6] / drivers / hwmon / pc87360.c
1 /*
2  *  pc87360.c - Part of lm_sensors, Linux kernel modules
3  *              for hardware monitoring
4  *  Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
5  *
6  *  Copied from smsc47m1.c:
7  *  Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  *  Supports the following chips:
24  *
25  *  Chip        #vin    #fan    #pwm    #temp   devid
26  *  PC87360     -       2       2       -       0xE1
27  *  PC87363     -       2       2       -       0xE8
28  *  PC87364     -       3       3       -       0xE4
29  *  PC87365     11      3       3       2       0xE5
30  *  PC87366     11      3       3       3-4     0xE9
31  *
32  *  This driver assumes that no more than one chip is present, and one of
33  *  the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
34  */
35
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/jiffies.h>
40 #include <linux/i2c.h>
41 #include <linux/i2c-isa.h>
42 #include <linux/i2c-sensor.h>
43 #include <linux/i2c-vid.h>
44 #include <linux/hwmon.h>
45 #include <linux/err.h>
46 #include <asm/io.h>
47
48 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
49 static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
50 static struct i2c_force_data forces[] = {{ NULL }};
51 static u8 devid;
52 static unsigned int extra_isa[3];
53 static u8 confreg[4];
54
55 enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
56 static struct i2c_address_data addr_data = {
57         .normal_i2c             = normal_i2c,
58         .normal_isa             = normal_isa,
59         .forces                 = forces,
60 };
61
62 static int init = 1;
63 module_param(init, int, 0);
64 MODULE_PARM_DESC(init,
65  "Chip initialization level:\n"
66  " 0: None\n"
67  "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
68  " 2: Forcibly enable all voltage and temperature channels, except in9\n"
69  " 3: Forcibly enable all voltage and temperature channels, including in9");
70
71 /*
72  * Super-I/O registers and operations
73  */
74
75 #define DEV     0x07    /* Register: Logical device select */
76 #define DEVID   0x20    /* Register: Device ID */
77 #define ACT     0x30    /* Register: Device activation */
78 #define BASE    0x60    /* Register: Base address */
79
80 #define FSCM    0x09    /* Logical device: fans */
81 #define VLM     0x0d    /* Logical device: voltages */
82 #define TMS     0x0e    /* Logical device: temperatures */
83 static const u8 logdev[3] = { FSCM, VLM, TMS };
84
85 #define LD_FAN          0
86 #define LD_IN           1
87 #define LD_TEMP         2
88
89 static inline void superio_outb(int sioaddr, int reg, int val)
90 {
91         outb(reg, sioaddr);
92         outb(val, sioaddr+1);
93 }
94
95 static inline int superio_inb(int sioaddr, int reg)
96 {
97         outb(reg, sioaddr);
98         return inb(sioaddr+1);
99 }
100
101 static inline void superio_exit(int sioaddr)
102 {
103         outb(0x02, sioaddr);
104         outb(0x02, sioaddr+1);
105 }
106
107 /*
108  * Logical devices
109  */
110
111 #define PC87360_EXTENT          0x10
112 #define PC87365_REG_BANK        0x09
113 #define NO_BANK                 0xff
114
115 /*
116  * Fan registers and conversions
117  */
118
119 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
120 #define PC87360_REG_PRESCALE(nr)        (0x00 + 2 * (nr))
121 #define PC87360_REG_PWM(nr)             (0x01 + 2 * (nr))
122 #define PC87360_REG_FAN_MIN(nr)         (0x06 + 3 * (nr))
123 #define PC87360_REG_FAN(nr)             (0x07 + 3 * (nr))
124 #define PC87360_REG_FAN_STATUS(nr)      (0x08 + 3 * (nr))
125
126 #define FAN_FROM_REG(val,div)           ((val) == 0 ? 0: \
127                                          480000 / ((val)*(div)))
128 #define FAN_TO_REG(val,div)             ((val) <= 100 ? 0 : \
129                                          480000 / ((val)*(div)))
130 #define FAN_DIV_FROM_REG(val)           (1 << ((val >> 5) & 0x03))
131 #define FAN_STATUS_FROM_REG(val)        ((val) & 0x07)
132
133 #define FAN_CONFIG_MONITOR(val,nr)      (((val) >> (2 + nr * 3)) & 1)
134 #define FAN_CONFIG_CONTROL(val,nr)      (((val) >> (3 + nr * 3)) & 1)
135 #define FAN_CONFIG_INVERT(val,nr)       (((val) >> (4 + nr * 3)) & 1)
136
137 #define PWM_FROM_REG(val,inv)           ((inv) ? 255 - (val) : (val))
138 static inline u8 PWM_TO_REG(int val, int inv)
139 {
140         if (inv)
141                 val = 255 - val;
142         if (val < 0)
143                 return 0;
144         if (val > 255)
145                 return 255;
146         return val;
147 }
148
149 /*
150  * Voltage registers and conversions
151  */
152
153 #define PC87365_REG_IN_CONVRATE         0x07
154 #define PC87365_REG_IN_CONFIG           0x08
155 #define PC87365_REG_IN                  0x0B
156 #define PC87365_REG_IN_MIN              0x0D
157 #define PC87365_REG_IN_MAX              0x0C
158 #define PC87365_REG_IN_STATUS           0x0A
159 #define PC87365_REG_IN_ALARMS1          0x00
160 #define PC87365_REG_IN_ALARMS2          0x01
161 #define PC87365_REG_VID                 0x06
162
163 #define IN_FROM_REG(val,ref)            (((val) * (ref) + 128) / 256)
164 #define IN_TO_REG(val,ref)              ((val) < 0 ? 0 : \
165                                          (val)*256 >= (ref)*255 ? 255: \
166                                          ((val) * 256 + (ref)/2) / (ref))
167
168 /*
169  * Temperature registers and conversions
170  */
171
172 #define PC87365_REG_TEMP_CONFIG         0x08
173 #define PC87365_REG_TEMP                0x0B
174 #define PC87365_REG_TEMP_MIN            0x0D
175 #define PC87365_REG_TEMP_MAX            0x0C
176 #define PC87365_REG_TEMP_CRIT           0x0E
177 #define PC87365_REG_TEMP_STATUS         0x0A
178 #define PC87365_REG_TEMP_ALARMS         0x00
179
180 #define TEMP_FROM_REG(val)              ((val) * 1000)
181 #define TEMP_TO_REG(val)                ((val) < -55000 ? -55 : \
182                                          (val) > 127000 ? 127 : \
183                                          (val) < 0 ? ((val) - 500) / 1000 : \
184                                          ((val) + 500) / 1000)
185
186 /*
187  * Client data (each client gets its own)
188  */
189
190 struct pc87360_data {
191         struct i2c_client client;
192         struct class_device *class_dev;
193         struct semaphore lock;
194         struct semaphore update_lock;
195         char valid;             /* !=0 if following fields are valid */
196         unsigned long last_updated;     /* In jiffies */
197
198         int address[3];
199
200         u8 fannr, innr, tempnr;
201
202         u8 fan[3];              /* Register value */
203         u8 fan_min[3];          /* Register value */
204         u8 fan_status[3];       /* Register value */
205         u8 pwm[3];              /* Register value */
206         u16 fan_conf;           /* Configuration register values, combined */
207
208         u16 in_vref;            /* 1 mV/bit */
209         u8 in[14];              /* Register value */
210         u8 in_min[14];          /* Register value */
211         u8 in_max[14];          /* Register value */
212         u8 in_crit[3];          /* Register value */
213         u8 in_status[14];       /* Register value */
214         u16 in_alarms;          /* Register values, combined, masked */
215         u8 vid_conf;            /* Configuration register value */
216         u8 vrm;
217         u8 vid;                 /* Register value */
218
219         s8 temp[3];             /* Register value */
220         s8 temp_min[3];         /* Register value */
221         s8 temp_max[3];         /* Register value */
222         s8 temp_crit[3];        /* Register value */
223         u8 temp_status[3];      /* Register value */
224         u8 temp_alarms;         /* Register value, masked */
225 };
226
227 /*
228  * Functions declaration
229  */
230
231 static int pc87360_attach_adapter(struct i2c_adapter *adapter);
232 static int pc87360_detect(struct i2c_adapter *adapter, int address, int kind);
233 static int pc87360_detach_client(struct i2c_client *client);
234
235 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
236                               u8 reg);
237 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
238                                 u8 reg, u8 value);
239 static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
240 static struct pc87360_data *pc87360_update_device(struct device *dev);
241
242 /*
243  * Driver data (common to all clients)
244  */
245
246 static struct i2c_driver pc87360_driver = {
247         .owner          = THIS_MODULE,
248         .name           = "pc87360",
249         .flags          = I2C_DF_NOTIFY,
250         .attach_adapter = pc87360_attach_adapter,
251         .detach_client  = pc87360_detach_client,
252 };
253
254 /*
255  * Sysfs stuff
256  */
257
258 static ssize_t set_fan_min(struct device *dev, const char *buf,
259         size_t count, int nr)
260 {
261         struct i2c_client *client = to_i2c_client(dev);
262         struct pc87360_data *data = i2c_get_clientdata(client);
263         long fan_min = simple_strtol(buf, NULL, 10);
264
265         down(&data->update_lock);
266         fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[nr]));
267
268         /* If it wouldn't fit, change clock divisor */
269         while (fan_min > 255
270             && (data->fan_status[nr] & 0x60) != 0x60) {
271                 fan_min >>= 1;
272                 data->fan[nr] >>= 1;
273                 data->fan_status[nr] += 0x20;
274         }
275         data->fan_min[nr] = fan_min > 255 ? 255 : fan_min;
276         pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(nr),
277                             data->fan_min[nr]);
278
279         /* Write new divider, preserve alarm bits */
280         pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(nr),
281                             data->fan_status[nr] & 0xF9);
282         up(&data->update_lock);
283
284         return count;
285 }
286
287 #define show_and_set_fan(offset) \
288 static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
289 { \
290         struct pc87360_data *data = pc87360_update_device(dev); \
291         return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[offset-1], \
292                        FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
293 } \
294 static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
295 { \
296         struct pc87360_data *data = pc87360_update_device(dev); \
297         return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[offset-1], \
298                        FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
299 } \
300 static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
301 { \
302         struct pc87360_data *data = pc87360_update_device(dev); \
303         return sprintf(buf, "%u\n", \
304                        FAN_DIV_FROM_REG(data->fan_status[offset-1])); \
305 } \
306 static ssize_t show_fan##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
307 { \
308         struct pc87360_data *data = pc87360_update_device(dev); \
309         return sprintf(buf, "%u\n", \
310                        FAN_STATUS_FROM_REG(data->fan_status[offset-1])); \
311 } \
312 static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
313         size_t count) \
314 { \
315         return set_fan_min(dev, buf, count, offset-1); \
316 } \
317 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
318         show_fan##offset##_input, NULL); \
319 static DEVICE_ATTR(fan##offset##_min, S_IWUSR | S_IRUGO, \
320         show_fan##offset##_min, set_fan##offset##_min); \
321 static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
322         show_fan##offset##_div, NULL); \
323 static DEVICE_ATTR(fan##offset##_status, S_IRUGO, \
324         show_fan##offset##_status, NULL);
325 show_and_set_fan(1)
326 show_and_set_fan(2)
327 show_and_set_fan(3)
328
329 #define show_and_set_pwm(offset) \
330 static ssize_t show_pwm##offset(struct device *dev, struct device_attribute *attr, char *buf) \
331 { \
332         struct pc87360_data *data = pc87360_update_device(dev); \
333         return sprintf(buf, "%u\n", \
334                        PWM_FROM_REG(data->pwm[offset-1], \
335                                     FAN_CONFIG_INVERT(data->fan_conf, \
336                                                       offset-1))); \
337 } \
338 static ssize_t set_pwm##offset(struct device *dev, struct device_attribute *attr, const char *buf, \
339         size_t count) \
340 { \
341         struct i2c_client *client = to_i2c_client(dev); \
342         struct pc87360_data *data = i2c_get_clientdata(client); \
343         long val = simple_strtol(buf, NULL, 10); \
344  \
345         down(&data->update_lock); \
346         data->pwm[offset-1] = PWM_TO_REG(val, \
347                               FAN_CONFIG_INVERT(data->fan_conf, offset-1)); \
348         pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(offset-1), \
349                             data->pwm[offset-1]); \
350         up(&data->update_lock); \
351         return count; \
352 } \
353 static DEVICE_ATTR(pwm##offset, S_IWUSR | S_IRUGO, \
354         show_pwm##offset, set_pwm##offset);
355 show_and_set_pwm(1)
356 show_and_set_pwm(2)
357 show_and_set_pwm(3)
358
359 #define show_and_set_in(offset) \
360 static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
361 { \
362         struct pc87360_data *data = pc87360_update_device(dev); \
363         return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
364                        data->in_vref)); \
365 } \
366 static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
367 { \
368         struct pc87360_data *data = pc87360_update_device(dev); \
369         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
370                        data->in_vref)); \
371 } \
372 static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
373 { \
374         struct pc87360_data *data = pc87360_update_device(dev); \
375         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
376                        data->in_vref)); \
377 } \
378 static ssize_t show_in##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
379 { \
380         struct pc87360_data *data = pc87360_update_device(dev); \
381         return sprintf(buf, "%u\n", data->in_status[offset]); \
382 } \
383 static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
384         size_t count) \
385 { \
386         struct i2c_client *client = to_i2c_client(dev); \
387         struct pc87360_data *data = i2c_get_clientdata(client); \
388         long val = simple_strtol(buf, NULL, 10); \
389  \
390         down(&data->update_lock); \
391         data->in_min[offset] = IN_TO_REG(val, data->in_vref); \
392         pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MIN, \
393                             data->in_min[offset]); \
394         up(&data->update_lock); \
395         return count; \
396 } \
397 static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
398         size_t count) \
399 { \
400         struct i2c_client *client = to_i2c_client(dev); \
401         struct pc87360_data *data = i2c_get_clientdata(client); \
402         long val = simple_strtol(buf, NULL, 10); \
403  \
404         down(&data->update_lock); \
405         data->in_max[offset] = IN_TO_REG(val, \
406                                data->in_vref); \
407         pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MAX, \
408                             data->in_max[offset]); \
409         up(&data->update_lock); \
410         return count; \
411 } \
412 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
413         show_in##offset##_input, NULL); \
414 static DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
415         show_in##offset##_min, set_in##offset##_min); \
416 static DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
417         show_in##offset##_max, set_in##offset##_max); \
418 static DEVICE_ATTR(in##offset##_status, S_IRUGO, \
419         show_in##offset##_status, NULL);
420 show_and_set_in(0)
421 show_and_set_in(1)
422 show_and_set_in(2)
423 show_and_set_in(3)
424 show_and_set_in(4)
425 show_and_set_in(5)
426 show_and_set_in(6)
427 show_and_set_in(7)
428 show_and_set_in(8)
429 show_and_set_in(9)
430 show_and_set_in(10)
431
432 #define show_and_set_therm(offset) \
433 static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
434 { \
435         struct pc87360_data *data = pc87360_update_device(dev); \
436         return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset+7], \
437                        data->in_vref)); \
438 } \
439 static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
440 { \
441         struct pc87360_data *data = pc87360_update_device(dev); \
442         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset+7], \
443                        data->in_vref)); \
444 } \
445 static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
446 { \
447         struct pc87360_data *data = pc87360_update_device(dev); \
448         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset+7], \
449                        data->in_vref)); \
450 } \
451 static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
452 { \
453         struct pc87360_data *data = pc87360_update_device(dev); \
454         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[offset-4], \
455                        data->in_vref)); \
456 } \
457 static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
458 { \
459         struct pc87360_data *data = pc87360_update_device(dev); \
460         return sprintf(buf, "%u\n", data->in_status[offset+7]); \
461 } \
462 static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
463         size_t count) \
464 { \
465         struct i2c_client *client = to_i2c_client(dev); \
466         struct pc87360_data *data = i2c_get_clientdata(client); \
467         long val = simple_strtol(buf, NULL, 10); \
468  \
469         down(&data->update_lock); \
470         data->in_min[offset+7] = IN_TO_REG(val, data->in_vref); \
471         pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MIN, \
472                             data->in_min[offset+7]); \
473         up(&data->update_lock); \
474         return count; \
475 } \
476 static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
477         size_t count) \
478 { \
479         struct i2c_client *client = to_i2c_client(dev); \
480         struct pc87360_data *data = i2c_get_clientdata(client); \
481         long val = simple_strtol(buf, NULL, 10); \
482  \
483         down(&data->update_lock); \
484         data->in_max[offset+7] = IN_TO_REG(val, data->in_vref); \
485         pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MAX, \
486                             data->in_max[offset+7]); \
487         up(&data->update_lock); \
488         return count; \
489 } \
490 static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
491         size_t count) \
492 { \
493         struct i2c_client *client = to_i2c_client(dev); \
494         struct pc87360_data *data = i2c_get_clientdata(client); \
495         long val = simple_strtol(buf, NULL, 10); \
496  \
497         down(&data->update_lock); \
498         data->in_crit[offset-4] = IN_TO_REG(val, data->in_vref); \
499         pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_CRIT, \
500                             data->in_crit[offset-4]); \
501         up(&data->update_lock); \
502         return count; \
503 } \
504 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
505         show_temp##offset##_input, NULL); \
506 static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
507         show_temp##offset##_min, set_temp##offset##_min); \
508 static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
509         show_temp##offset##_max, set_temp##offset##_max); \
510 static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
511         show_temp##offset##_crit, set_temp##offset##_crit); \
512 static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
513         show_temp##offset##_status, NULL);
514 show_and_set_therm(4)
515 show_and_set_therm(5)
516 show_and_set_therm(6)
517
518 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
519 {
520         struct pc87360_data *data = pc87360_update_device(dev);
521         return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
522 }
523 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
524
525 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
526 {
527         struct pc87360_data *data = pc87360_update_device(dev);
528         return sprintf(buf, "%u\n", data->vrm);
529 }
530 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
531 {
532         struct i2c_client *client = to_i2c_client(dev);
533         struct pc87360_data *data = i2c_get_clientdata(client);
534         data->vrm = simple_strtoul(buf, NULL, 10);
535         return count;
536 }
537 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
538
539 static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
540 {
541         struct pc87360_data *data = pc87360_update_device(dev);
542         return sprintf(buf, "%u\n", data->in_alarms);
543 }
544 static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
545
546 #define show_and_set_temp(offset) \
547 static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
548 { \
549         struct pc87360_data *data = pc87360_update_device(dev); \
550         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
551 } \
552 static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
553 { \
554         struct pc87360_data *data = pc87360_update_device(dev); \
555         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
556 } \
557 static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
558 { \
559         struct pc87360_data *data = pc87360_update_device(dev); \
560         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
561 }\
562 static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
563 { \
564         struct pc87360_data *data = pc87360_update_device(dev); \
565         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[offset-1])); \
566 }\
567 static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
568 { \
569         struct pc87360_data *data = pc87360_update_device(dev); \
570         return sprintf(buf, "%d\n", data->temp_status[offset-1]); \
571 }\
572 static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
573         size_t count) \
574 { \
575         struct i2c_client *client = to_i2c_client(dev); \
576         struct pc87360_data *data = i2c_get_clientdata(client); \
577         long val = simple_strtol(buf, NULL, 10); \
578  \
579         down(&data->update_lock); \
580         data->temp_min[offset-1] = TEMP_TO_REG(val); \
581         pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MIN, \
582                             data->temp_min[offset-1]); \
583         up(&data->update_lock); \
584         return count; \
585 } \
586 static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
587         size_t count) \
588 { \
589         struct i2c_client *client = to_i2c_client(dev); \
590         struct pc87360_data *data = i2c_get_clientdata(client); \
591         long val = simple_strtol(buf, NULL, 10); \
592  \
593         down(&data->update_lock); \
594         data->temp_max[offset-1] = TEMP_TO_REG(val); \
595         pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MAX, \
596                             data->temp_max[offset-1]); \
597         up(&data->update_lock); \
598         return count; \
599 } \
600 static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
601         size_t count) \
602 { \
603         struct i2c_client *client = to_i2c_client(dev); \
604         struct pc87360_data *data = i2c_get_clientdata(client); \
605         long val = simple_strtol(buf, NULL, 10); \
606  \
607         down(&data->update_lock); \
608         data->temp_crit[offset-1] = TEMP_TO_REG(val); \
609         pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_CRIT, \
610                             data->temp_crit[offset-1]); \
611         up(&data->update_lock); \
612         return count; \
613 } \
614 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
615         show_temp##offset##_input, NULL); \
616 static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
617         show_temp##offset##_min, set_temp##offset##_min); \
618 static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
619         show_temp##offset##_max, set_temp##offset##_max); \
620 static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
621         show_temp##offset##_crit, set_temp##offset##_crit); \
622 static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
623         show_temp##offset##_status, NULL);
624 show_and_set_temp(1)
625 show_and_set_temp(2)
626 show_and_set_temp(3)
627
628 static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
629 {
630         struct pc87360_data *data = pc87360_update_device(dev);
631         return sprintf(buf, "%u\n", data->temp_alarms);
632 }
633 static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
634
635 /*
636  * Device detection, registration and update
637  */
638
639 static int pc87360_attach_adapter(struct i2c_adapter *adapter)
640 {
641         return i2c_detect(adapter, &addr_data, pc87360_detect);
642 }
643
644 static int pc87360_find(int sioaddr, u8 *devid, int *address)
645 {
646         u16 val;
647         int i;
648         int nrdev; /* logical device count */
649
650         /* No superio_enter */
651
652         /* Identify device */
653         val = superio_inb(sioaddr, DEVID);
654         switch (val) {
655         case 0xE1: /* PC87360 */
656         case 0xE8: /* PC87363 */
657         case 0xE4: /* PC87364 */
658                 nrdev = 1;
659                 break;
660         case 0xE5: /* PC87365 */
661         case 0xE9: /* PC87366 */
662                 nrdev = 3;
663                 break;
664         default:
665                 superio_exit(sioaddr);
666                 return -ENODEV;
667         }
668         /* Remember the device id */
669         *devid = val;
670
671         for (i = 0; i < nrdev; i++) {
672                 /* select logical device */
673                 superio_outb(sioaddr, DEV, logdev[i]);
674
675                 val = superio_inb(sioaddr, ACT);
676                 if (!(val & 0x01)) {
677                         printk(KERN_INFO "pc87360: Device 0x%02x not "
678                                "activated\n", logdev[i]);
679                         continue;
680                 }
681
682                 val = (superio_inb(sioaddr, BASE) << 8)
683                     | superio_inb(sioaddr, BASE + 1);
684                 if (!val) {
685                         printk(KERN_INFO "pc87360: Base address not set for "
686                                "device 0x%02x\n", logdev[i]);
687                         continue;
688                 }
689
690                 address[i] = val;
691
692                 if (i==0) { /* Fans */
693                         confreg[0] = superio_inb(sioaddr, 0xF0);
694                         confreg[1] = superio_inb(sioaddr, 0xF1);
695
696 #ifdef DEBUG
697                         printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
698                                "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
699                                (confreg[0]>>3)&1, (confreg[0]>>4)&1);
700                         printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
701                                "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
702                                (confreg[0]>>6)&1, (confreg[0]>>7)&1);
703                         printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
704                                "ctrl=%d inv=%d\n", confreg[1]&1,
705                                (confreg[1]>>1)&1, (confreg[1]>>2)&1);
706 #endif
707                 } else if (i==1) { /* Voltages */
708                         /* Are we using thermistors? */
709                         if (*devid == 0xE9) { /* PC87366 */
710                                 /* These registers are not logical-device
711                                    specific, just that we won't need them if
712                                    we don't use the VLM device */
713                                 confreg[2] = superio_inb(sioaddr, 0x2B);
714                                 confreg[3] = superio_inb(sioaddr, 0x25);
715
716                                 if (confreg[2] & 0x40) {
717                                         printk(KERN_INFO "pc87360: Using "
718                                                "thermistors for temperature "
719                                                "monitoring\n");
720                                 }
721                                 if (confreg[3] & 0xE0) {
722                                         printk(KERN_INFO "pc87360: VID "
723                                                "inputs routed (mode %u)\n",
724                                                confreg[3] >> 5);
725                                 }
726                         }
727                 }
728         }
729
730         superio_exit(sioaddr);
731         return 0;
732 }
733
734 /* We don't really care about the address.
735    Read from extra_isa instead. */
736 int pc87360_detect(struct i2c_adapter *adapter, int address, int kind)
737 {
738         int i;
739         struct i2c_client *new_client;
740         struct pc87360_data *data;
741         int err = 0;
742         const char *name = "pc87360";
743         int use_thermistors = 0;
744
745         if (!i2c_is_isa_adapter(adapter))
746                 return -ENODEV;
747
748         if (!(data = kmalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
749                 return -ENOMEM;
750         memset(data, 0x00, sizeof(struct pc87360_data));
751
752         new_client = &data->client;
753         i2c_set_clientdata(new_client, data);
754         new_client->addr = address;
755         init_MUTEX(&data->lock);
756         new_client->adapter = adapter;
757         new_client->driver = &pc87360_driver;
758         new_client->flags = 0;
759
760         data->fannr = 2;
761         data->innr = 0;
762         data->tempnr = 0;
763
764         switch (devid) {
765         case 0xe8:
766                 name = "pc87363";
767                 break;
768         case 0xe4:
769                 name = "pc87364";
770                 data->fannr = 3;
771                 break;
772         case 0xe5:
773                 name = "pc87365";
774                 data->fannr = extra_isa[0] ? 3 : 0;
775                 data->innr = extra_isa[1] ? 11 : 0;
776                 data->tempnr = extra_isa[2] ? 2 : 0;
777                 break;
778         case 0xe9:
779                 name = "pc87366";
780                 data->fannr = extra_isa[0] ? 3 : 0;
781                 data->innr = extra_isa[1] ? 14 : 0;
782                 data->tempnr = extra_isa[2] ? 3 : 0;
783                 break;
784         }
785
786         strcpy(new_client->name, name);
787         data->valid = 0;
788         init_MUTEX(&data->update_lock);
789
790         for (i = 0; i < 3; i++) {
791                 if (((data->address[i] = extra_isa[i]))
792                  && !request_region(extra_isa[i], PC87360_EXTENT,
793                                     pc87360_driver.name)) {
794                         dev_err(&new_client->dev, "Region 0x%x-0x%x already "
795                                 "in use!\n", extra_isa[i],
796                                 extra_isa[i]+PC87360_EXTENT-1);
797                         for (i--; i >= 0; i--)
798                                 release_region(extra_isa[i], PC87360_EXTENT);
799                         err = -EBUSY;
800                         goto ERROR1;
801                 }
802         }
803
804         /* Retrieve the fans configuration from Super-I/O space */
805         if (data->fannr)
806                 data->fan_conf = confreg[0] | (confreg[1] << 8);
807
808         if ((err = i2c_attach_client(new_client)))
809                 goto ERROR2;
810
811         /* Use the correct reference voltage
812            Unless both the VLM and the TMS logical devices agree to
813            use an external Vref, the internal one is used. */
814         if (data->innr) {
815                 i = pc87360_read_value(data, LD_IN, NO_BANK,
816                                        PC87365_REG_IN_CONFIG);
817                 if (data->tempnr) {
818                         i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
819                                                 PC87365_REG_TEMP_CONFIG);
820                 }
821                 data->in_vref = (i&0x02) ? 3025 : 2966;
822                 dev_dbg(&new_client->dev, "Using %s reference voltage\n",
823                         (i&0x02) ? "external" : "internal");
824
825                 data->vid_conf = confreg[3];
826                 data->vrm = 90;
827         }
828
829         /* Fan clock dividers may be needed before any data is read */
830         for (i = 0; i < data->fannr; i++) {
831                 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
832                         data->fan_status[i] = pc87360_read_value(data,
833                                               LD_FAN, NO_BANK,
834                                               PC87360_REG_FAN_STATUS(i));
835         }
836
837         if (init > 0) {
838                 if (devid == 0xe9 && data->address[1]) /* PC87366 */
839                         use_thermistors = confreg[2] & 0x40;
840
841                 pc87360_init_client(new_client, use_thermistors);
842         }
843
844         /* Register sysfs hooks */
845         data->class_dev = hwmon_device_register(&new_client->dev);
846         if (IS_ERR(data->class_dev)) {
847                 err = PTR_ERR(data->class_dev);
848                 goto ERROR3;
849         }
850
851         if (data->innr) {
852                 device_create_file(&new_client->dev, &dev_attr_in0_input);
853                 device_create_file(&new_client->dev, &dev_attr_in1_input);
854                 device_create_file(&new_client->dev, &dev_attr_in2_input);
855                 device_create_file(&new_client->dev, &dev_attr_in3_input);
856                 device_create_file(&new_client->dev, &dev_attr_in4_input);
857                 device_create_file(&new_client->dev, &dev_attr_in5_input);
858                 device_create_file(&new_client->dev, &dev_attr_in6_input);
859                 device_create_file(&new_client->dev, &dev_attr_in7_input);
860                 device_create_file(&new_client->dev, &dev_attr_in8_input);
861                 device_create_file(&new_client->dev, &dev_attr_in9_input);
862                 device_create_file(&new_client->dev, &dev_attr_in10_input);
863                 device_create_file(&new_client->dev, &dev_attr_in0_min);
864                 device_create_file(&new_client->dev, &dev_attr_in1_min);
865                 device_create_file(&new_client->dev, &dev_attr_in2_min);
866                 device_create_file(&new_client->dev, &dev_attr_in3_min);
867                 device_create_file(&new_client->dev, &dev_attr_in4_min);
868                 device_create_file(&new_client->dev, &dev_attr_in5_min);
869                 device_create_file(&new_client->dev, &dev_attr_in6_min);
870                 device_create_file(&new_client->dev, &dev_attr_in7_min);
871                 device_create_file(&new_client->dev, &dev_attr_in8_min);
872                 device_create_file(&new_client->dev, &dev_attr_in9_min);
873                 device_create_file(&new_client->dev, &dev_attr_in10_min);
874                 device_create_file(&new_client->dev, &dev_attr_in0_max);
875                 device_create_file(&new_client->dev, &dev_attr_in1_max);
876                 device_create_file(&new_client->dev, &dev_attr_in2_max);
877                 device_create_file(&new_client->dev, &dev_attr_in3_max);
878                 device_create_file(&new_client->dev, &dev_attr_in4_max);
879                 device_create_file(&new_client->dev, &dev_attr_in5_max);
880                 device_create_file(&new_client->dev, &dev_attr_in6_max);
881                 device_create_file(&new_client->dev, &dev_attr_in7_max);
882                 device_create_file(&new_client->dev, &dev_attr_in8_max);
883                 device_create_file(&new_client->dev, &dev_attr_in9_max);
884                 device_create_file(&new_client->dev, &dev_attr_in10_max);
885                 device_create_file(&new_client->dev, &dev_attr_in0_status);
886                 device_create_file(&new_client->dev, &dev_attr_in1_status);
887                 device_create_file(&new_client->dev, &dev_attr_in2_status);
888                 device_create_file(&new_client->dev, &dev_attr_in3_status);
889                 device_create_file(&new_client->dev, &dev_attr_in4_status);
890                 device_create_file(&new_client->dev, &dev_attr_in5_status);
891                 device_create_file(&new_client->dev, &dev_attr_in6_status);
892                 device_create_file(&new_client->dev, &dev_attr_in7_status);
893                 device_create_file(&new_client->dev, &dev_attr_in8_status);
894                 device_create_file(&new_client->dev, &dev_attr_in9_status);
895                 device_create_file(&new_client->dev, &dev_attr_in10_status);
896
897                 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
898                 device_create_file(&new_client->dev, &dev_attr_vrm);
899                 device_create_file(&new_client->dev, &dev_attr_alarms_in);
900         }
901
902         if (data->tempnr) {
903                 device_create_file(&new_client->dev, &dev_attr_temp1_input);
904                 device_create_file(&new_client->dev, &dev_attr_temp2_input);
905                 device_create_file(&new_client->dev, &dev_attr_temp1_min);
906                 device_create_file(&new_client->dev, &dev_attr_temp2_min);
907                 device_create_file(&new_client->dev, &dev_attr_temp1_max);
908                 device_create_file(&new_client->dev, &dev_attr_temp2_max);
909                 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
910                 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
911                 device_create_file(&new_client->dev, &dev_attr_temp1_status);
912                 device_create_file(&new_client->dev, &dev_attr_temp2_status);
913
914                 device_create_file(&new_client->dev, &dev_attr_alarms_temp);
915         }
916         if (data->tempnr == 3) {
917                 device_create_file(&new_client->dev, &dev_attr_temp3_input);
918                 device_create_file(&new_client->dev, &dev_attr_temp3_min);
919                 device_create_file(&new_client->dev, &dev_attr_temp3_max);
920                 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
921                 device_create_file(&new_client->dev, &dev_attr_temp3_status);
922         }
923         if (data->innr == 14) {
924                 device_create_file(&new_client->dev, &dev_attr_temp4_input);
925                 device_create_file(&new_client->dev, &dev_attr_temp5_input);
926                 device_create_file(&new_client->dev, &dev_attr_temp6_input);
927                 device_create_file(&new_client->dev, &dev_attr_temp4_min);
928                 device_create_file(&new_client->dev, &dev_attr_temp5_min);
929                 device_create_file(&new_client->dev, &dev_attr_temp6_min);
930                 device_create_file(&new_client->dev, &dev_attr_temp4_max);
931                 device_create_file(&new_client->dev, &dev_attr_temp5_max);
932                 device_create_file(&new_client->dev, &dev_attr_temp6_max);
933                 device_create_file(&new_client->dev, &dev_attr_temp4_crit);
934                 device_create_file(&new_client->dev, &dev_attr_temp5_crit);
935                 device_create_file(&new_client->dev, &dev_attr_temp6_crit);
936                 device_create_file(&new_client->dev, &dev_attr_temp4_status);
937                 device_create_file(&new_client->dev, &dev_attr_temp5_status);
938                 device_create_file(&new_client->dev, &dev_attr_temp6_status);
939         }
940
941         if (data->fannr) {
942                 if (FAN_CONFIG_MONITOR(data->fan_conf, 0)) {
943                         device_create_file(&new_client->dev,
944                                            &dev_attr_fan1_input);
945                         device_create_file(&new_client->dev,
946                                            &dev_attr_fan1_min);
947                         device_create_file(&new_client->dev,
948                                            &dev_attr_fan1_div);
949                         device_create_file(&new_client->dev,
950                                            &dev_attr_fan1_status);
951                 }
952
953                 if (FAN_CONFIG_MONITOR(data->fan_conf, 1)) {
954                         device_create_file(&new_client->dev,
955                                            &dev_attr_fan2_input);
956                         device_create_file(&new_client->dev,
957                                            &dev_attr_fan2_min);
958                         device_create_file(&new_client->dev,
959                                            &dev_attr_fan2_div);
960                         device_create_file(&new_client->dev,
961                                            &dev_attr_fan2_status);
962                 }
963
964                 if (FAN_CONFIG_CONTROL(data->fan_conf, 0))
965                         device_create_file(&new_client->dev, &dev_attr_pwm1);
966                 if (FAN_CONFIG_CONTROL(data->fan_conf, 1))
967                         device_create_file(&new_client->dev, &dev_attr_pwm2);
968         }
969         if (data->fannr == 3) {
970                 if (FAN_CONFIG_MONITOR(data->fan_conf, 2)) {
971                         device_create_file(&new_client->dev,
972                                            &dev_attr_fan3_input);
973                         device_create_file(&new_client->dev,
974                                            &dev_attr_fan3_min);
975                         device_create_file(&new_client->dev,
976                                            &dev_attr_fan3_div);
977                         device_create_file(&new_client->dev,
978                                            &dev_attr_fan3_status);
979                 }
980
981                 if (FAN_CONFIG_CONTROL(data->fan_conf, 2))
982                         device_create_file(&new_client->dev, &dev_attr_pwm3);
983         }
984
985         return 0;
986
987 ERROR3:
988         i2c_detach_client(new_client);
989 ERROR2:
990         for (i = 0; i < 3; i++) {
991                 if (data->address[i]) {
992                         release_region(data->address[i], PC87360_EXTENT);
993                 }
994         }
995 ERROR1:
996         kfree(data);
997         return err;
998 }
999
1000 static int pc87360_detach_client(struct i2c_client *client)
1001 {
1002         struct pc87360_data *data = i2c_get_clientdata(client);
1003         int i;
1004
1005         hwmon_device_unregister(data->class_dev);
1006
1007         if ((i = i2c_detach_client(client))) {
1008                 dev_err(&client->dev, "Client deregistration failed, "
1009                         "client not detached.\n");
1010                 return i;
1011         }
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                         normal_isa[0] = extra_isa[i];
1338                         break;
1339                 }
1340         }
1341
1342         if (normal_isa[0] == 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);