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