blob: 1ed8b7e2c35df01e57dfab60aab6c0dc8825c37e [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
Jean Delvare8e9afcb2006-12-12 18:18:28 +01006 IT8712F Super I/O chip w/LPC interface
Jean Delvare17d648b2006-08-28 14:23:46 +02007 IT8716F Super I/O chip w/LPC interface
Jean Delvare87673dd2006-08-28 14:37:19 +02008 IT8718F Super I/O chip w/LPC interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 Sis950 A clone of the IT8705F
10
11 Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
Jean Delvareb19367c2006-08-28 14:39:26 +020012 Copyright (C) 2005-2006 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27*/
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/jiffies.h>
33#include <linux/i2c.h>
Jean Delvarefde09502005-07-19 23:51:07 +020034#include <linux/i2c-isa.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040035#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020036#include <linux/hwmon-sysfs.h>
37#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040038#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010039#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020040#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
42
43
Jean Delvare91749992005-10-08 00:10:00 +020044static unsigned short isa_address;
Jean Delvare8e9afcb2006-12-12 18:18:28 +010045enum chips { it87, it8712, it8716, it8718 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define REG 0x2e /* The register to read/write */
48#define DEV 0x07 /* Register: Logical device select */
49#define VAL 0x2f /* The value to read/write */
50#define PME 0x04 /* The device with the fan registers in it */
Jean Delvare87673dd2006-08-28 14:37:19 +020051#define GPIO 0x07 /* The device with the IT8718F VID value in it */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define DEVID 0x20 /* Register: Device ID */
53#define DEVREV 0x22 /* Register: Device Revision */
54
55static inline int
56superio_inb(int reg)
57{
58 outb(reg, REG);
59 return inb(VAL);
60}
61
62static int superio_inw(int reg)
63{
64 int val;
65 outb(reg++, REG);
66 val = inb(VAL) << 8;
67 outb(reg, REG);
68 val |= inb(VAL);
69 return val;
70}
71
72static inline void
Jean Delvare87673dd2006-08-28 14:37:19 +020073superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +020076 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79static inline void
80superio_enter(void)
81{
82 outb(0x87, REG);
83 outb(0x01, REG);
84 outb(0x55, REG);
85 outb(0x55, REG);
86}
87
88static inline void
89superio_exit(void)
90{
91 outb(0x02, REG);
92 outb(0x02, VAL);
93}
94
Jean Delvare87673dd2006-08-28 14:37:19 +020095/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#define IT8712F_DEVID 0x8712
97#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +020098#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +020099#define IT8718F_DEVID 0x8718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#define IT87_ACT_REG 0x30
101#define IT87_BASE_REG 0x60
102
Jean Delvare87673dd2006-08-28 14:37:19 +0200103/* Logical device 7 registers (IT8712F and later) */
104#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
105#define IT87_SIO_VID_REG 0xfc /* VID value */
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/* 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
Jean Delvare87673dd2006-08-28 14:37:19 +0200113/* Values read from Super-I/O config space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static u16 chip_type;
Jean Delvare87673dd2006-08-28 14:37:19 +0200115static u8 vid_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
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
Jean Delvare87673dd2006-08-28 14:37:19 +0200134/* The IT8718F has the VID value in a different register, in Super-I/O
135 configuration space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define IT87_REG_VID 0x0a
Jean Delvare17d648b2006-08-28 14:23:46 +0200137/* Warning: register 0x0b is used for something completely different in
138 new chips/revisions. I suspect only 16-bit tachometer mode will work
139 for these. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200141#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
144
145#define IT87_REG_FAN(nr) (0x0d + (nr))
146#define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
Jean Delvare17d648b2006-08-28 14:23:46 +0200147#define IT87_REG_FANX(nr) (0x18 + (nr))
148#define IT87_REG_FANX_MIN(nr) (0x1b + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#define IT87_REG_FAN_MAIN_CTRL 0x13
150#define IT87_REG_FAN_CTL 0x14
151#define IT87_REG_PWM(nr) (0x15 + (nr))
152
153#define IT87_REG_VIN(nr) (0x20 + (nr))
154#define IT87_REG_TEMP(nr) (0x29 + (nr))
155
156#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
157#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
158#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
159#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define IT87_REG_VIN_ENABLE 0x50
162#define IT87_REG_TEMP_ENABLE 0x51
163
164#define IT87_REG_CHIPID 0x58
165
166#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
167#define IN_FROM_REG(val) ((val) * 16)
168
169static inline u8 FAN_TO_REG(long rpm, int div)
170{
171 if (rpm == 0)
172 return 255;
173 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
174 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
175 254);
176}
177
Jean Delvare17d648b2006-08-28 14:23:46 +0200178static inline u16 FAN16_TO_REG(long rpm)
179{
180 if (rpm == 0)
181 return 0xffff;
182 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
Jean Delvare17d648b2006-08-28 14:23:46 +0200186/* The divider is fixed to 2 in 16-bit mode */
187#define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
190 ((val)+500)/1000),-128,127))
191#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#define PWM_TO_REG(val) ((val) >> 1)
194#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
195
196static int DIV_TO_REG(int val)
197{
198 int answer = 0;
Jean Delvareb9e349f2006-08-28 14:26:22 +0200199 while (answer < 7 && (val >>= 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 answer++;
201 return answer;
202}
203#define DIV_FROM_REG(val) (1 << (val))
204
205
206/* For each registered IT87, we need to keep some data in memory. That
207 data is pointed to by it87_list[NR]->data. The structure itself is
208 dynamically allocated, at the same time when a new it87 client is
209 allocated. */
210struct it87_data {
211 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400212 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100213 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 enum chips type;
215
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100216 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 char valid; /* !=0 if following fields are valid */
218 unsigned long last_updated; /* In jiffies */
219
220 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200221 u8 in_max[8]; /* Register value */
222 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200223 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +0200224 u16 fan[3]; /* Register values, possibly combined */
225 u16 fan_min[3]; /* Register values, possibly combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 u8 temp[3]; /* Register value */
227 u8 temp_high[3]; /* Register value */
228 u8 temp_low[3]; /* Register value */
229 u8 sensor; /* Register value */
230 u8 fan_div[3]; /* Register encoding, shifted right */
231 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100232 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 u32 alarms; /* Register encoding, combined */
234 u8 fan_main_ctrl; /* Register value */
235 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
236};
237
238
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100239static int it87_detect(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static int it87_detach_client(struct i2c_client *client);
241
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100242static int it87_read_value(struct i2c_client *client, u8 reg);
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100243static void it87_write_value(struct i2c_client *client, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static struct it87_data *it87_update_device(struct device *dev);
245static int it87_check_pwm(struct i2c_client *client);
246static void it87_init_client(struct i2c_client *client, struct it87_data *data);
247
248
Jean Delvarefde09502005-07-19 23:51:07 +0200249static struct i2c_driver it87_isa_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100250 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200251 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100252 .name = "it87-isa",
253 },
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100254 .attach_adapter = it87_detect,
Jean Delvarefde09502005-07-19 23:51:07 +0200255 .detach_client = it87_detach_client,
256};
257
258
Jean Delvare20ad93d2005-06-05 11:53:25 +0200259static ssize_t show_in(struct device *dev, struct device_attribute *attr,
260 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200262 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
263 int nr = sensor_attr->index;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct it87_data *data = it87_update_device(dev);
266 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
267}
268
Jean Delvare20ad93d2005-06-05 11:53:25 +0200269static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
270 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200272 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
273 int nr = sensor_attr->index;
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct it87_data *data = it87_update_device(dev);
276 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
277}
278
Jean Delvare20ad93d2005-06-05 11:53:25 +0200279static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
280 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200282 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
283 int nr = sensor_attr->index;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct it87_data *data = it87_update_device(dev);
286 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
287}
288
Jean Delvare20ad93d2005-06-05 11:53:25 +0200289static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
290 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200292 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
293 int nr = sensor_attr->index;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct i2c_client *client = to_i2c_client(dev);
296 struct it87_data *data = i2c_get_clientdata(client);
297 unsigned long val = simple_strtoul(buf, NULL, 10);
298
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100299 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 data->in_min[nr] = IN_TO_REG(val);
301 it87_write_value(client, IT87_REG_VIN_MIN(nr),
302 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100303 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return count;
305}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200306static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
307 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200309 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
310 int nr = sensor_attr->index;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct i2c_client *client = to_i2c_client(dev);
313 struct it87_data *data = i2c_get_clientdata(client);
314 unsigned long val = simple_strtoul(buf, NULL, 10);
315
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100316 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 data->in_max[nr] = IN_TO_REG(val);
318 it87_write_value(client, IT87_REG_VIN_MAX(nr),
319 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100320 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return count;
322}
323
324#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200325static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
326 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200329static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
330 show_in_min, set_in_min, offset); \
331static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
332 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334show_in_offset(0);
335limit_in_offset(0);
336show_in_offset(1);
337limit_in_offset(1);
338show_in_offset(2);
339limit_in_offset(2);
340show_in_offset(3);
341limit_in_offset(3);
342show_in_offset(4);
343limit_in_offset(4);
344show_in_offset(5);
345limit_in_offset(5);
346show_in_offset(6);
347limit_in_offset(6);
348show_in_offset(7);
349limit_in_offset(7);
350show_in_offset(8);
351
352/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200353static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
354 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200356 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
357 int nr = sensor_attr->index;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct it87_data *data = it87_update_device(dev);
360 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
361}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200362static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
363 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200365 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
366 int nr = sensor_attr->index;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct it87_data *data = it87_update_device(dev);
369 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
370}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200371static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
372 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200374 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
375 int nr = sensor_attr->index;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct it87_data *data = it87_update_device(dev);
378 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
379}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200380static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
381 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200383 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
384 int nr = sensor_attr->index;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct i2c_client *client = to_i2c_client(dev);
387 struct it87_data *data = i2c_get_clientdata(client);
388 int val = simple_strtol(buf, NULL, 10);
389
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100390 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 data->temp_high[nr] = TEMP_TO_REG(val);
392 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100393 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return count;
395}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200396static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
397 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200399 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
400 int nr = sensor_attr->index;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct i2c_client *client = to_i2c_client(dev);
403 struct it87_data *data = i2c_get_clientdata(client);
404 int val = simple_strtol(buf, NULL, 10);
405
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100406 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 data->temp_low[nr] = TEMP_TO_REG(val);
408 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100409 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return count;
411}
412#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200413static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
414 show_temp, NULL, offset - 1); \
415static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
416 show_temp_max, set_temp_max, offset - 1); \
417static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
418 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420show_temp_offset(1);
421show_temp_offset(2);
422show_temp_offset(3);
423
Jean Delvare20ad93d2005-06-05 11:53:25 +0200424static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
425 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200427 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
428 int nr = sensor_attr->index;
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct it87_data *data = it87_update_device(dev);
431 u8 reg = data->sensor; /* In case the value is updated while we use it */
432
433 if (reg & (1 << nr))
434 return sprintf(buf, "3\n"); /* thermal diode */
435 if (reg & (8 << nr))
436 return sprintf(buf, "2\n"); /* thermistor */
437 return sprintf(buf, "0\n"); /* disabled */
438}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200439static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
440 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200442 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
443 int nr = sensor_attr->index;
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct i2c_client *client = to_i2c_client(dev);
446 struct it87_data *data = i2c_get_clientdata(client);
447 int val = simple_strtol(buf, NULL, 10);
448
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100449 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 data->sensor &= ~(1 << nr);
452 data->sensor &= ~(8 << nr);
453 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
454 if (val == 3)
455 data->sensor |= 1 << nr;
456 else if (val == 2)
457 data->sensor |= 8 << nr;
458 else if (val != 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100459 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return -EINVAL;
461 }
462 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100463 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return count;
465}
466#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200467static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
468 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470show_sensor_offset(1);
471show_sensor_offset(2);
472show_sensor_offset(3);
473
474/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200475static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
476 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200478 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
479 int nr = sensor_attr->index;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct it87_data *data = it87_update_device(dev);
482 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
483 DIV_FROM_REG(data->fan_div[nr])));
484}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200485static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
486 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200488 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
489 int nr = sensor_attr->index;
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct it87_data *data = it87_update_device(dev);
492 return sprintf(buf,"%d\n",
493 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
494}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200495static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
496 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200498 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
499 int nr = sensor_attr->index;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct it87_data *data = it87_update_device(dev);
502 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
503}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200504static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
505 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200507 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
508 int nr = sensor_attr->index;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct it87_data *data = it87_update_device(dev);
511 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
512}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200513static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
514 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200516 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
517 int nr = sensor_attr->index;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct it87_data *data = it87_update_device(dev);
520 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
521}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200522static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
523 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200525 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
526 int nr = sensor_attr->index;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct i2c_client *client = to_i2c_client(dev);
529 struct it87_data *data = i2c_get_clientdata(client);
530 int val = simple_strtol(buf, NULL, 10);
Jean Delvare07eab462005-11-23 15:44:31 -0800531 u8 reg = it87_read_value(client, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100533 mutex_lock(&data->update_lock);
Jean Delvare07eab462005-11-23 15:44:31 -0800534 switch (nr) {
535 case 0: data->fan_div[nr] = reg & 0x07; break;
536 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
537 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
538 }
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
541 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100542 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return count;
544}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200545static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
546 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200548 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
549 int nr = sensor_attr->index;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct i2c_client *client = to_i2c_client(dev);
552 struct it87_data *data = i2c_get_clientdata(client);
Jean Delvareb9e349f2006-08-28 14:26:22 +0200553 unsigned long val = simple_strtoul(buf, NULL, 10);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200554 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 u8 old;
556
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100557 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 old = it87_read_value(client, IT87_REG_FAN_DIV);
559
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200560 /* Save fan min limit */
561 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 switch (nr) {
564 case 0:
565 case 1:
566 data->fan_div[nr] = DIV_TO_REG(val);
567 break;
568 case 2:
569 if (val < 8)
570 data->fan_div[nr] = 1;
571 else
572 data->fan_div[nr] = 3;
573 }
574 val = old & 0x80;
575 val |= (data->fan_div[0] & 0x07);
576 val |= (data->fan_div[1] & 0x07) << 3;
577 if (data->fan_div[2] == 3)
578 val |= 0x1 << 6;
579 it87_write_value(client, IT87_REG_FAN_DIV, val);
580
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200581 /* Restore fan min limit */
582 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
583 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
584
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100585 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return count;
587}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200588static ssize_t set_pwm_enable(struct device *dev,
589 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200591 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
592 int nr = sensor_attr->index;
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct i2c_client *client = to_i2c_client(dev);
595 struct it87_data *data = i2c_get_clientdata(client);
596 int val = simple_strtol(buf, NULL, 10);
597
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100598 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (val == 0) {
601 int tmp;
602 /* make sure the fan is on when in on/off mode */
603 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
604 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
605 /* set on/off mode */
606 data->fan_main_ctrl &= ~(1 << nr);
607 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
608 } else if (val == 1) {
609 /* set SmartGuardian mode */
610 data->fan_main_ctrl |= (1 << nr);
611 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
612 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
613 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
614 } else {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100615 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return -EINVAL;
617 }
618
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100619 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return count;
621}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200622static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
623 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200625 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
626 int nr = sensor_attr->index;
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct i2c_client *client = to_i2c_client(dev);
629 struct it87_data *data = i2c_get_clientdata(client);
630 int val = simple_strtol(buf, NULL, 10);
631
632 if (val < 0 || val > 255)
633 return -EINVAL;
634
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100635 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 data->manual_pwm_ctl[nr] = val;
637 if (data->fan_main_ctrl & (1 << nr))
638 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100639 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return count;
641}
642
Jean Delvare20ad93d2005-06-05 11:53:25 +0200643#define show_fan_offset(offset) \
644static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
645 show_fan, NULL, offset - 1); \
646static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
647 show_fan_min, set_fan_min, offset - 1); \
648static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
649 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651show_fan_offset(1);
652show_fan_offset(2);
653show_fan_offset(3);
654
655#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200656static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
657 show_pwm_enable, set_pwm_enable, offset - 1); \
658static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
659 show_pwm, set_pwm, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661show_pwm_offset(1);
662show_pwm_offset(2);
663show_pwm_offset(3);
664
Jean Delvare17d648b2006-08-28 14:23:46 +0200665/* A different set of callbacks for 16-bit fans */
666static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
667 char *buf)
668{
669 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
670 int nr = sensor_attr->index;
671 struct it87_data *data = it87_update_device(dev);
672 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
673}
674
675static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
676 char *buf)
677{
678 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
679 int nr = sensor_attr->index;
680 struct it87_data *data = it87_update_device(dev);
681 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
682}
683
684static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
685 const char *buf, size_t count)
686{
687 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
688 int nr = sensor_attr->index;
689 struct i2c_client *client = to_i2c_client(dev);
690 struct it87_data *data = i2c_get_clientdata(client);
691 int val = simple_strtol(buf, NULL, 10);
692
693 mutex_lock(&data->update_lock);
694 data->fan_min[nr] = FAN16_TO_REG(val);
695 it87_write_value(client, IT87_REG_FAN_MIN(nr),
696 data->fan_min[nr] & 0xff);
697 it87_write_value(client, IT87_REG_FANX_MIN(nr),
698 data->fan_min[nr] >> 8);
699 mutex_unlock(&data->update_lock);
700 return count;
701}
702
703/* We want to use the same sysfs file names as 8-bit fans, but we need
704 different variable names, so we have to use SENSOR_ATTR instead of
705 SENSOR_DEVICE_ATTR. */
706#define show_fan16_offset(offset) \
707static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
708 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
709 show_fan16, NULL, offset - 1); \
710static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
711 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
712 show_fan16_min, set_fan16_min, offset - 1)
713
714show_fan16_offset(1);
715show_fan16_offset(2);
716show_fan16_offset(3);
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400719static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200722 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
Jean Delvare1d66c642005-04-18 21:16:59 -0700724static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400727show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 struct it87_data *data = it87_update_device(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +0100730 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400733store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 struct i2c_client *client = to_i2c_client(dev);
736 struct it87_data *data = i2c_get_clientdata(client);
737 u32 val;
738
739 val = simple_strtoul(buf, NULL, 10);
740 data->vrm = val;
741
742 return count;
743}
744static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400747show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 struct it87_data *data = it87_update_device(dev);
750 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
751}
752static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +0200753
754static struct attribute *it87_attributes[] = {
755 &sensor_dev_attr_in0_input.dev_attr.attr,
756 &sensor_dev_attr_in1_input.dev_attr.attr,
757 &sensor_dev_attr_in2_input.dev_attr.attr,
758 &sensor_dev_attr_in3_input.dev_attr.attr,
759 &sensor_dev_attr_in4_input.dev_attr.attr,
760 &sensor_dev_attr_in5_input.dev_attr.attr,
761 &sensor_dev_attr_in6_input.dev_attr.attr,
762 &sensor_dev_attr_in7_input.dev_attr.attr,
763 &sensor_dev_attr_in8_input.dev_attr.attr,
764 &sensor_dev_attr_in0_min.dev_attr.attr,
765 &sensor_dev_attr_in1_min.dev_attr.attr,
766 &sensor_dev_attr_in2_min.dev_attr.attr,
767 &sensor_dev_attr_in3_min.dev_attr.attr,
768 &sensor_dev_attr_in4_min.dev_attr.attr,
769 &sensor_dev_attr_in5_min.dev_attr.attr,
770 &sensor_dev_attr_in6_min.dev_attr.attr,
771 &sensor_dev_attr_in7_min.dev_attr.attr,
772 &sensor_dev_attr_in0_max.dev_attr.attr,
773 &sensor_dev_attr_in1_max.dev_attr.attr,
774 &sensor_dev_attr_in2_max.dev_attr.attr,
775 &sensor_dev_attr_in3_max.dev_attr.attr,
776 &sensor_dev_attr_in4_max.dev_attr.attr,
777 &sensor_dev_attr_in5_max.dev_attr.attr,
778 &sensor_dev_attr_in6_max.dev_attr.attr,
779 &sensor_dev_attr_in7_max.dev_attr.attr,
780
781 &sensor_dev_attr_temp1_input.dev_attr.attr,
782 &sensor_dev_attr_temp2_input.dev_attr.attr,
783 &sensor_dev_attr_temp3_input.dev_attr.attr,
784 &sensor_dev_attr_temp1_max.dev_attr.attr,
785 &sensor_dev_attr_temp2_max.dev_attr.attr,
786 &sensor_dev_attr_temp3_max.dev_attr.attr,
787 &sensor_dev_attr_temp1_min.dev_attr.attr,
788 &sensor_dev_attr_temp2_min.dev_attr.attr,
789 &sensor_dev_attr_temp3_min.dev_attr.attr,
790 &sensor_dev_attr_temp1_type.dev_attr.attr,
791 &sensor_dev_attr_temp2_type.dev_attr.attr,
792 &sensor_dev_attr_temp3_type.dev_attr.attr,
793
794 &dev_attr_alarms.attr,
795 NULL
796};
797
798static const struct attribute_group it87_group = {
799 .attrs = it87_attributes,
800};
801
802static struct attribute *it87_attributes_opt[] = {
803 &sensor_dev_attr_fan1_input16.dev_attr.attr,
804 &sensor_dev_attr_fan1_min16.dev_attr.attr,
805 &sensor_dev_attr_fan2_input16.dev_attr.attr,
806 &sensor_dev_attr_fan2_min16.dev_attr.attr,
807 &sensor_dev_attr_fan3_input16.dev_attr.attr,
808 &sensor_dev_attr_fan3_min16.dev_attr.attr,
809
810 &sensor_dev_attr_fan1_input.dev_attr.attr,
811 &sensor_dev_attr_fan1_min.dev_attr.attr,
812 &sensor_dev_attr_fan1_div.dev_attr.attr,
813 &sensor_dev_attr_fan2_input.dev_attr.attr,
814 &sensor_dev_attr_fan2_min.dev_attr.attr,
815 &sensor_dev_attr_fan2_div.dev_attr.attr,
816 &sensor_dev_attr_fan3_input.dev_attr.attr,
817 &sensor_dev_attr_fan3_min.dev_attr.attr,
818 &sensor_dev_attr_fan3_div.dev_attr.attr,
819
820 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
821 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
822 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
823 &sensor_dev_attr_pwm1.dev_attr.attr,
824 &sensor_dev_attr_pwm2.dev_attr.attr,
825 &sensor_dev_attr_pwm3.dev_attr.attr,
826
827 &dev_attr_vrm.attr,
828 &dev_attr_cpu0_vid.attr,
829 NULL
830};
831
832static const struct attribute_group it87_group_opt = {
833 .attrs = it87_attributes_opt,
834};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Jean Delvare2d8672c2005-07-19 23:56:35 +0200836/* SuperIO detection - will change isa_address if a chip is found */
Jean Delvare91749992005-10-08 00:10:00 +0200837static int __init it87_find(unsigned short *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 int err = -ENODEV;
840
841 superio_enter();
842 chip_type = superio_inw(DEVID);
843 if (chip_type != IT8712F_DEVID
Jean Delvare17d648b2006-08-28 14:23:46 +0200844 && chip_type != IT8716F_DEVID
Jean Delvare87673dd2006-08-28 14:37:19 +0200845 && chip_type != IT8718F_DEVID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 && chip_type != IT8705F_DEVID)
847 goto exit;
848
Jean Delvare87673dd2006-08-28 14:37:19 +0200849 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
851 pr_info("it87: Device not activated, skipping\n");
852 goto exit;
853 }
854
855 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
856 if (*address == 0) {
857 pr_info("it87: Base address not set, skipping\n");
858 goto exit;
859 }
860
861 err = 0;
862 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
863 chip_type, *address, superio_inb(DEVREV) & 0x0f);
864
Jean Delvare87673dd2006-08-28 14:37:19 +0200865 /* Read GPIO config and VID value from LDN 7 (GPIO) */
866 if (chip_type != IT8705F_DEVID) {
867 int reg;
868
869 superio_select(GPIO);
870 if (chip_type == it8718)
871 vid_value = superio_inb(IT87_SIO_VID_REG);
872
873 reg = superio_inb(IT87_SIO_PINX2_REG);
874 if (reg & (1 << 0))
875 pr_info("it87: in3 is VCC (+5V)\n");
876 if (reg & (1 << 1))
877 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
878 }
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880exit:
881 superio_exit();
882 return err;
883}
884
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200885/* This function is called by i2c_probe */
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100886static int it87_detect(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct i2c_client *new_client;
889 struct it87_data *data;
890 int err = 0;
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100891 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 int enable_pwm_interface;
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* Reserve the ISA region */
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100895 if (!request_region(isa_address, IT87_EXTENT,
896 it87_isa_driver.driver.name)){
897 err = -EBUSY;
898 goto ERROR0;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200901 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 err = -ENOMEM;
903 goto ERROR1;
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 new_client = &data->client;
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100907 mutex_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 i2c_set_clientdata(new_client, data);
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100909 new_client->addr = isa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 new_client->adapter = adapter;
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100911 new_client->driver = &it87_isa_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /* Now, we do the remaining detection. */
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100914 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
915 || it87_read_value(new_client, IT87_REG_CHIPID) != 0x90) {
916 err = -ENODEV;
917 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919
920 /* Determine the chip type. */
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100921 switch (chip_type) {
922 case IT8712F_DEVID:
923 data->type = it8712;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 name = "it8712";
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100925 break;
926 case IT8716F_DEVID:
927 data->type = it8716;
Jean Delvare17d648b2006-08-28 14:23:46 +0200928 name = "it8716";
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100929 break;
930 case IT8718F_DEVID:
931 data->type = it8718;
Jean Delvare87673dd2006-08-28 14:37:19 +0200932 name = "it8718";
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100933 break;
934 default:
935 data->type = it87;
936 name = "it87";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939 /* Fill in the remaining client fields and put it into the global list */
940 strlcpy(new_client->name, name, I2C_NAME_SIZE);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100941 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /* Tell the I2C layer a new client has arrived */
944 if ((err = i2c_attach_client(new_client)))
945 goto ERROR2;
946
947 /* Check PWM configuration */
948 enable_pwm_interface = it87_check_pwm(new_client);
949
950 /* Initialize the IT87 chip */
951 it87_init_client(new_client, data);
952
953 /* Register sysfs hooks */
Jean Delvare87808be2006-09-24 21:17:13 +0200954 if ((err = sysfs_create_group(&new_client->dev.kobj, &it87_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400955 goto ERROR3;
Jean Delvare17d648b2006-08-28 14:23:46 +0200956
Jean Delvare9060f8b2006-08-28 14:24:17 +0200957 /* Do not create fan files for disabled fans */
Jean Delvare87673dd2006-08-28 14:37:19 +0200958 if (data->type == it8716 || data->type == it8718) {
959 /* 16-bit tachometers */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200960 if (data->has_fan & (1 << 0)) {
Jean Delvare87808be2006-09-24 21:17:13 +0200961 if ((err = device_create_file(&new_client->dev,
962 &sensor_dev_attr_fan1_input16.dev_attr))
963 || (err = device_create_file(&new_client->dev,
964 &sensor_dev_attr_fan1_min16.dev_attr)))
965 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +0200966 }
967 if (data->has_fan & (1 << 1)) {
Jean Delvare87808be2006-09-24 21:17:13 +0200968 if ((err = device_create_file(&new_client->dev,
969 &sensor_dev_attr_fan2_input16.dev_attr))
970 || (err = device_create_file(&new_client->dev,
971 &sensor_dev_attr_fan2_min16.dev_attr)))
972 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +0200973 }
974 if (data->has_fan & (1 << 2)) {
Jean Delvare87808be2006-09-24 21:17:13 +0200975 if ((err = device_create_file(&new_client->dev,
976 &sensor_dev_attr_fan3_input16.dev_attr))
977 || (err = device_create_file(&new_client->dev,
978 &sensor_dev_attr_fan3_min16.dev_attr)))
979 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +0200980 }
Jean Delvare17d648b2006-08-28 14:23:46 +0200981 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +0200982 /* 8-bit tachometers with clock divider */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200983 if (data->has_fan & (1 << 0)) {
Jean Delvare87808be2006-09-24 21:17:13 +0200984 if ((err = device_create_file(&new_client->dev,
985 &sensor_dev_attr_fan1_input.dev_attr))
986 || (err = device_create_file(&new_client->dev,
987 &sensor_dev_attr_fan1_min.dev_attr))
988 || (err = device_create_file(&new_client->dev,
989 &sensor_dev_attr_fan1_div.dev_attr)))
990 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +0200991 }
992 if (data->has_fan & (1 << 1)) {
Jean Delvare87808be2006-09-24 21:17:13 +0200993 if ((err = device_create_file(&new_client->dev,
994 &sensor_dev_attr_fan2_input.dev_attr))
995 || (err = device_create_file(&new_client->dev,
996 &sensor_dev_attr_fan2_min.dev_attr))
997 || (err = device_create_file(&new_client->dev,
998 &sensor_dev_attr_fan2_div.dev_attr)))
999 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001000 }
1001 if (data->has_fan & (1 << 2)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001002 if ((err = device_create_file(&new_client->dev,
1003 &sensor_dev_attr_fan3_input.dev_attr))
1004 || (err = device_create_file(&new_client->dev,
1005 &sensor_dev_attr_fan3_min.dev_attr))
1006 || (err = device_create_file(&new_client->dev,
1007 &sensor_dev_attr_fan3_div.dev_attr)))
1008 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001009 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001010 }
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (enable_pwm_interface) {
Jean Delvare87808be2006-09-24 21:17:13 +02001013 if ((err = device_create_file(&new_client->dev,
1014 &sensor_dev_attr_pwm1_enable.dev_attr))
1015 || (err = device_create_file(&new_client->dev,
1016 &sensor_dev_attr_pwm2_enable.dev_attr))
1017 || (err = device_create_file(&new_client->dev,
1018 &sensor_dev_attr_pwm3_enable.dev_attr))
1019 || (err = device_create_file(&new_client->dev,
1020 &sensor_dev_attr_pwm1.dev_attr))
1021 || (err = device_create_file(&new_client->dev,
1022 &sensor_dev_attr_pwm2.dev_attr))
1023 || (err = device_create_file(&new_client->dev,
1024 &sensor_dev_attr_pwm3.dev_attr)))
1025 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027
Jean Delvare87673dd2006-08-28 14:37:19 +02001028 if (data->type == it8712 || data->type == it8716
1029 || data->type == it8718) {
Jean Delvare303760b2005-07-31 21:52:01 +02001030 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001031 /* VID reading from Super-I/O config space if available */
1032 data->vid = vid_value;
Jean Delvare87808be2006-09-24 21:17:13 +02001033 if ((err = device_create_file(&new_client->dev,
1034 &dev_attr_vrm))
1035 || (err = device_create_file(&new_client->dev,
1036 &dev_attr_cpu0_vid)))
1037 goto ERROR4;
1038 }
1039
1040 data->class_dev = hwmon_device_register(&new_client->dev);
1041 if (IS_ERR(data->class_dev)) {
1042 err = PTR_ERR(data->class_dev);
1043 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045
1046 return 0;
1047
Jean Delvare87808be2006-09-24 21:17:13 +02001048ERROR4:
1049 sysfs_remove_group(&new_client->dev.kobj, &it87_group);
1050 sysfs_remove_group(&new_client->dev.kobj, &it87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001051ERROR3:
1052 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053ERROR2:
1054 kfree(data);
1055ERROR1:
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001056 release_region(isa_address, IT87_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057ERROR0:
1058 return err;
1059}
1060
1061static int it87_detach_client(struct i2c_client *client)
1062{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001063 struct it87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 int err;
1065
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001066 hwmon_device_unregister(data->class_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001067 sysfs_remove_group(&client->dev.kobj, &it87_group);
1068 sysfs_remove_group(&client->dev.kobj, &it87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001069
Jean Delvare7bef5592005-07-27 22:14:49 +02001070 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001073 release_region(client->addr, IT87_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001074 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 return 0;
1077}
1078
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001079/* ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1081 would slow down the IT87 access and should not be necessary. */
1082static int it87_read_value(struct i2c_client *client, u8 reg)
1083{
1084 struct it87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 int res;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001086
1087 mutex_lock(&data->lock);
1088 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1089 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
1090 mutex_unlock(&data->lock);
1091
1092 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001095/* ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1097 would slow down the IT87 access and should not be necessary. */
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001098static void it87_write_value(struct i2c_client *client, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 struct it87_data *data = i2c_get_clientdata(client);
1101
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001102 mutex_lock(&data->lock);
1103 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1104 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
1105 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108/* Return 1 if and only if the PWM interface is safe to use */
1109static int it87_check_pwm(struct i2c_client *client)
1110{
1111 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1112 * and polarity set to active low is sign that this is the case so we
1113 * disable pwm control to protect the user. */
1114 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1115 if ((tmp & 0x87) == 0) {
1116 if (fix_pwm_polarity) {
1117 /* The user asks us to attempt a chip reconfiguration.
1118 * This means switching to active high polarity and
1119 * inverting all fan speed values. */
1120 int i;
1121 u8 pwm[3];
1122
1123 for (i = 0; i < 3; i++)
1124 pwm[i] = it87_read_value(client,
1125 IT87_REG_PWM(i));
1126
1127 /* If any fan is in automatic pwm mode, the polarity
1128 * might be correct, as suspicious as it seems, so we
1129 * better don't change anything (but still disable the
1130 * PWM interface). */
1131 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1132 dev_info(&client->dev, "Reconfiguring PWM to "
1133 "active high polarity\n");
1134 it87_write_value(client, IT87_REG_FAN_CTL,
1135 tmp | 0x87);
1136 for (i = 0; i < 3; i++)
1137 it87_write_value(client,
1138 IT87_REG_PWM(i),
1139 0x7f & ~pwm[i]);
1140 return 1;
1141 }
1142
1143 dev_info(&client->dev, "PWM configuration is "
1144 "too broken to be fixed\n");
1145 }
1146
1147 dev_info(&client->dev, "Detected broken BIOS "
1148 "defaults, disabling PWM interface\n");
1149 return 0;
1150 } else if (fix_pwm_polarity) {
1151 dev_info(&client->dev, "PWM configuration looks "
1152 "sane, won't touch\n");
1153 }
1154
1155 return 1;
1156}
1157
1158/* Called when we have found a new IT87. */
1159static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1160{
1161 int tmp, i;
1162
1163 /* initialize to sane defaults:
1164 * - if the chip is in manual pwm mode, this will be overwritten with
1165 * the actual settings on the chip (so in this case, initialization
1166 * is not needed)
1167 * - if in automatic or on/off mode, we could switch to manual mode,
1168 * read the registers and set manual_pwm_ctl accordingly, but currently
1169 * this is not implemented, so we initialize to something sane */
1170 for (i = 0; i < 3; i++) {
1171 data->manual_pwm_ctl[i] = 0xff;
1172 }
1173
Jean Delvarec5df9b72006-08-28 14:37:54 +02001174 /* Some chips seem to have default value 0xff for all limit
1175 * registers. For low voltage limits it makes no sense and triggers
1176 * alarms, so change to 0 instead. For high temperature limits, it
1177 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1178 * but is still confusing, so change to 127 degrees C. */
1179 for (i = 0; i < 8; i++) {
1180 tmp = it87_read_value(client, IT87_REG_VIN_MIN(i));
1181 if (tmp == 0xff)
1182 it87_write_value(client, IT87_REG_VIN_MIN(i), 0);
1183 }
1184 for (i = 0; i < 3; i++) {
1185 tmp = it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1186 if (tmp == 0xff)
1187 it87_write_value(client, IT87_REG_TEMP_HIGH(i), 127);
1188 }
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /* Check if temperature channnels are reset manually or by some reason */
1191 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1192 if ((tmp & 0x3f) == 0) {
1193 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1194 tmp = (tmp & 0xc0) | 0x2a;
1195 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1196 }
1197 data->sensor = tmp;
1198
1199 /* Check if voltage monitors are reset manually or by some reason */
1200 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1201 if ((tmp & 0xff) == 0) {
1202 /* Enable all voltage monitors */
1203 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1204 }
1205
1206 /* Check if tachometers are reset manually or by some reason */
1207 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1208 if ((data->fan_main_ctrl & 0x70) == 0) {
1209 /* Enable all fan tachometers */
1210 data->fan_main_ctrl |= 0x70;
1211 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1212 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001213 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Jean Delvare17d648b2006-08-28 14:23:46 +02001215 /* Set tachometers to 16-bit mode if needed */
Jean Delvare87673dd2006-08-28 14:37:19 +02001216 if (data->type == it8716 || data->type == it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001217 tmp = it87_read_value(client, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001218 if (~tmp & 0x07 & data->has_fan) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001219 dev_dbg(&client->dev,
1220 "Setting fan1-3 to 16-bit mode\n");
1221 it87_write_value(client, IT87_REG_FAN_16BIT,
1222 tmp | 0x07);
1223 }
1224 }
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /* Set current fan mode registers and the default settings for the
1227 * other mode registers */
1228 for (i = 0; i < 3; i++) {
1229 if (data->fan_main_ctrl & (1 << i)) {
1230 /* pwm mode */
1231 tmp = it87_read_value(client, IT87_REG_PWM(i));
1232 if (tmp & 0x80) {
1233 /* automatic pwm - not yet implemented, but
1234 * leave the settings made by the BIOS alone
1235 * until a change is requested via the sysfs
1236 * interface */
1237 } else {
1238 /* manual pwm */
1239 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1240 }
1241 }
1242 }
1243
1244 /* Start monitoring */
1245 it87_write_value(client, IT87_REG_CONFIG,
1246 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1247 | (update_vbat ? 0x41 : 0x01));
1248}
1249
1250static struct it87_data *it87_update_device(struct device *dev)
1251{
1252 struct i2c_client *client = to_i2c_client(dev);
1253 struct it87_data *data = i2c_get_clientdata(client);
1254 int i;
1255
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001256 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1259 || !data->valid) {
1260
1261 if (update_vbat) {
1262 /* Cleared after each update, so reenable. Value
1263 returned by this read will be previous value */
1264 it87_write_value(client, IT87_REG_CONFIG,
1265 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1266 }
1267 for (i = 0; i <= 7; i++) {
1268 data->in[i] =
1269 it87_read_value(client, IT87_REG_VIN(i));
1270 data->in_min[i] =
1271 it87_read_value(client, IT87_REG_VIN_MIN(i));
1272 data->in_max[i] =
1273 it87_read_value(client, IT87_REG_VIN_MAX(i));
1274 }
Jean Delvare3543a532006-08-28 14:27:25 +02001275 /* in8 (battery) has no limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 data->in[8] =
1277 it87_read_value(client, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 for (i = 0; i < 3; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02001280 /* Skip disabled fans */
1281 if (!(data->has_fan & (1 << i)))
1282 continue;
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 data->fan_min[i] =
1285 it87_read_value(client, IT87_REG_FAN_MIN(i));
Jean Delvare17d648b2006-08-28 14:23:46 +02001286 data->fan[i] = it87_read_value(client,
1287 IT87_REG_FAN(i));
1288 /* Add high byte if in 16-bit mode */
Jean Delvare87673dd2006-08-28 14:37:19 +02001289 if (data->type == it8716 || data->type == it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001290 data->fan[i] |= it87_read_value(client,
1291 IT87_REG_FANX(i)) << 8;
1292 data->fan_min[i] |= it87_read_value(client,
1293 IT87_REG_FANX_MIN(i)) << 8;
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296 for (i = 0; i < 3; i++) {
1297 data->temp[i] =
1298 it87_read_value(client, IT87_REG_TEMP(i));
1299 data->temp_high[i] =
1300 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1301 data->temp_low[i] =
1302 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1303 }
1304
Jean Delvare17d648b2006-08-28 14:23:46 +02001305 /* Newer chips don't have clock dividers */
Jean Delvare87673dd2006-08-28 14:37:19 +02001306 if ((data->has_fan & 0x07) && data->type != it8716
1307 && data->type != it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001308 i = it87_read_value(client, IT87_REG_FAN_DIV);
1309 data->fan_div[0] = i & 0x07;
1310 data->fan_div[1] = (i >> 3) & 0x07;
1311 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 data->alarms =
1315 it87_read_value(client, IT87_REG_ALARM1) |
1316 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1317 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1318 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1319
1320 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1321 /* The 8705 does not have VID capability */
Jean Delvare17d648b2006-08-28 14:23:46 +02001322 if (data->type == it8712 || data->type == it8716) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 data->vid = it87_read_value(client, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02001324 /* The older IT8712F revisions had only 5 VID pins,
1325 but we assume it is always safe to read 6 bits. */
1326 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328 data->last_updated = jiffies;
1329 data->valid = 1;
1330 }
1331
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001332 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 return data;
1335}
1336
1337static int __init sm_it87_init(void)
1338{
Jean Delvare91749992005-10-08 00:10:00 +02001339 int res;
Jean Delvarefde09502005-07-19 23:51:07 +02001340
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001341 if ((res = it87_find(&isa_address)))
Jean Delvarefde09502005-07-19 23:51:07 +02001342 return res;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001343 return i2c_isa_add_driver(&it87_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
1346static void __exit sm_it87_exit(void)
1347{
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001348 i2c_isa_del_driver(&it87_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
1351
Jean Delvareb19367c2006-08-28 14:39:26 +02001352MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>, "
1353 "Jean Delvare <khali@linux-fr.org>");
Jean Delvare87673dd2006-08-28 14:37:19 +02001354MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355module_param(update_vbat, bool, 0);
1356MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1357module_param(fix_pwm_polarity, bool, 0);
1358MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1359MODULE_LICENSE("GPL");
1360
1361module_init(sm_it87_init);
1362module_exit(sm_it87_exit);