blob: af5740d5d70fb459588f17cc32ffbcbf9d5342b9 [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
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/io.h>
41
42
43/* Addresses to scan */
Jean Delvarec5e3fbf2006-01-18 22:39:48 +010044static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
Jean Delvare91749992005-10-08 00:10:00 +020045static unsigned short isa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/* Insmod parameters */
Jean Delvare87673dd2006-08-28 14:37:19 +020048I2C_CLIENT_INSMOD_4(it87, it8712, it8716, it8718);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define REG 0x2e /* The register to read/write */
51#define DEV 0x07 /* Register: Logical device select */
52#define VAL 0x2f /* The value to read/write */
53#define PME 0x04 /* The device with the fan registers in it */
Jean Delvare87673dd2006-08-28 14:37:19 +020054#define GPIO 0x07 /* The device with the IT8718F VID value in it */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define DEVID 0x20 /* Register: Device ID */
56#define DEVREV 0x22 /* Register: Device Revision */
57
58static inline int
59superio_inb(int reg)
60{
61 outb(reg, REG);
62 return inb(VAL);
63}
64
65static int superio_inw(int reg)
66{
67 int val;
68 outb(reg++, REG);
69 val = inb(VAL) << 8;
70 outb(reg, REG);
71 val |= inb(VAL);
72 return val;
73}
74
75static inline void
Jean Delvare87673dd2006-08-28 14:37:19 +020076superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +020079 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static inline void
83superio_enter(void)
84{
85 outb(0x87, REG);
86 outb(0x01, REG);
87 outb(0x55, REG);
88 outb(0x55, REG);
89}
90
91static inline void
92superio_exit(void)
93{
94 outb(0x02, REG);
95 outb(0x02, VAL);
96}
97
Jean Delvare87673dd2006-08-28 14:37:19 +020098/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define IT8712F_DEVID 0x8712
100#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200101#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200102#define IT8718F_DEVID 0x8718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#define IT87_ACT_REG 0x30
104#define IT87_BASE_REG 0x60
105
Jean Delvare87673dd2006-08-28 14:37:19 +0200106/* Logical device 7 registers (IT8712F and later) */
107#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
108#define IT87_SIO_VID_REG 0xfc /* VID value */
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/* Update battery voltage after every reading if true */
111static int update_vbat;
112
113/* Not all BIOSes properly configure the PWM registers */
114static int fix_pwm_polarity;
115
Jean Delvare87673dd2006-08-28 14:37:19 +0200116/* Values read from Super-I/O config space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static u16 chip_type;
Jean Delvare87673dd2006-08-28 14:37:19 +0200118static u8 vid_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/* Many IT87 constants specified below */
121
122/* Length of ISA address segment */
123#define IT87_EXTENT 8
124
125/* Where are the ISA address/data registers relative to the base address */
126#define IT87_ADDR_REG_OFFSET 5
127#define IT87_DATA_REG_OFFSET 6
128
129/*----- The IT87 registers -----*/
130
131#define IT87_REG_CONFIG 0x00
132
133#define IT87_REG_ALARM1 0x01
134#define IT87_REG_ALARM2 0x02
135#define IT87_REG_ALARM3 0x03
136
Jean Delvare87673dd2006-08-28 14:37:19 +0200137/* The IT8718F has the VID value in a different register, in Super-I/O
138 configuration space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define IT87_REG_VID 0x0a
Jean Delvare17d648b2006-08-28 14:23:46 +0200140/* Warning: register 0x0b is used for something completely different in
141 new chips/revisions. I suspect only 16-bit tachometer mode will work
142 for these. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200144#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
147
148#define IT87_REG_FAN(nr) (0x0d + (nr))
149#define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
Jean Delvare17d648b2006-08-28 14:23:46 +0200150#define IT87_REG_FANX(nr) (0x18 + (nr))
151#define IT87_REG_FANX_MIN(nr) (0x1b + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define IT87_REG_FAN_MAIN_CTRL 0x13
153#define IT87_REG_FAN_CTL 0x14
154#define IT87_REG_PWM(nr) (0x15 + (nr))
155
156#define IT87_REG_VIN(nr) (0x20 + (nr))
157#define IT87_REG_TEMP(nr) (0x29 + (nr))
158
159#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
160#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
161#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
162#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
163
164#define IT87_REG_I2C_ADDR 0x48
165
166#define IT87_REG_VIN_ENABLE 0x50
167#define IT87_REG_TEMP_ENABLE 0x51
168
169#define IT87_REG_CHIPID 0x58
170
171#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
172#define IN_FROM_REG(val) ((val) * 16)
173
174static inline u8 FAN_TO_REG(long rpm, int div)
175{
176 if (rpm == 0)
177 return 255;
178 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
179 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
180 254);
181}
182
Jean Delvare17d648b2006-08-28 14:23:46 +0200183static inline u16 FAN16_TO_REG(long rpm)
184{
185 if (rpm == 0)
186 return 0xffff;
187 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
Jean Delvare17d648b2006-08-28 14:23:46 +0200191/* The divider is fixed to 2 in 16-bit mode */
192#define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
195 ((val)+500)/1000),-128,127))
196#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#define PWM_TO_REG(val) ((val) >> 1)
199#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
200
201static int DIV_TO_REG(int val)
202{
203 int answer = 0;
Jean Delvareb9e349f2006-08-28 14:26:22 +0200204 while (answer < 7 && (val >>= 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 answer++;
206 return answer;
207}
208#define DIV_FROM_REG(val) (1 << (val))
209
210
211/* For each registered IT87, we need to keep some data in memory. That
212 data is pointed to by it87_list[NR]->data. The structure itself is
213 dynamically allocated, at the same time when a new it87 client is
214 allocated. */
215struct it87_data {
216 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400217 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100218 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 enum chips type;
220
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100221 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 char valid; /* !=0 if following fields are valid */
223 unsigned long last_updated; /* In jiffies */
224
225 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200226 u8 in_max[8]; /* Register value */
227 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200228 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +0200229 u16 fan[3]; /* Register values, possibly combined */
230 u16 fan_min[3]; /* Register values, possibly combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 u8 temp[3]; /* Register value */
232 u8 temp_high[3]; /* Register value */
233 u8 temp_low[3]; /* Register value */
234 u8 sensor; /* Register value */
235 u8 fan_div[3]; /* Register encoding, shifted right */
236 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100237 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 u32 alarms; /* Register encoding, combined */
239 u8 fan_main_ctrl; /* Register value */
240 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
241};
242
243
244static int it87_attach_adapter(struct i2c_adapter *adapter);
Jean Delvare2d8672c2005-07-19 23:56:35 +0200245static int it87_isa_attach_adapter(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
247static int it87_detach_client(struct i2c_client *client);
248
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100249static int it87_read_value(struct i2c_client *client, u8 reg);
250static int it87_write_value(struct i2c_client *client, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static struct it87_data *it87_update_device(struct device *dev);
252static int it87_check_pwm(struct i2c_client *client);
253static void it87_init_client(struct i2c_client *client, struct it87_data *data);
254
255
256static struct i2c_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100257 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100258 .name = "it87",
259 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .id = I2C_DRIVERID_IT87,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .attach_adapter = it87_attach_adapter,
262 .detach_client = it87_detach_client,
263};
264
Jean Delvarefde09502005-07-19 23:51:07 +0200265static struct i2c_driver it87_isa_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100266 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200267 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100268 .name = "it87-isa",
269 },
Jean Delvare2d8672c2005-07-19 23:56:35 +0200270 .attach_adapter = it87_isa_attach_adapter,
Jean Delvarefde09502005-07-19 23:51:07 +0200271 .detach_client = it87_detach_client,
272};
273
274
Jean Delvare20ad93d2005-06-05 11:53:25 +0200275static ssize_t show_in(struct device *dev, struct device_attribute *attr,
276 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200278 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
279 int nr = sensor_attr->index;
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct it87_data *data = it87_update_device(dev);
282 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
283}
284
Jean Delvare20ad93d2005-06-05 11:53:25 +0200285static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
286 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200288 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
289 int nr = sensor_attr->index;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 struct it87_data *data = it87_update_device(dev);
292 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
293}
294
Jean Delvare20ad93d2005-06-05 11:53:25 +0200295static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
296 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200298 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
299 int nr = sensor_attr->index;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 struct it87_data *data = it87_update_device(dev);
302 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
303}
304
Jean Delvare20ad93d2005-06-05 11:53:25 +0200305static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
306 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200308 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
309 int nr = sensor_attr->index;
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct i2c_client *client = to_i2c_client(dev);
312 struct it87_data *data = i2c_get_clientdata(client);
313 unsigned long val = simple_strtoul(buf, NULL, 10);
314
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100315 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 data->in_min[nr] = IN_TO_REG(val);
317 it87_write_value(client, IT87_REG_VIN_MIN(nr),
318 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100319 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return count;
321}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200322static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
323 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200325 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
326 int nr = sensor_attr->index;
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct i2c_client *client = to_i2c_client(dev);
329 struct it87_data *data = i2c_get_clientdata(client);
330 unsigned long val = simple_strtoul(buf, NULL, 10);
331
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100332 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 data->in_max[nr] = IN_TO_REG(val);
334 it87_write_value(client, IT87_REG_VIN_MAX(nr),
335 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100336 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return count;
338}
339
340#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200341static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
342 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200345static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
346 show_in_min, set_in_min, offset); \
347static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
348 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350show_in_offset(0);
351limit_in_offset(0);
352show_in_offset(1);
353limit_in_offset(1);
354show_in_offset(2);
355limit_in_offset(2);
356show_in_offset(3);
357limit_in_offset(3);
358show_in_offset(4);
359limit_in_offset(4);
360show_in_offset(5);
361limit_in_offset(5);
362show_in_offset(6);
363limit_in_offset(6);
364show_in_offset(7);
365limit_in_offset(7);
366show_in_offset(8);
367
368/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200369static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
370 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200372 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
373 int nr = sensor_attr->index;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct it87_data *data = it87_update_device(dev);
376 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
377}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200378static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
379 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200381 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
382 int nr = sensor_attr->index;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct it87_data *data = it87_update_device(dev);
385 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
386}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200387static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
388 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200390 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
391 int nr = sensor_attr->index;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct it87_data *data = it87_update_device(dev);
394 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
395}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200396static ssize_t set_temp_max(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_high[nr] = TEMP_TO_REG(val);
408 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[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}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200412static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
413 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200415 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
416 int nr = sensor_attr->index;
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct i2c_client *client = to_i2c_client(dev);
419 struct it87_data *data = i2c_get_clientdata(client);
420 int val = simple_strtol(buf, NULL, 10);
421
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100422 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 data->temp_low[nr] = TEMP_TO_REG(val);
424 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100425 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return count;
427}
428#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200429static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
430 show_temp, NULL, offset - 1); \
431static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
432 show_temp_max, set_temp_max, offset - 1); \
433static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
434 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436show_temp_offset(1);
437show_temp_offset(2);
438show_temp_offset(3);
439
Jean Delvare20ad93d2005-06-05 11:53:25 +0200440static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
441 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200443 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
444 int nr = sensor_attr->index;
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct it87_data *data = it87_update_device(dev);
447 u8 reg = data->sensor; /* In case the value is updated while we use it */
448
449 if (reg & (1 << nr))
450 return sprintf(buf, "3\n"); /* thermal diode */
451 if (reg & (8 << nr))
452 return sprintf(buf, "2\n"); /* thermistor */
453 return sprintf(buf, "0\n"); /* disabled */
454}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200455static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
456 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200458 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
459 int nr = sensor_attr->index;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct i2c_client *client = to_i2c_client(dev);
462 struct it87_data *data = i2c_get_clientdata(client);
463 int val = simple_strtol(buf, NULL, 10);
464
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100465 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 data->sensor &= ~(1 << nr);
468 data->sensor &= ~(8 << nr);
469 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
470 if (val == 3)
471 data->sensor |= 1 << nr;
472 else if (val == 2)
473 data->sensor |= 8 << nr;
474 else if (val != 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100475 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -EINVAL;
477 }
478 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100479 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return count;
481}
482#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200483static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
484 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486show_sensor_offset(1);
487show_sensor_offset(2);
488show_sensor_offset(3);
489
490/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200491static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
492 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200494 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
495 int nr = sensor_attr->index;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct it87_data *data = it87_update_device(dev);
498 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
499 DIV_FROM_REG(data->fan_div[nr])));
500}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200501static ssize_t show_fan_min(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",
509 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
510}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200511static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
512 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200514 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
515 int nr = sensor_attr->index;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 struct it87_data *data = it87_update_device(dev);
518 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
519}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200520static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
521 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200523 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
524 int nr = sensor_attr->index;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 struct it87_data *data = it87_update_device(dev);
527 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
528}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200529static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
530 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200532 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
533 int nr = sensor_attr->index;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 struct it87_data *data = it87_update_device(dev);
536 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
537}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200538static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
539 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200541 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
542 int nr = sensor_attr->index;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct i2c_client *client = to_i2c_client(dev);
545 struct it87_data *data = i2c_get_clientdata(client);
546 int val = simple_strtol(buf, NULL, 10);
Jean Delvare07eab462005-11-23 15:44:31 -0800547 u8 reg = it87_read_value(client, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100549 mutex_lock(&data->update_lock);
Jean Delvare07eab462005-11-23 15:44:31 -0800550 switch (nr) {
551 case 0: data->fan_div[nr] = reg & 0x07; break;
552 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
553 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
554 }
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
557 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100558 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return count;
560}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200561static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
562 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200564 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
565 int nr = sensor_attr->index;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct i2c_client *client = to_i2c_client(dev);
568 struct it87_data *data = i2c_get_clientdata(client);
Jean Delvareb9e349f2006-08-28 14:26:22 +0200569 unsigned long val = simple_strtoul(buf, NULL, 10);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200570 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 u8 old;
572
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100573 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 old = it87_read_value(client, IT87_REG_FAN_DIV);
575
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200576 /* Save fan min limit */
577 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 switch (nr) {
580 case 0:
581 case 1:
582 data->fan_div[nr] = DIV_TO_REG(val);
583 break;
584 case 2:
585 if (val < 8)
586 data->fan_div[nr] = 1;
587 else
588 data->fan_div[nr] = 3;
589 }
590 val = old & 0x80;
591 val |= (data->fan_div[0] & 0x07);
592 val |= (data->fan_div[1] & 0x07) << 3;
593 if (data->fan_div[2] == 3)
594 val |= 0x1 << 6;
595 it87_write_value(client, IT87_REG_FAN_DIV, val);
596
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200597 /* Restore fan min limit */
598 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
599 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
600
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100601 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return count;
603}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200604static ssize_t set_pwm_enable(struct device *dev,
605 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200607 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
608 int nr = sensor_attr->index;
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct i2c_client *client = to_i2c_client(dev);
611 struct it87_data *data = i2c_get_clientdata(client);
612 int val = simple_strtol(buf, NULL, 10);
613
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100614 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (val == 0) {
617 int tmp;
618 /* make sure the fan is on when in on/off mode */
619 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
620 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
621 /* set on/off mode */
622 data->fan_main_ctrl &= ~(1 << nr);
623 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
624 } else if (val == 1) {
625 /* set SmartGuardian mode */
626 data->fan_main_ctrl |= (1 << nr);
627 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
628 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
629 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
630 } else {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100631 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -EINVAL;
633 }
634
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100635 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return count;
637}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200638static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
639 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200641 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
642 int nr = sensor_attr->index;
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct i2c_client *client = to_i2c_client(dev);
645 struct it87_data *data = i2c_get_clientdata(client);
646 int val = simple_strtol(buf, NULL, 10);
647
648 if (val < 0 || val > 255)
649 return -EINVAL;
650
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100651 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 data->manual_pwm_ctl[nr] = val;
653 if (data->fan_main_ctrl & (1 << nr))
654 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100655 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return count;
657}
658
Jean Delvare20ad93d2005-06-05 11:53:25 +0200659#define show_fan_offset(offset) \
660static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
661 show_fan, NULL, offset - 1); \
662static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
663 show_fan_min, set_fan_min, offset - 1); \
664static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
665 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667show_fan_offset(1);
668show_fan_offset(2);
669show_fan_offset(3);
670
671#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200672static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
673 show_pwm_enable, set_pwm_enable, offset - 1); \
674static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
675 show_pwm, set_pwm, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677show_pwm_offset(1);
678show_pwm_offset(2);
679show_pwm_offset(3);
680
Jean Delvare17d648b2006-08-28 14:23:46 +0200681/* A different set of callbacks for 16-bit fans */
682static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
683 char *buf)
684{
685 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
686 int nr = sensor_attr->index;
687 struct it87_data *data = it87_update_device(dev);
688 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
689}
690
691static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
692 char *buf)
693{
694 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
695 int nr = sensor_attr->index;
696 struct it87_data *data = it87_update_device(dev);
697 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
698}
699
700static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
701 const char *buf, size_t count)
702{
703 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
704 int nr = sensor_attr->index;
705 struct i2c_client *client = to_i2c_client(dev);
706 struct it87_data *data = i2c_get_clientdata(client);
707 int val = simple_strtol(buf, NULL, 10);
708
709 mutex_lock(&data->update_lock);
710 data->fan_min[nr] = FAN16_TO_REG(val);
711 it87_write_value(client, IT87_REG_FAN_MIN(nr),
712 data->fan_min[nr] & 0xff);
713 it87_write_value(client, IT87_REG_FANX_MIN(nr),
714 data->fan_min[nr] >> 8);
715 mutex_unlock(&data->update_lock);
716 return count;
717}
718
719/* We want to use the same sysfs file names as 8-bit fans, but we need
720 different variable names, so we have to use SENSOR_ATTR instead of
721 SENSOR_DEVICE_ATTR. */
722#define show_fan16_offset(offset) \
723static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
724 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
725 show_fan16, NULL, offset - 1); \
726static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
727 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
728 show_fan16_min, set_fan16_min, offset - 1)
729
730show_fan16_offset(1);
731show_fan16_offset(2);
732show_fan16_offset(3);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400735static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200738 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
Jean Delvare1d66c642005-04-18 21:16:59 -0700740static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400743show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct it87_data *data = it87_update_device(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +0100746 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400749store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 struct i2c_client *client = to_i2c_client(dev);
752 struct it87_data *data = i2c_get_clientdata(client);
753 u32 val;
754
755 val = simple_strtoul(buf, NULL, 10);
756 data->vrm = val;
757
758 return count;
759}
760static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
761#define device_create_file_vrm(client) \
762device_create_file(&client->dev, &dev_attr_vrm)
763
764static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400765show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 struct it87_data *data = it87_update_device(dev);
768 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
769}
770static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
771#define device_create_file_vid(client) \
772device_create_file(&client->dev, &dev_attr_cpu0_vid)
773
774/* This function is called when:
775 * it87_driver is inserted (when this module is loaded), for each
776 available adapter
777 * when a new adapter is inserted (and it87_driver is still present) */
778static int it87_attach_adapter(struct i2c_adapter *adapter)
779{
780 if (!(adapter->class & I2C_CLASS_HWMON))
781 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200782 return i2c_probe(adapter, &addr_data, it87_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Jean Delvare2d8672c2005-07-19 23:56:35 +0200785static int it87_isa_attach_adapter(struct i2c_adapter *adapter)
786{
787 return it87_detect(adapter, isa_address, -1);
788}
789
790/* SuperIO detection - will change isa_address if a chip is found */
Jean Delvare91749992005-10-08 00:10:00 +0200791static int __init it87_find(unsigned short *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 int err = -ENODEV;
794
795 superio_enter();
796 chip_type = superio_inw(DEVID);
797 if (chip_type != IT8712F_DEVID
Jean Delvare17d648b2006-08-28 14:23:46 +0200798 && chip_type != IT8716F_DEVID
Jean Delvare87673dd2006-08-28 14:37:19 +0200799 && chip_type != IT8718F_DEVID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 && chip_type != IT8705F_DEVID)
801 goto exit;
802
Jean Delvare87673dd2006-08-28 14:37:19 +0200803 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
805 pr_info("it87: Device not activated, skipping\n");
806 goto exit;
807 }
808
809 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
810 if (*address == 0) {
811 pr_info("it87: Base address not set, skipping\n");
812 goto exit;
813 }
814
815 err = 0;
816 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
817 chip_type, *address, superio_inb(DEVREV) & 0x0f);
818
Jean Delvare87673dd2006-08-28 14:37:19 +0200819 /* Read GPIO config and VID value from LDN 7 (GPIO) */
820 if (chip_type != IT8705F_DEVID) {
821 int reg;
822
823 superio_select(GPIO);
824 if (chip_type == it8718)
825 vid_value = superio_inb(IT87_SIO_VID_REG);
826
827 reg = superio_inb(IT87_SIO_PINX2_REG);
828 if (reg & (1 << 0))
829 pr_info("it87: in3 is VCC (+5V)\n");
830 if (reg & (1 << 1))
831 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
832 }
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834exit:
835 superio_exit();
836 return err;
837}
838
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200839/* This function is called by i2c_probe */
Ben Dooksc49efce2005-10-26 21:07:25 +0200840static int it87_detect(struct i2c_adapter *adapter, int address, int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 int i;
843 struct i2c_client *new_client;
844 struct it87_data *data;
845 int err = 0;
846 const char *name = "";
847 int is_isa = i2c_is_isa_adapter(adapter);
848 int enable_pwm_interface;
849
850 if (!is_isa &&
851 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
852 goto ERROR0;
853
854 /* Reserve the ISA region */
855 if (is_isa)
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100856 if (!request_region(address, IT87_EXTENT,
857 it87_isa_driver.driver.name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 goto ERROR0;
859
Jean Delvare91749992005-10-08 00:10:00 +0200860 /* For now, we presume we have a valid client. We create the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 client structure, even though we cannot fill it completely yet.
862 But it allows us to access it87_{read,write}_value. */
863
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200864 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 err = -ENOMEM;
866 goto ERROR1;
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 new_client = &data->client;
870 if (is_isa)
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100871 mutex_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 i2c_set_clientdata(new_client, data);
873 new_client->addr = address;
874 new_client->adapter = adapter;
Jean Delvarefde09502005-07-19 23:51:07 +0200875 new_client->driver = is_isa ? &it87_isa_driver : &it87_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 new_client->flags = 0;
877
878 /* Now, we do the remaining detection. */
879
880 if (kind < 0) {
881 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
882 || (!is_isa
883 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
884 err = -ENODEV;
885 goto ERROR2;
886 }
887 }
888
889 /* Determine the chip type. */
890 if (kind <= 0) {
891 i = it87_read_value(new_client, IT87_REG_CHIPID);
892 if (i == 0x90) {
893 kind = it87;
Jean Delvare17d648b2006-08-28 14:23:46 +0200894 if (is_isa) {
895 switch (chip_type) {
896 case IT8712F_DEVID:
897 kind = it8712;
898 break;
899 case IT8716F_DEVID:
900 kind = it8716;
901 break;
Jean Delvare87673dd2006-08-28 14:37:19 +0200902 case IT8718F_DEVID:
903 kind = it8718;
904 break;
Jean Delvare17d648b2006-08-28 14:23:46 +0200905 }
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908 else {
909 if (kind == 0)
910 dev_info(&adapter->dev,
911 "Ignoring 'force' parameter for unknown chip at "
912 "adapter %d, address 0x%02x\n",
913 i2c_adapter_id(adapter), address);
914 err = -ENODEV;
915 goto ERROR2;
916 }
917 }
918
919 if (kind == it87) {
920 name = "it87";
921 } else if (kind == it8712) {
922 name = "it8712";
Jean Delvare17d648b2006-08-28 14:23:46 +0200923 } else if (kind == it8716) {
924 name = "it8716";
Jean Delvare87673dd2006-08-28 14:37:19 +0200925 } else if (kind == it8718) {
926 name = "it8718";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
929 /* Fill in the remaining client fields and put it into the global list */
930 strlcpy(new_client->name, name, I2C_NAME_SIZE);
931 data->type = kind;
932 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100933 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* Tell the I2C layer a new client has arrived */
936 if ((err = i2c_attach_client(new_client)))
937 goto ERROR2;
938
Jean Delvarec5e3fbf2006-01-18 22:39:48 +0100939 if (!is_isa)
940 dev_info(&new_client->dev, "The I2C interface to IT87xxF "
941 "hardware monitoring chips is deprecated. Please "
942 "report if you still rely on it.\n");
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /* Check PWM configuration */
945 enable_pwm_interface = it87_check_pwm(new_client);
946
947 /* Initialize the IT87 chip */
948 it87_init_client(new_client, data);
949
950 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400951 data->class_dev = hwmon_device_register(&new_client->dev);
952 if (IS_ERR(data->class_dev)) {
953 err = PTR_ERR(data->class_dev);
954 goto ERROR3;
955 }
956
Jean Delvare20ad93d2005-06-05 11:53:25 +0200957 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
958 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
959 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
960 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
961 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
962 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
963 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
964 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
965 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
966 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
967 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
968 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
969 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
970 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
971 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
972 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
973 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
974 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
975 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
976 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
977 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
978 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
979 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
980 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
981 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
982 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
983 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
984 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
985 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
986 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
987 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
988 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
989 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
990 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
991 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
992 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
993 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
Jean Delvare17d648b2006-08-28 14:23:46 +0200994
Jean Delvare9060f8b2006-08-28 14:24:17 +0200995 /* Do not create fan files for disabled fans */
Jean Delvare87673dd2006-08-28 14:37:19 +0200996 if (data->type == it8716 || data->type == it8718) {
997 /* 16-bit tachometers */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200998 if (data->has_fan & (1 << 0)) {
999 device_create_file(&new_client->dev,
1000 &sensor_dev_attr_fan1_input16.dev_attr);
1001 device_create_file(&new_client->dev,
1002 &sensor_dev_attr_fan1_min16.dev_attr);
1003 }
1004 if (data->has_fan & (1 << 1)) {
1005 device_create_file(&new_client->dev,
1006 &sensor_dev_attr_fan2_input16.dev_attr);
1007 device_create_file(&new_client->dev,
1008 &sensor_dev_attr_fan2_min16.dev_attr);
1009 }
1010 if (data->has_fan & (1 << 2)) {
1011 device_create_file(&new_client->dev,
1012 &sensor_dev_attr_fan3_input16.dev_attr);
1013 device_create_file(&new_client->dev,
1014 &sensor_dev_attr_fan3_min16.dev_attr);
1015 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001016 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001017 /* 8-bit tachometers with clock divider */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001018 if (data->has_fan & (1 << 0)) {
1019 device_create_file(&new_client->dev,
1020 &sensor_dev_attr_fan1_input.dev_attr);
1021 device_create_file(&new_client->dev,
1022 &sensor_dev_attr_fan1_min.dev_attr);
1023 device_create_file(&new_client->dev,
1024 &sensor_dev_attr_fan1_div.dev_attr);
1025 }
1026 if (data->has_fan & (1 << 1)) {
1027 device_create_file(&new_client->dev,
1028 &sensor_dev_attr_fan2_input.dev_attr);
1029 device_create_file(&new_client->dev,
1030 &sensor_dev_attr_fan2_min.dev_attr);
1031 device_create_file(&new_client->dev,
1032 &sensor_dev_attr_fan2_div.dev_attr);
1033 }
1034 if (data->has_fan & (1 << 2)) {
1035 device_create_file(&new_client->dev,
1036 &sensor_dev_attr_fan3_input.dev_attr);
1037 device_create_file(&new_client->dev,
1038 &sensor_dev_attr_fan3_min.dev_attr);
1039 device_create_file(&new_client->dev,
1040 &sensor_dev_attr_fan3_div.dev_attr);
1041 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001042 }
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 device_create_file(&new_client->dev, &dev_attr_alarms);
1045 if (enable_pwm_interface) {
Jean Delvare20ad93d2005-06-05 11:53:25 +02001046 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
1047 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
1048 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
1049 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
1050 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
1051 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
Jean Delvare87673dd2006-08-28 14:37:19 +02001054 if (data->type == it8712 || data->type == it8716
1055 || data->type == it8718) {
Jean Delvare303760b2005-07-31 21:52:01 +02001056 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001057 /* VID reading from Super-I/O config space if available */
1058 data->vid = vid_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 device_create_file_vrm(new_client);
1060 device_create_file_vid(new_client);
1061 }
1062
1063 return 0;
1064
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001065ERROR3:
1066 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067ERROR2:
1068 kfree(data);
1069ERROR1:
1070 if (is_isa)
1071 release_region(address, IT87_EXTENT);
1072ERROR0:
1073 return err;
1074}
1075
1076static int it87_detach_client(struct i2c_client *client)
1077{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001078 struct it87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 int err;
1080
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001081 hwmon_device_unregister(data->class_dev);
1082
Jean Delvare7bef5592005-07-27 22:14:49 +02001083 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 if(i2c_is_isa_client(client))
1087 release_region(client->addr, IT87_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001088 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 return 0;
1091}
1092
Steven Cole44bbe872005-05-03 18:21:25 -06001093/* The SMBus locks itself, but ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 We don't want to lock the whole ISA bus, so we lock each client
1095 separately.
1096 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. */
1098static int it87_read_value(struct i2c_client *client, u8 reg)
1099{
1100 struct it87_data *data = i2c_get_clientdata(client);
1101
1102 int res;
1103 if (i2c_is_isa_client(client)) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001104 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1106 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001107 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return res;
1109 } else
1110 return i2c_smbus_read_byte_data(client, reg);
1111}
1112
Steven Cole44bbe872005-05-03 18:21:25 -06001113/* The SMBus locks itself, but ISA access muse be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 We don't want to lock the whole ISA bus, so we lock each client
1115 separately.
1116 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1117 would slow down the IT87 access and should not be necessary. */
1118static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
1119{
1120 struct it87_data *data = i2c_get_clientdata(client);
1121
1122 if (i2c_is_isa_client(client)) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001123 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1125 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001126 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return 0;
1128 } else
1129 return i2c_smbus_write_byte_data(client, reg, value);
1130}
1131
1132/* Return 1 if and only if the PWM interface is safe to use */
1133static int it87_check_pwm(struct i2c_client *client)
1134{
1135 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1136 * and polarity set to active low is sign that this is the case so we
1137 * disable pwm control to protect the user. */
1138 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1139 if ((tmp & 0x87) == 0) {
1140 if (fix_pwm_polarity) {
1141 /* The user asks us to attempt a chip reconfiguration.
1142 * This means switching to active high polarity and
1143 * inverting all fan speed values. */
1144 int i;
1145 u8 pwm[3];
1146
1147 for (i = 0; i < 3; i++)
1148 pwm[i] = it87_read_value(client,
1149 IT87_REG_PWM(i));
1150
1151 /* If any fan is in automatic pwm mode, the polarity
1152 * might be correct, as suspicious as it seems, so we
1153 * better don't change anything (but still disable the
1154 * PWM interface). */
1155 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1156 dev_info(&client->dev, "Reconfiguring PWM to "
1157 "active high polarity\n");
1158 it87_write_value(client, IT87_REG_FAN_CTL,
1159 tmp | 0x87);
1160 for (i = 0; i < 3; i++)
1161 it87_write_value(client,
1162 IT87_REG_PWM(i),
1163 0x7f & ~pwm[i]);
1164 return 1;
1165 }
1166
1167 dev_info(&client->dev, "PWM configuration is "
1168 "too broken to be fixed\n");
1169 }
1170
1171 dev_info(&client->dev, "Detected broken BIOS "
1172 "defaults, disabling PWM interface\n");
1173 return 0;
1174 } else if (fix_pwm_polarity) {
1175 dev_info(&client->dev, "PWM configuration looks "
1176 "sane, won't touch\n");
1177 }
1178
1179 return 1;
1180}
1181
1182/* Called when we have found a new IT87. */
1183static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1184{
1185 int tmp, i;
1186
1187 /* initialize to sane defaults:
1188 * - if the chip is in manual pwm mode, this will be overwritten with
1189 * the actual settings on the chip (so in this case, initialization
1190 * is not needed)
1191 * - if in automatic or on/off mode, we could switch to manual mode,
1192 * read the registers and set manual_pwm_ctl accordingly, but currently
1193 * this is not implemented, so we initialize to something sane */
1194 for (i = 0; i < 3; i++) {
1195 data->manual_pwm_ctl[i] = 0xff;
1196 }
1197
Jean Delvarec5df9b72006-08-28 14:37:54 +02001198 /* Some chips seem to have default value 0xff for all limit
1199 * registers. For low voltage limits it makes no sense and triggers
1200 * alarms, so change to 0 instead. For high temperature limits, it
1201 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1202 * but is still confusing, so change to 127 degrees C. */
1203 for (i = 0; i < 8; i++) {
1204 tmp = it87_read_value(client, IT87_REG_VIN_MIN(i));
1205 if (tmp == 0xff)
1206 it87_write_value(client, IT87_REG_VIN_MIN(i), 0);
1207 }
1208 for (i = 0; i < 3; i++) {
1209 tmp = it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1210 if (tmp == 0xff)
1211 it87_write_value(client, IT87_REG_TEMP_HIGH(i), 127);
1212 }
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 /* Check if temperature channnels are reset manually or by some reason */
1215 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1216 if ((tmp & 0x3f) == 0) {
1217 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1218 tmp = (tmp & 0xc0) | 0x2a;
1219 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1220 }
1221 data->sensor = tmp;
1222
1223 /* Check if voltage monitors are reset manually or by some reason */
1224 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1225 if ((tmp & 0xff) == 0) {
1226 /* Enable all voltage monitors */
1227 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1228 }
1229
1230 /* Check if tachometers are reset manually or by some reason */
1231 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1232 if ((data->fan_main_ctrl & 0x70) == 0) {
1233 /* Enable all fan tachometers */
1234 data->fan_main_ctrl |= 0x70;
1235 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1236 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001237 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Jean Delvare17d648b2006-08-28 14:23:46 +02001239 /* Set tachometers to 16-bit mode if needed */
Jean Delvare87673dd2006-08-28 14:37:19 +02001240 if (data->type == it8716 || data->type == it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001241 tmp = it87_read_value(client, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001242 if (~tmp & 0x07 & data->has_fan) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001243 dev_dbg(&client->dev,
1244 "Setting fan1-3 to 16-bit mode\n");
1245 it87_write_value(client, IT87_REG_FAN_16BIT,
1246 tmp | 0x07);
1247 }
1248 }
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /* Set current fan mode registers and the default settings for the
1251 * other mode registers */
1252 for (i = 0; i < 3; i++) {
1253 if (data->fan_main_ctrl & (1 << i)) {
1254 /* pwm mode */
1255 tmp = it87_read_value(client, IT87_REG_PWM(i));
1256 if (tmp & 0x80) {
1257 /* automatic pwm - not yet implemented, but
1258 * leave the settings made by the BIOS alone
1259 * until a change is requested via the sysfs
1260 * interface */
1261 } else {
1262 /* manual pwm */
1263 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1264 }
1265 }
1266 }
1267
1268 /* Start monitoring */
1269 it87_write_value(client, IT87_REG_CONFIG,
1270 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1271 | (update_vbat ? 0x41 : 0x01));
1272}
1273
1274static struct it87_data *it87_update_device(struct device *dev)
1275{
1276 struct i2c_client *client = to_i2c_client(dev);
1277 struct it87_data *data = i2c_get_clientdata(client);
1278 int i;
1279
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001280 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1283 || !data->valid) {
1284
1285 if (update_vbat) {
1286 /* Cleared after each update, so reenable. Value
1287 returned by this read will be previous value */
1288 it87_write_value(client, IT87_REG_CONFIG,
1289 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1290 }
1291 for (i = 0; i <= 7; i++) {
1292 data->in[i] =
1293 it87_read_value(client, IT87_REG_VIN(i));
1294 data->in_min[i] =
1295 it87_read_value(client, IT87_REG_VIN_MIN(i));
1296 data->in_max[i] =
1297 it87_read_value(client, IT87_REG_VIN_MAX(i));
1298 }
Jean Delvare3543a532006-08-28 14:27:25 +02001299 /* in8 (battery) has no limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 data->in[8] =
1301 it87_read_value(client, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 for (i = 0; i < 3; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02001304 /* Skip disabled fans */
1305 if (!(data->has_fan & (1 << i)))
1306 continue;
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 data->fan_min[i] =
1309 it87_read_value(client, IT87_REG_FAN_MIN(i));
Jean Delvare17d648b2006-08-28 14:23:46 +02001310 data->fan[i] = it87_read_value(client,
1311 IT87_REG_FAN(i));
1312 /* Add high byte if in 16-bit mode */
Jean Delvare87673dd2006-08-28 14:37:19 +02001313 if (data->type == it8716 || data->type == it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001314 data->fan[i] |= it87_read_value(client,
1315 IT87_REG_FANX(i)) << 8;
1316 data->fan_min[i] |= it87_read_value(client,
1317 IT87_REG_FANX_MIN(i)) << 8;
1318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320 for (i = 0; i < 3; i++) {
1321 data->temp[i] =
1322 it87_read_value(client, IT87_REG_TEMP(i));
1323 data->temp_high[i] =
1324 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1325 data->temp_low[i] =
1326 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1327 }
1328
Jean Delvare17d648b2006-08-28 14:23:46 +02001329 /* Newer chips don't have clock dividers */
Jean Delvare87673dd2006-08-28 14:37:19 +02001330 if ((data->has_fan & 0x07) && data->type != it8716
1331 && data->type != it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001332 i = it87_read_value(client, IT87_REG_FAN_DIV);
1333 data->fan_div[0] = i & 0x07;
1334 data->fan_div[1] = (i >> 3) & 0x07;
1335 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 data->alarms =
1339 it87_read_value(client, IT87_REG_ALARM1) |
1340 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1341 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1342 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1343
1344 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1345 /* The 8705 does not have VID capability */
Jean Delvare17d648b2006-08-28 14:23:46 +02001346 if (data->type == it8712 || data->type == it8716) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 data->vid = it87_read_value(client, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02001348 /* The older IT8712F revisions had only 5 VID pins,
1349 but we assume it is always safe to read 6 bits. */
1350 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 data->last_updated = jiffies;
1353 data->valid = 1;
1354 }
1355
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001356 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 return data;
1359}
1360
1361static int __init sm_it87_init(void)
1362{
Jean Delvare91749992005-10-08 00:10:00 +02001363 int res;
Jean Delvarefde09502005-07-19 23:51:07 +02001364
1365 res = i2c_add_driver(&it87_driver);
1366 if (res)
1367 return res;
1368
Jean Delvare91749992005-10-08 00:10:00 +02001369 if (!it87_find(&isa_address)) {
1370 res = i2c_isa_add_driver(&it87_isa_driver);
1371 if (res) {
1372 i2c_del_driver(&it87_driver);
1373 return res;
1374 }
Jean Delvarefde09502005-07-19 23:51:07 +02001375 }
1376
1377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
1380static void __exit sm_it87_exit(void)
1381{
Jean Delvarebe79c382006-02-07 17:53:32 +01001382 if (isa_address)
1383 i2c_isa_del_driver(&it87_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 i2c_del_driver(&it87_driver);
1385}
1386
1387
Jean Delvareb19367c2006-08-28 14:39:26 +02001388MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>, "
1389 "Jean Delvare <khali@linux-fr.org>");
Jean Delvare87673dd2006-08-28 14:37:19 +02001390MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391module_param(update_vbat, bool, 0);
1392MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1393module_param(fix_pwm_polarity, bool, 0);
1394MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1395MODULE_LICENSE("GPL");
1396
1397module_init(sm_it87_init);
1398module_exit(sm_it87_exit);