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