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