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