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