vserver 1.9.3
[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[] = { I2C_CLIENT_END };
46 static unsigned short normal_i2c_range[] = { 0x20, 0x2f, I2C_CLIENT_END };
47 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
48 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
49
50 /* Insmod parameters */
51 SENSORS_INSMOD_2(it87, it8712);
52
53 #define REG     0x2e    /* The register to read/write */
54 #define DEV     0x07    /* Register: Logical device select */
55 #define VAL     0x2f    /* The value to read/write */
56 #define PME     0x04    /* The device with the fan registers in it */
57 #define DEVID   0x20    /* Register: Device ID */
58
59 static inline void
60 superio_outb(int reg, int val)
61 {
62         outb(reg, REG);
63         outb(val, VAL);
64 }
65
66 static inline int
67 superio_inb(int reg)
68 {
69         outb(reg, REG);
70         return inb(VAL);
71 }
72
73 static inline void
74 superio_select(void)
75 {
76         outb(DEV, REG);
77         outb(PME, VAL);
78 }
79
80 static inline void
81 superio_enter(void)
82 {
83         outb(0x87, REG);
84         outb(0x01, REG);
85         outb(0x55, REG);
86         outb(0x55, REG);
87 }
88
89 static inline void
90 superio_exit(void)
91 {
92         outb(0x02, REG);
93         outb(0x02, VAL);
94 }
95
96 /* just IT8712F for now - this should be extended to support the other
97    chips as well */
98 #define IT8712F_DEVID 0x8712
99 #define IT87_ACT_REG  0x30
100 #define IT87_BASE_REG 0x60
101
102 /* Update battery voltage after every reading if true */
103 static int update_vbat;
104
105 /* Reset the registers on init if true */
106 static int reset;
107
108 /* Many IT87 constants specified below */
109
110 /* Length of ISA address segment */
111 #define IT87_EXTENT 8
112
113 /* Where are the ISA address/data registers relative to the base address */
114 #define IT87_ADDR_REG_OFFSET 5
115 #define IT87_DATA_REG_OFFSET 6
116
117 /*----- The IT87 registers -----*/
118
119 #define IT87_REG_CONFIG        0x00
120
121 #define IT87_REG_ALARM1        0x01
122 #define IT87_REG_ALARM2        0x02
123 #define IT87_REG_ALARM3        0x03
124
125 #define IT87_REG_VID           0x0a
126 #define IT87_REG_FAN_DIV       0x0b
127
128 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
129
130 #define IT87_REG_FAN(nr)       (0x0d + (nr))
131 #define IT87_REG_FAN_MIN(nr)   (0x10 + (nr))
132 #define IT87_REG_FAN_MAIN_CTRL 0x13
133
134 #define IT87_REG_VIN(nr)       (0x20 + (nr))
135 #define IT87_REG_TEMP(nr)      (0x29 + (nr))
136
137 #define IT87_REG_VIN_MAX(nr)   (0x30 + (nr) * 2)
138 #define IT87_REG_VIN_MIN(nr)   (0x31 + (nr) * 2)
139 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
140 #define IT87_REG_TEMP_LOW(nr)  (0x41 + (nr) * 2)
141
142 #define IT87_REG_I2C_ADDR      0x48
143
144 #define IT87_REG_VIN_ENABLE    0x50
145 #define IT87_REG_TEMP_ENABLE   0x51
146
147 #define IT87_REG_CHIPID        0x58
148
149 #define IN_TO_REG(val)  (SENSORS_LIMIT((((val) + 8)/16),0,255))
150 #define IN_FROM_REG(val) ((val) * 16)
151
152 static inline u8 FAN_TO_REG(long rpm, int div)
153 {
154         if (rpm == 0)
155                 return 255;
156         rpm = SENSORS_LIMIT(rpm, 1, 1000000);
157         return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
158                              254);
159 }
160
161 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
162
163 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
164                                         ((val)+500)/1000),-128,127))
165 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
166
167 #define ALARMS_FROM_REG(val) (val)
168
169 static int DIV_TO_REG(int val)
170 {
171         int answer = 0;
172         while ((val >>= 1) != 0)
173                 answer++;
174         return answer;
175 }
176 #define DIV_FROM_REG(val) (1 << (val))
177
178
179 /* For each registered IT87, we need to keep some data in memory. That
180    data is pointed to by it87_list[NR]->data. The structure itself is
181    dynamically allocated, at the same time when a new it87 client is
182    allocated. */
183 struct it87_data {
184         struct i2c_client client;
185         struct semaphore lock;
186         enum chips type;
187
188         struct semaphore update_lock;
189         char valid;             /* !=0 if following fields are valid */
190         unsigned long last_updated;     /* In jiffies */
191
192         u8 in[9];               /* Register value */
193         u8 in_max[9];           /* Register value */
194         u8 in_min[9];           /* Register value */
195         u8 fan[3];              /* Register value */
196         u8 fan_min[3];          /* Register value */
197         u8 temp[3];             /* Register value */
198         u8 temp_high[3];        /* Register value */
199         u8 temp_low[3];         /* Register value */
200         u8 sensor;              /* Register value */
201         u8 fan_div[3];          /* Register encoding, shifted right */
202         u8 vid;                 /* Register encoding, combined */
203         int vrm;
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]));
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]));
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]));
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);
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);
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]));
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]));
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]));
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);
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);
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 static ssize_t
547 show_vrm_reg(struct device *dev, char *buf)
548 {
549         struct it87_data *data = it87_update_device(dev);
550         return sprintf(buf, "%ld\n", (long) data->vrm);
551 }
552 static ssize_t
553 store_vrm_reg(struct device *dev, const char *buf, size_t count)
554 {
555         struct i2c_client *client = to_i2c_client(dev);
556         struct it87_data *data = i2c_get_clientdata(client);
557         u32 val;
558
559         val = simple_strtoul(buf, NULL, 10);
560         data->vrm = val;
561
562         return count;
563 }
564 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
565 #define device_create_file_vrm(client) \
566 device_create_file(&client->dev, &dev_attr_vrm)
567
568 static ssize_t
569 show_vid_reg(struct device *dev, char *buf)
570 {
571         struct it87_data *data = it87_update_device(dev);
572         return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
573 }
574 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
575 #define device_create_file_vid(client) \
576 device_create_file(&client->dev, &dev_attr_cpu0_vid)
577
578 /* This function is called when:
579      * it87_driver is inserted (when this module is loaded), for each
580        available adapter
581      * when a new adapter is inserted (and it87_driver is still present) */
582 static int it87_attach_adapter(struct i2c_adapter *adapter)
583 {
584         if (!(adapter->class & I2C_CLASS_HWMON))
585                 return 0;
586         return i2c_detect(adapter, &addr_data, it87_detect);
587 }
588
589 /* SuperIO detection - will change normal_isa[0] if a chip is found */
590 static int it87_find(int *address)
591 {
592         u16 val;
593
594         superio_enter();
595         val = (superio_inb(DEVID) << 8) |
596                superio_inb(DEVID + 1);
597         if (val != IT8712F_DEVID) {
598                 superio_exit();
599                 return -ENODEV;
600         }
601
602         superio_select();
603         val = (superio_inb(IT87_BASE_REG) << 8) |
604                superio_inb(IT87_BASE_REG + 1);
605         superio_exit();
606         *address = val & ~(IT87_EXTENT - 1);
607         if (*address == 0) {
608                 return -ENODEV;
609         }
610         return 0;
611 }
612
613 /* This function is called by i2c_detect */
614 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
615 {
616         int i;
617         struct i2c_client *new_client;
618         struct it87_data *data;
619         int err = 0;
620         const char *name = "";
621         int is_isa = i2c_is_isa_adapter(adapter);
622
623         if (!is_isa && 
624             !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
625                 goto ERROR0;
626
627         /* Reserve the ISA region */
628         if (is_isa)
629                 if (!request_region(address, IT87_EXTENT, name))
630                         goto ERROR0;
631
632         /* Probe whether there is anything available on this address. Already
633            done for SMBus clients */
634         if (kind < 0) {
635                 if (is_isa) {
636
637 #define REALLY_SLOW_IO
638                         /* We need the timeouts for at least some IT87-like chips. But only
639                            if we read 'undefined' registers. */
640                         i = inb_p(address + 1);
641                         if (inb_p(address + 2) != i
642                          || inb_p(address + 3) != i
643                          || inb_p(address + 7) != i) {
644                                 err = -ENODEV;
645                                 goto ERROR1;
646                         }
647 #undef REALLY_SLOW_IO
648
649                         /* Let's just hope nothing breaks here */
650                         i = inb_p(address + 5) & 0x7f;
651                         outb_p(~i & 0x7f, address + 5);
652                         if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
653                                 outb_p(i, address + 5);
654                                 err = -ENODEV;
655                                 goto ERROR1;
656                         }
657                 }
658         }
659
660         /* OK. For now, we presume we have a valid client. We now create the
661            client structure, even though we cannot fill it completely yet.
662            But it allows us to access it87_{read,write}_value. */
663
664         if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
665                 err = -ENOMEM;
666                 goto ERROR1;
667         }
668         memset(data, 0, sizeof(struct it87_data));
669
670         new_client = &data->client;
671         if (is_isa)
672                 init_MUTEX(&data->lock);
673         i2c_set_clientdata(new_client, data);
674         new_client->addr = address;
675         new_client->adapter = adapter;
676         new_client->driver = &it87_driver;
677         new_client->flags = 0;
678
679         /* Now, we do the remaining detection. */
680
681         if (kind < 0) {
682                 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
683                   || (!is_isa
684                    && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
685                         err = -ENODEV;
686                         goto ERROR2;
687                 }
688         }
689
690         /* Determine the chip type. */
691         if (kind <= 0) {
692                 i = it87_read_value(new_client, IT87_REG_CHIPID);
693                 if (i == 0x90) {
694                         u16 val;
695                         kind = it87;
696                         val = (superio_inb(DEVID) << 8) |
697                         superio_inb(DEVID + 1);
698                         if (val == IT8712F_DEVID) kind = it8712;
699                 }
700                 else {
701                         if (kind == 0)
702                                 dev_info(&adapter->dev, 
703                                         "Ignoring 'force' parameter for unknown chip at "
704                                         "adapter %d, address 0x%02x\n",
705                                         i2c_adapter_id(adapter), address);
706                         err = -ENODEV;
707                         goto ERROR2;
708                 }
709         }
710
711         if (kind == it87) {
712                 name = "it87";
713         } else if (kind == it8712) {
714                 name = "it8712";
715         }
716
717         /* Fill in the remaining client fields and put it into the global list */
718         strlcpy(new_client->name, name, I2C_NAME_SIZE);
719
720         data->type = kind;
721
722         new_client->id = it87_id++;
723         data->valid = 0;
724         init_MUTEX(&data->update_lock);
725
726         /* Tell the I2C layer a new client has arrived */
727         if ((err = i2c_attach_client(new_client)))
728                 goto ERROR2;
729
730         /* Initialize the IT87 chip */
731         it87_init_client(new_client, data);
732
733         /* Register sysfs hooks */
734         device_create_file(&new_client->dev, &dev_attr_in0_input);
735         device_create_file(&new_client->dev, &dev_attr_in1_input);
736         device_create_file(&new_client->dev, &dev_attr_in2_input);
737         device_create_file(&new_client->dev, &dev_attr_in3_input);
738         device_create_file(&new_client->dev, &dev_attr_in4_input);
739         device_create_file(&new_client->dev, &dev_attr_in5_input);
740         device_create_file(&new_client->dev, &dev_attr_in6_input);
741         device_create_file(&new_client->dev, &dev_attr_in7_input);
742         device_create_file(&new_client->dev, &dev_attr_in8_input);
743         device_create_file(&new_client->dev, &dev_attr_in0_min);
744         device_create_file(&new_client->dev, &dev_attr_in1_min);
745         device_create_file(&new_client->dev, &dev_attr_in2_min);
746         device_create_file(&new_client->dev, &dev_attr_in3_min);
747         device_create_file(&new_client->dev, &dev_attr_in4_min);
748         device_create_file(&new_client->dev, &dev_attr_in5_min);
749         device_create_file(&new_client->dev, &dev_attr_in6_min);
750         device_create_file(&new_client->dev, &dev_attr_in7_min);
751         device_create_file(&new_client->dev, &dev_attr_in0_max);
752         device_create_file(&new_client->dev, &dev_attr_in1_max);
753         device_create_file(&new_client->dev, &dev_attr_in2_max);
754         device_create_file(&new_client->dev, &dev_attr_in3_max);
755         device_create_file(&new_client->dev, &dev_attr_in4_max);
756         device_create_file(&new_client->dev, &dev_attr_in5_max);
757         device_create_file(&new_client->dev, &dev_attr_in6_max);
758         device_create_file(&new_client->dev, &dev_attr_in7_max);
759         device_create_file(&new_client->dev, &dev_attr_temp1_input);
760         device_create_file(&new_client->dev, &dev_attr_temp2_input);
761         device_create_file(&new_client->dev, &dev_attr_temp3_input);
762         device_create_file(&new_client->dev, &dev_attr_temp1_max);
763         device_create_file(&new_client->dev, &dev_attr_temp2_max);
764         device_create_file(&new_client->dev, &dev_attr_temp3_max);
765         device_create_file(&new_client->dev, &dev_attr_temp1_min);
766         device_create_file(&new_client->dev, &dev_attr_temp2_min);
767         device_create_file(&new_client->dev, &dev_attr_temp3_min);
768         device_create_file(&new_client->dev, &dev_attr_temp1_type);
769         device_create_file(&new_client->dev, &dev_attr_temp2_type);
770         device_create_file(&new_client->dev, &dev_attr_temp3_type);
771         device_create_file(&new_client->dev, &dev_attr_fan1_input);
772         device_create_file(&new_client->dev, &dev_attr_fan2_input);
773         device_create_file(&new_client->dev, &dev_attr_fan3_input);
774         device_create_file(&new_client->dev, &dev_attr_fan1_min);
775         device_create_file(&new_client->dev, &dev_attr_fan2_min);
776         device_create_file(&new_client->dev, &dev_attr_fan3_min);
777         device_create_file(&new_client->dev, &dev_attr_fan1_div);
778         device_create_file(&new_client->dev, &dev_attr_fan2_div);
779         device_create_file(&new_client->dev, &dev_attr_fan3_div);
780         device_create_file(&new_client->dev, &dev_attr_alarms);
781
782         if (data->type == it8712) {
783                 device_create_file_vrm(new_client);
784                 device_create_file_vid(new_client);
785                 data->vrm = i2c_which_vrm();
786         }
787
788         return 0;
789
790 ERROR2:
791         kfree(data);
792 ERROR1:
793         if (is_isa)
794                 release_region(address, IT87_EXTENT);
795 ERROR0:
796         return err;
797 }
798
799 static int it87_detach_client(struct i2c_client *client)
800 {
801         int err;
802
803         if ((err = i2c_detach_client(client))) {
804                 dev_err(&client->dev,
805                         "Client deregistration failed, client not detached.\n");
806                 return err;
807         }
808
809         if(i2c_is_isa_client(client))
810                 release_region(client->addr, IT87_EXTENT);
811         kfree(i2c_get_clientdata(client));
812
813         return 0;
814 }
815
816 /* The SMBus locks itself, but ISA access must be locked explicitely! 
817    We don't want to lock the whole ISA bus, so we lock each client
818    separately.
819    We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
820    would slow down the IT87 access and should not be necessary. */
821 static int it87_read_value(struct i2c_client *client, u8 reg)
822 {
823         struct it87_data *data = i2c_get_clientdata(client);
824
825         int res;
826         if (i2c_is_isa_client(client)) {
827                 down(&data->lock);
828                 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
829                 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
830                 up(&data->lock);
831                 return res;
832         } else
833                 return i2c_smbus_read_byte_data(client, reg);
834 }
835
836 /* The SMBus locks itself, but ISA access muse be locked explicitely! 
837    We don't want to lock the whole ISA bus, so we lock each client
838    separately.
839    We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
840    would slow down the IT87 access and should not be necessary. */
841 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
842 {
843         struct it87_data *data = i2c_get_clientdata(client);
844
845         if (i2c_is_isa_client(client)) {
846                 down(&data->lock);
847                 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
848                 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
849                 up(&data->lock);
850                 return 0;
851         } else
852                 return i2c_smbus_write_byte_data(client, reg, value);
853 }
854
855 /* Called when we have found a new IT87. */
856 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
857 {
858         int tmp;
859
860         if (reset) {
861                 /* Reset all except Watchdog values and last conversion values
862                    This sets fan-divs to 2, among others */
863                 it87_write_value(client, IT87_REG_CONFIG, 0x80);
864         }
865
866         /* Check if temperature channnels are reset manually or by some reason */
867         tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
868         if ((tmp & 0x3f) == 0) {
869                 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
870                 tmp = (tmp & 0xc0) | 0x2a;
871                 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
872         }
873         data->sensor = tmp;
874
875         /* Check if voltage monitors are reset manually or by some reason */
876         tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
877         if ((tmp & 0xff) == 0) {
878                 /* Enable all voltage monitors */
879                 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
880         }
881
882         /* Check if tachometers are reset manually or by some reason */
883         tmp = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
884         if ((tmp & 0x70) == 0) {
885                 /* Enable all fan tachometers */
886                 tmp = (tmp & 0x8f) | 0x70;
887                 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, tmp);
888         }
889
890         /* Start monitoring */
891         it87_write_value(client, IT87_REG_CONFIG,
892                          (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
893                          | (update_vbat ? 0x41 : 0x01));
894 }
895
896 static struct it87_data *it87_update_device(struct device *dev)
897 {
898         struct i2c_client *client = to_i2c_client(dev);
899         struct it87_data *data = i2c_get_clientdata(client);
900         int i;
901
902         down(&data->update_lock);
903
904         if ((jiffies - data->last_updated > HZ + HZ / 2) ||
905             (jiffies < data->last_updated) || !data->valid) {
906
907                 if (update_vbat) {
908                         /* Cleared after each update, so reenable.  Value
909                           returned by this read will be previous value */       
910                         it87_write_value(client, IT87_REG_CONFIG,
911                            it87_read_value(client, IT87_REG_CONFIG) | 0x40);
912                 }
913                 for (i = 0; i <= 7; i++) {
914                         data->in[i] =
915                             it87_read_value(client, IT87_REG_VIN(i));
916                         data->in_min[i] =
917                             it87_read_value(client, IT87_REG_VIN_MIN(i));
918                         data->in_max[i] =
919                             it87_read_value(client, IT87_REG_VIN_MAX(i));
920                 }
921                 data->in[8] =
922                     it87_read_value(client, IT87_REG_VIN(8));
923                 /* Temperature sensor doesn't have limit registers, set
924                    to min and max value */
925                 data->in_min[8] = 0;
926                 data->in_max[8] = 255;
927
928                 for (i = 0; i < 3; i++) {
929                         data->fan[i] =
930                             it87_read_value(client, IT87_REG_FAN(i));
931                         data->fan_min[i] =
932                             it87_read_value(client, IT87_REG_FAN_MIN(i));
933                 }
934                 for (i = 0; i < 3; i++) {
935                         data->temp[i] =
936                             it87_read_value(client, IT87_REG_TEMP(i));
937                         data->temp_high[i] =
938                             it87_read_value(client, IT87_REG_TEMP_HIGH(i));
939                         data->temp_low[i] =
940                             it87_read_value(client, IT87_REG_TEMP_LOW(i));
941                 }
942
943                 /* The 8705 does not have VID capability */
944                 data->vid = 0x1f;
945
946                 i = it87_read_value(client, IT87_REG_FAN_DIV);
947                 data->fan_div[0] = i & 0x07;
948                 data->fan_div[1] = (i >> 3) & 0x07;
949                 data->fan_div[2] = (i & 0x40) ? 3 : 1;
950
951                 data->alarms =
952                         it87_read_value(client, IT87_REG_ALARM1) |
953                         (it87_read_value(client, IT87_REG_ALARM2) << 8) |
954                         (it87_read_value(client, IT87_REG_ALARM3) << 16);
955
956                 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
957                 /* The 8705 does not have VID capability */
958                 if (data->type == it8712) {
959                         data->vid = it87_read_value(client, IT87_REG_VID);
960                         data->vid &= 0x1f;
961                 }
962                 data->last_updated = jiffies;
963                 data->valid = 1;
964         }
965
966         up(&data->update_lock);
967
968         return data;
969 }
970
971 static int __init sm_it87_init(void)
972 {
973         int addr;
974
975         if (!it87_find(&addr)) {
976                 normal_isa[0] = addr;
977         }
978         return i2c_add_driver(&it87_driver);
979 }
980
981 static void __exit sm_it87_exit(void)
982 {
983         i2c_del_driver(&it87_driver);
984 }
985
986
987 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
988 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
989 module_param(update_vbat, bool, 0);
990 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
991 module_param(reset, bool, 0);
992 MODULE_PARM_DESC(reset, "Reset the chip's registers, default no");
993 MODULE_LICENSE("GPL");
994
995 module_init(sm_it87_init);
996 module_exit(sm_it87_exit);