blob: 92c5b2420f9b40e1596fc3a5eec1d0c8b9ad66fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring.
4
5 Supports: IT8705F Super I/O chip w/LPC interface & SMBus
6 IT8712F Super I/O chip w/LPC interface & SMBus
7 Sis950 A clone of the IT8705F
8
9 Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
10 Largely inspired by lm78.c of the same package
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
27/*
28 djg@pdp8.net David Gesswein 7/18/01
29 Modified to fix bug with not all alarms enabled.
30 Added ability to read battery voltage and select temperature sensor
31 type at module load time.
32*/
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/jiffies.h>
38#include <linux/i2c.h>
39#include <linux/i2c-sensor.h>
40#include <linux/i2c-vid.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020041#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040042#include <linux/hwmon.h>
43#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/io.h>
45
46
47/* Addresses to scan */
48static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
49 0x2e, 0x2f, I2C_CLIENT_END };
50static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
51
52/* Insmod parameters */
53SENSORS_INSMOD_2(it87, it8712);
54
55#define REG 0x2e /* The register to read/write */
56#define DEV 0x07 /* Register: Logical device select */
57#define VAL 0x2f /* The value to read/write */
58#define PME 0x04 /* The device with the fan registers in it */
59#define DEVID 0x20 /* Register: Device ID */
60#define DEVREV 0x22 /* Register: Device Revision */
61
62static inline int
63superio_inb(int reg)
64{
65 outb(reg, REG);
66 return inb(VAL);
67}
68
69static int superio_inw(int reg)
70{
71 int val;
72 outb(reg++, REG);
73 val = inb(VAL) << 8;
74 outb(reg, REG);
75 val |= inb(VAL);
76 return val;
77}
78
79static inline void
80superio_select(void)
81{
82 outb(DEV, REG);
83 outb(PME, VAL);
84}
85
86static inline void
87superio_enter(void)
88{
89 outb(0x87, REG);
90 outb(0x01, REG);
91 outb(0x55, REG);
92 outb(0x55, REG);
93}
94
95static inline void
96superio_exit(void)
97{
98 outb(0x02, REG);
99 outb(0x02, VAL);
100}
101
102#define IT8712F_DEVID 0x8712
103#define IT8705F_DEVID 0x8705
104#define IT87_ACT_REG 0x30
105#define IT87_BASE_REG 0x60
106
107/* Update battery voltage after every reading if true */
108static int update_vbat;
109
110/* Not all BIOSes properly configure the PWM registers */
111static int fix_pwm_polarity;
112
113/* Chip Type */
114
115static u16 chip_type;
116
117/* Many IT87 constants specified below */
118
119/* Length of ISA address segment */
120#define IT87_EXTENT 8
121
122/* Where are the ISA address/data registers relative to the base address */
123#define IT87_ADDR_REG_OFFSET 5
124#define IT87_DATA_REG_OFFSET 6
125
126/*----- The IT87 registers -----*/
127
128#define IT87_REG_CONFIG 0x00
129
130#define IT87_REG_ALARM1 0x01
131#define IT87_REG_ALARM2 0x02
132#define IT87_REG_ALARM3 0x03
133
134#define IT87_REG_VID 0x0a
135#define IT87_REG_FAN_DIV 0x0b
136
137/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
138
139#define IT87_REG_FAN(nr) (0x0d + (nr))
140#define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
141#define IT87_REG_FAN_MAIN_CTRL 0x13
142#define IT87_REG_FAN_CTL 0x14
143#define IT87_REG_PWM(nr) (0x15 + (nr))
144
145#define IT87_REG_VIN(nr) (0x20 + (nr))
146#define IT87_REG_TEMP(nr) (0x29 + (nr))
147
148#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
149#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
150#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
151#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
152
153#define IT87_REG_I2C_ADDR 0x48
154
155#define IT87_REG_VIN_ENABLE 0x50
156#define IT87_REG_TEMP_ENABLE 0x51
157
158#define IT87_REG_CHIPID 0x58
159
160#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
161#define IN_FROM_REG(val) ((val) * 16)
162
163static inline u8 FAN_TO_REG(long rpm, int div)
164{
165 if (rpm == 0)
166 return 255;
167 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
168 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
169 254);
170}
171
172#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
173
174#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
175 ((val)+500)/1000),-128,127))
176#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#define PWM_TO_REG(val) ((val) >> 1)
179#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
180
181static int DIV_TO_REG(int val)
182{
183 int answer = 0;
184 while ((val >>= 1) != 0)
185 answer++;
186 return answer;
187}
188#define DIV_FROM_REG(val) (1 << (val))
189
190
191/* For each registered IT87, we need to keep some data in memory. That
192 data is pointed to by it87_list[NR]->data. The structure itself is
193 dynamically allocated, at the same time when a new it87 client is
194 allocated. */
195struct it87_data {
196 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400197 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct semaphore lock;
199 enum chips type;
200
201 struct semaphore update_lock;
202 char valid; /* !=0 if following fields are valid */
203 unsigned long last_updated; /* In jiffies */
204
205 u8 in[9]; /* Register value */
206 u8 in_max[9]; /* Register value */
207 u8 in_min[9]; /* Register value */
208 u8 fan[3]; /* Register value */
209 u8 fan_min[3]; /* Register value */
210 u8 temp[3]; /* Register value */
211 u8 temp_high[3]; /* Register value */
212 u8 temp_low[3]; /* Register value */
213 u8 sensor; /* Register value */
214 u8 fan_div[3]; /* Register encoding, shifted right */
215 u8 vid; /* Register encoding, combined */
216 int vrm;
217 u32 alarms; /* Register encoding, combined */
218 u8 fan_main_ctrl; /* Register value */
219 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
220};
221
222
223static int it87_attach_adapter(struct i2c_adapter *adapter);
224static int it87_find(int *address);
225static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
226static int it87_detach_client(struct i2c_client *client);
227
228static int it87_read_value(struct i2c_client *client, u8 register);
229static int it87_write_value(struct i2c_client *client, u8 register,
230 u8 value);
231static struct it87_data *it87_update_device(struct device *dev);
232static int it87_check_pwm(struct i2c_client *client);
233static void it87_init_client(struct i2c_client *client, struct it87_data *data);
234
235
236static struct i2c_driver it87_driver = {
237 .owner = THIS_MODULE,
238 .name = "it87",
239 .id = I2C_DRIVERID_IT87,
240 .flags = I2C_DF_NOTIFY,
241 .attach_adapter = it87_attach_adapter,
242 .detach_client = it87_detach_client,
243};
244
Jean Delvare20ad93d2005-06-05 11:53:25 +0200245static ssize_t show_in(struct device *dev, struct device_attribute *attr,
246 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200248 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
249 int nr = sensor_attr->index;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct it87_data *data = it87_update_device(dev);
252 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
253}
254
Jean Delvare20ad93d2005-06-05 11:53:25 +0200255static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
256 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200258 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
259 int nr = sensor_attr->index;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 struct it87_data *data = it87_update_device(dev);
262 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
263}
264
Jean Delvare20ad93d2005-06-05 11:53:25 +0200265static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
266 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200268 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
269 int nr = sensor_attr->index;
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct it87_data *data = it87_update_device(dev);
272 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
273}
274
Jean Delvare20ad93d2005-06-05 11:53:25 +0200275static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
276 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200278 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
279 int nr = sensor_attr->index;
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct i2c_client *client = to_i2c_client(dev);
282 struct it87_data *data = i2c_get_clientdata(client);
283 unsigned long val = simple_strtoul(buf, NULL, 10);
284
285 down(&data->update_lock);
286 data->in_min[nr] = IN_TO_REG(val);
287 it87_write_value(client, IT87_REG_VIN_MIN(nr),
288 data->in_min[nr]);
289 up(&data->update_lock);
290 return count;
291}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200292static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
293 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200295 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
296 int nr = sensor_attr->index;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct i2c_client *client = to_i2c_client(dev);
299 struct it87_data *data = i2c_get_clientdata(client);
300 unsigned long val = simple_strtoul(buf, NULL, 10);
301
302 down(&data->update_lock);
303 data->in_max[nr] = IN_TO_REG(val);
304 it87_write_value(client, IT87_REG_VIN_MAX(nr),
305 data->in_max[nr]);
306 up(&data->update_lock);
307 return count;
308}
309
310#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200311static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
312 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200315static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
316 show_in_min, set_in_min, offset); \
317static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
318 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320show_in_offset(0);
321limit_in_offset(0);
322show_in_offset(1);
323limit_in_offset(1);
324show_in_offset(2);
325limit_in_offset(2);
326show_in_offset(3);
327limit_in_offset(3);
328show_in_offset(4);
329limit_in_offset(4);
330show_in_offset(5);
331limit_in_offset(5);
332show_in_offset(6);
333limit_in_offset(6);
334show_in_offset(7);
335limit_in_offset(7);
336show_in_offset(8);
337
338/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200339static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
340 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200342 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
343 int nr = sensor_attr->index;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct it87_data *data = it87_update_device(dev);
346 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
347}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200348static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
349 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200351 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
352 int nr = sensor_attr->index;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct it87_data *data = it87_update_device(dev);
355 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
356}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200357static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
358 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200360 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
361 int nr = sensor_attr->index;
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct it87_data *data = it87_update_device(dev);
364 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
365}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200366static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
367 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200369 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
370 int nr = sensor_attr->index;
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct i2c_client *client = to_i2c_client(dev);
373 struct it87_data *data = i2c_get_clientdata(client);
374 int val = simple_strtol(buf, NULL, 10);
375
376 down(&data->update_lock);
377 data->temp_high[nr] = TEMP_TO_REG(val);
378 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
379 up(&data->update_lock);
380 return count;
381}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200382static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
383 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200385 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
386 int nr = sensor_attr->index;
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct i2c_client *client = to_i2c_client(dev);
389 struct it87_data *data = i2c_get_clientdata(client);
390 int val = simple_strtol(buf, NULL, 10);
391
392 down(&data->update_lock);
393 data->temp_low[nr] = TEMP_TO_REG(val);
394 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
395 up(&data->update_lock);
396 return count;
397}
398#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200399static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
400 show_temp, NULL, offset - 1); \
401static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
402 show_temp_max, set_temp_max, offset - 1); \
403static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
404 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406show_temp_offset(1);
407show_temp_offset(2);
408show_temp_offset(3);
409
Jean Delvare20ad93d2005-06-05 11:53:25 +0200410static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
411 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200413 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
414 int nr = sensor_attr->index;
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct it87_data *data = it87_update_device(dev);
417 u8 reg = data->sensor; /* In case the value is updated while we use it */
418
419 if (reg & (1 << nr))
420 return sprintf(buf, "3\n"); /* thermal diode */
421 if (reg & (8 << nr))
422 return sprintf(buf, "2\n"); /* thermistor */
423 return sprintf(buf, "0\n"); /* disabled */
424}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200425static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
426 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200428 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
429 int nr = sensor_attr->index;
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct i2c_client *client = to_i2c_client(dev);
432 struct it87_data *data = i2c_get_clientdata(client);
433 int val = simple_strtol(buf, NULL, 10);
434
435 down(&data->update_lock);
436
437 data->sensor &= ~(1 << nr);
438 data->sensor &= ~(8 << nr);
439 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
440 if (val == 3)
441 data->sensor |= 1 << nr;
442 else if (val == 2)
443 data->sensor |= 8 << nr;
444 else if (val != 0) {
445 up(&data->update_lock);
446 return -EINVAL;
447 }
448 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
449 up(&data->update_lock);
450 return count;
451}
452#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200453static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
454 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456show_sensor_offset(1);
457show_sensor_offset(2);
458show_sensor_offset(3);
459
460/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200461static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
462 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200464 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
465 int nr = sensor_attr->index;
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct it87_data *data = it87_update_device(dev);
468 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
469 DIV_FROM_REG(data->fan_div[nr])));
470}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200471static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
472 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200474 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
475 int nr = sensor_attr->index;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 struct it87_data *data = it87_update_device(dev);
478 return sprintf(buf,"%d\n",
479 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
480}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200481static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
482 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200484 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
485 int nr = sensor_attr->index;
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct it87_data *data = it87_update_device(dev);
488 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
489}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200490static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
491 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200493 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
494 int nr = sensor_attr->index;
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct it87_data *data = it87_update_device(dev);
497 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
498}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200499static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
500 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200502 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
503 int nr = sensor_attr->index;
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct it87_data *data = it87_update_device(dev);
506 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
507}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200508static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
509 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200511 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
512 int nr = sensor_attr->index;
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct i2c_client *client = to_i2c_client(dev);
515 struct it87_data *data = i2c_get_clientdata(client);
516 int val = simple_strtol(buf, NULL, 10);
517
518 down(&data->update_lock);
519 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
520 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
521 up(&data->update_lock);
522 return count;
523}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200524static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
525 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200527 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
528 int nr = sensor_attr->index;
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct i2c_client *client = to_i2c_client(dev);
531 struct it87_data *data = i2c_get_clientdata(client);
532 int val = simple_strtol(buf, NULL, 10);
533 int i, min[3];
534 u8 old;
535
536 down(&data->update_lock);
537 old = it87_read_value(client, IT87_REG_FAN_DIV);
538
539 for (i = 0; i < 3; i++)
540 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
541
542 switch (nr) {
543 case 0:
544 case 1:
545 data->fan_div[nr] = DIV_TO_REG(val);
546 break;
547 case 2:
548 if (val < 8)
549 data->fan_div[nr] = 1;
550 else
551 data->fan_div[nr] = 3;
552 }
553 val = old & 0x80;
554 val |= (data->fan_div[0] & 0x07);
555 val |= (data->fan_div[1] & 0x07) << 3;
556 if (data->fan_div[2] == 3)
557 val |= 0x1 << 6;
558 it87_write_value(client, IT87_REG_FAN_DIV, val);
559
560 for (i = 0; i < 3; i++) {
561 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
562 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
563 }
564 up(&data->update_lock);
565 return count;
566}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200567static ssize_t set_pwm_enable(struct device *dev,
568 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200570 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
571 int nr = sensor_attr->index;
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct i2c_client *client = to_i2c_client(dev);
574 struct it87_data *data = i2c_get_clientdata(client);
575 int val = simple_strtol(buf, NULL, 10);
576
577 down(&data->update_lock);
578
579 if (val == 0) {
580 int tmp;
581 /* make sure the fan is on when in on/off mode */
582 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
583 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
584 /* set on/off mode */
585 data->fan_main_ctrl &= ~(1 << nr);
586 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
587 } else if (val == 1) {
588 /* set SmartGuardian mode */
589 data->fan_main_ctrl |= (1 << nr);
590 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
591 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
592 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
593 } else {
594 up(&data->update_lock);
595 return -EINVAL;
596 }
597
598 up(&data->update_lock);
599 return count;
600}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200601static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
602 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200604 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
605 int nr = sensor_attr->index;
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 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
611 if (val < 0 || val > 255)
612 return -EINVAL;
613
614 down(&data->update_lock);
615 data->manual_pwm_ctl[nr] = val;
616 if (data->fan_main_ctrl & (1 << nr))
617 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
618 up(&data->update_lock);
619 return count;
620}
621
Jean Delvare20ad93d2005-06-05 11:53:25 +0200622#define show_fan_offset(offset) \
623static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
624 show_fan, NULL, offset - 1); \
625static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
626 show_fan_min, set_fan_min, offset - 1); \
627static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
628 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630show_fan_offset(1);
631show_fan_offset(2);
632show_fan_offset(3);
633
634#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200635static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
636 show_pwm_enable, set_pwm_enable, offset - 1); \
637static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
638 show_pwm, set_pwm, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640show_pwm_offset(1);
641show_pwm_offset(2);
642show_pwm_offset(3);
643
644/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400645static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200648 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
Jean Delvare1d66c642005-04-18 21:16:59 -0700650static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400653show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 struct it87_data *data = it87_update_device(dev);
656 return sprintf(buf, "%ld\n", (long) data->vrm);
657}
658static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400659store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct i2c_client *client = to_i2c_client(dev);
662 struct it87_data *data = i2c_get_clientdata(client);
663 u32 val;
664
665 val = simple_strtoul(buf, NULL, 10);
666 data->vrm = val;
667
668 return count;
669}
670static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
671#define device_create_file_vrm(client) \
672device_create_file(&client->dev, &dev_attr_vrm)
673
674static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400675show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct it87_data *data = it87_update_device(dev);
678 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
679}
680static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
681#define device_create_file_vid(client) \
682device_create_file(&client->dev, &dev_attr_cpu0_vid)
683
684/* This function is called when:
685 * it87_driver is inserted (when this module is loaded), for each
686 available adapter
687 * when a new adapter is inserted (and it87_driver is still present) */
688static int it87_attach_adapter(struct i2c_adapter *adapter)
689{
690 if (!(adapter->class & I2C_CLASS_HWMON))
691 return 0;
692 return i2c_detect(adapter, &addr_data, it87_detect);
693}
694
695/* SuperIO detection - will change normal_isa[0] if a chip is found */
696static int it87_find(int *address)
697{
698 int err = -ENODEV;
699
700 superio_enter();
701 chip_type = superio_inw(DEVID);
702 if (chip_type != IT8712F_DEVID
703 && chip_type != IT8705F_DEVID)
704 goto exit;
705
706 superio_select();
707 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
708 pr_info("it87: Device not activated, skipping\n");
709 goto exit;
710 }
711
712 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
713 if (*address == 0) {
714 pr_info("it87: Base address not set, skipping\n");
715 goto exit;
716 }
717
718 err = 0;
719 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
720 chip_type, *address, superio_inb(DEVREV) & 0x0f);
721
722exit:
723 superio_exit();
724 return err;
725}
726
727/* This function is called by i2c_detect */
728int it87_detect(struct i2c_adapter *adapter, int address, int kind)
729{
730 int i;
731 struct i2c_client *new_client;
732 struct it87_data *data;
733 int err = 0;
734 const char *name = "";
735 int is_isa = i2c_is_isa_adapter(adapter);
736 int enable_pwm_interface;
737
738 if (!is_isa &&
739 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
740 goto ERROR0;
741
742 /* Reserve the ISA region */
743 if (is_isa)
744 if (!request_region(address, IT87_EXTENT, it87_driver.name))
745 goto ERROR0;
746
747 /* Probe whether there is anything available on this address. Already
748 done for SMBus and Super-I/O clients */
749 if (kind < 0) {
750 if (is_isa && !chip_type) {
751#define REALLY_SLOW_IO
752 /* We need the timeouts for at least some IT87-like chips. But only
753 if we read 'undefined' registers. */
754 i = inb_p(address + 1);
755 if (inb_p(address + 2) != i
756 || inb_p(address + 3) != i
757 || inb_p(address + 7) != i) {
758 err = -ENODEV;
759 goto ERROR1;
760 }
761#undef REALLY_SLOW_IO
762
763 /* Let's just hope nothing breaks here */
764 i = inb_p(address + 5) & 0x7f;
765 outb_p(~i & 0x7f, address + 5);
766 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
767 outb_p(i, address + 5);
768 err = -ENODEV;
769 goto ERROR1;
770 }
771 }
772 }
773
774 /* OK. For now, we presume we have a valid client. We now create the
775 client structure, even though we cannot fill it completely yet.
776 But it allows us to access it87_{read,write}_value. */
777
778 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
779 err = -ENOMEM;
780 goto ERROR1;
781 }
782 memset(data, 0, sizeof(struct it87_data));
783
784 new_client = &data->client;
785 if (is_isa)
786 init_MUTEX(&data->lock);
787 i2c_set_clientdata(new_client, data);
788 new_client->addr = address;
789 new_client->adapter = adapter;
790 new_client->driver = &it87_driver;
791 new_client->flags = 0;
792
793 /* Now, we do the remaining detection. */
794
795 if (kind < 0) {
796 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
797 || (!is_isa
798 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
799 err = -ENODEV;
800 goto ERROR2;
801 }
802 }
803
804 /* Determine the chip type. */
805 if (kind <= 0) {
806 i = it87_read_value(new_client, IT87_REG_CHIPID);
807 if (i == 0x90) {
808 kind = it87;
809 if ((is_isa) && (chip_type == IT8712F_DEVID))
810 kind = it8712;
811 }
812 else {
813 if (kind == 0)
814 dev_info(&adapter->dev,
815 "Ignoring 'force' parameter for unknown chip at "
816 "adapter %d, address 0x%02x\n",
817 i2c_adapter_id(adapter), address);
818 err = -ENODEV;
819 goto ERROR2;
820 }
821 }
822
823 if (kind == it87) {
824 name = "it87";
825 } else if (kind == it8712) {
826 name = "it8712";
827 }
828
829 /* Fill in the remaining client fields and put it into the global list */
830 strlcpy(new_client->name, name, I2C_NAME_SIZE);
831 data->type = kind;
832 data->valid = 0;
833 init_MUTEX(&data->update_lock);
834
835 /* Tell the I2C layer a new client has arrived */
836 if ((err = i2c_attach_client(new_client)))
837 goto ERROR2;
838
839 /* Check PWM configuration */
840 enable_pwm_interface = it87_check_pwm(new_client);
841
842 /* Initialize the IT87 chip */
843 it87_init_client(new_client, data);
844
845 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400846 data->class_dev = hwmon_device_register(&new_client->dev);
847 if (IS_ERR(data->class_dev)) {
848 err = PTR_ERR(data->class_dev);
849 goto ERROR3;
850 }
851
Jean Delvare20ad93d2005-06-05 11:53:25 +0200852 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
853 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
854 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
855 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
856 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
857 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
858 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
859 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
860 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
861 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
862 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
863 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
864 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
865 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
866 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
889 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
890 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
891 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
896 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
897 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 device_create_file(&new_client->dev, &dev_attr_alarms);
899 if (enable_pwm_interface) {
Jean Delvare20ad93d2005-06-05 11:53:25 +0200900 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
901 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
902 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
903 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
904 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
905 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
908 if (data->type == it8712) {
909 data->vrm = i2c_which_vrm();
910 device_create_file_vrm(new_client);
911 device_create_file_vid(new_client);
912 }
913
914 return 0;
915
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400916ERROR3:
917 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918ERROR2:
919 kfree(data);
920ERROR1:
921 if (is_isa)
922 release_region(address, IT87_EXTENT);
923ERROR0:
924 return err;
925}
926
927static int it87_detach_client(struct i2c_client *client)
928{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400929 struct it87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 int err;
931
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400932 hwmon_device_unregister(data->class_dev);
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if ((err = i2c_detach_client(client))) {
935 dev_err(&client->dev,
936 "Client deregistration failed, client not detached.\n");
937 return err;
938 }
939
940 if(i2c_is_isa_client(client))
941 release_region(client->addr, IT87_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400942 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 return 0;
945}
946
Steven Cole44bbe872005-05-03 18:21:25 -0600947/* The SMBus locks itself, but ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 We don't want to lock the whole ISA bus, so we lock each client
949 separately.
950 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
951 would slow down the IT87 access and should not be necessary. */
952static int it87_read_value(struct i2c_client *client, u8 reg)
953{
954 struct it87_data *data = i2c_get_clientdata(client);
955
956 int res;
957 if (i2c_is_isa_client(client)) {
958 down(&data->lock);
959 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
960 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
961 up(&data->lock);
962 return res;
963 } else
964 return i2c_smbus_read_byte_data(client, reg);
965}
966
Steven Cole44bbe872005-05-03 18:21:25 -0600967/* The SMBus locks itself, but ISA access muse be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 We don't want to lock the whole ISA bus, so we lock each client
969 separately.
970 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
971 would slow down the IT87 access and should not be necessary. */
972static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
973{
974 struct it87_data *data = i2c_get_clientdata(client);
975
976 if (i2c_is_isa_client(client)) {
977 down(&data->lock);
978 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
979 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
980 up(&data->lock);
981 return 0;
982 } else
983 return i2c_smbus_write_byte_data(client, reg, value);
984}
985
986/* Return 1 if and only if the PWM interface is safe to use */
987static int it87_check_pwm(struct i2c_client *client)
988{
989 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
990 * and polarity set to active low is sign that this is the case so we
991 * disable pwm control to protect the user. */
992 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
993 if ((tmp & 0x87) == 0) {
994 if (fix_pwm_polarity) {
995 /* The user asks us to attempt a chip reconfiguration.
996 * This means switching to active high polarity and
997 * inverting all fan speed values. */
998 int i;
999 u8 pwm[3];
1000
1001 for (i = 0; i < 3; i++)
1002 pwm[i] = it87_read_value(client,
1003 IT87_REG_PWM(i));
1004
1005 /* If any fan is in automatic pwm mode, the polarity
1006 * might be correct, as suspicious as it seems, so we
1007 * better don't change anything (but still disable the
1008 * PWM interface). */
1009 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1010 dev_info(&client->dev, "Reconfiguring PWM to "
1011 "active high polarity\n");
1012 it87_write_value(client, IT87_REG_FAN_CTL,
1013 tmp | 0x87);
1014 for (i = 0; i < 3; i++)
1015 it87_write_value(client,
1016 IT87_REG_PWM(i),
1017 0x7f & ~pwm[i]);
1018 return 1;
1019 }
1020
1021 dev_info(&client->dev, "PWM configuration is "
1022 "too broken to be fixed\n");
1023 }
1024
1025 dev_info(&client->dev, "Detected broken BIOS "
1026 "defaults, disabling PWM interface\n");
1027 return 0;
1028 } else if (fix_pwm_polarity) {
1029 dev_info(&client->dev, "PWM configuration looks "
1030 "sane, won't touch\n");
1031 }
1032
1033 return 1;
1034}
1035
1036/* Called when we have found a new IT87. */
1037static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1038{
1039 int tmp, i;
1040
1041 /* initialize to sane defaults:
1042 * - if the chip is in manual pwm mode, this will be overwritten with
1043 * the actual settings on the chip (so in this case, initialization
1044 * is not needed)
1045 * - if in automatic or on/off mode, we could switch to manual mode,
1046 * read the registers and set manual_pwm_ctl accordingly, but currently
1047 * this is not implemented, so we initialize to something sane */
1048 for (i = 0; i < 3; i++) {
1049 data->manual_pwm_ctl[i] = 0xff;
1050 }
1051
1052 /* Check if temperature channnels are reset manually or by some reason */
1053 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1054 if ((tmp & 0x3f) == 0) {
1055 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1056 tmp = (tmp & 0xc0) | 0x2a;
1057 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1058 }
1059 data->sensor = tmp;
1060
1061 /* Check if voltage monitors are reset manually or by some reason */
1062 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1063 if ((tmp & 0xff) == 0) {
1064 /* Enable all voltage monitors */
1065 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1066 }
1067
1068 /* Check if tachometers are reset manually or by some reason */
1069 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1070 if ((data->fan_main_ctrl & 0x70) == 0) {
1071 /* Enable all fan tachometers */
1072 data->fan_main_ctrl |= 0x70;
1073 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1074 }
1075
1076 /* Set current fan mode registers and the default settings for the
1077 * other mode registers */
1078 for (i = 0; i < 3; i++) {
1079 if (data->fan_main_ctrl & (1 << i)) {
1080 /* pwm mode */
1081 tmp = it87_read_value(client, IT87_REG_PWM(i));
1082 if (tmp & 0x80) {
1083 /* automatic pwm - not yet implemented, but
1084 * leave the settings made by the BIOS alone
1085 * until a change is requested via the sysfs
1086 * interface */
1087 } else {
1088 /* manual pwm */
1089 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1090 }
1091 }
1092 }
1093
1094 /* Start monitoring */
1095 it87_write_value(client, IT87_REG_CONFIG,
1096 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1097 | (update_vbat ? 0x41 : 0x01));
1098}
1099
1100static struct it87_data *it87_update_device(struct device *dev)
1101{
1102 struct i2c_client *client = to_i2c_client(dev);
1103 struct it87_data *data = i2c_get_clientdata(client);
1104 int i;
1105
1106 down(&data->update_lock);
1107
1108 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1109 || !data->valid) {
1110
1111 if (update_vbat) {
1112 /* Cleared after each update, so reenable. Value
1113 returned by this read will be previous value */
1114 it87_write_value(client, IT87_REG_CONFIG,
1115 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1116 }
1117 for (i = 0; i <= 7; i++) {
1118 data->in[i] =
1119 it87_read_value(client, IT87_REG_VIN(i));
1120 data->in_min[i] =
1121 it87_read_value(client, IT87_REG_VIN_MIN(i));
1122 data->in_max[i] =
1123 it87_read_value(client, IT87_REG_VIN_MAX(i));
1124 }
1125 data->in[8] =
1126 it87_read_value(client, IT87_REG_VIN(8));
1127 /* Temperature sensor doesn't have limit registers, set
1128 to min and max value */
1129 data->in_min[8] = 0;
1130 data->in_max[8] = 255;
1131
1132 for (i = 0; i < 3; i++) {
1133 data->fan[i] =
1134 it87_read_value(client, IT87_REG_FAN(i));
1135 data->fan_min[i] =
1136 it87_read_value(client, IT87_REG_FAN_MIN(i));
1137 }
1138 for (i = 0; i < 3; i++) {
1139 data->temp[i] =
1140 it87_read_value(client, IT87_REG_TEMP(i));
1141 data->temp_high[i] =
1142 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1143 data->temp_low[i] =
1144 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1145 }
1146
1147 i = it87_read_value(client, IT87_REG_FAN_DIV);
1148 data->fan_div[0] = i & 0x07;
1149 data->fan_div[1] = (i >> 3) & 0x07;
1150 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1151
1152 data->alarms =
1153 it87_read_value(client, IT87_REG_ALARM1) |
1154 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1155 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1156 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1157
1158 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1159 /* The 8705 does not have VID capability */
1160 if (data->type == it8712) {
1161 data->vid = it87_read_value(client, IT87_REG_VID);
1162 data->vid &= 0x1f;
1163 }
1164 data->last_updated = jiffies;
1165 data->valid = 1;
1166 }
1167
1168 up(&data->update_lock);
1169
1170 return data;
1171}
1172
1173static int __init sm_it87_init(void)
1174{
1175 int addr;
1176
1177 if (!it87_find(&addr)) {
1178 normal_isa[0] = addr;
1179 }
1180 return i2c_add_driver(&it87_driver);
1181}
1182
1183static void __exit sm_it87_exit(void)
1184{
1185 i2c_del_driver(&it87_driver);
1186}
1187
1188
1189MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1190MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1191module_param(update_vbat, bool, 0);
1192MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1193module_param(fix_pwm_polarity, bool, 0);
1194MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1195MODULE_LICENSE("GPL");
1196
1197module_init(sm_it87_init);
1198module_exit(sm_it87_exit);