blob: 722ef0cd5c006ba25e32c3020d7a99f78480f393 [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>
Jean Delvarefde09502005-07-19 23:51:07 +020039#include <linux/i2c-isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/i2c-sensor.h>
41#include <linux/i2c-vid.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020042#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040043#include <linux/hwmon.h>
44#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/io.h>
46
47
48/* Addresses to scan */
49static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
50 0x2e, 0x2f, I2C_CLIENT_END };
Jean Delvare2d8672c2005-07-19 23:56:35 +020051static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
52static unsigned short isa_address = 0x290;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* Insmod parameters */
55SENSORS_INSMOD_2(it87, it8712);
56
57#define REG 0x2e /* The register to read/write */
58#define DEV 0x07 /* Register: Logical device select */
59#define VAL 0x2f /* The value to read/write */
60#define PME 0x04 /* The device with the fan registers in it */
61#define DEVID 0x20 /* Register: Device ID */
62#define DEVREV 0x22 /* Register: Device Revision */
63
64static inline int
65superio_inb(int reg)
66{
67 outb(reg, REG);
68 return inb(VAL);
69}
70
71static int superio_inw(int reg)
72{
73 int val;
74 outb(reg++, REG);
75 val = inb(VAL) << 8;
76 outb(reg, REG);
77 val |= inb(VAL);
78 return val;
79}
80
81static inline void
82superio_select(void)
83{
84 outb(DEV, REG);
85 outb(PME, VAL);
86}
87
88static inline void
89superio_enter(void)
90{
91 outb(0x87, REG);
92 outb(0x01, REG);
93 outb(0x55, REG);
94 outb(0x55, REG);
95}
96
97static inline void
98superio_exit(void)
99{
100 outb(0x02, REG);
101 outb(0x02, VAL);
102}
103
104#define IT8712F_DEVID 0x8712
105#define IT8705F_DEVID 0x8705
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
137#define IT87_REG_FAN_DIV 0x0b
138
139/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
140
141#define IT87_REG_FAN(nr) (0x0d + (nr))
142#define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
143#define IT87_REG_FAN_MAIN_CTRL 0x13
144#define IT87_REG_FAN_CTL 0x14
145#define IT87_REG_PWM(nr) (0x15 + (nr))
146
147#define IT87_REG_VIN(nr) (0x20 + (nr))
148#define IT87_REG_TEMP(nr) (0x29 + (nr))
149
150#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
151#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
152#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
153#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
154
155#define IT87_REG_I2C_ADDR 0x48
156
157#define IT87_REG_VIN_ENABLE 0x50
158#define IT87_REG_TEMP_ENABLE 0x51
159
160#define IT87_REG_CHIPID 0x58
161
162#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
163#define IN_FROM_REG(val) ((val) * 16)
164
165static inline u8 FAN_TO_REG(long rpm, int div)
166{
167 if (rpm == 0)
168 return 255;
169 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
170 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
171 254);
172}
173
174#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
175
176#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
177 ((val)+500)/1000),-128,127))
178#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#define PWM_TO_REG(val) ((val) >> 1)
181#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
182
183static int DIV_TO_REG(int val)
184{
185 int answer = 0;
186 while ((val >>= 1) != 0)
187 answer++;
188 return answer;
189}
190#define DIV_FROM_REG(val) (1 << (val))
191
192
193/* For each registered IT87, we need to keep some data in memory. That
194 data is pointed to by it87_list[NR]->data. The structure itself is
195 dynamically allocated, at the same time when a new it87 client is
196 allocated. */
197struct it87_data {
198 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400199 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct semaphore lock;
201 enum chips type;
202
203 struct semaphore update_lock;
204 char valid; /* !=0 if following fields are valid */
205 unsigned long last_updated; /* In jiffies */
206
207 u8 in[9]; /* Register value */
208 u8 in_max[9]; /* Register value */
209 u8 in_min[9]; /* Register value */
210 u8 fan[3]; /* Register value */
211 u8 fan_min[3]; /* Register value */
212 u8 temp[3]; /* Register value */
213 u8 temp_high[3]; /* Register value */
214 u8 temp_low[3]; /* Register value */
215 u8 sensor; /* Register value */
216 u8 fan_div[3]; /* Register encoding, shifted right */
217 u8 vid; /* Register encoding, combined */
218 int vrm;
219 u32 alarms; /* Register encoding, combined */
220 u8 fan_main_ctrl; /* Register value */
221 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
222};
223
224
225static int it87_attach_adapter(struct i2c_adapter *adapter);
Jean Delvare2d8672c2005-07-19 23:56:35 +0200226static int it87_isa_attach_adapter(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
228static int it87_detach_client(struct i2c_client *client);
229
230static int it87_read_value(struct i2c_client *client, u8 register);
231static int it87_write_value(struct i2c_client *client, u8 register,
232 u8 value);
233static struct it87_data *it87_update_device(struct device *dev);
234static int it87_check_pwm(struct i2c_client *client);
235static void it87_init_client(struct i2c_client *client, struct it87_data *data);
236
237
238static struct i2c_driver it87_driver = {
239 .owner = THIS_MODULE,
240 .name = "it87",
241 .id = I2C_DRIVERID_IT87,
242 .flags = I2C_DF_NOTIFY,
243 .attach_adapter = it87_attach_adapter,
244 .detach_client = it87_detach_client,
245};
246
Jean Delvarefde09502005-07-19 23:51:07 +0200247static struct i2c_driver it87_isa_driver = {
248 .owner = THIS_MODULE,
249 .name = "it87-isa",
Jean Delvare2d8672c2005-07-19 23:56:35 +0200250 .attach_adapter = it87_isa_attach_adapter,
Jean Delvarefde09502005-07-19 23:51:07 +0200251 .detach_client = it87_detach_client,
252};
253
254
Jean Delvare20ad93d2005-06-05 11:53:25 +0200255static ssize_t show_in(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[nr]));
263}
264
Jean Delvare20ad93d2005-06-05 11:53:25 +0200265static ssize_t show_in_min(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_min[nr]));
273}
274
Jean Delvare20ad93d2005-06-05 11:53:25 +0200275static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
276 char *buf)
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 it87_data *data = it87_update_device(dev);
282 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
283}
284
Jean Delvare20ad93d2005-06-05 11:53:25 +0200285static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
286 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200288 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
289 int nr = sensor_attr->index;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 struct i2c_client *client = to_i2c_client(dev);
292 struct it87_data *data = i2c_get_clientdata(client);
293 unsigned long val = simple_strtoul(buf, NULL, 10);
294
295 down(&data->update_lock);
296 data->in_min[nr] = IN_TO_REG(val);
297 it87_write_value(client, IT87_REG_VIN_MIN(nr),
298 data->in_min[nr]);
299 up(&data->update_lock);
300 return count;
301}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200302static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
303 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200305 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
306 int nr = sensor_attr->index;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 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
312 down(&data->update_lock);
313 data->in_max[nr] = IN_TO_REG(val);
314 it87_write_value(client, IT87_REG_VIN_MAX(nr),
315 data->in_max[nr]);
316 up(&data->update_lock);
317 return count;
318}
319
320#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200321static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
322 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200325static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
326 show_in_min, set_in_min, offset); \
327static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
328 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330show_in_offset(0);
331limit_in_offset(0);
332show_in_offset(1);
333limit_in_offset(1);
334show_in_offset(2);
335limit_in_offset(2);
336show_in_offset(3);
337limit_in_offset(3);
338show_in_offset(4);
339limit_in_offset(4);
340show_in_offset(5);
341limit_in_offset(5);
342show_in_offset(6);
343limit_in_offset(6);
344show_in_offset(7);
345limit_in_offset(7);
346show_in_offset(8);
347
348/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200349static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
350 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200352 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
353 int nr = sensor_attr->index;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct it87_data *data = it87_update_device(dev);
356 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
357}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200358static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
359 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200361 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
362 int nr = sensor_attr->index;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct it87_data *data = it87_update_device(dev);
365 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
366}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200367static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
368 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200370 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
371 int nr = sensor_attr->index;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct it87_data *data = it87_update_device(dev);
374 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
375}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200376static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
377 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200379 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
380 int nr = sensor_attr->index;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct i2c_client *client = to_i2c_client(dev);
383 struct it87_data *data = i2c_get_clientdata(client);
384 int val = simple_strtol(buf, NULL, 10);
385
386 down(&data->update_lock);
387 data->temp_high[nr] = TEMP_TO_REG(val);
388 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
389 up(&data->update_lock);
390 return count;
391}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200392static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
393 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200395 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
396 int nr = sensor_attr->index;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct i2c_client *client = to_i2c_client(dev);
399 struct it87_data *data = i2c_get_clientdata(client);
400 int val = simple_strtol(buf, NULL, 10);
401
402 down(&data->update_lock);
403 data->temp_low[nr] = TEMP_TO_REG(val);
404 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
405 up(&data->update_lock);
406 return count;
407}
408#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200409static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
410 show_temp, NULL, offset - 1); \
411static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
412 show_temp_max, set_temp_max, offset - 1); \
413static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
414 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416show_temp_offset(1);
417show_temp_offset(2);
418show_temp_offset(3);
419
Jean Delvare20ad93d2005-06-05 11:53:25 +0200420static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
421 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200423 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
424 int nr = sensor_attr->index;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 struct it87_data *data = it87_update_device(dev);
427 u8 reg = data->sensor; /* In case the value is updated while we use it */
428
429 if (reg & (1 << nr))
430 return sprintf(buf, "3\n"); /* thermal diode */
431 if (reg & (8 << nr))
432 return sprintf(buf, "2\n"); /* thermistor */
433 return sprintf(buf, "0\n"); /* disabled */
434}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200435static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
436 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200438 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
439 int nr = sensor_attr->index;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct i2c_client *client = to_i2c_client(dev);
442 struct it87_data *data = i2c_get_clientdata(client);
443 int val = simple_strtol(buf, NULL, 10);
444
445 down(&data->update_lock);
446
447 data->sensor &= ~(1 << nr);
448 data->sensor &= ~(8 << nr);
449 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
450 if (val == 3)
451 data->sensor |= 1 << nr;
452 else if (val == 2)
453 data->sensor |= 8 << nr;
454 else if (val != 0) {
455 up(&data->update_lock);
456 return -EINVAL;
457 }
458 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
459 up(&data->update_lock);
460 return count;
461}
462#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200463static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
464 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466show_sensor_offset(1);
467show_sensor_offset(2);
468show_sensor_offset(3);
469
470/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200471static ssize_t show_fan(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", FAN_FROM_REG(data->fan[nr],
479 DIV_FROM_REG(data->fan_div[nr])));
480}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200481static ssize_t show_fan_min(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",
489 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
490}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200491static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
492 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200494 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
495 int nr = sensor_attr->index;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct it87_data *data = it87_update_device(dev);
498 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
499}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200500static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
501 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200503 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
504 int nr = sensor_attr->index;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct it87_data *data = it87_update_device(dev);
507 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
508}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200509static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
510 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200512 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
513 int nr = sensor_attr->index;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct it87_data *data = it87_update_device(dev);
516 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
517}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200518static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
519 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200521 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
522 int nr = sensor_attr->index;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct i2c_client *client = to_i2c_client(dev);
525 struct it87_data *data = i2c_get_clientdata(client);
526 int val = simple_strtol(buf, NULL, 10);
527
528 down(&data->update_lock);
529 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
530 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
531 up(&data->update_lock);
532 return count;
533}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200534static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
535 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200537 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
538 int nr = sensor_attr->index;
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct i2c_client *client = to_i2c_client(dev);
541 struct it87_data *data = i2c_get_clientdata(client);
542 int val = simple_strtol(buf, NULL, 10);
543 int i, min[3];
544 u8 old;
545
546 down(&data->update_lock);
547 old = it87_read_value(client, IT87_REG_FAN_DIV);
548
549 for (i = 0; i < 3; i++)
550 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
551
552 switch (nr) {
553 case 0:
554 case 1:
555 data->fan_div[nr] = DIV_TO_REG(val);
556 break;
557 case 2:
558 if (val < 8)
559 data->fan_div[nr] = 1;
560 else
561 data->fan_div[nr] = 3;
562 }
563 val = old & 0x80;
564 val |= (data->fan_div[0] & 0x07);
565 val |= (data->fan_div[1] & 0x07) << 3;
566 if (data->fan_div[2] == 3)
567 val |= 0x1 << 6;
568 it87_write_value(client, IT87_REG_FAN_DIV, val);
569
570 for (i = 0; i < 3; i++) {
571 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
572 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
573 }
574 up(&data->update_lock);
575 return count;
576}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200577static ssize_t set_pwm_enable(struct device *dev,
578 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200580 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
581 int nr = sensor_attr->index;
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct i2c_client *client = to_i2c_client(dev);
584 struct it87_data *data = i2c_get_clientdata(client);
585 int val = simple_strtol(buf, NULL, 10);
586
587 down(&data->update_lock);
588
589 if (val == 0) {
590 int tmp;
591 /* make sure the fan is on when in on/off mode */
592 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
593 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
594 /* set on/off mode */
595 data->fan_main_ctrl &= ~(1 << nr);
596 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
597 } else if (val == 1) {
598 /* set SmartGuardian mode */
599 data->fan_main_ctrl |= (1 << nr);
600 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
601 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
602 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
603 } else {
604 up(&data->update_lock);
605 return -EINVAL;
606 }
607
608 up(&data->update_lock);
609 return count;
610}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200611static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
612 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200614 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
615 int nr = sensor_attr->index;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct i2c_client *client = to_i2c_client(dev);
618 struct it87_data *data = i2c_get_clientdata(client);
619 int val = simple_strtol(buf, NULL, 10);
620
621 if (val < 0 || val > 255)
622 return -EINVAL;
623
624 down(&data->update_lock);
625 data->manual_pwm_ctl[nr] = val;
626 if (data->fan_main_ctrl & (1 << nr))
627 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
628 up(&data->update_lock);
629 return count;
630}
631
Jean Delvare20ad93d2005-06-05 11:53:25 +0200632#define show_fan_offset(offset) \
633static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
634 show_fan, NULL, offset - 1); \
635static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
636 show_fan_min, set_fan_min, offset - 1); \
637static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
638 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640show_fan_offset(1);
641show_fan_offset(2);
642show_fan_offset(3);
643
644#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200645static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
646 show_pwm_enable, set_pwm_enable, offset - 1); \
647static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
648 show_pwm, set_pwm, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650show_pwm_offset(1);
651show_pwm_offset(2);
652show_pwm_offset(3);
653
654/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400655static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200658 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
Jean Delvare1d66c642005-04-18 21:16:59 -0700660static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400663show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct it87_data *data = it87_update_device(dev);
666 return sprintf(buf, "%ld\n", (long) data->vrm);
667}
668static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400669store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 struct i2c_client *client = to_i2c_client(dev);
672 struct it87_data *data = i2c_get_clientdata(client);
673 u32 val;
674
675 val = simple_strtoul(buf, NULL, 10);
676 data->vrm = val;
677
678 return count;
679}
680static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
681#define device_create_file_vrm(client) \
682device_create_file(&client->dev, &dev_attr_vrm)
683
684static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400685show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 struct it87_data *data = it87_update_device(dev);
688 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
689}
690static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
691#define device_create_file_vid(client) \
692device_create_file(&client->dev, &dev_attr_cpu0_vid)
693
694/* This function is called when:
695 * it87_driver is inserted (when this module is loaded), for each
696 available adapter
697 * when a new adapter is inserted (and it87_driver is still present) */
698static int it87_attach_adapter(struct i2c_adapter *adapter)
699{
700 if (!(adapter->class & I2C_CLASS_HWMON))
701 return 0;
702 return i2c_detect(adapter, &addr_data, it87_detect);
703}
704
Jean Delvare2d8672c2005-07-19 23:56:35 +0200705static int it87_isa_attach_adapter(struct i2c_adapter *adapter)
706{
707 return it87_detect(adapter, isa_address, -1);
708}
709
710/* SuperIO detection - will change isa_address if a chip is found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711static int it87_find(int *address)
712{
713 int err = -ENODEV;
714
715 superio_enter();
716 chip_type = superio_inw(DEVID);
717 if (chip_type != IT8712F_DEVID
718 && chip_type != IT8705F_DEVID)
719 goto exit;
720
721 superio_select();
722 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
723 pr_info("it87: Device not activated, skipping\n");
724 goto exit;
725 }
726
727 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
728 if (*address == 0) {
729 pr_info("it87: Base address not set, skipping\n");
730 goto exit;
731 }
732
733 err = 0;
734 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
735 chip_type, *address, superio_inb(DEVREV) & 0x0f);
736
737exit:
738 superio_exit();
739 return err;
740}
741
742/* This function is called by i2c_detect */
743int it87_detect(struct i2c_adapter *adapter, int address, int kind)
744{
745 int i;
746 struct i2c_client *new_client;
747 struct it87_data *data;
748 int err = 0;
749 const char *name = "";
750 int is_isa = i2c_is_isa_adapter(adapter);
751 int enable_pwm_interface;
752
753 if (!is_isa &&
754 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
755 goto ERROR0;
756
757 /* Reserve the ISA region */
758 if (is_isa)
Jean Delvarefde09502005-07-19 23:51:07 +0200759 if (!request_region(address, IT87_EXTENT, it87_isa_driver.name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 goto ERROR0;
761
762 /* Probe whether there is anything available on this address. Already
763 done for SMBus and Super-I/O clients */
764 if (kind < 0) {
765 if (is_isa && !chip_type) {
766#define REALLY_SLOW_IO
767 /* We need the timeouts for at least some IT87-like chips. But only
768 if we read 'undefined' registers. */
769 i = inb_p(address + 1);
770 if (inb_p(address + 2) != i
771 || inb_p(address + 3) != i
772 || inb_p(address + 7) != i) {
773 err = -ENODEV;
774 goto ERROR1;
775 }
776#undef REALLY_SLOW_IO
777
778 /* Let's just hope nothing breaks here */
779 i = inb_p(address + 5) & 0x7f;
780 outb_p(~i & 0x7f, address + 5);
781 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
782 outb_p(i, address + 5);
783 err = -ENODEV;
784 goto ERROR1;
785 }
786 }
787 }
788
789 /* OK. For now, we presume we have a valid client. We now create the
790 client structure, even though we cannot fill it completely yet.
791 But it allows us to access it87_{read,write}_value. */
792
793 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
794 err = -ENOMEM;
795 goto ERROR1;
796 }
797 memset(data, 0, sizeof(struct it87_data));
798
799 new_client = &data->client;
800 if (is_isa)
801 init_MUTEX(&data->lock);
802 i2c_set_clientdata(new_client, data);
803 new_client->addr = address;
804 new_client->adapter = adapter;
Jean Delvarefde09502005-07-19 23:51:07 +0200805 new_client->driver = is_isa ? &it87_isa_driver : &it87_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 new_client->flags = 0;
807
808 /* Now, we do the remaining detection. */
809
810 if (kind < 0) {
811 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
812 || (!is_isa
813 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
814 err = -ENODEV;
815 goto ERROR2;
816 }
817 }
818
819 /* Determine the chip type. */
820 if (kind <= 0) {
821 i = it87_read_value(new_client, IT87_REG_CHIPID);
822 if (i == 0x90) {
823 kind = it87;
824 if ((is_isa) && (chip_type == IT8712F_DEVID))
825 kind = it8712;
826 }
827 else {
828 if (kind == 0)
829 dev_info(&adapter->dev,
830 "Ignoring 'force' parameter for unknown chip at "
831 "adapter %d, address 0x%02x\n",
832 i2c_adapter_id(adapter), address);
833 err = -ENODEV;
834 goto ERROR2;
835 }
836 }
837
838 if (kind == it87) {
839 name = "it87";
840 } else if (kind == it8712) {
841 name = "it8712";
842 }
843
844 /* Fill in the remaining client fields and put it into the global list */
845 strlcpy(new_client->name, name, I2C_NAME_SIZE);
846 data->type = kind;
847 data->valid = 0;
848 init_MUTEX(&data->update_lock);
849
850 /* Tell the I2C layer a new client has arrived */
851 if ((err = i2c_attach_client(new_client)))
852 goto ERROR2;
853
854 /* Check PWM configuration */
855 enable_pwm_interface = it87_check_pwm(new_client);
856
857 /* Initialize the IT87 chip */
858 it87_init_client(new_client, data);
859
860 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400861 data->class_dev = hwmon_device_register(&new_client->dev);
862 if (IS_ERR(data->class_dev)) {
863 err = PTR_ERR(data->class_dev);
864 goto ERROR3;
865 }
866
Jean Delvare20ad93d2005-06-05 11:53:25 +0200867 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
889 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
890 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
891 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
896 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
897 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
898 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
899 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
900 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
901 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
902 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
903 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
904 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
905 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
906 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
907 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
908 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
909 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
910 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
911 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
912 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 device_create_file(&new_client->dev, &dev_attr_alarms);
914 if (enable_pwm_interface) {
Jean Delvare20ad93d2005-06-05 11:53:25 +0200915 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
916 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
917 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
918 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
919 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
920 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
923 if (data->type == it8712) {
924 data->vrm = i2c_which_vrm();
925 device_create_file_vrm(new_client);
926 device_create_file_vid(new_client);
927 }
928
929 return 0;
930
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400931ERROR3:
932 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933ERROR2:
934 kfree(data);
935ERROR1:
936 if (is_isa)
937 release_region(address, IT87_EXTENT);
938ERROR0:
939 return err;
940}
941
942static int it87_detach_client(struct i2c_client *client)
943{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400944 struct it87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int err;
946
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400947 hwmon_device_unregister(data->class_dev);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if ((err = i2c_detach_client(client))) {
950 dev_err(&client->dev,
951 "Client deregistration failed, client not detached.\n");
952 return err;
953 }
954
955 if(i2c_is_isa_client(client))
956 release_region(client->addr, IT87_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400957 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 return 0;
960}
961
Steven Cole44bbe872005-05-03 18:21:25 -0600962/* The SMBus locks itself, but ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 We don't want to lock the whole ISA bus, so we lock each client
964 separately.
965 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
966 would slow down the IT87 access and should not be necessary. */
967static int it87_read_value(struct i2c_client *client, u8 reg)
968{
969 struct it87_data *data = i2c_get_clientdata(client);
970
971 int res;
972 if (i2c_is_isa_client(client)) {
973 down(&data->lock);
974 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
975 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
976 up(&data->lock);
977 return res;
978 } else
979 return i2c_smbus_read_byte_data(client, reg);
980}
981
Steven Cole44bbe872005-05-03 18:21:25 -0600982/* The SMBus locks itself, but ISA access muse be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 We don't want to lock the whole ISA bus, so we lock each client
984 separately.
985 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
986 would slow down the IT87 access and should not be necessary. */
987static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
988{
989 struct it87_data *data = i2c_get_clientdata(client);
990
991 if (i2c_is_isa_client(client)) {
992 down(&data->lock);
993 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
994 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
995 up(&data->lock);
996 return 0;
997 } else
998 return i2c_smbus_write_byte_data(client, reg, value);
999}
1000
1001/* Return 1 if and only if the PWM interface is safe to use */
1002static int it87_check_pwm(struct i2c_client *client)
1003{
1004 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1005 * and polarity set to active low is sign that this is the case so we
1006 * disable pwm control to protect the user. */
1007 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1008 if ((tmp & 0x87) == 0) {
1009 if (fix_pwm_polarity) {
1010 /* The user asks us to attempt a chip reconfiguration.
1011 * This means switching to active high polarity and
1012 * inverting all fan speed values. */
1013 int i;
1014 u8 pwm[3];
1015
1016 for (i = 0; i < 3; i++)
1017 pwm[i] = it87_read_value(client,
1018 IT87_REG_PWM(i));
1019
1020 /* If any fan is in automatic pwm mode, the polarity
1021 * might be correct, as suspicious as it seems, so we
1022 * better don't change anything (but still disable the
1023 * PWM interface). */
1024 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1025 dev_info(&client->dev, "Reconfiguring PWM to "
1026 "active high polarity\n");
1027 it87_write_value(client, IT87_REG_FAN_CTL,
1028 tmp | 0x87);
1029 for (i = 0; i < 3; i++)
1030 it87_write_value(client,
1031 IT87_REG_PWM(i),
1032 0x7f & ~pwm[i]);
1033 return 1;
1034 }
1035
1036 dev_info(&client->dev, "PWM configuration is "
1037 "too broken to be fixed\n");
1038 }
1039
1040 dev_info(&client->dev, "Detected broken BIOS "
1041 "defaults, disabling PWM interface\n");
1042 return 0;
1043 } else if (fix_pwm_polarity) {
1044 dev_info(&client->dev, "PWM configuration looks "
1045 "sane, won't touch\n");
1046 }
1047
1048 return 1;
1049}
1050
1051/* Called when we have found a new IT87. */
1052static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1053{
1054 int tmp, i;
1055
1056 /* initialize to sane defaults:
1057 * - if the chip is in manual pwm mode, this will be overwritten with
1058 * the actual settings on the chip (so in this case, initialization
1059 * is not needed)
1060 * - if in automatic or on/off mode, we could switch to manual mode,
1061 * read the registers and set manual_pwm_ctl accordingly, but currently
1062 * this is not implemented, so we initialize to something sane */
1063 for (i = 0; i < 3; i++) {
1064 data->manual_pwm_ctl[i] = 0xff;
1065 }
1066
1067 /* Check if temperature channnels are reset manually or by some reason */
1068 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1069 if ((tmp & 0x3f) == 0) {
1070 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1071 tmp = (tmp & 0xc0) | 0x2a;
1072 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1073 }
1074 data->sensor = tmp;
1075
1076 /* Check if voltage monitors are reset manually or by some reason */
1077 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1078 if ((tmp & 0xff) == 0) {
1079 /* Enable all voltage monitors */
1080 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1081 }
1082
1083 /* Check if tachometers are reset manually or by some reason */
1084 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1085 if ((data->fan_main_ctrl & 0x70) == 0) {
1086 /* Enable all fan tachometers */
1087 data->fan_main_ctrl |= 0x70;
1088 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1089 }
1090
1091 /* Set current fan mode registers and the default settings for the
1092 * other mode registers */
1093 for (i = 0; i < 3; i++) {
1094 if (data->fan_main_ctrl & (1 << i)) {
1095 /* pwm mode */
1096 tmp = it87_read_value(client, IT87_REG_PWM(i));
1097 if (tmp & 0x80) {
1098 /* automatic pwm - not yet implemented, but
1099 * leave the settings made by the BIOS alone
1100 * until a change is requested via the sysfs
1101 * interface */
1102 } else {
1103 /* manual pwm */
1104 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1105 }
1106 }
1107 }
1108
1109 /* Start monitoring */
1110 it87_write_value(client, IT87_REG_CONFIG,
1111 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1112 | (update_vbat ? 0x41 : 0x01));
1113}
1114
1115static struct it87_data *it87_update_device(struct device *dev)
1116{
1117 struct i2c_client *client = to_i2c_client(dev);
1118 struct it87_data *data = i2c_get_clientdata(client);
1119 int i;
1120
1121 down(&data->update_lock);
1122
1123 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1124 || !data->valid) {
1125
1126 if (update_vbat) {
1127 /* Cleared after each update, so reenable. Value
1128 returned by this read will be previous value */
1129 it87_write_value(client, IT87_REG_CONFIG,
1130 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1131 }
1132 for (i = 0; i <= 7; i++) {
1133 data->in[i] =
1134 it87_read_value(client, IT87_REG_VIN(i));
1135 data->in_min[i] =
1136 it87_read_value(client, IT87_REG_VIN_MIN(i));
1137 data->in_max[i] =
1138 it87_read_value(client, IT87_REG_VIN_MAX(i));
1139 }
1140 data->in[8] =
1141 it87_read_value(client, IT87_REG_VIN(8));
1142 /* Temperature sensor doesn't have limit registers, set
1143 to min and max value */
1144 data->in_min[8] = 0;
1145 data->in_max[8] = 255;
1146
1147 for (i = 0; i < 3; i++) {
1148 data->fan[i] =
1149 it87_read_value(client, IT87_REG_FAN(i));
1150 data->fan_min[i] =
1151 it87_read_value(client, IT87_REG_FAN_MIN(i));
1152 }
1153 for (i = 0; i < 3; i++) {
1154 data->temp[i] =
1155 it87_read_value(client, IT87_REG_TEMP(i));
1156 data->temp_high[i] =
1157 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1158 data->temp_low[i] =
1159 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1160 }
1161
1162 i = it87_read_value(client, IT87_REG_FAN_DIV);
1163 data->fan_div[0] = i & 0x07;
1164 data->fan_div[1] = (i >> 3) & 0x07;
1165 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1166
1167 data->alarms =
1168 it87_read_value(client, IT87_REG_ALARM1) |
1169 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1170 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1171 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1172
1173 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1174 /* The 8705 does not have VID capability */
1175 if (data->type == it8712) {
1176 data->vid = it87_read_value(client, IT87_REG_VID);
1177 data->vid &= 0x1f;
1178 }
1179 data->last_updated = jiffies;
1180 data->valid = 1;
1181 }
1182
1183 up(&data->update_lock);
1184
1185 return data;
1186}
1187
1188static int __init sm_it87_init(void)
1189{
Jean Delvarefde09502005-07-19 23:51:07 +02001190 int addr, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 if (!it87_find(&addr)) {
Jean Delvare2d8672c2005-07-19 23:56:35 +02001193 isa_address = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Jean Delvarefde09502005-07-19 23:51:07 +02001195
1196 res = i2c_add_driver(&it87_driver);
1197 if (res)
1198 return res;
1199
1200 res = i2c_isa_add_driver(&it87_isa_driver);
1201 if (res) {
1202 i2c_del_driver(&it87_driver);
1203 return res;
1204 }
1205
1206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209static void __exit sm_it87_exit(void)
1210{
Jean Delvarefde09502005-07-19 23:51:07 +02001211 i2c_isa_del_driver(&it87_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 i2c_del_driver(&it87_driver);
1213}
1214
1215
1216MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1217MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1218module_param(update_vbat, bool, 0);
1219MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1220module_param(fix_pwm_polarity, bool, 0);
1221MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1222MODULE_LICENSE("GPL");
1223
1224module_init(sm_it87_init);
1225module_exit(sm_it87_exit);