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