it87: Add support for the IT8718F
[deliverable/linux.git] / drivers / hwmon / 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 IT8716F Super I/O chip w/LPC interface
8 IT8718F Super I/O chip w/LPC interface
9 Sis950 A clone of the IT8705F
10
11 Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
12 Largely inspired by lm78.c of the same package
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29 /*
30 djg@pdp8.net David Gesswein 7/18/01
31 Modified to fix bug with not all alarms enabled.
32 Added ability to read battery voltage and select temperature sensor
33 type at module load time.
34 */
35
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/jiffies.h>
40 #include <linux/i2c.h>
41 #include <linux/i2c-isa.h>
42 #include <linux/hwmon.h>
43 #include <linux/hwmon-sysfs.h>
44 #include <linux/hwmon-vid.h>
45 #include <linux/err.h>
46 #include <linux/mutex.h>
47 #include <asm/io.h>
48
49
50 /* Addresses to scan */
51 static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
52 static unsigned short isa_address;
53
54 /* Insmod parameters */
55 I2C_CLIENT_INSMOD_4(it87, it8712, it8716, it8718);
56
57 #define REG 0x2e /* The register to read/write */
58 #define DEV 0x07 /* Register: Logical device select */
59 #define VAL 0x2f /* The value to read/write */
60 #define PME 0x04 /* The device with the fan registers in it */
61 #define GPIO 0x07 /* The device with the IT8718F VID value in it */
62 #define DEVID 0x20 /* Register: Device ID */
63 #define DEVREV 0x22 /* Register: Device Revision */
64
65 static inline int
66 superio_inb(int reg)
67 {
68 outb(reg, REG);
69 return inb(VAL);
70 }
71
72 static int superio_inw(int reg)
73 {
74 int val;
75 outb(reg++, REG);
76 val = inb(VAL) << 8;
77 outb(reg, REG);
78 val |= inb(VAL);
79 return val;
80 }
81
82 static inline void
83 superio_select(int ldn)
84 {
85 outb(DEV, REG);
86 outb(ldn, VAL);
87 }
88
89 static inline void
90 superio_enter(void)
91 {
92 outb(0x87, REG);
93 outb(0x01, REG);
94 outb(0x55, REG);
95 outb(0x55, REG);
96 }
97
98 static inline void
99 superio_exit(void)
100 {
101 outb(0x02, REG);
102 outb(0x02, VAL);
103 }
104
105 /* Logical device 4 registers */
106 #define IT8712F_DEVID 0x8712
107 #define IT8705F_DEVID 0x8705
108 #define IT8716F_DEVID 0x8716
109 #define IT8718F_DEVID 0x8718
110 #define IT87_ACT_REG 0x30
111 #define IT87_BASE_REG 0x60
112
113 /* Logical device 7 registers (IT8712F and later) */
114 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
115 #define IT87_SIO_VID_REG 0xfc /* VID value */
116
117 /* Update battery voltage after every reading if true */
118 static int update_vbat;
119
120 /* Not all BIOSes properly configure the PWM registers */
121 static int fix_pwm_polarity;
122
123 /* Values read from Super-I/O config space */
124 static u16 chip_type;
125 static u8 vid_value;
126
127 /* Many IT87 constants specified below */
128
129 /* Length of ISA address segment */
130 #define IT87_EXTENT 8
131
132 /* Where are the ISA address/data registers relative to the base address */
133 #define IT87_ADDR_REG_OFFSET 5
134 #define IT87_DATA_REG_OFFSET 6
135
136 /*----- The IT87 registers -----*/
137
138 #define IT87_REG_CONFIG 0x00
139
140 #define IT87_REG_ALARM1 0x01
141 #define IT87_REG_ALARM2 0x02
142 #define IT87_REG_ALARM3 0x03
143
144 /* The IT8718F has the VID value in a different register, in Super-I/O
145 configuration space. */
146 #define IT87_REG_VID 0x0a
147 /* Warning: register 0x0b is used for something completely different in
148 new chips/revisions. I suspect only 16-bit tachometer mode will work
149 for these. */
150 #define IT87_REG_FAN_DIV 0x0b
151 #define IT87_REG_FAN_16BIT 0x0c
152
153 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
154
155 #define IT87_REG_FAN(nr) (0x0d + (nr))
156 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
157 #define IT87_REG_FANX(nr) (0x18 + (nr))
158 #define IT87_REG_FANX_MIN(nr) (0x1b + (nr))
159 #define IT87_REG_FAN_MAIN_CTRL 0x13
160 #define IT87_REG_FAN_CTL 0x14
161 #define IT87_REG_PWM(nr) (0x15 + (nr))
162
163 #define IT87_REG_VIN(nr) (0x20 + (nr))
164 #define IT87_REG_TEMP(nr) (0x29 + (nr))
165
166 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
167 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
168 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
169 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
170
171 #define IT87_REG_I2C_ADDR 0x48
172
173 #define IT87_REG_VIN_ENABLE 0x50
174 #define IT87_REG_TEMP_ENABLE 0x51
175
176 #define IT87_REG_CHIPID 0x58
177
178 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
179 #define IN_FROM_REG(val) ((val) * 16)
180
181 static inline u8 FAN_TO_REG(long rpm, int div)
182 {
183 if (rpm == 0)
184 return 255;
185 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
186 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
187 254);
188 }
189
190 static inline u16 FAN16_TO_REG(long rpm)
191 {
192 if (rpm == 0)
193 return 0xffff;
194 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
195 }
196
197 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
198 /* The divider is fixed to 2 in 16-bit mode */
199 #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
200
201 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
202 ((val)+500)/1000),-128,127))
203 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
204
205 #define PWM_TO_REG(val) ((val) >> 1)
206 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
207
208 static int DIV_TO_REG(int val)
209 {
210 int answer = 0;
211 while (answer < 7 && (val >>= 1))
212 answer++;
213 return answer;
214 }
215 #define DIV_FROM_REG(val) (1 << (val))
216
217
218 /* For each registered IT87, we need to keep some data in memory. That
219 data is pointed to by it87_list[NR]->data. The structure itself is
220 dynamically allocated, at the same time when a new it87 client is
221 allocated. */
222 struct it87_data {
223 struct i2c_client client;
224 struct class_device *class_dev;
225 struct mutex lock;
226 enum chips type;
227
228 struct mutex update_lock;
229 char valid; /* !=0 if following fields are valid */
230 unsigned long last_updated; /* In jiffies */
231
232 u8 in[9]; /* Register value */
233 u8 in_max[8]; /* Register value */
234 u8 in_min[8]; /* Register value */
235 u8 has_fan; /* Bitfield, fans enabled */
236 u16 fan[3]; /* Register values, possibly combined */
237 u16 fan_min[3]; /* Register values, possibly combined */
238 u8 temp[3]; /* Register value */
239 u8 temp_high[3]; /* Register value */
240 u8 temp_low[3]; /* Register value */
241 u8 sensor; /* Register value */
242 u8 fan_div[3]; /* Register encoding, shifted right */
243 u8 vid; /* Register encoding, combined */
244 u8 vrm;
245 u32 alarms; /* Register encoding, combined */
246 u8 fan_main_ctrl; /* Register value */
247 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
248 };
249
250
251 static int it87_attach_adapter(struct i2c_adapter *adapter);
252 static int it87_isa_attach_adapter(struct i2c_adapter *adapter);
253 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
254 static int it87_detach_client(struct i2c_client *client);
255
256 static int it87_read_value(struct i2c_client *client, u8 reg);
257 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value);
258 static struct it87_data *it87_update_device(struct device *dev);
259 static int it87_check_pwm(struct i2c_client *client);
260 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
261
262
263 static struct i2c_driver it87_driver = {
264 .driver = {
265 .name = "it87",
266 },
267 .id = I2C_DRIVERID_IT87,
268 .attach_adapter = it87_attach_adapter,
269 .detach_client = it87_detach_client,
270 };
271
272 static struct i2c_driver it87_isa_driver = {
273 .driver = {
274 .owner = THIS_MODULE,
275 .name = "it87-isa",
276 },
277 .attach_adapter = it87_isa_attach_adapter,
278 .detach_client = it87_detach_client,
279 };
280
281
282 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
283 char *buf)
284 {
285 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
286 int nr = sensor_attr->index;
287
288 struct it87_data *data = it87_update_device(dev);
289 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
290 }
291
292 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
293 char *buf)
294 {
295 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
296 int nr = sensor_attr->index;
297
298 struct it87_data *data = it87_update_device(dev);
299 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
300 }
301
302 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
303 char *buf)
304 {
305 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
306 int nr = sensor_attr->index;
307
308 struct it87_data *data = it87_update_device(dev);
309 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
310 }
311
312 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
313 const char *buf, size_t count)
314 {
315 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
316 int nr = sensor_attr->index;
317
318 struct i2c_client *client = to_i2c_client(dev);
319 struct it87_data *data = i2c_get_clientdata(client);
320 unsigned long val = simple_strtoul(buf, NULL, 10);
321
322 mutex_lock(&data->update_lock);
323 data->in_min[nr] = IN_TO_REG(val);
324 it87_write_value(client, IT87_REG_VIN_MIN(nr),
325 data->in_min[nr]);
326 mutex_unlock(&data->update_lock);
327 return count;
328 }
329 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
330 const char *buf, size_t count)
331 {
332 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
333 int nr = sensor_attr->index;
334
335 struct i2c_client *client = to_i2c_client(dev);
336 struct it87_data *data = i2c_get_clientdata(client);
337 unsigned long val = simple_strtoul(buf, NULL, 10);
338
339 mutex_lock(&data->update_lock);
340 data->in_max[nr] = IN_TO_REG(val);
341 it87_write_value(client, IT87_REG_VIN_MAX(nr),
342 data->in_max[nr]);
343 mutex_unlock(&data->update_lock);
344 return count;
345 }
346
347 #define show_in_offset(offset) \
348 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
349 show_in, NULL, offset);
350
351 #define limit_in_offset(offset) \
352 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
353 show_in_min, set_in_min, offset); \
354 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
355 show_in_max, set_in_max, offset);
356
357 show_in_offset(0);
358 limit_in_offset(0);
359 show_in_offset(1);
360 limit_in_offset(1);
361 show_in_offset(2);
362 limit_in_offset(2);
363 show_in_offset(3);
364 limit_in_offset(3);
365 show_in_offset(4);
366 limit_in_offset(4);
367 show_in_offset(5);
368 limit_in_offset(5);
369 show_in_offset(6);
370 limit_in_offset(6);
371 show_in_offset(7);
372 limit_in_offset(7);
373 show_in_offset(8);
374
375 /* 3 temperatures */
376 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
377 char *buf)
378 {
379 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
380 int nr = sensor_attr->index;
381
382 struct it87_data *data = it87_update_device(dev);
383 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
384 }
385 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
386 char *buf)
387 {
388 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
389 int nr = sensor_attr->index;
390
391 struct it87_data *data = it87_update_device(dev);
392 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
393 }
394 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
395 char *buf)
396 {
397 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
398 int nr = sensor_attr->index;
399
400 struct it87_data *data = it87_update_device(dev);
401 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
402 }
403 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
404 const char *buf, size_t count)
405 {
406 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
407 int nr = sensor_attr->index;
408
409 struct i2c_client *client = to_i2c_client(dev);
410 struct it87_data *data = i2c_get_clientdata(client);
411 int val = simple_strtol(buf, NULL, 10);
412
413 mutex_lock(&data->update_lock);
414 data->temp_high[nr] = TEMP_TO_REG(val);
415 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
416 mutex_unlock(&data->update_lock);
417 return count;
418 }
419 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
420 const char *buf, size_t count)
421 {
422 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
423 int nr = sensor_attr->index;
424
425 struct i2c_client *client = to_i2c_client(dev);
426 struct it87_data *data = i2c_get_clientdata(client);
427 int val = simple_strtol(buf, NULL, 10);
428
429 mutex_lock(&data->update_lock);
430 data->temp_low[nr] = TEMP_TO_REG(val);
431 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
432 mutex_unlock(&data->update_lock);
433 return count;
434 }
435 #define show_temp_offset(offset) \
436 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
437 show_temp, NULL, offset - 1); \
438 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
439 show_temp_max, set_temp_max, offset - 1); \
440 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
441 show_temp_min, set_temp_min, offset - 1);
442
443 show_temp_offset(1);
444 show_temp_offset(2);
445 show_temp_offset(3);
446
447 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
448 char *buf)
449 {
450 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
451 int nr = sensor_attr->index;
452
453 struct it87_data *data = it87_update_device(dev);
454 u8 reg = data->sensor; /* In case the value is updated while we use it */
455
456 if (reg & (1 << nr))
457 return sprintf(buf, "3\n"); /* thermal diode */
458 if (reg & (8 << nr))
459 return sprintf(buf, "2\n"); /* thermistor */
460 return sprintf(buf, "0\n"); /* disabled */
461 }
462 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
463 const char *buf, size_t count)
464 {
465 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
466 int nr = sensor_attr->index;
467
468 struct i2c_client *client = to_i2c_client(dev);
469 struct it87_data *data = i2c_get_clientdata(client);
470 int val = simple_strtol(buf, NULL, 10);
471
472 mutex_lock(&data->update_lock);
473
474 data->sensor &= ~(1 << nr);
475 data->sensor &= ~(8 << nr);
476 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
477 if (val == 3)
478 data->sensor |= 1 << nr;
479 else if (val == 2)
480 data->sensor |= 8 << nr;
481 else if (val != 0) {
482 mutex_unlock(&data->update_lock);
483 return -EINVAL;
484 }
485 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
486 mutex_unlock(&data->update_lock);
487 return count;
488 }
489 #define show_sensor_offset(offset) \
490 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
491 show_sensor, set_sensor, offset - 1);
492
493 show_sensor_offset(1);
494 show_sensor_offset(2);
495 show_sensor_offset(3);
496
497 /* 3 Fans */
498 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
499 char *buf)
500 {
501 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
502 int nr = sensor_attr->index;
503
504 struct it87_data *data = it87_update_device(dev);
505 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
506 DIV_FROM_REG(data->fan_div[nr])));
507 }
508 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
509 char *buf)
510 {
511 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
512 int nr = sensor_attr->index;
513
514 struct it87_data *data = it87_update_device(dev);
515 return sprintf(buf,"%d\n",
516 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
517 }
518 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
519 char *buf)
520 {
521 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
522 int nr = sensor_attr->index;
523
524 struct it87_data *data = it87_update_device(dev);
525 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
526 }
527 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
528 char *buf)
529 {
530 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
531 int nr = sensor_attr->index;
532
533 struct it87_data *data = it87_update_device(dev);
534 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
535 }
536 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
537 char *buf)
538 {
539 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
540 int nr = sensor_attr->index;
541
542 struct it87_data *data = it87_update_device(dev);
543 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
544 }
545 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
546 const char *buf, size_t count)
547 {
548 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
549 int nr = sensor_attr->index;
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 u8 reg = it87_read_value(client, IT87_REG_FAN_DIV);
555
556 mutex_lock(&data->update_lock);
557 switch (nr) {
558 case 0: data->fan_div[nr] = reg & 0x07; break;
559 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
560 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
561 }
562
563 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
564 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
565 mutex_unlock(&data->update_lock);
566 return count;
567 }
568 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
569 const char *buf, size_t count)
570 {
571 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
572 int nr = sensor_attr->index;
573
574 struct i2c_client *client = to_i2c_client(dev);
575 struct it87_data *data = i2c_get_clientdata(client);
576 unsigned long val = simple_strtoul(buf, NULL, 10);
577 int min;
578 u8 old;
579
580 mutex_lock(&data->update_lock);
581 old = it87_read_value(client, IT87_REG_FAN_DIV);
582
583 /* Save fan min limit */
584 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
585
586 switch (nr) {
587 case 0:
588 case 1:
589 data->fan_div[nr] = DIV_TO_REG(val);
590 break;
591 case 2:
592 if (val < 8)
593 data->fan_div[nr] = 1;
594 else
595 data->fan_div[nr] = 3;
596 }
597 val = old & 0x80;
598 val |= (data->fan_div[0] & 0x07);
599 val |= (data->fan_div[1] & 0x07) << 3;
600 if (data->fan_div[2] == 3)
601 val |= 0x1 << 6;
602 it87_write_value(client, IT87_REG_FAN_DIV, val);
603
604 /* Restore fan min limit */
605 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
606 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
607
608 mutex_unlock(&data->update_lock);
609 return count;
610 }
611 static ssize_t set_pwm_enable(struct device *dev,
612 struct device_attribute *attr, const char *buf, size_t count)
613 {
614 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
615 int nr = sensor_attr->index;
616
617 struct i2c_client *client = to_i2c_client(dev);
618 struct it87_data *data = i2c_get_clientdata(client);
619 int val = simple_strtol(buf, NULL, 10);
620
621 mutex_lock(&data->update_lock);
622
623 if (val == 0) {
624 int tmp;
625 /* make sure the fan is on when in on/off mode */
626 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
627 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
628 /* set on/off mode */
629 data->fan_main_ctrl &= ~(1 << nr);
630 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
631 } else if (val == 1) {
632 /* set SmartGuardian mode */
633 data->fan_main_ctrl |= (1 << nr);
634 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
635 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
636 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
637 } else {
638 mutex_unlock(&data->update_lock);
639 return -EINVAL;
640 }
641
642 mutex_unlock(&data->update_lock);
643 return count;
644 }
645 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
646 const char *buf, size_t count)
647 {
648 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
649 int nr = sensor_attr->index;
650
651 struct i2c_client *client = to_i2c_client(dev);
652 struct it87_data *data = i2c_get_clientdata(client);
653 int val = simple_strtol(buf, NULL, 10);
654
655 if (val < 0 || val > 255)
656 return -EINVAL;
657
658 mutex_lock(&data->update_lock);
659 data->manual_pwm_ctl[nr] = val;
660 if (data->fan_main_ctrl & (1 << nr))
661 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
662 mutex_unlock(&data->update_lock);
663 return count;
664 }
665
666 #define show_fan_offset(offset) \
667 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
668 show_fan, NULL, offset - 1); \
669 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
670 show_fan_min, set_fan_min, offset - 1); \
671 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
672 show_fan_div, set_fan_div, offset - 1);
673
674 show_fan_offset(1);
675 show_fan_offset(2);
676 show_fan_offset(3);
677
678 #define show_pwm_offset(offset) \
679 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
680 show_pwm_enable, set_pwm_enable, offset - 1); \
681 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
682 show_pwm, set_pwm, offset - 1);
683
684 show_pwm_offset(1);
685 show_pwm_offset(2);
686 show_pwm_offset(3);
687
688 /* A different set of callbacks for 16-bit fans */
689 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
690 char *buf)
691 {
692 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
693 int nr = sensor_attr->index;
694 struct it87_data *data = it87_update_device(dev);
695 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
696 }
697
698 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
699 char *buf)
700 {
701 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
702 int nr = sensor_attr->index;
703 struct it87_data *data = it87_update_device(dev);
704 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
705 }
706
707 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
708 const char *buf, size_t count)
709 {
710 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
711 int nr = sensor_attr->index;
712 struct i2c_client *client = to_i2c_client(dev);
713 struct it87_data *data = i2c_get_clientdata(client);
714 int val = simple_strtol(buf, NULL, 10);
715
716 mutex_lock(&data->update_lock);
717 data->fan_min[nr] = FAN16_TO_REG(val);
718 it87_write_value(client, IT87_REG_FAN_MIN(nr),
719 data->fan_min[nr] & 0xff);
720 it87_write_value(client, IT87_REG_FANX_MIN(nr),
721 data->fan_min[nr] >> 8);
722 mutex_unlock(&data->update_lock);
723 return count;
724 }
725
726 /* We want to use the same sysfs file names as 8-bit fans, but we need
727 different variable names, so we have to use SENSOR_ATTR instead of
728 SENSOR_DEVICE_ATTR. */
729 #define show_fan16_offset(offset) \
730 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
731 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
732 show_fan16, NULL, offset - 1); \
733 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
734 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
735 show_fan16_min, set_fan16_min, offset - 1)
736
737 show_fan16_offset(1);
738 show_fan16_offset(2);
739 show_fan16_offset(3);
740
741 /* Alarms */
742 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
743 {
744 struct it87_data *data = it87_update_device(dev);
745 return sprintf(buf, "%u\n", data->alarms);
746 }
747 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
748
749 static ssize_t
750 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
751 {
752 struct it87_data *data = it87_update_device(dev);
753 return sprintf(buf, "%u\n", data->vrm);
754 }
755 static ssize_t
756 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
757 {
758 struct i2c_client *client = to_i2c_client(dev);
759 struct it87_data *data = i2c_get_clientdata(client);
760 u32 val;
761
762 val = simple_strtoul(buf, NULL, 10);
763 data->vrm = val;
764
765 return count;
766 }
767 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
768 #define device_create_file_vrm(client) \
769 device_create_file(&client->dev, &dev_attr_vrm)
770
771 static ssize_t
772 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
773 {
774 struct it87_data *data = it87_update_device(dev);
775 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
776 }
777 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
778 #define device_create_file_vid(client) \
779 device_create_file(&client->dev, &dev_attr_cpu0_vid)
780
781 /* This function is called when:
782 * it87_driver is inserted (when this module is loaded), for each
783 available adapter
784 * when a new adapter is inserted (and it87_driver is still present) */
785 static int it87_attach_adapter(struct i2c_adapter *adapter)
786 {
787 if (!(adapter->class & I2C_CLASS_HWMON))
788 return 0;
789 return i2c_probe(adapter, &addr_data, it87_detect);
790 }
791
792 static int it87_isa_attach_adapter(struct i2c_adapter *adapter)
793 {
794 return it87_detect(adapter, isa_address, -1);
795 }
796
797 /* SuperIO detection - will change isa_address if a chip is found */
798 static int __init it87_find(unsigned short *address)
799 {
800 int err = -ENODEV;
801
802 superio_enter();
803 chip_type = superio_inw(DEVID);
804 if (chip_type != IT8712F_DEVID
805 && chip_type != IT8716F_DEVID
806 && chip_type != IT8718F_DEVID
807 && chip_type != IT8705F_DEVID)
808 goto exit;
809
810 superio_select(PME);
811 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
812 pr_info("it87: Device not activated, skipping\n");
813 goto exit;
814 }
815
816 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
817 if (*address == 0) {
818 pr_info("it87: Base address not set, skipping\n");
819 goto exit;
820 }
821
822 err = 0;
823 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
824 chip_type, *address, superio_inb(DEVREV) & 0x0f);
825
826 /* Read GPIO config and VID value from LDN 7 (GPIO) */
827 if (chip_type != IT8705F_DEVID) {
828 int reg;
829
830 superio_select(GPIO);
831 if (chip_type == it8718)
832 vid_value = superio_inb(IT87_SIO_VID_REG);
833
834 reg = superio_inb(IT87_SIO_PINX2_REG);
835 if (reg & (1 << 0))
836 pr_info("it87: in3 is VCC (+5V)\n");
837 if (reg & (1 << 1))
838 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
839 }
840
841 exit:
842 superio_exit();
843 return err;
844 }
845
846 /* This function is called by i2c_probe */
847 static int it87_detect(struct i2c_adapter *adapter, int address, int kind)
848 {
849 int i;
850 struct i2c_client *new_client;
851 struct it87_data *data;
852 int err = 0;
853 const char *name = "";
854 int is_isa = i2c_is_isa_adapter(adapter);
855 int enable_pwm_interface;
856
857 if (!is_isa &&
858 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
859 goto ERROR0;
860
861 /* Reserve the ISA region */
862 if (is_isa)
863 if (!request_region(address, IT87_EXTENT,
864 it87_isa_driver.driver.name))
865 goto ERROR0;
866
867 /* For now, we presume we have a valid client. We create the
868 client structure, even though we cannot fill it completely yet.
869 But it allows us to access it87_{read,write}_value. */
870
871 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
872 err = -ENOMEM;
873 goto ERROR1;
874 }
875
876 new_client = &data->client;
877 if (is_isa)
878 mutex_init(&data->lock);
879 i2c_set_clientdata(new_client, data);
880 new_client->addr = address;
881 new_client->adapter = adapter;
882 new_client->driver = is_isa ? &it87_isa_driver : &it87_driver;
883 new_client->flags = 0;
884
885 /* Now, we do the remaining detection. */
886
887 if (kind < 0) {
888 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
889 || (!is_isa
890 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
891 err = -ENODEV;
892 goto ERROR2;
893 }
894 }
895
896 /* Determine the chip type. */
897 if (kind <= 0) {
898 i = it87_read_value(new_client, IT87_REG_CHIPID);
899 if (i == 0x90) {
900 kind = it87;
901 if (is_isa) {
902 switch (chip_type) {
903 case IT8712F_DEVID:
904 kind = it8712;
905 break;
906 case IT8716F_DEVID:
907 kind = it8716;
908 break;
909 case IT8718F_DEVID:
910 kind = it8718;
911 break;
912 }
913 }
914 }
915 else {
916 if (kind == 0)
917 dev_info(&adapter->dev,
918 "Ignoring 'force' parameter for unknown chip at "
919 "adapter %d, address 0x%02x\n",
920 i2c_adapter_id(adapter), address);
921 err = -ENODEV;
922 goto ERROR2;
923 }
924 }
925
926 if (kind == it87) {
927 name = "it87";
928 } else if (kind == it8712) {
929 name = "it8712";
930 } else if (kind == it8716) {
931 name = "it8716";
932 } else if (kind == it8718) {
933 name = "it8718";
934 }
935
936 /* Fill in the remaining client fields and put it into the global list */
937 strlcpy(new_client->name, name, I2C_NAME_SIZE);
938 data->type = kind;
939 data->valid = 0;
940 mutex_init(&data->update_lock);
941
942 /* Tell the I2C layer a new client has arrived */
943 if ((err = i2c_attach_client(new_client)))
944 goto ERROR2;
945
946 if (!is_isa)
947 dev_info(&new_client->dev, "The I2C interface to IT87xxF "
948 "hardware monitoring chips is deprecated. Please "
949 "report if you still rely on it.\n");
950
951 /* Check PWM configuration */
952 enable_pwm_interface = it87_check_pwm(new_client);
953
954 /* Initialize the IT87 chip */
955 it87_init_client(new_client, data);
956
957 /* Register sysfs hooks */
958 data->class_dev = hwmon_device_register(&new_client->dev);
959 if (IS_ERR(data->class_dev)) {
960 err = PTR_ERR(data->class_dev);
961 goto ERROR3;
962 }
963
964 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
965 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
966 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
967 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
968 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
969 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
970 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
971 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
972 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
973 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
974 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
975 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
976 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
977 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
978 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
979 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
980 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
981 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
982 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
983 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
984 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
985 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
986 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
987 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
988 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
989 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
990 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
991 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
992 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
993 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
994 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
995 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
996 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
997 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
998 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
999 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
1000 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
1001
1002 /* Do not create fan files for disabled fans */
1003 if (data->type == it8716 || data->type == it8718) {
1004 /* 16-bit tachometers */
1005 if (data->has_fan & (1 << 0)) {
1006 device_create_file(&new_client->dev,
1007 &sensor_dev_attr_fan1_input16.dev_attr);
1008 device_create_file(&new_client->dev,
1009 &sensor_dev_attr_fan1_min16.dev_attr);
1010 }
1011 if (data->has_fan & (1 << 1)) {
1012 device_create_file(&new_client->dev,
1013 &sensor_dev_attr_fan2_input16.dev_attr);
1014 device_create_file(&new_client->dev,
1015 &sensor_dev_attr_fan2_min16.dev_attr);
1016 }
1017 if (data->has_fan & (1 << 2)) {
1018 device_create_file(&new_client->dev,
1019 &sensor_dev_attr_fan3_input16.dev_attr);
1020 device_create_file(&new_client->dev,
1021 &sensor_dev_attr_fan3_min16.dev_attr);
1022 }
1023 } else {
1024 /* 8-bit tachometers with clock divider */
1025 if (data->has_fan & (1 << 0)) {
1026 device_create_file(&new_client->dev,
1027 &sensor_dev_attr_fan1_input.dev_attr);
1028 device_create_file(&new_client->dev,
1029 &sensor_dev_attr_fan1_min.dev_attr);
1030 device_create_file(&new_client->dev,
1031 &sensor_dev_attr_fan1_div.dev_attr);
1032 }
1033 if (data->has_fan & (1 << 1)) {
1034 device_create_file(&new_client->dev,
1035 &sensor_dev_attr_fan2_input.dev_attr);
1036 device_create_file(&new_client->dev,
1037 &sensor_dev_attr_fan2_min.dev_attr);
1038 device_create_file(&new_client->dev,
1039 &sensor_dev_attr_fan2_div.dev_attr);
1040 }
1041 if (data->has_fan & (1 << 2)) {
1042 device_create_file(&new_client->dev,
1043 &sensor_dev_attr_fan3_input.dev_attr);
1044 device_create_file(&new_client->dev,
1045 &sensor_dev_attr_fan3_min.dev_attr);
1046 device_create_file(&new_client->dev,
1047 &sensor_dev_attr_fan3_div.dev_attr);
1048 }
1049 }
1050
1051 device_create_file(&new_client->dev, &dev_attr_alarms);
1052 if (enable_pwm_interface) {
1053 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
1054 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
1055 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
1056 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
1057 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
1058 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
1059 }
1060
1061 if (data->type == it8712 || data->type == it8716
1062 || data->type == it8718) {
1063 data->vrm = vid_which_vrm();
1064 /* VID reading from Super-I/O config space if available */
1065 data->vid = vid_value;
1066 device_create_file_vrm(new_client);
1067 device_create_file_vid(new_client);
1068 }
1069
1070 return 0;
1071
1072 ERROR3:
1073 i2c_detach_client(new_client);
1074 ERROR2:
1075 kfree(data);
1076 ERROR1:
1077 if (is_isa)
1078 release_region(address, IT87_EXTENT);
1079 ERROR0:
1080 return err;
1081 }
1082
1083 static int it87_detach_client(struct i2c_client *client)
1084 {
1085 struct it87_data *data = i2c_get_clientdata(client);
1086 int err;
1087
1088 hwmon_device_unregister(data->class_dev);
1089
1090 if ((err = i2c_detach_client(client)))
1091 return err;
1092
1093 if(i2c_is_isa_client(client))
1094 release_region(client->addr, IT87_EXTENT);
1095 kfree(data);
1096
1097 return 0;
1098 }
1099
1100 /* The SMBus locks itself, but ISA access must be locked explicitly!
1101 We don't want to lock the whole ISA bus, so we lock each client
1102 separately.
1103 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1104 would slow down the IT87 access and should not be necessary. */
1105 static int it87_read_value(struct i2c_client *client, u8 reg)
1106 {
1107 struct it87_data *data = i2c_get_clientdata(client);
1108
1109 int res;
1110 if (i2c_is_isa_client(client)) {
1111 mutex_lock(&data->lock);
1112 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1113 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
1114 mutex_unlock(&data->lock);
1115 return res;
1116 } else
1117 return i2c_smbus_read_byte_data(client, reg);
1118 }
1119
1120 /* The SMBus locks itself, but ISA access muse be locked explicitly!
1121 We don't want to lock the whole ISA bus, so we lock each client
1122 separately.
1123 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1124 would slow down the IT87 access and should not be necessary. */
1125 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
1126 {
1127 struct it87_data *data = i2c_get_clientdata(client);
1128
1129 if (i2c_is_isa_client(client)) {
1130 mutex_lock(&data->lock);
1131 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1132 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
1133 mutex_unlock(&data->lock);
1134 return 0;
1135 } else
1136 return i2c_smbus_write_byte_data(client, reg, value);
1137 }
1138
1139 /* Return 1 if and only if the PWM interface is safe to use */
1140 static int it87_check_pwm(struct i2c_client *client)
1141 {
1142 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1143 * and polarity set to active low is sign that this is the case so we
1144 * disable pwm control to protect the user. */
1145 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1146 if ((tmp & 0x87) == 0) {
1147 if (fix_pwm_polarity) {
1148 /* The user asks us to attempt a chip reconfiguration.
1149 * This means switching to active high polarity and
1150 * inverting all fan speed values. */
1151 int i;
1152 u8 pwm[3];
1153
1154 for (i = 0; i < 3; i++)
1155 pwm[i] = it87_read_value(client,
1156 IT87_REG_PWM(i));
1157
1158 /* If any fan is in automatic pwm mode, the polarity
1159 * might be correct, as suspicious as it seems, so we
1160 * better don't change anything (but still disable the
1161 * PWM interface). */
1162 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1163 dev_info(&client->dev, "Reconfiguring PWM to "
1164 "active high polarity\n");
1165 it87_write_value(client, IT87_REG_FAN_CTL,
1166 tmp | 0x87);
1167 for (i = 0; i < 3; i++)
1168 it87_write_value(client,
1169 IT87_REG_PWM(i),
1170 0x7f & ~pwm[i]);
1171 return 1;
1172 }
1173
1174 dev_info(&client->dev, "PWM configuration is "
1175 "too broken to be fixed\n");
1176 }
1177
1178 dev_info(&client->dev, "Detected broken BIOS "
1179 "defaults, disabling PWM interface\n");
1180 return 0;
1181 } else if (fix_pwm_polarity) {
1182 dev_info(&client->dev, "PWM configuration looks "
1183 "sane, won't touch\n");
1184 }
1185
1186 return 1;
1187 }
1188
1189 /* Called when we have found a new IT87. */
1190 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1191 {
1192 int tmp, i;
1193
1194 /* initialize to sane defaults:
1195 * - if the chip is in manual pwm mode, this will be overwritten with
1196 * the actual settings on the chip (so in this case, initialization
1197 * is not needed)
1198 * - if in automatic or on/off mode, we could switch to manual mode,
1199 * read the registers and set manual_pwm_ctl accordingly, but currently
1200 * this is not implemented, so we initialize to something sane */
1201 for (i = 0; i < 3; i++) {
1202 data->manual_pwm_ctl[i] = 0xff;
1203 }
1204
1205 /* Check if temperature channnels are reset manually or by some reason */
1206 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1207 if ((tmp & 0x3f) == 0) {
1208 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1209 tmp = (tmp & 0xc0) | 0x2a;
1210 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1211 }
1212 data->sensor = tmp;
1213
1214 /* Check if voltage monitors are reset manually or by some reason */
1215 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1216 if ((tmp & 0xff) == 0) {
1217 /* Enable all voltage monitors */
1218 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1219 }
1220
1221 /* Check if tachometers are reset manually or by some reason */
1222 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1223 if ((data->fan_main_ctrl & 0x70) == 0) {
1224 /* Enable all fan tachometers */
1225 data->fan_main_ctrl |= 0x70;
1226 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1227 }
1228 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
1229
1230 /* Set tachometers to 16-bit mode if needed */
1231 if (data->type == it8716 || data->type == it8718) {
1232 tmp = it87_read_value(client, IT87_REG_FAN_16BIT);
1233 if (~tmp & 0x07 & data->has_fan) {
1234 dev_dbg(&client->dev,
1235 "Setting fan1-3 to 16-bit mode\n");
1236 it87_write_value(client, IT87_REG_FAN_16BIT,
1237 tmp | 0x07);
1238 }
1239 }
1240
1241 /* Set current fan mode registers and the default settings for the
1242 * other mode registers */
1243 for (i = 0; i < 3; i++) {
1244 if (data->fan_main_ctrl & (1 << i)) {
1245 /* pwm mode */
1246 tmp = it87_read_value(client, IT87_REG_PWM(i));
1247 if (tmp & 0x80) {
1248 /* automatic pwm - not yet implemented, but
1249 * leave the settings made by the BIOS alone
1250 * until a change is requested via the sysfs
1251 * interface */
1252 } else {
1253 /* manual pwm */
1254 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1255 }
1256 }
1257 }
1258
1259 /* Start monitoring */
1260 it87_write_value(client, IT87_REG_CONFIG,
1261 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1262 | (update_vbat ? 0x41 : 0x01));
1263 }
1264
1265 static struct it87_data *it87_update_device(struct device *dev)
1266 {
1267 struct i2c_client *client = to_i2c_client(dev);
1268 struct it87_data *data = i2c_get_clientdata(client);
1269 int i;
1270
1271 mutex_lock(&data->update_lock);
1272
1273 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1274 || !data->valid) {
1275
1276 if (update_vbat) {
1277 /* Cleared after each update, so reenable. Value
1278 returned by this read will be previous value */
1279 it87_write_value(client, IT87_REG_CONFIG,
1280 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1281 }
1282 for (i = 0; i <= 7; i++) {
1283 data->in[i] =
1284 it87_read_value(client, IT87_REG_VIN(i));
1285 data->in_min[i] =
1286 it87_read_value(client, IT87_REG_VIN_MIN(i));
1287 data->in_max[i] =
1288 it87_read_value(client, IT87_REG_VIN_MAX(i));
1289 }
1290 /* in8 (battery) has no limit registers */
1291 data->in[8] =
1292 it87_read_value(client, IT87_REG_VIN(8));
1293
1294 for (i = 0; i < 3; i++) {
1295 /* Skip disabled fans */
1296 if (!(data->has_fan & (1 << i)))
1297 continue;
1298
1299 data->fan_min[i] =
1300 it87_read_value(client, IT87_REG_FAN_MIN(i));
1301 data->fan[i] = it87_read_value(client,
1302 IT87_REG_FAN(i));
1303 /* Add high byte if in 16-bit mode */
1304 if (data->type == it8716 || data->type == it8718) {
1305 data->fan[i] |= it87_read_value(client,
1306 IT87_REG_FANX(i)) << 8;
1307 data->fan_min[i] |= it87_read_value(client,
1308 IT87_REG_FANX_MIN(i)) << 8;
1309 }
1310 }
1311 for (i = 0; i < 3; i++) {
1312 data->temp[i] =
1313 it87_read_value(client, IT87_REG_TEMP(i));
1314 data->temp_high[i] =
1315 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1316 data->temp_low[i] =
1317 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1318 }
1319
1320 /* Newer chips don't have clock dividers */
1321 if ((data->has_fan & 0x07) && data->type != it8716
1322 && data->type != it8718) {
1323 i = it87_read_value(client, IT87_REG_FAN_DIV);
1324 data->fan_div[0] = i & 0x07;
1325 data->fan_div[1] = (i >> 3) & 0x07;
1326 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1327 }
1328
1329 data->alarms =
1330 it87_read_value(client, IT87_REG_ALARM1) |
1331 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1332 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1333 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1334
1335 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1336 /* The 8705 does not have VID capability */
1337 if (data->type == it8712 || data->type == it8716) {
1338 data->vid = it87_read_value(client, IT87_REG_VID);
1339 /* The older IT8712F revisions had only 5 VID pins,
1340 but we assume it is always safe to read 6 bits. */
1341 data->vid &= 0x3f;
1342 }
1343 data->last_updated = jiffies;
1344 data->valid = 1;
1345 }
1346
1347 mutex_unlock(&data->update_lock);
1348
1349 return data;
1350 }
1351
1352 static int __init sm_it87_init(void)
1353 {
1354 int res;
1355
1356 res = i2c_add_driver(&it87_driver);
1357 if (res)
1358 return res;
1359
1360 if (!it87_find(&isa_address)) {
1361 res = i2c_isa_add_driver(&it87_isa_driver);
1362 if (res) {
1363 i2c_del_driver(&it87_driver);
1364 return res;
1365 }
1366 }
1367
1368 return 0;
1369 }
1370
1371 static void __exit sm_it87_exit(void)
1372 {
1373 if (isa_address)
1374 i2c_isa_del_driver(&it87_isa_driver);
1375 i2c_del_driver(&it87_driver);
1376 }
1377
1378
1379 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1380 MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F, SiS950 driver");
1381 module_param(update_vbat, bool, 0);
1382 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1383 module_param(fix_pwm_polarity, bool, 0);
1384 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1385 MODULE_LICENSE("GPL");
1386
1387 module_init(sm_it87_init);
1388 module_exit(sm_it87_exit);
This page took 0.058933 seconds and 6 git commands to generate.