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