smsc47m1: dev_warn fix
[deliverable/linux.git] / drivers / hwmon / it87.c
CommitLineData
1da177e4
LT
1/*
2 it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring.
4
91749996 5 Supports: IT8705F Super I/O chip w/LPC interface
1da177e4
LT
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
1da177e4
LT
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/jiffies.h>
38#include <linux/i2c.h>
fde09509 39#include <linux/i2c-isa.h>
943b0830 40#include <linux/hwmon.h>
303760b4
JD
41#include <linux/hwmon-sysfs.h>
42#include <linux/hwmon-vid.h>
943b0830 43#include <linux/err.h>
9a61bf63 44#include <linux/mutex.h>
1da177e4
LT
45#include <asm/io.h>
46
47
48/* Addresses to scan */
c5e3fbf2 49static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
91749996 50static unsigned short isa_address;
1da177e4
LT
51
52/* Insmod parameters */
f4b50261 53I2C_CLIENT_INSMOD_2(it87, it8712);
1da177e4
LT
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
1da177e4
LT
178#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;
943b0830 197 struct class_device *class_dev;
9a61bf63 198 struct mutex lock;
1da177e4
LT
199 enum chips type;
200
9a61bf63 201 struct mutex update_lock;
1da177e4
LT
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 */
a7be58a1 216 u8 vrm;
1da177e4
LT
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);
2d8672c5 224static int it87_isa_attach_adapter(struct i2c_adapter *adapter);
1da177e4
LT
225static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
226static int it87_detach_client(struct i2c_client *client);
227
f6c27fc1
DJ
228static int it87_read_value(struct i2c_client *client, u8 reg);
229static int it87_write_value(struct i2c_client *client, u8 reg, u8 value);
1da177e4
LT
230static struct it87_data *it87_update_device(struct device *dev);
231static int it87_check_pwm(struct i2c_client *client);
232static void it87_init_client(struct i2c_client *client, struct it87_data *data);
233
234
235static struct i2c_driver it87_driver = {
cdaf7934 236 .driver = {
cdaf7934
LR
237 .name = "it87",
238 },
1da177e4 239 .id = I2C_DRIVERID_IT87,
1da177e4
LT
240 .attach_adapter = it87_attach_adapter,
241 .detach_client = it87_detach_client,
242};
243
fde09509 244static struct i2c_driver it87_isa_driver = {
cdaf7934 245 .driver = {
87218842 246 .owner = THIS_MODULE,
cdaf7934
LR
247 .name = "it87-isa",
248 },
2d8672c5 249 .attach_adapter = it87_isa_attach_adapter,
fde09509
JD
250 .detach_client = it87_detach_client,
251};
252
253
20ad93d4
JD
254static ssize_t show_in(struct device *dev, struct device_attribute *attr,
255 char *buf)
1da177e4 256{
20ad93d4
JD
257 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
258 int nr = sensor_attr->index;
259
1da177e4
LT
260 struct it87_data *data = it87_update_device(dev);
261 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
262}
263
20ad93d4
JD
264static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
265 char *buf)
1da177e4 266{
20ad93d4
JD
267 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
268 int nr = sensor_attr->index;
269
1da177e4
LT
270 struct it87_data *data = it87_update_device(dev);
271 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
272}
273
20ad93d4
JD
274static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
275 char *buf)
1da177e4 276{
20ad93d4
JD
277 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
278 int nr = sensor_attr->index;
279
1da177e4
LT
280 struct it87_data *data = it87_update_device(dev);
281 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
282}
283
20ad93d4
JD
284static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
285 const char *buf, size_t count)
1da177e4 286{
20ad93d4
JD
287 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
288 int nr = sensor_attr->index;
289
1da177e4
LT
290 struct i2c_client *client = to_i2c_client(dev);
291 struct it87_data *data = i2c_get_clientdata(client);
292 unsigned long val = simple_strtoul(buf, NULL, 10);
293
9a61bf63 294 mutex_lock(&data->update_lock);
1da177e4
LT
295 data->in_min[nr] = IN_TO_REG(val);
296 it87_write_value(client, IT87_REG_VIN_MIN(nr),
297 data->in_min[nr]);
9a61bf63 298 mutex_unlock(&data->update_lock);
1da177e4
LT
299 return count;
300}
20ad93d4
JD
301static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
302 const char *buf, size_t count)
1da177e4 303{
20ad93d4
JD
304 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
305 int nr = sensor_attr->index;
306
1da177e4
LT
307 struct i2c_client *client = to_i2c_client(dev);
308 struct it87_data *data = i2c_get_clientdata(client);
309 unsigned long val = simple_strtoul(buf, NULL, 10);
310
9a61bf63 311 mutex_lock(&data->update_lock);
1da177e4
LT
312 data->in_max[nr] = IN_TO_REG(val);
313 it87_write_value(client, IT87_REG_VIN_MAX(nr),
314 data->in_max[nr]);
9a61bf63 315 mutex_unlock(&data->update_lock);
1da177e4
LT
316 return count;
317}
318
319#define show_in_offset(offset) \
20ad93d4
JD
320static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
321 show_in, NULL, offset);
1da177e4
LT
322
323#define limit_in_offset(offset) \
20ad93d4
JD
324static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
325 show_in_min, set_in_min, offset); \
326static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
327 show_in_max, set_in_max, offset);
1da177e4
LT
328
329show_in_offset(0);
330limit_in_offset(0);
331show_in_offset(1);
332limit_in_offset(1);
333show_in_offset(2);
334limit_in_offset(2);
335show_in_offset(3);
336limit_in_offset(3);
337show_in_offset(4);
338limit_in_offset(4);
339show_in_offset(5);
340limit_in_offset(5);
341show_in_offset(6);
342limit_in_offset(6);
343show_in_offset(7);
344limit_in_offset(7);
345show_in_offset(8);
346
347/* 3 temperatures */
20ad93d4
JD
348static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
349 char *buf)
1da177e4 350{
20ad93d4
JD
351 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
352 int nr = sensor_attr->index;
353
1da177e4
LT
354 struct it87_data *data = it87_update_device(dev);
355 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
356}
20ad93d4
JD
357static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
358 char *buf)
1da177e4 359{
20ad93d4
JD
360 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
361 int nr = sensor_attr->index;
362
1da177e4
LT
363 struct it87_data *data = it87_update_device(dev);
364 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
365}
20ad93d4
JD
366static ssize_t show_temp_min(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_low[nr]));
374}
20ad93d4
JD
375static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
376 const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
382 struct it87_data *data = i2c_get_clientdata(client);
383 int val = simple_strtol(buf, NULL, 10);
384
9a61bf63 385 mutex_lock(&data->update_lock);
1da177e4
LT
386 data->temp_high[nr] = TEMP_TO_REG(val);
387 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
9a61bf63 388 mutex_unlock(&data->update_lock);
1da177e4
LT
389 return count;
390}
20ad93d4
JD
391static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
392 const char *buf, size_t count)
1da177e4 393{
20ad93d4
JD
394 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
395 int nr = sensor_attr->index;
396
1da177e4
LT
397 struct i2c_client *client = to_i2c_client(dev);
398 struct it87_data *data = i2c_get_clientdata(client);
399 int val = simple_strtol(buf, NULL, 10);
400
9a61bf63 401 mutex_lock(&data->update_lock);
1da177e4
LT
402 data->temp_low[nr] = TEMP_TO_REG(val);
403 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
9a61bf63 404 mutex_unlock(&data->update_lock);
1da177e4
LT
405 return count;
406}
407#define show_temp_offset(offset) \
20ad93d4
JD
408static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
409 show_temp, NULL, offset - 1); \
410static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
411 show_temp_max, set_temp_max, offset - 1); \
412static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
413 show_temp_min, set_temp_min, offset - 1);
1da177e4
LT
414
415show_temp_offset(1);
416show_temp_offset(2);
417show_temp_offset(3);
418
20ad93d4
JD
419static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
420 char *buf)
1da177e4 421{
20ad93d4
JD
422 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
423 int nr = sensor_attr->index;
424
1da177e4
LT
425 struct it87_data *data = it87_update_device(dev);
426 u8 reg = data->sensor; /* In case the value is updated while we use it */
427
428 if (reg & (1 << nr))
429 return sprintf(buf, "3\n"); /* thermal diode */
430 if (reg & (8 << nr))
431 return sprintf(buf, "2\n"); /* thermistor */
432 return sprintf(buf, "0\n"); /* disabled */
433}
20ad93d4
JD
434static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
435 const char *buf, size_t count)
1da177e4 436{
20ad93d4
JD
437 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
438 int nr = sensor_attr->index;
439
1da177e4
LT
440 struct i2c_client *client = to_i2c_client(dev);
441 struct it87_data *data = i2c_get_clientdata(client);
442 int val = simple_strtol(buf, NULL, 10);
443
9a61bf63 444 mutex_lock(&data->update_lock);
1da177e4
LT
445
446 data->sensor &= ~(1 << nr);
447 data->sensor &= ~(8 << nr);
448 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
449 if (val == 3)
450 data->sensor |= 1 << nr;
451 else if (val == 2)
452 data->sensor |= 8 << nr;
453 else if (val != 0) {
9a61bf63 454 mutex_unlock(&data->update_lock);
1da177e4
LT
455 return -EINVAL;
456 }
457 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
9a61bf63 458 mutex_unlock(&data->update_lock);
1da177e4
LT
459 return count;
460}
461#define show_sensor_offset(offset) \
20ad93d4
JD
462static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
463 show_sensor, set_sensor, offset - 1);
1da177e4
LT
464
465show_sensor_offset(1);
466show_sensor_offset(2);
467show_sensor_offset(3);
468
469/* 3 Fans */
20ad93d4
JD
470static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
471 char *buf)
1da177e4 472{
20ad93d4
JD
473 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
474 int nr = sensor_attr->index;
475
1da177e4
LT
476 struct it87_data *data = it87_update_device(dev);
477 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
478 DIV_FROM_REG(data->fan_div[nr])));
479}
20ad93d4
JD
480static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
481 char *buf)
1da177e4 482{
20ad93d4
JD
483 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
484 int nr = sensor_attr->index;
485
1da177e4
LT
486 struct it87_data *data = it87_update_device(dev);
487 return sprintf(buf,"%d\n",
488 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
489}
20ad93d4
JD
490static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
491 char *buf)
1da177e4 492{
20ad93d4
JD
493 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
494 int nr = sensor_attr->index;
495
1da177e4
LT
496 struct it87_data *data = it87_update_device(dev);
497 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
498}
20ad93d4
JD
499static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
500 char *buf)
1da177e4 501{
20ad93d4
JD
502 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
503 int nr = sensor_attr->index;
504
1da177e4
LT
505 struct it87_data *data = it87_update_device(dev);
506 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
507}
20ad93d4
JD
508static ssize_t show_pwm(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", data->manual_pwm_ctl[nr]);
516}
20ad93d4
JD
517static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
518 const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
524 struct it87_data *data = i2c_get_clientdata(client);
525 int val = simple_strtol(buf, NULL, 10);
07eab46d 526 u8 reg = it87_read_value(client, IT87_REG_FAN_DIV);
1da177e4 527
9a61bf63 528 mutex_lock(&data->update_lock);
07eab46d
JD
529 switch (nr) {
530 case 0: data->fan_div[nr] = reg & 0x07; break;
531 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
532 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
533 }
534
1da177e4
LT
535 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
536 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf63 537 mutex_unlock(&data->update_lock);
1da177e4
LT
538 return count;
539}
20ad93d4
JD
540static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
541 const char *buf, size_t count)
1da177e4 542{
20ad93d4
JD
543 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
544 int nr = sensor_attr->index;
545
1da177e4
LT
546 struct i2c_client *client = to_i2c_client(dev);
547 struct it87_data *data = i2c_get_clientdata(client);
548 int val = simple_strtol(buf, NULL, 10);
549 int i, min[3];
550 u8 old;
551
9a61bf63 552 mutex_lock(&data->update_lock);
1da177e4
LT
553 old = it87_read_value(client, IT87_REG_FAN_DIV);
554
555 for (i = 0; i < 3; i++)
556 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
557
558 switch (nr) {
559 case 0:
560 case 1:
561 data->fan_div[nr] = DIV_TO_REG(val);
562 break;
563 case 2:
564 if (val < 8)
565 data->fan_div[nr] = 1;
566 else
567 data->fan_div[nr] = 3;
568 }
569 val = old & 0x80;
570 val |= (data->fan_div[0] & 0x07);
571 val |= (data->fan_div[1] & 0x07) << 3;
572 if (data->fan_div[2] == 3)
573 val |= 0x1 << 6;
574 it87_write_value(client, IT87_REG_FAN_DIV, val);
575
576 for (i = 0; i < 3; i++) {
577 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
578 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
579 }
9a61bf63 580 mutex_unlock(&data->update_lock);
1da177e4
LT
581 return count;
582}
20ad93d4
JD
583static ssize_t set_pwm_enable(struct device *dev,
584 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 585{
20ad93d4
JD
586 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
587 int nr = sensor_attr->index;
588
1da177e4
LT
589 struct i2c_client *client = to_i2c_client(dev);
590 struct it87_data *data = i2c_get_clientdata(client);
591 int val = simple_strtol(buf, NULL, 10);
592
9a61bf63 593 mutex_lock(&data->update_lock);
1da177e4
LT
594
595 if (val == 0) {
596 int tmp;
597 /* make sure the fan is on when in on/off mode */
598 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
599 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
600 /* set on/off mode */
601 data->fan_main_ctrl &= ~(1 << nr);
602 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
603 } else if (val == 1) {
604 /* set SmartGuardian mode */
605 data->fan_main_ctrl |= (1 << nr);
606 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
607 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
608 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
609 } else {
9a61bf63 610 mutex_unlock(&data->update_lock);
1da177e4
LT
611 return -EINVAL;
612 }
613
9a61bf63 614 mutex_unlock(&data->update_lock);
1da177e4
LT
615 return count;
616}
20ad93d4
JD
617static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
618 const char *buf, size_t count)
1da177e4 619{
20ad93d4
JD
620 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
621 int nr = sensor_attr->index;
622
1da177e4
LT
623 struct i2c_client *client = to_i2c_client(dev);
624 struct it87_data *data = i2c_get_clientdata(client);
625 int val = simple_strtol(buf, NULL, 10);
626
627 if (val < 0 || val > 255)
628 return -EINVAL;
629
9a61bf63 630 mutex_lock(&data->update_lock);
1da177e4
LT
631 data->manual_pwm_ctl[nr] = val;
632 if (data->fan_main_ctrl & (1 << nr))
633 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
9a61bf63 634 mutex_unlock(&data->update_lock);
1da177e4
LT
635 return count;
636}
637
20ad93d4
JD
638#define show_fan_offset(offset) \
639static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
640 show_fan, NULL, offset - 1); \
641static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
642 show_fan_min, set_fan_min, offset - 1); \
643static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
644 show_fan_div, set_fan_div, offset - 1);
1da177e4
LT
645
646show_fan_offset(1);
647show_fan_offset(2);
648show_fan_offset(3);
649
650#define show_pwm_offset(offset) \
20ad93d4
JD
651static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
652 show_pwm_enable, set_pwm_enable, offset - 1); \
653static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
654 show_pwm, set_pwm, offset - 1);
1da177e4
LT
655
656show_pwm_offset(1);
657show_pwm_offset(2);
658show_pwm_offset(3);
659
660/* Alarms */
30f74292 661static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
662{
663 struct it87_data *data = it87_update_device(dev);
68188ba7 664 return sprintf(buf, "%u\n", data->alarms);
1da177e4 665}
1d66c64c 666static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1da177e4
LT
667
668static ssize_t
30f74292 669show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
670{
671 struct it87_data *data = it87_update_device(dev);
a7be58a1 672 return sprintf(buf, "%u\n", data->vrm);
1da177e4
LT
673}
674static ssize_t
30f74292 675store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
676{
677 struct i2c_client *client = to_i2c_client(dev);
678 struct it87_data *data = i2c_get_clientdata(client);
679 u32 val;
680
681 val = simple_strtoul(buf, NULL, 10);
682 data->vrm = val;
683
684 return count;
685}
686static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
687#define device_create_file_vrm(client) \
688device_create_file(&client->dev, &dev_attr_vrm)
689
690static ssize_t
30f74292 691show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
692{
693 struct it87_data *data = it87_update_device(dev);
694 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
695}
696static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
697#define device_create_file_vid(client) \
698device_create_file(&client->dev, &dev_attr_cpu0_vid)
699
700/* This function is called when:
701 * it87_driver is inserted (when this module is loaded), for each
702 available adapter
703 * when a new adapter is inserted (and it87_driver is still present) */
704static int it87_attach_adapter(struct i2c_adapter *adapter)
705{
706 if (!(adapter->class & I2C_CLASS_HWMON))
707 return 0;
2ed2dc3c 708 return i2c_probe(adapter, &addr_data, it87_detect);
1da177e4
LT
709}
710
2d8672c5
JD
711static int it87_isa_attach_adapter(struct i2c_adapter *adapter)
712{
713 return it87_detect(adapter, isa_address, -1);
714}
715
716/* SuperIO detection - will change isa_address if a chip is found */
91749996 717static int __init it87_find(unsigned short *address)
1da177e4
LT
718{
719 int err = -ENODEV;
720
721 superio_enter();
722 chip_type = superio_inw(DEVID);
723 if (chip_type != IT8712F_DEVID
724 && chip_type != IT8705F_DEVID)
725 goto exit;
726
727 superio_select();
728 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
729 pr_info("it87: Device not activated, skipping\n");
730 goto exit;
731 }
732
733 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
734 if (*address == 0) {
735 pr_info("it87: Base address not set, skipping\n");
736 goto exit;
737 }
738
739 err = 0;
740 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
741 chip_type, *address, superio_inb(DEVREV) & 0x0f);
742
743exit:
744 superio_exit();
745 return err;
746}
747
2ed2dc3c 748/* This function is called by i2c_probe */
c49efcef 749static int it87_detect(struct i2c_adapter *adapter, int address, int kind)
1da177e4
LT
750{
751 int i;
752 struct i2c_client *new_client;
753 struct it87_data *data;
754 int err = 0;
755 const char *name = "";
756 int is_isa = i2c_is_isa_adapter(adapter);
757 int enable_pwm_interface;
758
759 if (!is_isa &&
760 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
761 goto ERROR0;
762
763 /* Reserve the ISA region */
764 if (is_isa)
cdaf7934
LR
765 if (!request_region(address, IT87_EXTENT,
766 it87_isa_driver.driver.name))
1da177e4
LT
767 goto ERROR0;
768
91749996 769 /* For now, we presume we have a valid client. We create the
1da177e4
LT
770 client structure, even though we cannot fill it completely yet.
771 But it allows us to access it87_{read,write}_value. */
772
ba9c2e8d 773 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
1da177e4
LT
774 err = -ENOMEM;
775 goto ERROR1;
776 }
1da177e4
LT
777
778 new_client = &data->client;
779 if (is_isa)
9a61bf63 780 mutex_init(&data->lock);
1da177e4
LT
781 i2c_set_clientdata(new_client, data);
782 new_client->addr = address;
783 new_client->adapter = adapter;
fde09509 784 new_client->driver = is_isa ? &it87_isa_driver : &it87_driver;
1da177e4
LT
785 new_client->flags = 0;
786
787 /* Now, we do the remaining detection. */
788
789 if (kind < 0) {
790 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
791 || (!is_isa
792 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
793 err = -ENODEV;
794 goto ERROR2;
795 }
796 }
797
798 /* Determine the chip type. */
799 if (kind <= 0) {
800 i = it87_read_value(new_client, IT87_REG_CHIPID);
801 if (i == 0x90) {
802 kind = it87;
803 if ((is_isa) && (chip_type == IT8712F_DEVID))
804 kind = it8712;
805 }
806 else {
807 if (kind == 0)
808 dev_info(&adapter->dev,
809 "Ignoring 'force' parameter for unknown chip at "
810 "adapter %d, address 0x%02x\n",
811 i2c_adapter_id(adapter), address);
812 err = -ENODEV;
813 goto ERROR2;
814 }
815 }
816
817 if (kind == it87) {
818 name = "it87";
819 } else if (kind == it8712) {
820 name = "it8712";
821 }
822
823 /* Fill in the remaining client fields and put it into the global list */
824 strlcpy(new_client->name, name, I2C_NAME_SIZE);
825 data->type = kind;
826 data->valid = 0;
9a61bf63 827 mutex_init(&data->update_lock);
1da177e4
LT
828
829 /* Tell the I2C layer a new client has arrived */
830 if ((err = i2c_attach_client(new_client)))
831 goto ERROR2;
832
c5e3fbf2
JD
833 if (!is_isa)
834 dev_info(&new_client->dev, "The I2C interface to IT87xxF "
835 "hardware monitoring chips is deprecated. Please "
836 "report if you still rely on it.\n");
837
1da177e4
LT
838 /* Check PWM configuration */
839 enable_pwm_interface = it87_check_pwm(new_client);
840
841 /* Initialize the IT87 chip */
842 it87_init_client(new_client, data);
843
844 /* Register sysfs hooks */
943b0830
MH
845 data->class_dev = hwmon_device_register(&new_client->dev);
846 if (IS_ERR(data->class_dev)) {
847 err = PTR_ERR(data->class_dev);
848 goto ERROR3;
849 }
850
20ad93d4
JD
851 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
852 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
853 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
854 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
855 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
856 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
857 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
858 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
859 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
860 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
861 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
862 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
863 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
864 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
865 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
866 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
889 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
890 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
891 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
896 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
1da177e4
LT
897 device_create_file(&new_client->dev, &dev_attr_alarms);
898 if (enable_pwm_interface) {
20ad93d4
JD
899 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
900 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
901 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
902 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
903 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
904 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
1da177e4
LT
905 }
906
907 if (data->type == it8712) {
303760b4 908 data->vrm = vid_which_vrm();
1da177e4
LT
909 device_create_file_vrm(new_client);
910 device_create_file_vid(new_client);
911 }
912
913 return 0;
914
943b0830
MH
915ERROR3:
916 i2c_detach_client(new_client);
1da177e4
LT
917ERROR2:
918 kfree(data);
919ERROR1:
920 if (is_isa)
921 release_region(address, IT87_EXTENT);
922ERROR0:
923 return err;
924}
925
926static int it87_detach_client(struct i2c_client *client)
927{
943b0830 928 struct it87_data *data = i2c_get_clientdata(client);
1da177e4
LT
929 int err;
930
943b0830
MH
931 hwmon_device_unregister(data->class_dev);
932
7bef5594 933 if ((err = i2c_detach_client(client)))
1da177e4 934 return err;
1da177e4
LT
935
936 if(i2c_is_isa_client(client))
937 release_region(client->addr, IT87_EXTENT);
943b0830 938 kfree(data);
1da177e4
LT
939
940 return 0;
941}
942
44bbe87e 943/* The SMBus locks itself, but ISA access must be locked explicitly!
1da177e4
LT
944 We don't want to lock the whole ISA bus, so we lock each client
945 separately.
946 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
947 would slow down the IT87 access and should not be necessary. */
948static int it87_read_value(struct i2c_client *client, u8 reg)
949{
950 struct it87_data *data = i2c_get_clientdata(client);
951
952 int res;
953 if (i2c_is_isa_client(client)) {
9a61bf63 954 mutex_lock(&data->lock);
1da177e4
LT
955 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
956 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
9a61bf63 957 mutex_unlock(&data->lock);
1da177e4
LT
958 return res;
959 } else
960 return i2c_smbus_read_byte_data(client, reg);
961}
962
44bbe87e 963/* The SMBus locks itself, but ISA access muse be locked explicitly!
1da177e4
LT
964 We don't want to lock the whole ISA bus, so we lock each client
965 separately.
966 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
967 would slow down the IT87 access and should not be necessary. */
968static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
969{
970 struct it87_data *data = i2c_get_clientdata(client);
971
972 if (i2c_is_isa_client(client)) {
9a61bf63 973 mutex_lock(&data->lock);
1da177e4
LT
974 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
975 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
9a61bf63 976 mutex_unlock(&data->lock);
1da177e4
LT
977 return 0;
978 } else
979 return i2c_smbus_write_byte_data(client, reg, value);
980}
981
982/* Return 1 if and only if the PWM interface is safe to use */
983static int it87_check_pwm(struct i2c_client *client)
984{
985 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
986 * and polarity set to active low is sign that this is the case so we
987 * disable pwm control to protect the user. */
988 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
989 if ((tmp & 0x87) == 0) {
990 if (fix_pwm_polarity) {
991 /* The user asks us to attempt a chip reconfiguration.
992 * This means switching to active high polarity and
993 * inverting all fan speed values. */
994 int i;
995 u8 pwm[3];
996
997 for (i = 0; i < 3; i++)
998 pwm[i] = it87_read_value(client,
999 IT87_REG_PWM(i));
1000
1001 /* If any fan is in automatic pwm mode, the polarity
1002 * might be correct, as suspicious as it seems, so we
1003 * better don't change anything (but still disable the
1004 * PWM interface). */
1005 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1006 dev_info(&client->dev, "Reconfiguring PWM to "
1007 "active high polarity\n");
1008 it87_write_value(client, IT87_REG_FAN_CTL,
1009 tmp | 0x87);
1010 for (i = 0; i < 3; i++)
1011 it87_write_value(client,
1012 IT87_REG_PWM(i),
1013 0x7f & ~pwm[i]);
1014 return 1;
1015 }
1016
1017 dev_info(&client->dev, "PWM configuration is "
1018 "too broken to be fixed\n");
1019 }
1020
1021 dev_info(&client->dev, "Detected broken BIOS "
1022 "defaults, disabling PWM interface\n");
1023 return 0;
1024 } else if (fix_pwm_polarity) {
1025 dev_info(&client->dev, "PWM configuration looks "
1026 "sane, won't touch\n");
1027 }
1028
1029 return 1;
1030}
1031
1032/* Called when we have found a new IT87. */
1033static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1034{
1035 int tmp, i;
1036
1037 /* initialize to sane defaults:
1038 * - if the chip is in manual pwm mode, this will be overwritten with
1039 * the actual settings on the chip (so in this case, initialization
1040 * is not needed)
1041 * - if in automatic or on/off mode, we could switch to manual mode,
1042 * read the registers and set manual_pwm_ctl accordingly, but currently
1043 * this is not implemented, so we initialize to something sane */
1044 for (i = 0; i < 3; i++) {
1045 data->manual_pwm_ctl[i] = 0xff;
1046 }
1047
1048 /* Check if temperature channnels are reset manually or by some reason */
1049 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1050 if ((tmp & 0x3f) == 0) {
1051 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1052 tmp = (tmp & 0xc0) | 0x2a;
1053 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1054 }
1055 data->sensor = tmp;
1056
1057 /* Check if voltage monitors are reset manually or by some reason */
1058 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1059 if ((tmp & 0xff) == 0) {
1060 /* Enable all voltage monitors */
1061 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1062 }
1063
1064 /* Check if tachometers are reset manually or by some reason */
1065 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1066 if ((data->fan_main_ctrl & 0x70) == 0) {
1067 /* Enable all fan tachometers */
1068 data->fan_main_ctrl |= 0x70;
1069 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1070 }
1071
1072 /* Set current fan mode registers and the default settings for the
1073 * other mode registers */
1074 for (i = 0; i < 3; i++) {
1075 if (data->fan_main_ctrl & (1 << i)) {
1076 /* pwm mode */
1077 tmp = it87_read_value(client, IT87_REG_PWM(i));
1078 if (tmp & 0x80) {
1079 /* automatic pwm - not yet implemented, but
1080 * leave the settings made by the BIOS alone
1081 * until a change is requested via the sysfs
1082 * interface */
1083 } else {
1084 /* manual pwm */
1085 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1086 }
1087 }
1088 }
1089
1090 /* Start monitoring */
1091 it87_write_value(client, IT87_REG_CONFIG,
1092 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1093 | (update_vbat ? 0x41 : 0x01));
1094}
1095
1096static struct it87_data *it87_update_device(struct device *dev)
1097{
1098 struct i2c_client *client = to_i2c_client(dev);
1099 struct it87_data *data = i2c_get_clientdata(client);
1100 int i;
1101
9a61bf63 1102 mutex_lock(&data->update_lock);
1da177e4
LT
1103
1104 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1105 || !data->valid) {
1106
1107 if (update_vbat) {
1108 /* Cleared after each update, so reenable. Value
1109 returned by this read will be previous value */
1110 it87_write_value(client, IT87_REG_CONFIG,
1111 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1112 }
1113 for (i = 0; i <= 7; i++) {
1114 data->in[i] =
1115 it87_read_value(client, IT87_REG_VIN(i));
1116 data->in_min[i] =
1117 it87_read_value(client, IT87_REG_VIN_MIN(i));
1118 data->in_max[i] =
1119 it87_read_value(client, IT87_REG_VIN_MAX(i));
1120 }
1121 data->in[8] =
1122 it87_read_value(client, IT87_REG_VIN(8));
1123 /* Temperature sensor doesn't have limit registers, set
1124 to min and max value */
1125 data->in_min[8] = 0;
1126 data->in_max[8] = 255;
1127
1128 for (i = 0; i < 3; i++) {
1129 data->fan[i] =
1130 it87_read_value(client, IT87_REG_FAN(i));
1131 data->fan_min[i] =
1132 it87_read_value(client, IT87_REG_FAN_MIN(i));
1133 }
1134 for (i = 0; i < 3; i++) {
1135 data->temp[i] =
1136 it87_read_value(client, IT87_REG_TEMP(i));
1137 data->temp_high[i] =
1138 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1139 data->temp_low[i] =
1140 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1141 }
1142
1143 i = it87_read_value(client, IT87_REG_FAN_DIV);
1144 data->fan_div[0] = i & 0x07;
1145 data->fan_div[1] = (i >> 3) & 0x07;
1146 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1147
1148 data->alarms =
1149 it87_read_value(client, IT87_REG_ALARM1) |
1150 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1151 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1152 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1153
1154 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1155 /* The 8705 does not have VID capability */
1156 if (data->type == it8712) {
1157 data->vid = it87_read_value(client, IT87_REG_VID);
1158 data->vid &= 0x1f;
1159 }
1160 data->last_updated = jiffies;
1161 data->valid = 1;
1162 }
1163
9a61bf63 1164 mutex_unlock(&data->update_lock);
1da177e4
LT
1165
1166 return data;
1167}
1168
1169static int __init sm_it87_init(void)
1170{
91749996 1171 int res;
fde09509
JD
1172
1173 res = i2c_add_driver(&it87_driver);
1174 if (res)
1175 return res;
1176
91749996
JD
1177 if (!it87_find(&isa_address)) {
1178 res = i2c_isa_add_driver(&it87_isa_driver);
1179 if (res) {
1180 i2c_del_driver(&it87_driver);
1181 return res;
1182 }
fde09509
JD
1183 }
1184
1185 return 0;
1da177e4
LT
1186}
1187
1188static void __exit sm_it87_exit(void)
1189{
be79c383
JD
1190 if (isa_address)
1191 i2c_isa_del_driver(&it87_isa_driver);
1da177e4
LT
1192 i2c_del_driver(&it87_driver);
1193}
1194
1195
1196MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1197MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1198module_param(update_vbat, bool, 0);
1199MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1200module_param(fix_pwm_polarity, bool, 0);
1201MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1202MODULE_LICENSE("GPL");
1203
1204module_init(sm_it87_init);
1205module_exit(sm_it87_exit);
This page took 0.348698 seconds and 5 git commands to generate.