patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / i2c / chips / it87.c
1 /*
2     it87.c - Part of lm_sensors, Linux kernel modules for hardware
3              monitoring.
4
5     Supports: IT8705F  Super I/O chip w/LPC interface
6               IT8712F  Super I/O chip w/LPC interface & SMbus
7               Sis950   A clone of the IT8705F
8
9     Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com> 
10     Largely inspired by lm78.c of the same package
11
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; either version 2 of the License, or
15     (at your option) any later version.
16
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU General Public License for more details.
21
22     You should have received a copy of the GNU General Public License
23     along with this program; if not, write to the Free Software
24     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27 /*
28     djg@pdp8.net David Gesswein 7/18/01
29     Modified to fix bug with not all alarms enabled.
30     Added ability to read battery voltage and select temperature sensor
31     type at module load time.
32 */
33
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/i2c.h>
39 #include <linux/i2c-sensor.h>
40 #include <asm/io.h>
41
42
43 /* Addresses to scan */
44 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
45 static unsigned short normal_i2c_range[] = { 0x20, 0x2f, I2C_CLIENT_END };
46 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
47 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
48
49 /* Insmod parameters */
50 SENSORS_INSMOD_1(it87);
51
52 #define REG     0x2e    /* The register to read/write */
53 #define DEV     0x07    /* Register: Logical device select */
54 #define VAL     0x2f    /* The value to read/write */
55 #define PME     0x04    /* The device with the fan registers in it */
56 #define DEVID   0x20    /* Register: Device ID */
57
58 static inline void
59 superio_outb(int reg, int val)
60 {
61         outb(reg, REG);
62         outb(val, VAL);
63 }
64
65 static inline int
66 superio_inb(int reg)
67 {
68         outb(reg, REG);
69         return inb(VAL);
70 }
71
72 static inline void
73 superio_select(void)
74 {
75         outb(DEV, REG);
76         outb(PME, VAL);
77 }
78
79 static inline void
80 superio_enter(void)
81 {
82         outb(0x87, REG);
83         outb(0x01, REG);
84         outb(0x55, REG);
85         outb(0x55, REG);
86 }
87
88 static inline void
89 superio_exit(void)
90 {
91         outb(0x02, REG);
92         outb(0x02, VAL);
93 }
94
95 /* just IT8712F for now - this should be extended to support the other
96    chips as well */
97 #define IT8712F_DEVID 0x8712
98 #define IT87_ACT_REG  0x30
99 #define IT87_BASE_REG 0x60
100
101 /* Update battery voltage after every reading if true */
102 static int update_vbat;
103
104 /* Reset the registers on init if true */
105 static int reset;
106
107 /* Many IT87 constants specified below */
108
109 /* Length of ISA address segment */
110 #define IT87_EXTENT 8
111
112 /* Where are the ISA address/data registers relative to the base address */
113 #define IT87_ADDR_REG_OFFSET 5
114 #define IT87_DATA_REG_OFFSET 6
115
116 /*----- The IT87 registers -----*/
117
118 #define IT87_REG_CONFIG        0x00
119
120 #define IT87_REG_ALARM1        0x01
121 #define IT87_REG_ALARM2        0x02
122 #define IT87_REG_ALARM3        0x03
123
124 #define IT87_REG_VID           0x0a
125 #define IT87_REG_FAN_DIV       0x0b
126
127 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
128
129 #define IT87_REG_FAN(nr)       (0x0d + (nr))
130 #define IT87_REG_FAN_MIN(nr)   (0x10 + (nr))
131 #define IT87_REG_FAN_CTRL      0x13
132
133 #define IT87_REG_VIN(nr)       (0x20 + (nr))
134 #define IT87_REG_TEMP(nr)      (0x29 + (nr))
135
136 #define IT87_REG_VIN_MAX(nr)   (0x30 + (nr) * 2)
137 #define IT87_REG_VIN_MIN(nr)   (0x31 + (nr) * 2)
138 #define IT87_REG_TEMP_HIGH(nr) (0x40 + ((nr) * 2))
139 #define IT87_REG_TEMP_LOW(nr)  (0x41 + ((nr) * 2))
140
141 #define IT87_REG_I2C_ADDR      0x48
142
143 #define IT87_REG_VIN_ENABLE    0x50
144 #define IT87_REG_TEMP_ENABLE   0x51
145
146 #define IT87_REG_CHIPID        0x58
147
148 #define IN_TO_REG(val)  (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
149 #define IN_FROM_REG(val) (((val) *  16) / 10)
150
151 static inline u8 FAN_TO_REG(long rpm, int div)
152 {
153         if (rpm == 0)
154                 return 255;
155         rpm = SENSORS_LIMIT(rpm, 1, 1000000);
156         return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
157                              254);
158 }
159
160 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
161
162 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-5)/10):\
163                                         ((val)+5)/10),0,255))
164 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*10)
165
166 #define VID_FROM_REG(val) ((val)==0x1f?0:(val)>=0x10?510-(val)*10:\
167                                 205-(val)*5)
168 #define ALARMS_FROM_REG(val) (val)
169
170 static int DIV_TO_REG(int val)
171 {
172         int answer = 0;
173         while ((val >>= 1))
174                 answer++;
175         return answer;
176 }
177 #define DIV_FROM_REG(val) (1 << (val))
178
179
180 /* For each registered IT87, we need to keep some data in memory. That
181    data is pointed to by it87_list[NR]->data. The structure itself is
182    dynamically allocated, at the same time when a new it87 client is
183    allocated. */
184 struct it87_data {
185         struct i2c_client client;
186         struct semaphore lock;
187         enum chips type;
188
189         struct semaphore update_lock;
190         char valid;             /* !=0 if following fields are valid */
191         unsigned long last_updated;     /* In jiffies */
192
193         u8 in[9];               /* Register value */
194         u8 in_max[9];           /* Register value */
195         u8 in_min[9];           /* Register value */
196         u8 fan[3];              /* Register value */
197         u8 fan_min[3];          /* Register value */
198         u8 temp[3];             /* Register value */
199         u8 temp_high[3];        /* Register value */
200         u8 temp_low[3];         /* Register value */
201         u8 sensor;              /* Register value */
202         u8 fan_div[3];          /* Register encoding, shifted right */
203         u8 vid;                 /* Register encoding, combined */
204         u32 alarms;             /* Register encoding, combined */
205 };
206
207
208 static int it87_attach_adapter(struct i2c_adapter *adapter);
209 static int it87_find(int *address);
210 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
211 static int it87_detach_client(struct i2c_client *client);
212
213 static int it87_read_value(struct i2c_client *client, u8 register);
214 static int it87_write_value(struct i2c_client *client, u8 register,
215                         u8 value);
216 static struct it87_data *it87_update_device(struct device *dev);
217 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
218
219
220 static struct i2c_driver it87_driver = {
221         .owner          = THIS_MODULE,
222         .name           = "it87",
223         .id             = I2C_DRIVERID_IT87,
224         .flags          = I2C_DF_NOTIFY,
225         .attach_adapter = it87_attach_adapter,
226         .detach_client  = it87_detach_client,
227 };
228
229 static int it87_id = 0;
230
231 static ssize_t show_in(struct device *dev, char *buf, int nr)
232 {
233         struct it87_data *data = it87_update_device(dev);
234         return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr])*10 );
235 }
236
237 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
238 {
239         struct it87_data *data = it87_update_device(dev);
240         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr])*10 );
241 }
242
243 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
244 {
245         struct it87_data *data = it87_update_device(dev);
246         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr])*10 );
247 }
248
249 static ssize_t set_in_min(struct device *dev, const char *buf, 
250                 size_t count, int nr)
251 {
252         struct i2c_client *client = to_i2c_client(dev);
253         struct it87_data *data = i2c_get_clientdata(client);
254         unsigned long val = simple_strtoul(buf, NULL, 10)/10;
255         data->in_min[nr] = IN_TO_REG(val);
256         it87_write_value(client, IT87_REG_VIN_MIN(nr), 
257                         data->in_min[nr]);
258         return count;
259 }
260 static ssize_t set_in_max(struct device *dev, const char *buf, 
261                 size_t count, int nr)
262 {
263         struct i2c_client *client = to_i2c_client(dev);
264         struct it87_data *data = i2c_get_clientdata(client);
265         unsigned long val = simple_strtoul(buf, NULL, 10)/10;
266         data->in_max[nr] = IN_TO_REG(val);
267         it87_write_value(client, IT87_REG_VIN_MAX(nr), 
268                         data->in_max[nr]);
269         return count;
270 }
271
272 #define show_in_offset(offset)                                  \
273 static ssize_t                                                  \
274         show_in##offset (struct device *dev, char *buf)         \
275 {                                                               \
276         return show_in(dev, buf, 0x##offset);                   \
277 }                                                               \
278 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL)
279
280 #define limit_in_offset(offset)                                 \
281 static ssize_t                                                  \
282         show_in##offset##_min (struct device *dev, char *buf)   \
283 {                                                               \
284         return show_in_min(dev, buf, 0x##offset);               \
285 }                                                               \
286 static ssize_t                                                  \
287         show_in##offset##_max (struct device *dev, char *buf)   \
288 {                                                               \
289         return show_in_max(dev, buf, 0x##offset);               \
290 }                                                               \
291 static ssize_t set_in##offset##_min (struct device *dev,        \
292                 const char *buf, size_t count)                  \
293 {                                                               \
294         return set_in_min(dev, buf, count, 0x##offset);         \
295 }                                                               \
296 static ssize_t set_in##offset##_max (struct device *dev,        \
297                         const char *buf, size_t count)          \
298 {                                                               \
299         return set_in_max(dev, buf, count, 0x##offset);         \
300 }                                                               \
301 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,         \
302                 show_in##offset##_min, set_in##offset##_min)    \
303 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,         \
304                 show_in##offset##_max, set_in##offset##_max)
305
306 show_in_offset(0);
307 limit_in_offset(0);
308 show_in_offset(1);
309 limit_in_offset(1);
310 show_in_offset(2);
311 limit_in_offset(2);
312 show_in_offset(3);
313 limit_in_offset(3);
314 show_in_offset(4);
315 limit_in_offset(4);
316 show_in_offset(5);
317 limit_in_offset(5);
318 show_in_offset(6);
319 limit_in_offset(6);
320 show_in_offset(7);
321 limit_in_offset(7);
322 show_in_offset(8);
323
324 /* 3 temperatures */
325 static ssize_t show_temp(struct device *dev, char *buf, int nr)
326 {
327         struct it87_data *data = it87_update_device(dev);
328         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])*100 );
329 }
330 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
331 {
332         struct it87_data *data = it87_update_device(dev);
333         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr])*100);
334 }
335 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
336 {
337         struct it87_data *data = it87_update_device(dev);
338         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr])*100);
339 }
340 static ssize_t set_temp_max(struct device *dev, const char *buf, 
341                 size_t count, int nr)
342 {
343         struct i2c_client *client = to_i2c_client(dev);
344         struct it87_data *data = i2c_get_clientdata(client);
345         int val = simple_strtol(buf, NULL, 10)/100;
346         data->temp_high[nr] = TEMP_TO_REG(val);
347         it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
348         return count;
349 }
350 static ssize_t set_temp_min(struct device *dev, const char *buf, 
351                 size_t count, int nr)
352 {
353         struct i2c_client *client = to_i2c_client(dev);
354         struct it87_data *data = i2c_get_clientdata(client);
355         int val = simple_strtol(buf, NULL, 10)/100;
356         data->temp_low[nr] = TEMP_TO_REG(val);
357         it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
358         return count;
359 }
360 #define show_temp_offset(offset)                                        \
361 static ssize_t show_temp_##offset (struct device *dev, char *buf)       \
362 {                                                                       \
363         return show_temp(dev, buf, 0x##offset - 1);                     \
364 }                                                                       \
365 static ssize_t                                                          \
366 show_temp_##offset##_max (struct device *dev, char *buf)                \
367 {                                                                       \
368         return show_temp_max(dev, buf, 0x##offset - 1);                 \
369 }                                                                       \
370 static ssize_t                                                          \
371 show_temp_##offset##_min (struct device *dev, char *buf)                \
372 {                                                                       \
373         return show_temp_min(dev, buf, 0x##offset - 1);                 \
374 }                                                                       \
375 static ssize_t set_temp_##offset##_max (struct device *dev,             \
376                 const char *buf, size_t count)                          \
377 {                                                                       \
378         return set_temp_max(dev, buf, count, 0x##offset - 1);           \
379 }                                                                       \
380 static ssize_t set_temp_##offset##_min (struct device *dev,             \
381                 const char *buf, size_t count)                          \
382 {                                                                       \
383         return set_temp_min(dev, buf, count, 0x##offset - 1);           \
384 }                                                                       \
385 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL) \
386 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,               \
387                 show_temp_##offset##_max, set_temp_##offset##_max)      \
388 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,               \
389                 show_temp_##offset##_min, set_temp_##offset##_min)      
390
391 show_temp_offset(1);
392 show_temp_offset(2);
393 show_temp_offset(3);
394
395 static ssize_t show_sensor(struct device *dev, char *buf, int nr)
396 {
397         struct it87_data *data = it87_update_device(dev);
398         if (data->sensor & (1 << nr))
399                 return sprintf(buf, "3\n");  /* thermal diode */
400         if (data->sensor & (8 << nr))
401                 return sprintf(buf, "2\n");  /* thermistor */
402         return sprintf(buf, "0\n");      /* disabled */
403 }
404 static ssize_t set_sensor(struct device *dev, const char *buf, 
405                 size_t count, int nr)
406 {
407         struct i2c_client *client = to_i2c_client(dev);
408         struct it87_data *data = i2c_get_clientdata(client);
409         int val = simple_strtol(buf, NULL, 10);
410
411         data->sensor &= ~(1 << nr);
412         data->sensor &= ~(8 << nr);
413         /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
414         if (val == 3)
415             data->sensor |= 1 << nr;
416         else if (val == 2)
417             data->sensor |= 8 << nr;
418         else if (val != 0)
419                 return -EINVAL;
420         it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
421         return count;
422 }
423 #define show_sensor_offset(offset)                                      \
424 static ssize_t show_sensor_##offset (struct device *dev, char *buf)     \
425 {                                                                       \
426         return show_sensor(dev, buf, 0x##offset - 1);                   \
427 }                                                                       \
428 static ssize_t set_sensor_##offset (struct device *dev,                 \
429                 const char *buf, size_t count)                          \
430 {                                                                       \
431         return set_sensor(dev, buf, count, 0x##offset - 1);             \
432 }                                                                       \
433 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR,                      \
434                 show_sensor_##offset, set_sensor_##offset)
435
436 show_sensor_offset(1);
437 show_sensor_offset(2);
438 show_sensor_offset(3);
439
440 /* 3 Fans */
441 static ssize_t show_fan(struct device *dev, char *buf, int nr)
442 {
443         struct it87_data *data = it87_update_device(dev);
444         return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr], 
445                                 DIV_FROM_REG(data->fan_div[nr])) );
446 }
447 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
448 {
449         struct it87_data *data = it87_update_device(dev);
450         return sprintf(buf,"%d\n",
451                 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])) );
452 }
453 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
454 {
455         struct it87_data *data = it87_update_device(dev);
456         return sprintf(buf,"%d\n", DIV_FROM_REG(data->fan_div[nr]) );
457 }
458 static ssize_t set_fan_min(struct device *dev, const char *buf, 
459                 size_t count, int nr)
460 {
461         struct i2c_client *client = to_i2c_client(dev);
462         struct it87_data *data = i2c_get_clientdata(client);
463         int val = simple_strtol(buf, NULL, 10);
464         data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
465         it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
466         return count;
467 }
468 static ssize_t set_fan_div(struct device *dev, const char *buf, 
469                 size_t count, int nr)
470 {
471         struct i2c_client *client = to_i2c_client(dev);
472         struct it87_data *data = i2c_get_clientdata(client);
473         int val = simple_strtol(buf, NULL, 10);
474         int i, min[3];
475         u8 old = it87_read_value(client, IT87_REG_FAN_DIV);
476
477         for (i = 0; i < 3; i++)
478                 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
479
480         switch (nr) {
481         case 0:
482         case 1:
483                 data->fan_div[nr] = DIV_TO_REG(val);
484                 break;
485         case 2:
486                 if (val < 8)
487                         data->fan_div[nr] = 1;
488                 else
489                         data->fan_div[nr] = 3;
490         }
491         val = old & 0x100;
492         val |= (data->fan_div[0] & 0x07);
493         val |= (data->fan_div[1] & 0x07) << 3;
494         if (data->fan_div[2] == 3)
495                 val |= 0x1 << 6;
496         it87_write_value(client, IT87_REG_FAN_DIV, val);
497
498         for (i = 0; i < 3; i++) {
499                 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
500                 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
501         }
502         return count;
503 }
504
505 #define show_fan_offset(offset)                                         \
506 static ssize_t show_fan_##offset (struct device *dev, char *buf)        \
507 {                                                                       \
508         return show_fan(dev, buf, 0x##offset - 1);                      \
509 }                                                                       \
510 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf)  \
511 {                                                                       \
512         return show_fan_min(dev, buf, 0x##offset - 1);                  \
513 }                                                                       \
514 static ssize_t show_fan_##offset##_div (struct device *dev, char *buf)  \
515 {                                                                       \
516         return show_fan_div(dev, buf, 0x##offset - 1);                  \
517 }                                                                       \
518 static ssize_t set_fan_##offset##_min (struct device *dev,              \
519         const char *buf, size_t count)                                  \
520 {                                                                       \
521         return set_fan_min(dev, buf, count, 0x##offset - 1);            \
522 }                                                                       \
523 static ssize_t set_fan_##offset##_div (struct device *dev,              \
524                 const char *buf, size_t count)                          \
525 {                                                                       \
526         return set_fan_div(dev, buf, count, 0x##offset - 1);            \
527 }                                                                       \
528 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL) \
529 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,                \
530                 show_fan_##offset##_min, set_fan_##offset##_min)        \
531 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR,                \
532                 show_fan_##offset##_div, set_fan_##offset##_div)
533
534 show_fan_offset(1);
535 show_fan_offset(2);
536 show_fan_offset(3);
537
538 /* Alarms */
539 static ssize_t show_alarms(struct device *dev, char *buf)
540 {
541         struct it87_data *data = it87_update_device(dev);
542         return sprintf(buf,"%d\n", ALARMS_FROM_REG(data->alarms));
543 }
544 static DEVICE_ATTR(alarms, S_IRUGO | S_IWUSR, show_alarms, NULL);
545
546 /* This function is called when:
547      * it87_driver is inserted (when this module is loaded), for each
548        available adapter
549      * when a new adapter is inserted (and it87_driver is still present) */
550 static int it87_attach_adapter(struct i2c_adapter *adapter)
551 {
552         if (!(adapter->class & I2C_CLASS_HWMON))
553                 return 0;
554         return i2c_detect(adapter, &addr_data, it87_detect);
555 }
556
557 /* SuperIO detection - will change normal_isa[0] if a chip is found */
558 static int it87_find(int *address)
559 {
560         u16 val;
561
562         superio_enter();
563         val = (superio_inb(DEVID) << 8) |
564                superio_inb(DEVID + 1);
565         if (val != IT8712F_DEVID) {
566                 superio_exit();
567                 return -ENODEV;
568         }
569
570         superio_select();
571         val = (superio_inb(IT87_BASE_REG) << 8) |
572                superio_inb(IT87_BASE_REG + 1);
573         superio_exit();
574         *address = val & ~(IT87_EXTENT - 1);
575         if (*address == 0) {
576                 return -ENODEV;
577         }
578         return 0;
579 }
580
581 /* This function is called by i2c_detect */
582 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
583 {
584         int i;
585         struct i2c_client *new_client;
586         struct it87_data *data;
587         int err = 0;
588         const char *name = "";
589         int is_isa = i2c_is_isa_adapter(adapter);
590
591         if (!is_isa && 
592             !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
593                 goto ERROR0;
594
595         /* Reserve the ISA region */
596         if (is_isa)
597                 if (!request_region(address, IT87_EXTENT, name))
598                         goto ERROR0;
599
600         /* Probe whether there is anything available on this address. Already
601            done for SMBus clients */
602         if (kind < 0) {
603                 if (is_isa) {
604
605 #define REALLY_SLOW_IO
606                         /* We need the timeouts for at least some IT87-like chips. But only
607                            if we read 'undefined' registers. */
608                         i = inb_p(address + 1);
609                         if (inb_p(address + 2) != i
610                          || inb_p(address + 3) != i
611                          || inb_p(address + 7) != i) {
612                                 err = -ENODEV;
613                                 goto ERROR1;
614                         }
615 #undef REALLY_SLOW_IO
616
617                         /* Let's just hope nothing breaks here */
618                         i = inb_p(address + 5) & 0x7f;
619                         outb_p(~i & 0x7f, address + 5);
620                         if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
621                                 outb_p(i, address + 5);
622                                 err = -ENODEV;
623                                 goto ERROR1;
624                         }
625                 }
626         }
627
628         /* OK. For now, we presume we have a valid client. We now create the
629            client structure, even though we cannot fill it completely yet.
630            But it allows us to access it87_{read,write}_value. */
631
632         if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
633                 err = -ENOMEM;
634                 goto ERROR1;
635         }
636         memset(data, 0, sizeof(struct it87_data));
637
638         new_client = &data->client;
639         if (is_isa)
640                 init_MUTEX(&data->lock);
641         i2c_set_clientdata(new_client, data);
642         new_client->addr = address;
643         new_client->adapter = adapter;
644         new_client->driver = &it87_driver;
645         new_client->flags = 0;
646
647         /* Now, we do the remaining detection. */
648
649         if (kind < 0) {
650                 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
651                   || (!is_isa
652                    && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
653                         err = -ENODEV;
654                         goto ERROR2;
655                 }
656         }
657
658         /* Determine the chip type. */
659         if (kind <= 0) {
660                 i = it87_read_value(new_client, IT87_REG_CHIPID);
661                 if (i == 0x90) {
662                         kind = it87;
663                 }
664                 else {
665                         if (kind == 0)
666                                 dev_info(&adapter->dev, 
667                                         "Ignoring 'force' parameter for unknown chip at "
668                                         "adapter %d, address 0x%02x\n",
669                                         i2c_adapter_id(adapter), address);
670                         err = -ENODEV;
671                         goto ERROR2;
672                 }
673         }
674
675         if (kind == it87) {
676                 name = "it87";
677         }
678
679         /* Fill in the remaining client fields and put it into the global list */
680         strlcpy(new_client->name, name, I2C_NAME_SIZE);
681
682         data->type = kind;
683
684         new_client->id = it87_id++;
685         data->valid = 0;
686         init_MUTEX(&data->update_lock);
687
688         /* Tell the I2C layer a new client has arrived */
689         if ((err = i2c_attach_client(new_client)))
690                 goto ERROR2;
691
692         /* Initialize the IT87 chip */
693         it87_init_client(new_client, data);
694
695         /* Register sysfs hooks */
696         device_create_file(&new_client->dev, &dev_attr_in0_input);
697         device_create_file(&new_client->dev, &dev_attr_in1_input);
698         device_create_file(&new_client->dev, &dev_attr_in2_input);
699         device_create_file(&new_client->dev, &dev_attr_in3_input);
700         device_create_file(&new_client->dev, &dev_attr_in4_input);
701         device_create_file(&new_client->dev, &dev_attr_in5_input);
702         device_create_file(&new_client->dev, &dev_attr_in6_input);
703         device_create_file(&new_client->dev, &dev_attr_in7_input);
704         device_create_file(&new_client->dev, &dev_attr_in8_input);
705         device_create_file(&new_client->dev, &dev_attr_in0_min);
706         device_create_file(&new_client->dev, &dev_attr_in1_min);
707         device_create_file(&new_client->dev, &dev_attr_in2_min);
708         device_create_file(&new_client->dev, &dev_attr_in3_min);
709         device_create_file(&new_client->dev, &dev_attr_in4_min);
710         device_create_file(&new_client->dev, &dev_attr_in5_min);
711         device_create_file(&new_client->dev, &dev_attr_in6_min);
712         device_create_file(&new_client->dev, &dev_attr_in7_min);
713         device_create_file(&new_client->dev, &dev_attr_in0_max);
714         device_create_file(&new_client->dev, &dev_attr_in1_max);
715         device_create_file(&new_client->dev, &dev_attr_in2_max);
716         device_create_file(&new_client->dev, &dev_attr_in3_max);
717         device_create_file(&new_client->dev, &dev_attr_in4_max);
718         device_create_file(&new_client->dev, &dev_attr_in5_max);
719         device_create_file(&new_client->dev, &dev_attr_in6_max);
720         device_create_file(&new_client->dev, &dev_attr_in7_max);
721         device_create_file(&new_client->dev, &dev_attr_temp1_input);
722         device_create_file(&new_client->dev, &dev_attr_temp2_input);
723         device_create_file(&new_client->dev, &dev_attr_temp3_input);
724         device_create_file(&new_client->dev, &dev_attr_temp1_max);
725         device_create_file(&new_client->dev, &dev_attr_temp2_max);
726         device_create_file(&new_client->dev, &dev_attr_temp3_max);
727         device_create_file(&new_client->dev, &dev_attr_temp1_min);
728         device_create_file(&new_client->dev, &dev_attr_temp2_min);
729         device_create_file(&new_client->dev, &dev_attr_temp3_min);
730         device_create_file(&new_client->dev, &dev_attr_temp1_type);
731         device_create_file(&new_client->dev, &dev_attr_temp2_type);
732         device_create_file(&new_client->dev, &dev_attr_temp3_type);
733         device_create_file(&new_client->dev, &dev_attr_fan1_input);
734         device_create_file(&new_client->dev, &dev_attr_fan2_input);
735         device_create_file(&new_client->dev, &dev_attr_fan3_input);
736         device_create_file(&new_client->dev, &dev_attr_fan1_min);
737         device_create_file(&new_client->dev, &dev_attr_fan2_min);
738         device_create_file(&new_client->dev, &dev_attr_fan3_min);
739         device_create_file(&new_client->dev, &dev_attr_fan1_div);
740         device_create_file(&new_client->dev, &dev_attr_fan2_div);
741         device_create_file(&new_client->dev, &dev_attr_fan3_div);
742         device_create_file(&new_client->dev, &dev_attr_alarms);
743
744         return 0;
745
746 ERROR2:
747         kfree(data);
748 ERROR1:
749         if (is_isa)
750                 release_region(address, IT87_EXTENT);
751 ERROR0:
752         return err;
753 }
754
755 static int it87_detach_client(struct i2c_client *client)
756 {
757         int err;
758
759         if ((err = i2c_detach_client(client))) {
760                 dev_err(&client->dev,
761                         "Client deregistration failed, client not detached.\n");
762                 return err;
763         }
764
765         if(i2c_is_isa_client(client))
766                 release_region(client->addr, IT87_EXTENT);
767         kfree(i2c_get_clientdata(client));
768
769         return 0;
770 }
771
772 /* The SMBus locks itself, but ISA access must be locked explicitely! 
773    We don't want to lock the whole ISA bus, so we lock each client
774    separately.
775    We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
776    would slow down the IT87 access and should not be necessary. 
777    There are some ugly typecasts here, but the good new is - they should
778    nowhere else be necessary! */
779 static int it87_read_value(struct i2c_client *client, u8 reg)
780 {
781         struct it87_data *data = i2c_get_clientdata(client);
782
783         int res;
784         if (i2c_is_isa_client(client)) {
785                 down(&data->lock);
786                 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
787                 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
788                 up(&data->lock);
789                 return res;
790         } else
791                 return i2c_smbus_read_byte_data(client, reg);
792 }
793
794 /* The SMBus locks itself, but ISA access muse be locked explicitely! 
795    We don't want to lock the whole ISA bus, so we lock each client
796    separately.
797    We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
798    would slow down the IT87 access and should not be necessary. 
799    There are some ugly typecasts here, but the good new is - they should
800    nowhere else be necessary! */
801 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
802 {
803         struct it87_data *data = i2c_get_clientdata(client);
804
805         if (i2c_is_isa_client(client)) {
806                 down(&data->lock);
807                 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
808                 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
809                 up(&data->lock);
810                 return 0;
811         } else
812                 return i2c_smbus_write_byte_data(client, reg, value);
813 }
814
815 /* Called when we have found a new IT87. */
816 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
817 {
818         int tmp;
819
820         if (reset) {
821                 /* Reset all except Watchdog values and last conversion values
822                    This sets fan-divs to 2, among others */
823                 it87_write_value(client, IT87_REG_CONFIG, 0x80);
824         }
825
826         /* Check if temperature channnels are reset manually or by some reason */
827         tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
828         if ((tmp & 0x3f) == 0) {
829                 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
830                 tmp = (tmp & 0xc0) | 0x2a;
831                 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
832         }
833         data->sensor = tmp;
834
835         /* Check if voltage monitors are reset manually or by some reason */
836         tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
837         if ((tmp & 0xff) == 0) {
838                 /* Enable all voltage monitors */
839                 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
840         }
841
842         /* Check if tachometers are reset manually or by some reason */
843         tmp = it87_read_value(client, IT87_REG_FAN_CTRL);
844         if ((tmp & 0x70) == 0) {
845                 /* Enable all fan tachometers */
846                 tmp = (tmp & 0x8f) | 0x70;
847                 it87_write_value(client, IT87_REG_FAN_CTRL, tmp);
848         }
849
850         /* Start monitoring */
851         it87_write_value(client, IT87_REG_CONFIG,
852                          (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
853                          | (update_vbat ? 0x41 : 0x01));
854 }
855
856 static struct it87_data *it87_update_device(struct device *dev)
857 {
858         struct i2c_client *client = to_i2c_client(dev);
859         struct it87_data *data = i2c_get_clientdata(client);
860         int i;
861
862         down(&data->update_lock);
863
864         if ((jiffies - data->last_updated > HZ + HZ / 2) ||
865             (jiffies < data->last_updated) || !data->valid) {
866
867                 if (update_vbat) {
868                         /* Cleared after each update, so reenable.  Value
869                           returned by this read will be previous value */       
870                         it87_write_value(client, IT87_REG_CONFIG,
871                            it87_read_value(client, IT87_REG_CONFIG) | 0x40);
872                 }
873                 for (i = 0; i <= 7; i++) {
874                         data->in[i] =
875                             it87_read_value(client, IT87_REG_VIN(i));
876                         data->in_min[i] =
877                             it87_read_value(client, IT87_REG_VIN_MIN(i));
878                         data->in_max[i] =
879                             it87_read_value(client, IT87_REG_VIN_MAX(i));
880                 }
881                 data->in[8] =
882                     it87_read_value(client, IT87_REG_VIN(8));
883                 /* Temperature sensor doesn't have limit registers, set
884                    to min and max value */
885                 data->in_min[8] = 0;
886                 data->in_max[8] = 255;
887
888                 for (i = 0; i < 3; i++) {
889                         data->fan[i] =
890                             it87_read_value(client, IT87_REG_FAN(i));
891                         data->fan_min[i] =
892                             it87_read_value(client, IT87_REG_FAN_MIN(i));
893                 }
894                 for (i = 0; i < 3; i++) {
895                         data->temp[i] =
896                             it87_read_value(client, IT87_REG_TEMP(i));
897                         data->temp_high[i] =
898                             it87_read_value(client, IT87_REG_TEMP_HIGH(i));
899                         data->temp_low[i] =
900                             it87_read_value(client, IT87_REG_TEMP_LOW(i));
901                 }
902
903                 /* The 8705 does not have VID capability */
904                 data->vid = 0x1f;
905
906                 i = it87_read_value(client, IT87_REG_FAN_DIV);
907                 data->fan_div[0] = i & 0x07;
908                 data->fan_div[1] = (i >> 3) & 0x07;
909                 data->fan_div[2] = (i & 0x40) ? 3 : 1;
910
911                 data->alarms =
912                         it87_read_value(client, IT87_REG_ALARM1) |
913                         (it87_read_value(client, IT87_REG_ALARM2) << 8) |
914                         (it87_read_value(client, IT87_REG_ALARM3) << 16);
915
916                 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
917
918                 data->last_updated = jiffies;
919                 data->valid = 1;
920         }
921
922         up(&data->update_lock);
923
924         return data;
925 }
926
927 static int __init sm_it87_init(void)
928 {
929         int addr;
930
931         if (!it87_find(&addr)) {
932                 normal_isa[0] = addr;
933         }
934         return i2c_add_driver(&it87_driver);
935 }
936
937 static void __exit sm_it87_exit(void)
938 {
939         i2c_del_driver(&it87_driver);
940 }
941
942
943 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
944 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
945 MODULE_PARM(update_vbat, "i");
946 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
947 MODULE_PARM(reset, "i");
948 MODULE_PARM_DESC(reset, "Reset the chip's registers, default no");
949 MODULE_LICENSE("GPL");
950
951 module_init(sm_it87_init);
952 module_exit(sm_it87_exit);