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