blob: 24d520bcc0b69fbc5f0fdabdb9ac979d9de6d96b [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
Jean Delvare91749992005-10-08 00:10:00 +02005 Supports: IT8705F Super I/O chip w/LPC interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 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>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040040#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020041#include <linux/hwmon-sysfs.h>
42#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040043#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 };
Jean Delvare91749992005-10-08 00:10:00 +020050static unsigned short isa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020053I2C_CLIENT_INSMOD_2(it87, it8712);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
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);
Jean Delvare2d8672c2005-07-19 23:56:35 +0200224static int it87_isa_attach_adapter(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static 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 = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100237 .driver = {
238 .owner = THIS_MODULE,
239 .name = "it87",
240 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .id = I2C_DRIVERID_IT87,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .attach_adapter = it87_attach_adapter,
243 .detach_client = it87_detach_client,
244};
245
Jean Delvarefde09502005-07-19 23:51:07 +0200246static struct i2c_driver it87_isa_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100247 .driver = {
248 .owner = THIS_MODULE,
249 .name = "it87-isa",
250 },
Jean Delvare2d8672c2005-07-19 23:56:35 +0200251 .attach_adapter = it87_isa_attach_adapter,
Jean Delvarefde09502005-07-19 23:51:07 +0200252 .detach_client = it87_detach_client,
253};
254
255
Jean Delvare20ad93d2005-06-05 11:53:25 +0200256static ssize_t show_in(struct device *dev, struct device_attribute *attr,
257 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200259 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
260 int nr = sensor_attr->index;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct it87_data *data = it87_update_device(dev);
263 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
264}
265
Jean Delvare20ad93d2005-06-05 11:53:25 +0200266static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
267 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200269 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
270 int nr = sensor_attr->index;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct it87_data *data = it87_update_device(dev);
273 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
274}
275
Jean Delvare20ad93d2005-06-05 11:53:25 +0200276static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
277 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200279 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
280 int nr = sensor_attr->index;
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct it87_data *data = it87_update_device(dev);
283 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
284}
285
Jean Delvare20ad93d2005-06-05 11:53:25 +0200286static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
287 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200289 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
290 int nr = sensor_attr->index;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct i2c_client *client = to_i2c_client(dev);
293 struct it87_data *data = i2c_get_clientdata(client);
294 unsigned long val = simple_strtoul(buf, NULL, 10);
295
296 down(&data->update_lock);
297 data->in_min[nr] = IN_TO_REG(val);
298 it87_write_value(client, IT87_REG_VIN_MIN(nr),
299 data->in_min[nr]);
300 up(&data->update_lock);
301 return count;
302}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200303static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
304 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200306 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
307 int nr = sensor_attr->index;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 struct i2c_client *client = to_i2c_client(dev);
310 struct it87_data *data = i2c_get_clientdata(client);
311 unsigned long val = simple_strtoul(buf, NULL, 10);
312
313 down(&data->update_lock);
314 data->in_max[nr] = IN_TO_REG(val);
315 it87_write_value(client, IT87_REG_VIN_MAX(nr),
316 data->in_max[nr]);
317 up(&data->update_lock);
318 return count;
319}
320
321#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200322static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
323 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200326static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
327 show_in_min, set_in_min, offset); \
328static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
329 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331show_in_offset(0);
332limit_in_offset(0);
333show_in_offset(1);
334limit_in_offset(1);
335show_in_offset(2);
336limit_in_offset(2);
337show_in_offset(3);
338limit_in_offset(3);
339show_in_offset(4);
340limit_in_offset(4);
341show_in_offset(5);
342limit_in_offset(5);
343show_in_offset(6);
344limit_in_offset(6);
345show_in_offset(7);
346limit_in_offset(7);
347show_in_offset(8);
348
349/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200350static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
351 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200353 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
354 int nr = sensor_attr->index;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct it87_data *data = it87_update_device(dev);
357 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
358}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200359static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
360 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200362 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
363 int nr = sensor_attr->index;
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct it87_data *data = it87_update_device(dev);
366 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
367}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200368static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
369 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200371 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
372 int nr = sensor_attr->index;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct it87_data *data = it87_update_device(dev);
375 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
376}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200377static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
378 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200380 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
381 int nr = sensor_attr->index;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct i2c_client *client = to_i2c_client(dev);
384 struct it87_data *data = i2c_get_clientdata(client);
385 int val = simple_strtol(buf, NULL, 10);
386
387 down(&data->update_lock);
388 data->temp_high[nr] = TEMP_TO_REG(val);
389 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
390 up(&data->update_lock);
391 return count;
392}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200393static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
394 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200396 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
397 int nr = sensor_attr->index;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct i2c_client *client = to_i2c_client(dev);
400 struct it87_data *data = i2c_get_clientdata(client);
401 int val = simple_strtol(buf, NULL, 10);
402
403 down(&data->update_lock);
404 data->temp_low[nr] = TEMP_TO_REG(val);
405 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
406 up(&data->update_lock);
407 return count;
408}
409#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200410static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
411 show_temp, NULL, offset - 1); \
412static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
413 show_temp_max, set_temp_max, offset - 1); \
414static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
415 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417show_temp_offset(1);
418show_temp_offset(2);
419show_temp_offset(3);
420
Jean Delvare20ad93d2005-06-05 11:53:25 +0200421static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
422 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200424 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
425 int nr = sensor_attr->index;
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct it87_data *data = it87_update_device(dev);
428 u8 reg = data->sensor; /* In case the value is updated while we use it */
429
430 if (reg & (1 << nr))
431 return sprintf(buf, "3\n"); /* thermal diode */
432 if (reg & (8 << nr))
433 return sprintf(buf, "2\n"); /* thermistor */
434 return sprintf(buf, "0\n"); /* disabled */
435}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200436static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
437 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200439 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
440 int nr = sensor_attr->index;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct i2c_client *client = to_i2c_client(dev);
443 struct it87_data *data = i2c_get_clientdata(client);
444 int val = simple_strtol(buf, NULL, 10);
445
446 down(&data->update_lock);
447
448 data->sensor &= ~(1 << nr);
449 data->sensor &= ~(8 << nr);
450 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
451 if (val == 3)
452 data->sensor |= 1 << nr;
453 else if (val == 2)
454 data->sensor |= 8 << nr;
455 else if (val != 0) {
456 up(&data->update_lock);
457 return -EINVAL;
458 }
459 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
460 up(&data->update_lock);
461 return count;
462}
463#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200464static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
465 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467show_sensor_offset(1);
468show_sensor_offset(2);
469show_sensor_offset(3);
470
471/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200472static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
473 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200475 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
476 int nr = sensor_attr->index;
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct it87_data *data = it87_update_device(dev);
479 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
480 DIV_FROM_REG(data->fan_div[nr])));
481}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200482static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
483 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200485 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
486 int nr = sensor_attr->index;
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct it87_data *data = it87_update_device(dev);
489 return sprintf(buf,"%d\n",
490 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
491}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200492static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
493 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200495 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
496 int nr = sensor_attr->index;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct it87_data *data = it87_update_device(dev);
499 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
500}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200501static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
502 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200504 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
505 int nr = sensor_attr->index;
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct it87_data *data = it87_update_device(dev);
508 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
509}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200510static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
511 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200513 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
514 int nr = sensor_attr->index;
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct it87_data *data = it87_update_device(dev);
517 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
518}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200519static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
520 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200522 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
523 int nr = sensor_attr->index;
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 struct i2c_client *client = to_i2c_client(dev);
526 struct it87_data *data = i2c_get_clientdata(client);
527 int val = simple_strtol(buf, NULL, 10);
Jean Delvare07eab462005-11-23 15:44:31 -0800528 u8 reg = it87_read_value(client, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 down(&data->update_lock);
Jean Delvare07eab462005-11-23 15:44:31 -0800531 switch (nr) {
532 case 0: data->fan_div[nr] = reg & 0x07; break;
533 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
534 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
535 }
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
538 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
539 up(&data->update_lock);
540 return count;
541}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200542static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
543 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200545 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
546 int nr = sensor_attr->index;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct i2c_client *client = to_i2c_client(dev);
549 struct it87_data *data = i2c_get_clientdata(client);
550 int val = simple_strtol(buf, NULL, 10);
551 int i, min[3];
552 u8 old;
553
554 down(&data->update_lock);
555 old = it87_read_value(client, IT87_REG_FAN_DIV);
556
557 for (i = 0; i < 3; i++)
558 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
559
560 switch (nr) {
561 case 0:
562 case 1:
563 data->fan_div[nr] = DIV_TO_REG(val);
564 break;
565 case 2:
566 if (val < 8)
567 data->fan_div[nr] = 1;
568 else
569 data->fan_div[nr] = 3;
570 }
571 val = old & 0x80;
572 val |= (data->fan_div[0] & 0x07);
573 val |= (data->fan_div[1] & 0x07) << 3;
574 if (data->fan_div[2] == 3)
575 val |= 0x1 << 6;
576 it87_write_value(client, IT87_REG_FAN_DIV, val);
577
578 for (i = 0; i < 3; i++) {
579 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
580 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
581 }
582 up(&data->update_lock);
583 return count;
584}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200585static ssize_t set_pwm_enable(struct device *dev,
586 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200588 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
589 int nr = sensor_attr->index;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 struct i2c_client *client = to_i2c_client(dev);
592 struct it87_data *data = i2c_get_clientdata(client);
593 int val = simple_strtol(buf, NULL, 10);
594
595 down(&data->update_lock);
596
597 if (val == 0) {
598 int tmp;
599 /* make sure the fan is on when in on/off mode */
600 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
601 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
602 /* set on/off mode */
603 data->fan_main_ctrl &= ~(1 << nr);
604 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
605 } else if (val == 1) {
606 /* set SmartGuardian mode */
607 data->fan_main_ctrl |= (1 << nr);
608 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
609 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
610 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
611 } else {
612 up(&data->update_lock);
613 return -EINVAL;
614 }
615
616 up(&data->update_lock);
617 return count;
618}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200619static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
620 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200622 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
623 int nr = sensor_attr->index;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct i2c_client *client = to_i2c_client(dev);
626 struct it87_data *data = i2c_get_clientdata(client);
627 int val = simple_strtol(buf, NULL, 10);
628
629 if (val < 0 || val > 255)
630 return -EINVAL;
631
632 down(&data->update_lock);
633 data->manual_pwm_ctl[nr] = val;
634 if (data->fan_main_ctrl & (1 << nr))
635 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
636 up(&data->update_lock);
637 return count;
638}
639
Jean Delvare20ad93d2005-06-05 11:53:25 +0200640#define show_fan_offset(offset) \
641static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
642 show_fan, NULL, offset - 1); \
643static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
644 show_fan_min, set_fan_min, offset - 1); \
645static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
646 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648show_fan_offset(1);
649show_fan_offset(2);
650show_fan_offset(3);
651
652#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200653static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
654 show_pwm_enable, set_pwm_enable, offset - 1); \
655static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
656 show_pwm, set_pwm, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658show_pwm_offset(1);
659show_pwm_offset(2);
660show_pwm_offset(3);
661
662/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400663static ssize_t show_alarms(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);
Jean Delvare68188ba2005-05-16 18:52:38 +0200666 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
Jean Delvare1d66c642005-04-18 21:16:59 -0700668static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400671show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct it87_data *data = it87_update_device(dev);
674 return sprintf(buf, "%ld\n", (long) data->vrm);
675}
676static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400677store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct i2c_client *client = to_i2c_client(dev);
680 struct it87_data *data = i2c_get_clientdata(client);
681 u32 val;
682
683 val = simple_strtoul(buf, NULL, 10);
684 data->vrm = val;
685
686 return count;
687}
688static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
689#define device_create_file_vrm(client) \
690device_create_file(&client->dev, &dev_attr_vrm)
691
692static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400693show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 struct it87_data *data = it87_update_device(dev);
696 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
697}
698static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
699#define device_create_file_vid(client) \
700device_create_file(&client->dev, &dev_attr_cpu0_vid)
701
702/* This function is called when:
703 * it87_driver is inserted (when this module is loaded), for each
704 available adapter
705 * when a new adapter is inserted (and it87_driver is still present) */
706static int it87_attach_adapter(struct i2c_adapter *adapter)
707{
708 if (!(adapter->class & I2C_CLASS_HWMON))
709 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200710 return i2c_probe(adapter, &addr_data, it87_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Jean Delvare2d8672c2005-07-19 23:56:35 +0200713static int it87_isa_attach_adapter(struct i2c_adapter *adapter)
714{
715 return it87_detect(adapter, isa_address, -1);
716}
717
718/* SuperIO detection - will change isa_address if a chip is found */
Jean Delvare91749992005-10-08 00:10:00 +0200719static int __init it87_find(unsigned short *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 int err = -ENODEV;
722
723 superio_enter();
724 chip_type = superio_inw(DEVID);
725 if (chip_type != IT8712F_DEVID
726 && chip_type != IT8705F_DEVID)
727 goto exit;
728
729 superio_select();
730 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
731 pr_info("it87: Device not activated, skipping\n");
732 goto exit;
733 }
734
735 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
736 if (*address == 0) {
737 pr_info("it87: Base address not set, skipping\n");
738 goto exit;
739 }
740
741 err = 0;
742 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
743 chip_type, *address, superio_inb(DEVREV) & 0x0f);
744
745exit:
746 superio_exit();
747 return err;
748}
749
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200750/* This function is called by i2c_probe */
Ben Dooksc49efce2005-10-26 21:07:25 +0200751static int it87_detect(struct i2c_adapter *adapter, int address, int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 int i;
754 struct i2c_client *new_client;
755 struct it87_data *data;
756 int err = 0;
757 const char *name = "";
758 int is_isa = i2c_is_isa_adapter(adapter);
759 int enable_pwm_interface;
760
761 if (!is_isa &&
762 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
763 goto ERROR0;
764
765 /* Reserve the ISA region */
766 if (is_isa)
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100767 if (!request_region(address, IT87_EXTENT,
768 it87_isa_driver.driver.name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 goto ERROR0;
770
Jean Delvare91749992005-10-08 00:10:00 +0200771 /* For now, we presume we have a valid client. We create the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 client structure, even though we cannot fill it completely yet.
773 But it allows us to access it87_{read,write}_value. */
774
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200775 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 err = -ENOMEM;
777 goto ERROR1;
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 new_client = &data->client;
781 if (is_isa)
782 init_MUTEX(&data->lock);
783 i2c_set_clientdata(new_client, data);
784 new_client->addr = address;
785 new_client->adapter = adapter;
Jean Delvarefde09502005-07-19 23:51:07 +0200786 new_client->driver = is_isa ? &it87_isa_driver : &it87_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 new_client->flags = 0;
788
789 /* Now, we do the remaining detection. */
790
791 if (kind < 0) {
792 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
793 || (!is_isa
794 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
795 err = -ENODEV;
796 goto ERROR2;
797 }
798 }
799
800 /* Determine the chip type. */
801 if (kind <= 0) {
802 i = it87_read_value(new_client, IT87_REG_CHIPID);
803 if (i == 0x90) {
804 kind = it87;
805 if ((is_isa) && (chip_type == IT8712F_DEVID))
806 kind = it8712;
807 }
808 else {
809 if (kind == 0)
810 dev_info(&adapter->dev,
811 "Ignoring 'force' parameter for unknown chip at "
812 "adapter %d, address 0x%02x\n",
813 i2c_adapter_id(adapter), address);
814 err = -ENODEV;
815 goto ERROR2;
816 }
817 }
818
819 if (kind == it87) {
820 name = "it87";
821 } else if (kind == it8712) {
822 name = "it8712";
823 }
824
825 /* Fill in the remaining client fields and put it into the global list */
826 strlcpy(new_client->name, name, I2C_NAME_SIZE);
827 data->type = kind;
828 data->valid = 0;
829 init_MUTEX(&data->update_lock);
830
831 /* Tell the I2C layer a new client has arrived */
832 if ((err = i2c_attach_client(new_client)))
833 goto ERROR2;
834
835 /* Check PWM configuration */
836 enable_pwm_interface = it87_check_pwm(new_client);
837
838 /* Initialize the IT87 chip */
839 it87_init_client(new_client, data);
840
841 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400842 data->class_dev = hwmon_device_register(&new_client->dev);
843 if (IS_ERR(data->class_dev)) {
844 err = PTR_ERR(data->class_dev);
845 goto ERROR3;
846 }
847
Jean Delvare20ad93d2005-06-05 11:53:25 +0200848 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
849 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
850 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
851 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
852 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
853 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
854 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
855 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
856 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
857 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
858 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
859 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
860 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
861 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
862 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
863 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
864 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
865 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
866 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
889 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
890 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
891 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 device_create_file(&new_client->dev, &dev_attr_alarms);
895 if (enable_pwm_interface) {
Jean Delvare20ad93d2005-06-05 11:53:25 +0200896 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
897 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
898 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
899 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
900 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
901 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
904 if (data->type == it8712) {
Jean Delvare303760b2005-07-31 21:52:01 +0200905 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 device_create_file_vrm(new_client);
907 device_create_file_vid(new_client);
908 }
909
910 return 0;
911
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400912ERROR3:
913 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914ERROR2:
915 kfree(data);
916ERROR1:
917 if (is_isa)
918 release_region(address, IT87_EXTENT);
919ERROR0:
920 return err;
921}
922
923static int it87_detach_client(struct i2c_client *client)
924{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400925 struct it87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int err;
927
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400928 hwmon_device_unregister(data->class_dev);
929
Jean Delvare7bef5592005-07-27 22:14:49 +0200930 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if(i2c_is_isa_client(client))
934 release_region(client->addr, IT87_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400935 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 return 0;
938}
939
Steven Cole44bbe872005-05-03 18:21:25 -0600940/* The SMBus locks itself, but ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 We don't want to lock the whole ISA bus, so we lock each client
942 separately.
943 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
944 would slow down the IT87 access and should not be necessary. */
945static int it87_read_value(struct i2c_client *client, u8 reg)
946{
947 struct it87_data *data = i2c_get_clientdata(client);
948
949 int res;
950 if (i2c_is_isa_client(client)) {
951 down(&data->lock);
952 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
953 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
954 up(&data->lock);
955 return res;
956 } else
957 return i2c_smbus_read_byte_data(client, reg);
958}
959
Steven Cole44bbe872005-05-03 18:21:25 -0600960/* The SMBus locks itself, but ISA access muse be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 We don't want to lock the whole ISA bus, so we lock each client
962 separately.
963 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
964 would slow down the IT87 access and should not be necessary. */
965static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
966{
967 struct it87_data *data = i2c_get_clientdata(client);
968
969 if (i2c_is_isa_client(client)) {
970 down(&data->lock);
971 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
972 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
973 up(&data->lock);
974 return 0;
975 } else
976 return i2c_smbus_write_byte_data(client, reg, value);
977}
978
979/* Return 1 if and only if the PWM interface is safe to use */
980static int it87_check_pwm(struct i2c_client *client)
981{
982 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
983 * and polarity set to active low is sign that this is the case so we
984 * disable pwm control to protect the user. */
985 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
986 if ((tmp & 0x87) == 0) {
987 if (fix_pwm_polarity) {
988 /* The user asks us to attempt a chip reconfiguration.
989 * This means switching to active high polarity and
990 * inverting all fan speed values. */
991 int i;
992 u8 pwm[3];
993
994 for (i = 0; i < 3; i++)
995 pwm[i] = it87_read_value(client,
996 IT87_REG_PWM(i));
997
998 /* If any fan is in automatic pwm mode, the polarity
999 * might be correct, as suspicious as it seems, so we
1000 * better don't change anything (but still disable the
1001 * PWM interface). */
1002 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1003 dev_info(&client->dev, "Reconfiguring PWM to "
1004 "active high polarity\n");
1005 it87_write_value(client, IT87_REG_FAN_CTL,
1006 tmp | 0x87);
1007 for (i = 0; i < 3; i++)
1008 it87_write_value(client,
1009 IT87_REG_PWM(i),
1010 0x7f & ~pwm[i]);
1011 return 1;
1012 }
1013
1014 dev_info(&client->dev, "PWM configuration is "
1015 "too broken to be fixed\n");
1016 }
1017
1018 dev_info(&client->dev, "Detected broken BIOS "
1019 "defaults, disabling PWM interface\n");
1020 return 0;
1021 } else if (fix_pwm_polarity) {
1022 dev_info(&client->dev, "PWM configuration looks "
1023 "sane, won't touch\n");
1024 }
1025
1026 return 1;
1027}
1028
1029/* Called when we have found a new IT87. */
1030static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1031{
1032 int tmp, i;
1033
1034 /* initialize to sane defaults:
1035 * - if the chip is in manual pwm mode, this will be overwritten with
1036 * the actual settings on the chip (so in this case, initialization
1037 * is not needed)
1038 * - if in automatic or on/off mode, we could switch to manual mode,
1039 * read the registers and set manual_pwm_ctl accordingly, but currently
1040 * this is not implemented, so we initialize to something sane */
1041 for (i = 0; i < 3; i++) {
1042 data->manual_pwm_ctl[i] = 0xff;
1043 }
1044
1045 /* Check if temperature channnels are reset manually or by some reason */
1046 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1047 if ((tmp & 0x3f) == 0) {
1048 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1049 tmp = (tmp & 0xc0) | 0x2a;
1050 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1051 }
1052 data->sensor = tmp;
1053
1054 /* Check if voltage monitors are reset manually or by some reason */
1055 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1056 if ((tmp & 0xff) == 0) {
1057 /* Enable all voltage monitors */
1058 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1059 }
1060
1061 /* Check if tachometers are reset manually or by some reason */
1062 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1063 if ((data->fan_main_ctrl & 0x70) == 0) {
1064 /* Enable all fan tachometers */
1065 data->fan_main_ctrl |= 0x70;
1066 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1067 }
1068
1069 /* Set current fan mode registers and the default settings for the
1070 * other mode registers */
1071 for (i = 0; i < 3; i++) {
1072 if (data->fan_main_ctrl & (1 << i)) {
1073 /* pwm mode */
1074 tmp = it87_read_value(client, IT87_REG_PWM(i));
1075 if (tmp & 0x80) {
1076 /* automatic pwm - not yet implemented, but
1077 * leave the settings made by the BIOS alone
1078 * until a change is requested via the sysfs
1079 * interface */
1080 } else {
1081 /* manual pwm */
1082 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1083 }
1084 }
1085 }
1086
1087 /* Start monitoring */
1088 it87_write_value(client, IT87_REG_CONFIG,
1089 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1090 | (update_vbat ? 0x41 : 0x01));
1091}
1092
1093static struct it87_data *it87_update_device(struct device *dev)
1094{
1095 struct i2c_client *client = to_i2c_client(dev);
1096 struct it87_data *data = i2c_get_clientdata(client);
1097 int i;
1098
1099 down(&data->update_lock);
1100
1101 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1102 || !data->valid) {
1103
1104 if (update_vbat) {
1105 /* Cleared after each update, so reenable. Value
1106 returned by this read will be previous value */
1107 it87_write_value(client, IT87_REG_CONFIG,
1108 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1109 }
1110 for (i = 0; i <= 7; i++) {
1111 data->in[i] =
1112 it87_read_value(client, IT87_REG_VIN(i));
1113 data->in_min[i] =
1114 it87_read_value(client, IT87_REG_VIN_MIN(i));
1115 data->in_max[i] =
1116 it87_read_value(client, IT87_REG_VIN_MAX(i));
1117 }
1118 data->in[8] =
1119 it87_read_value(client, IT87_REG_VIN(8));
1120 /* Temperature sensor doesn't have limit registers, set
1121 to min and max value */
1122 data->in_min[8] = 0;
1123 data->in_max[8] = 255;
1124
1125 for (i = 0; i < 3; i++) {
1126 data->fan[i] =
1127 it87_read_value(client, IT87_REG_FAN(i));
1128 data->fan_min[i] =
1129 it87_read_value(client, IT87_REG_FAN_MIN(i));
1130 }
1131 for (i = 0; i < 3; i++) {
1132 data->temp[i] =
1133 it87_read_value(client, IT87_REG_TEMP(i));
1134 data->temp_high[i] =
1135 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1136 data->temp_low[i] =
1137 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1138 }
1139
1140 i = it87_read_value(client, IT87_REG_FAN_DIV);
1141 data->fan_div[0] = i & 0x07;
1142 data->fan_div[1] = (i >> 3) & 0x07;
1143 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1144
1145 data->alarms =
1146 it87_read_value(client, IT87_REG_ALARM1) |
1147 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1148 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1149 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1150
1151 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1152 /* The 8705 does not have VID capability */
1153 if (data->type == it8712) {
1154 data->vid = it87_read_value(client, IT87_REG_VID);
1155 data->vid &= 0x1f;
1156 }
1157 data->last_updated = jiffies;
1158 data->valid = 1;
1159 }
1160
1161 up(&data->update_lock);
1162
1163 return data;
1164}
1165
1166static int __init sm_it87_init(void)
1167{
Jean Delvare91749992005-10-08 00:10:00 +02001168 int res;
Jean Delvarefde09502005-07-19 23:51:07 +02001169
1170 res = i2c_add_driver(&it87_driver);
1171 if (res)
1172 return res;
1173
Jean Delvare91749992005-10-08 00:10:00 +02001174 if (!it87_find(&isa_address)) {
1175 res = i2c_isa_add_driver(&it87_isa_driver);
1176 if (res) {
1177 i2c_del_driver(&it87_driver);
1178 return res;
1179 }
Jean Delvarefde09502005-07-19 23:51:07 +02001180 }
1181
1182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
1185static void __exit sm_it87_exit(void)
1186{
Jean Delvarefde09502005-07-19 23:51:07 +02001187 i2c_isa_del_driver(&it87_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 i2c_del_driver(&it87_driver);
1189}
1190
1191
1192MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1193MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1194module_param(update_vbat, bool, 0);
1195MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1196module_param(fix_pwm_polarity, bool, 0);
1197MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1198MODULE_LICENSE("GPL");
1199
1200module_init(sm_it87_init);
1201module_exit(sm_it87_exit);