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