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