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