blob: 6a9b65a10bbc989b3e429d628ff3eb31df188873 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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>
Jean Delvare20ad93d2005-06-05 11:53:25 +020040#include <linux/i2c-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#define PWM_TO_REG(val) ((val) >> 1)
177#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
178
179static int DIV_TO_REG(int val)
180{
181 int answer = 0;
182 while ((val >>= 1) != 0)
183 answer++;
184 return answer;
185}
186#define DIV_FROM_REG(val) (1 << (val))
187
188
189/* For each registered IT87, we need to keep some data in memory. That
190 data is pointed to by it87_list[NR]->data. The structure itself is
191 dynamically allocated, at the same time when a new it87 client is
192 allocated. */
193struct it87_data {
194 struct i2c_client client;
195 struct semaphore lock;
196 enum chips type;
197
198 struct semaphore update_lock;
199 char valid; /* !=0 if following fields are valid */
200 unsigned long last_updated; /* In jiffies */
201
202 u8 in[9]; /* Register value */
203 u8 in_max[9]; /* Register value */
204 u8 in_min[9]; /* Register value */
205 u8 fan[3]; /* Register value */
206 u8 fan_min[3]; /* Register value */
207 u8 temp[3]; /* Register value */
208 u8 temp_high[3]; /* Register value */
209 u8 temp_low[3]; /* Register value */
210 u8 sensor; /* Register value */
211 u8 fan_div[3]; /* Register encoding, shifted right */
212 u8 vid; /* Register encoding, combined */
213 int vrm;
214 u32 alarms; /* Register encoding, combined */
215 u8 fan_main_ctrl; /* Register value */
216 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
217};
218
219
220static int it87_attach_adapter(struct i2c_adapter *adapter);
221static int it87_find(int *address);
222static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
223static int it87_detach_client(struct i2c_client *client);
224
225static int it87_read_value(struct i2c_client *client, u8 register);
226static int it87_write_value(struct i2c_client *client, u8 register,
227 u8 value);
228static struct it87_data *it87_update_device(struct device *dev);
229static int it87_check_pwm(struct i2c_client *client);
230static void it87_init_client(struct i2c_client *client, struct it87_data *data);
231
232
233static struct i2c_driver it87_driver = {
234 .owner = THIS_MODULE,
235 .name = "it87",
236 .id = I2C_DRIVERID_IT87,
237 .flags = I2C_DF_NOTIFY,
238 .attach_adapter = it87_attach_adapter,
239 .detach_client = it87_detach_client,
240};
241
Jean Delvare20ad93d2005-06-05 11:53:25 +0200242static ssize_t show_in(struct device *dev, struct device_attribute *attr,
243 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200245 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
246 int nr = sensor_attr->index;
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct it87_data *data = it87_update_device(dev);
249 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
250}
251
Jean Delvare20ad93d2005-06-05 11:53:25 +0200252static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
253 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200255 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
256 int nr = sensor_attr->index;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct it87_data *data = it87_update_device(dev);
259 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
260}
261
Jean Delvare20ad93d2005-06-05 11:53:25 +0200262static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
263 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200265 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
266 int nr = sensor_attr->index;
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct it87_data *data = it87_update_device(dev);
269 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
270}
271
Jean Delvare20ad93d2005-06-05 11:53:25 +0200272static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
273 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200275 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
276 int nr = sensor_attr->index;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 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_min[nr] = IN_TO_REG(val);
284 it87_write_value(client, IT87_REG_VIN_MIN(nr),
285 data->in_min[nr]);
286 up(&data->update_lock);
287 return count;
288}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200289static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
290 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200292 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
293 int nr = sensor_attr->index;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct i2c_client *client = to_i2c_client(dev);
296 struct it87_data *data = i2c_get_clientdata(client);
297 unsigned long val = simple_strtoul(buf, NULL, 10);
298
299 down(&data->update_lock);
300 data->in_max[nr] = IN_TO_REG(val);
301 it87_write_value(client, IT87_REG_VIN_MAX(nr),
302 data->in_max[nr]);
303 up(&data->update_lock);
304 return count;
305}
306
307#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200308static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
309 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200312static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
313 show_in_min, set_in_min, offset); \
314static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
315 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317show_in_offset(0);
318limit_in_offset(0);
319show_in_offset(1);
320limit_in_offset(1);
321show_in_offset(2);
322limit_in_offset(2);
323show_in_offset(3);
324limit_in_offset(3);
325show_in_offset(4);
326limit_in_offset(4);
327show_in_offset(5);
328limit_in_offset(5);
329show_in_offset(6);
330limit_in_offset(6);
331show_in_offset(7);
332limit_in_offset(7);
333show_in_offset(8);
334
335/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200336static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
337 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200339 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
340 int nr = sensor_attr->index;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct it87_data *data = it87_update_device(dev);
343 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
344}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200345static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
346 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200348 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
349 int nr = sensor_attr->index;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct it87_data *data = it87_update_device(dev);
352 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
353}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200354static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
355 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200357 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
358 int nr = sensor_attr->index;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 struct it87_data *data = it87_update_device(dev);
361 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
362}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200363static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
364 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200366 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
367 int nr = sensor_attr->index;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct i2c_client *client = to_i2c_client(dev);
370 struct it87_data *data = i2c_get_clientdata(client);
371 int val = simple_strtol(buf, NULL, 10);
372
373 down(&data->update_lock);
374 data->temp_high[nr] = TEMP_TO_REG(val);
375 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
376 up(&data->update_lock);
377 return count;
378}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200379static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
380 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200382 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
383 int nr = sensor_attr->index;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 struct i2c_client *client = to_i2c_client(dev);
386 struct it87_data *data = i2c_get_clientdata(client);
387 int val = simple_strtol(buf, NULL, 10);
388
389 down(&data->update_lock);
390 data->temp_low[nr] = TEMP_TO_REG(val);
391 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
392 up(&data->update_lock);
393 return count;
394}
395#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200396static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
397 show_temp, NULL, offset - 1); \
398static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
399 show_temp_max, set_temp_max, offset - 1); \
400static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
401 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403show_temp_offset(1);
404show_temp_offset(2);
405show_temp_offset(3);
406
Jean Delvare20ad93d2005-06-05 11:53:25 +0200407static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
408 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200410 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
411 int nr = sensor_attr->index;
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct it87_data *data = it87_update_device(dev);
414 u8 reg = data->sensor; /* In case the value is updated while we use it */
415
416 if (reg & (1 << nr))
417 return sprintf(buf, "3\n"); /* thermal diode */
418 if (reg & (8 << nr))
419 return sprintf(buf, "2\n"); /* thermistor */
420 return sprintf(buf, "0\n"); /* disabled */
421}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200422static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
423 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200425 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
426 int nr = sensor_attr->index;
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct i2c_client *client = to_i2c_client(dev);
429 struct it87_data *data = i2c_get_clientdata(client);
430 int val = simple_strtol(buf, NULL, 10);
431
432 down(&data->update_lock);
433
434 data->sensor &= ~(1 << nr);
435 data->sensor &= ~(8 << nr);
436 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
437 if (val == 3)
438 data->sensor |= 1 << nr;
439 else if (val == 2)
440 data->sensor |= 8 << nr;
441 else if (val != 0) {
442 up(&data->update_lock);
443 return -EINVAL;
444 }
445 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
446 up(&data->update_lock);
447 return count;
448}
449#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200450static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
451 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453show_sensor_offset(1);
454show_sensor_offset(2);
455show_sensor_offset(3);
456
457/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200458static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
459 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200461 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
462 int nr = sensor_attr->index;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct it87_data *data = it87_update_device(dev);
465 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
466 DIV_FROM_REG(data->fan_div[nr])));
467}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200468static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
469 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200471 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
472 int nr = sensor_attr->index;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct it87_data *data = it87_update_device(dev);
475 return sprintf(buf,"%d\n",
476 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
477}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200478static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
479 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200481 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
482 int nr = sensor_attr->index;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct it87_data *data = it87_update_device(dev);
485 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
486}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200487static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
488 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200490 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
491 int nr = sensor_attr->index;
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct it87_data *data = it87_update_device(dev);
494 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
495}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200496static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
497 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200499 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
500 int nr = sensor_attr->index;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct it87_data *data = it87_update_device(dev);
503 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
504}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200505static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
506 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200508 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
509 int nr = sensor_attr->index;
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct i2c_client *client = to_i2c_client(dev);
512 struct it87_data *data = i2c_get_clientdata(client);
513 int val = simple_strtol(buf, NULL, 10);
514
515 down(&data->update_lock);
516 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
517 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
518 up(&data->update_lock);
519 return count;
520}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200521static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
522 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200524 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
525 int nr = sensor_attr->index;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct i2c_client *client = to_i2c_client(dev);
528 struct it87_data *data = i2c_get_clientdata(client);
529 int val = simple_strtol(buf, NULL, 10);
530 int i, min[3];
531 u8 old;
532
533 down(&data->update_lock);
534 old = it87_read_value(client, IT87_REG_FAN_DIV);
535
536 for (i = 0; i < 3; i++)
537 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
538
539 switch (nr) {
540 case 0:
541 case 1:
542 data->fan_div[nr] = DIV_TO_REG(val);
543 break;
544 case 2:
545 if (val < 8)
546 data->fan_div[nr] = 1;
547 else
548 data->fan_div[nr] = 3;
549 }
550 val = old & 0x80;
551 val |= (data->fan_div[0] & 0x07);
552 val |= (data->fan_div[1] & 0x07) << 3;
553 if (data->fan_div[2] == 3)
554 val |= 0x1 << 6;
555 it87_write_value(client, IT87_REG_FAN_DIV, val);
556
557 for (i = 0; i < 3; i++) {
558 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
559 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
560 }
561 up(&data->update_lock);
562 return count;
563}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200564static ssize_t set_pwm_enable(struct device *dev,
565 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200567 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
568 int nr = sensor_attr->index;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct i2c_client *client = to_i2c_client(dev);
571 struct it87_data *data = i2c_get_clientdata(client);
572 int val = simple_strtol(buf, NULL, 10);
573
574 down(&data->update_lock);
575
576 if (val == 0) {
577 int tmp;
578 /* make sure the fan is on when in on/off mode */
579 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
580 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
581 /* set on/off mode */
582 data->fan_main_ctrl &= ~(1 << nr);
583 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
584 } else if (val == 1) {
585 /* set SmartGuardian mode */
586 data->fan_main_ctrl |= (1 << nr);
587 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
588 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
589 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
590 } else {
591 up(&data->update_lock);
592 return -EINVAL;
593 }
594
595 up(&data->update_lock);
596 return count;
597}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200598static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
599 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200601 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
602 int nr = sensor_attr->index;
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct i2c_client *client = to_i2c_client(dev);
605 struct it87_data *data = i2c_get_clientdata(client);
606 int val = simple_strtol(buf, NULL, 10);
607
608 if (val < 0 || val > 255)
609 return -EINVAL;
610
611 down(&data->update_lock);
612 data->manual_pwm_ctl[nr] = val;
613 if (data->fan_main_ctrl & (1 << nr))
614 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
615 up(&data->update_lock);
616 return count;
617}
618
Jean Delvare20ad93d2005-06-05 11:53:25 +0200619#define show_fan_offset(offset) \
620static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
621 show_fan, NULL, offset - 1); \
622static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
623 show_fan_min, set_fan_min, offset - 1); \
624static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
625 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627show_fan_offset(1);
628show_fan_offset(2);
629show_fan_offset(3);
630
631#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200632static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
633 show_pwm_enable, set_pwm_enable, offset - 1); \
634static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
635 show_pwm, set_pwm, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637show_pwm_offset(1);
638show_pwm_offset(2);
639show_pwm_offset(3);
640
641/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400642static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200645 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
Jean Delvare1d66c642005-04-18 21:16:59 -0700647static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400650show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 struct it87_data *data = it87_update_device(dev);
653 return sprintf(buf, "%ld\n", (long) data->vrm);
654}
655static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400656store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 struct i2c_client *client = to_i2c_client(dev);
659 struct it87_data *data = i2c_get_clientdata(client);
660 u32 val;
661
662 val = simple_strtoul(buf, NULL, 10);
663 data->vrm = val;
664
665 return count;
666}
667static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
668#define device_create_file_vrm(client) \
669device_create_file(&client->dev, &dev_attr_vrm)
670
671static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400672show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct it87_data *data = it87_update_device(dev);
675 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
676}
677static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
678#define device_create_file_vid(client) \
679device_create_file(&client->dev, &dev_attr_cpu0_vid)
680
681/* This function is called when:
682 * it87_driver is inserted (when this module is loaded), for each
683 available adapter
684 * when a new adapter is inserted (and it87_driver is still present) */
685static int it87_attach_adapter(struct i2c_adapter *adapter)
686{
687 if (!(adapter->class & I2C_CLASS_HWMON))
688 return 0;
689 return i2c_detect(adapter, &addr_data, it87_detect);
690}
691
692/* SuperIO detection - will change normal_isa[0] if a chip is found */
693static int it87_find(int *address)
694{
695 int err = -ENODEV;
696
697 superio_enter();
698 chip_type = superio_inw(DEVID);
699 if (chip_type != IT8712F_DEVID
700 && chip_type != IT8705F_DEVID)
701 goto exit;
702
703 superio_select();
704 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
705 pr_info("it87: Device not activated, skipping\n");
706 goto exit;
707 }
708
709 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
710 if (*address == 0) {
711 pr_info("it87: Base address not set, skipping\n");
712 goto exit;
713 }
714
715 err = 0;
716 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
717 chip_type, *address, superio_inb(DEVREV) & 0x0f);
718
719exit:
720 superio_exit();
721 return err;
722}
723
724/* This function is called by i2c_detect */
725int it87_detect(struct i2c_adapter *adapter, int address, int kind)
726{
727 int i;
728 struct i2c_client *new_client;
729 struct it87_data *data;
730 int err = 0;
731 const char *name = "";
732 int is_isa = i2c_is_isa_adapter(adapter);
733 int enable_pwm_interface;
734
735 if (!is_isa &&
736 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
737 goto ERROR0;
738
739 /* Reserve the ISA region */
740 if (is_isa)
741 if (!request_region(address, IT87_EXTENT, it87_driver.name))
742 goto ERROR0;
743
744 /* Probe whether there is anything available on this address. Already
745 done for SMBus and Super-I/O clients */
746 if (kind < 0) {
747 if (is_isa && !chip_type) {
748#define REALLY_SLOW_IO
749 /* We need the timeouts for at least some IT87-like chips. But only
750 if we read 'undefined' registers. */
751 i = inb_p(address + 1);
752 if (inb_p(address + 2) != i
753 || inb_p(address + 3) != i
754 || inb_p(address + 7) != i) {
755 err = -ENODEV;
756 goto ERROR1;
757 }
758#undef REALLY_SLOW_IO
759
760 /* Let's just hope nothing breaks here */
761 i = inb_p(address + 5) & 0x7f;
762 outb_p(~i & 0x7f, address + 5);
763 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
764 outb_p(i, address + 5);
765 err = -ENODEV;
766 goto ERROR1;
767 }
768 }
769 }
770
771 /* OK. For now, we presume we have a valid client. We now create the
772 client structure, even though we cannot fill it completely yet.
773 But it allows us to access it87_{read,write}_value. */
774
775 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
776 err = -ENOMEM;
777 goto ERROR1;
778 }
779 memset(data, 0, sizeof(struct it87_data));
780
781 new_client = &data->client;
782 if (is_isa)
783 init_MUTEX(&data->lock);
784 i2c_set_clientdata(new_client, data);
785 new_client->addr = address;
786 new_client->adapter = adapter;
787 new_client->driver = &it87_driver;
788 new_client->flags = 0;
789
790 /* Now, we do the remaining detection. */
791
792 if (kind < 0) {
793 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
794 || (!is_isa
795 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
796 err = -ENODEV;
797 goto ERROR2;
798 }
799 }
800
801 /* Determine the chip type. */
802 if (kind <= 0) {
803 i = it87_read_value(new_client, IT87_REG_CHIPID);
804 if (i == 0x90) {
805 kind = it87;
806 if ((is_isa) && (chip_type == IT8712F_DEVID))
807 kind = it8712;
808 }
809 else {
810 if (kind == 0)
811 dev_info(&adapter->dev,
812 "Ignoring 'force' parameter for unknown chip at "
813 "adapter %d, address 0x%02x\n",
814 i2c_adapter_id(adapter), address);
815 err = -ENODEV;
816 goto ERROR2;
817 }
818 }
819
820 if (kind == it87) {
821 name = "it87";
822 } else if (kind == it8712) {
823 name = "it8712";
824 }
825
826 /* Fill in the remaining client fields and put it into the global list */
827 strlcpy(new_client->name, name, I2C_NAME_SIZE);
828 data->type = kind;
829 data->valid = 0;
830 init_MUTEX(&data->update_lock);
831
832 /* Tell the I2C layer a new client has arrived */
833 if ((err = i2c_attach_client(new_client)))
834 goto ERROR2;
835
836 /* Check PWM configuration */
837 enable_pwm_interface = it87_check_pwm(new_client);
838
839 /* Initialize the IT87 chip */
840 it87_init_client(new_client, data);
841
842 /* Register sysfs hooks */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200843 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
844 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
845 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
846 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
847 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
848 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
849 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
850 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
851 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
852 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
853 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
854 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
855 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
856 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
857 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
858 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
859 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
860 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
861 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
862 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
863 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
864 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
865 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
866 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 device_create_file(&new_client->dev, &dev_attr_alarms);
890 if (enable_pwm_interface) {
Jean Delvare20ad93d2005-06-05 11:53:25 +0200891 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
896 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
899 if (data->type == it8712) {
900 data->vrm = i2c_which_vrm();
901 device_create_file_vrm(new_client);
902 device_create_file_vid(new_client);
903 }
904
905 return 0;
906
907ERROR2:
908 kfree(data);
909ERROR1:
910 if (is_isa)
911 release_region(address, IT87_EXTENT);
912ERROR0:
913 return err;
914}
915
916static int it87_detach_client(struct i2c_client *client)
917{
918 int err;
919
920 if ((err = i2c_detach_client(client))) {
921 dev_err(&client->dev,
922 "Client deregistration failed, client not detached.\n");
923 return err;
924 }
925
926 if(i2c_is_isa_client(client))
927 release_region(client->addr, IT87_EXTENT);
928 kfree(i2c_get_clientdata(client));
929
930 return 0;
931}
932
Steven Cole44bbe872005-05-03 18:21:25 -0600933/* The SMBus locks itself, but ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 We don't want to lock the whole ISA bus, so we lock each client
935 separately.
936 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
937 would slow down the IT87 access and should not be necessary. */
938static int it87_read_value(struct i2c_client *client, u8 reg)
939{
940 struct it87_data *data = i2c_get_clientdata(client);
941
942 int res;
943 if (i2c_is_isa_client(client)) {
944 down(&data->lock);
945 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
946 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
947 up(&data->lock);
948 return res;
949 } else
950 return i2c_smbus_read_byte_data(client, reg);
951}
952
Steven Cole44bbe872005-05-03 18:21:25 -0600953/* The SMBus locks itself, but ISA access muse be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 We don't want to lock the whole ISA bus, so we lock each client
955 separately.
956 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
957 would slow down the IT87 access and should not be necessary. */
958static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
959{
960 struct it87_data *data = i2c_get_clientdata(client);
961
962 if (i2c_is_isa_client(client)) {
963 down(&data->lock);
964 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
965 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
966 up(&data->lock);
967 return 0;
968 } else
969 return i2c_smbus_write_byte_data(client, reg, value);
970}
971
972/* Return 1 if and only if the PWM interface is safe to use */
973static int it87_check_pwm(struct i2c_client *client)
974{
975 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
976 * and polarity set to active low is sign that this is the case so we
977 * disable pwm control to protect the user. */
978 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
979 if ((tmp & 0x87) == 0) {
980 if (fix_pwm_polarity) {
981 /* The user asks us to attempt a chip reconfiguration.
982 * This means switching to active high polarity and
983 * inverting all fan speed values. */
984 int i;
985 u8 pwm[3];
986
987 for (i = 0; i < 3; i++)
988 pwm[i] = it87_read_value(client,
989 IT87_REG_PWM(i));
990
991 /* If any fan is in automatic pwm mode, the polarity
992 * might be correct, as suspicious as it seems, so we
993 * better don't change anything (but still disable the
994 * PWM interface). */
995 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
996 dev_info(&client->dev, "Reconfiguring PWM to "
997 "active high polarity\n");
998 it87_write_value(client, IT87_REG_FAN_CTL,
999 tmp | 0x87);
1000 for (i = 0; i < 3; i++)
1001 it87_write_value(client,
1002 IT87_REG_PWM(i),
1003 0x7f & ~pwm[i]);
1004 return 1;
1005 }
1006
1007 dev_info(&client->dev, "PWM configuration is "
1008 "too broken to be fixed\n");
1009 }
1010
1011 dev_info(&client->dev, "Detected broken BIOS "
1012 "defaults, disabling PWM interface\n");
1013 return 0;
1014 } else if (fix_pwm_polarity) {
1015 dev_info(&client->dev, "PWM configuration looks "
1016 "sane, won't touch\n");
1017 }
1018
1019 return 1;
1020}
1021
1022/* Called when we have found a new IT87. */
1023static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1024{
1025 int tmp, i;
1026
1027 /* initialize to sane defaults:
1028 * - if the chip is in manual pwm mode, this will be overwritten with
1029 * the actual settings on the chip (so in this case, initialization
1030 * is not needed)
1031 * - if in automatic or on/off mode, we could switch to manual mode,
1032 * read the registers and set manual_pwm_ctl accordingly, but currently
1033 * this is not implemented, so we initialize to something sane */
1034 for (i = 0; i < 3; i++) {
1035 data->manual_pwm_ctl[i] = 0xff;
1036 }
1037
1038 /* Check if temperature channnels are reset manually or by some reason */
1039 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1040 if ((tmp & 0x3f) == 0) {
1041 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1042 tmp = (tmp & 0xc0) | 0x2a;
1043 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1044 }
1045 data->sensor = tmp;
1046
1047 /* Check if voltage monitors are reset manually or by some reason */
1048 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1049 if ((tmp & 0xff) == 0) {
1050 /* Enable all voltage monitors */
1051 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1052 }
1053
1054 /* Check if tachometers are reset manually or by some reason */
1055 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1056 if ((data->fan_main_ctrl & 0x70) == 0) {
1057 /* Enable all fan tachometers */
1058 data->fan_main_ctrl |= 0x70;
1059 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1060 }
1061
1062 /* Set current fan mode registers and the default settings for the
1063 * other mode registers */
1064 for (i = 0; i < 3; i++) {
1065 if (data->fan_main_ctrl & (1 << i)) {
1066 /* pwm mode */
1067 tmp = it87_read_value(client, IT87_REG_PWM(i));
1068 if (tmp & 0x80) {
1069 /* automatic pwm - not yet implemented, but
1070 * leave the settings made by the BIOS alone
1071 * until a change is requested via the sysfs
1072 * interface */
1073 } else {
1074 /* manual pwm */
1075 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1076 }
1077 }
1078 }
1079
1080 /* Start monitoring */
1081 it87_write_value(client, IT87_REG_CONFIG,
1082 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1083 | (update_vbat ? 0x41 : 0x01));
1084}
1085
1086static struct it87_data *it87_update_device(struct device *dev)
1087{
1088 struct i2c_client *client = to_i2c_client(dev);
1089 struct it87_data *data = i2c_get_clientdata(client);
1090 int i;
1091
1092 down(&data->update_lock);
1093
1094 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1095 || !data->valid) {
1096
1097 if (update_vbat) {
1098 /* Cleared after each update, so reenable. Value
1099 returned by this read will be previous value */
1100 it87_write_value(client, IT87_REG_CONFIG,
1101 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1102 }
1103 for (i = 0; i <= 7; i++) {
1104 data->in[i] =
1105 it87_read_value(client, IT87_REG_VIN(i));
1106 data->in_min[i] =
1107 it87_read_value(client, IT87_REG_VIN_MIN(i));
1108 data->in_max[i] =
1109 it87_read_value(client, IT87_REG_VIN_MAX(i));
1110 }
1111 data->in[8] =
1112 it87_read_value(client, IT87_REG_VIN(8));
1113 /* Temperature sensor doesn't have limit registers, set
1114 to min and max value */
1115 data->in_min[8] = 0;
1116 data->in_max[8] = 255;
1117
1118 for (i = 0; i < 3; i++) {
1119 data->fan[i] =
1120 it87_read_value(client, IT87_REG_FAN(i));
1121 data->fan_min[i] =
1122 it87_read_value(client, IT87_REG_FAN_MIN(i));
1123 }
1124 for (i = 0; i < 3; i++) {
1125 data->temp[i] =
1126 it87_read_value(client, IT87_REG_TEMP(i));
1127 data->temp_high[i] =
1128 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1129 data->temp_low[i] =
1130 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1131 }
1132
1133 i = it87_read_value(client, IT87_REG_FAN_DIV);
1134 data->fan_div[0] = i & 0x07;
1135 data->fan_div[1] = (i >> 3) & 0x07;
1136 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1137
1138 data->alarms =
1139 it87_read_value(client, IT87_REG_ALARM1) |
1140 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1141 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1142 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1143
1144 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1145 /* The 8705 does not have VID capability */
1146 if (data->type == it8712) {
1147 data->vid = it87_read_value(client, IT87_REG_VID);
1148 data->vid &= 0x1f;
1149 }
1150 data->last_updated = jiffies;
1151 data->valid = 1;
1152 }
1153
1154 up(&data->update_lock);
1155
1156 return data;
1157}
1158
1159static int __init sm_it87_init(void)
1160{
1161 int addr;
1162
1163 if (!it87_find(&addr)) {
1164 normal_isa[0] = addr;
1165 }
1166 return i2c_add_driver(&it87_driver);
1167}
1168
1169static void __exit sm_it87_exit(void)
1170{
1171 i2c_del_driver(&it87_driver);
1172}
1173
1174
1175MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1176MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1177module_param(update_vbat, bool, 0);
1178MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1179module_param(fix_pwm_polarity, bool, 0);
1180MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1181MODULE_LICENSE("GPL");
1182
1183module_init(sm_it87_init);
1184module_exit(sm_it87_exit);