blob: bb16668ad2bdb8eea52063beabf55077bda0cf30 [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
Jean Delvaref8d0c192007-02-14 21:15:02 +0100205static const unsigned int pwm_freq[8] = {
206 48000000 / 128,
207 24000000 / 128,
208 12000000 / 128,
209 8000000 / 128,
210 6000000 / 128,
211 3000000 / 128,
212 1500000 / 128,
213 750000 / 128,
214};
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217/* For each registered IT87, we need to keep some data in memory. That
218 data is pointed to by it87_list[NR]->data. The structure itself is
219 dynamically allocated, at the same time when a new it87 client is
220 allocated. */
221struct it87_data {
222 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400223 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100224 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 enum chips type;
226
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100227 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 char valid; /* !=0 if following fields are valid */
229 unsigned long last_updated; /* In jiffies */
230
231 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200232 u8 in_max[8]; /* Register value */
233 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200234 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +0200235 u16 fan[3]; /* Register values, possibly combined */
236 u16 fan_min[3]; /* Register values, possibly combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 u8 temp[3]; /* Register value */
238 u8 temp_high[3]; /* Register value */
239 u8 temp_low[3]; /* Register value */
240 u8 sensor; /* Register value */
241 u8 fan_div[3]; /* Register encoding, shifted right */
242 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100243 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 u32 alarms; /* Register encoding, combined */
245 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100246 u8 fan_ctl; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
248};
249
250
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100251static int it87_detect(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static int it87_detach_client(struct i2c_client *client);
253
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100254static int it87_read_value(struct i2c_client *client, u8 reg);
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100255static void it87_write_value(struct i2c_client *client, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static struct it87_data *it87_update_device(struct device *dev);
257static int it87_check_pwm(struct i2c_client *client);
258static void it87_init_client(struct i2c_client *client, struct it87_data *data);
259
260
Jean Delvarefde09502005-07-19 23:51:07 +0200261static struct i2c_driver it87_isa_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100262 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200263 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100264 .name = "it87-isa",
265 },
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100266 .attach_adapter = it87_detect,
Jean Delvarefde09502005-07-19 23:51:07 +0200267 .detach_client = it87_detach_client,
268};
269
270
Jean Delvare20ad93d2005-06-05 11:53:25 +0200271static ssize_t show_in(struct device *dev, struct device_attribute *attr,
272 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200274 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
275 int nr = sensor_attr->index;
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct it87_data *data = it87_update_device(dev);
278 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
279}
280
Jean Delvare20ad93d2005-06-05 11:53:25 +0200281static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
282 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200284 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
285 int nr = sensor_attr->index;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 struct it87_data *data = it87_update_device(dev);
288 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
289}
290
Jean Delvare20ad93d2005-06-05 11:53:25 +0200291static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
292 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200294 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
295 int nr = sensor_attr->index;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 struct it87_data *data = it87_update_device(dev);
298 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
299}
300
Jean Delvare20ad93d2005-06-05 11:53:25 +0200301static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
302 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200304 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
305 int nr = sensor_attr->index;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct i2c_client *client = to_i2c_client(dev);
308 struct it87_data *data = i2c_get_clientdata(client);
309 unsigned long val = simple_strtoul(buf, NULL, 10);
310
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100311 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 data->in_min[nr] = IN_TO_REG(val);
313 it87_write_value(client, IT87_REG_VIN_MIN(nr),
314 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100315 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return count;
317}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200318static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
319 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200321 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
322 int nr = sensor_attr->index;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct i2c_client *client = to_i2c_client(dev);
325 struct it87_data *data = i2c_get_clientdata(client);
326 unsigned long val = simple_strtoul(buf, NULL, 10);
327
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100328 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 data->in_max[nr] = IN_TO_REG(val);
330 it87_write_value(client, IT87_REG_VIN_MAX(nr),
331 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100332 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return count;
334}
335
336#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200337static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
338 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200341static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
342 show_in_min, set_in_min, offset); \
343static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
344 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346show_in_offset(0);
347limit_in_offset(0);
348show_in_offset(1);
349limit_in_offset(1);
350show_in_offset(2);
351limit_in_offset(2);
352show_in_offset(3);
353limit_in_offset(3);
354show_in_offset(4);
355limit_in_offset(4);
356show_in_offset(5);
357limit_in_offset(5);
358show_in_offset(6);
359limit_in_offset(6);
360show_in_offset(7);
361limit_in_offset(7);
362show_in_offset(8);
363
364/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200365static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
366 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200368 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
369 int nr = sensor_attr->index;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct it87_data *data = it87_update_device(dev);
372 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
373}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200374static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
375 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200377 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
378 int nr = sensor_attr->index;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 struct it87_data *data = it87_update_device(dev);
381 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
382}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200383static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
384 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200386 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
387 int nr = sensor_attr->index;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct it87_data *data = it87_update_device(dev);
390 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
391}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200392static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
393 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200395 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
396 int nr = sensor_attr->index;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct i2c_client *client = to_i2c_client(dev);
399 struct it87_data *data = i2c_get_clientdata(client);
400 int val = simple_strtol(buf, NULL, 10);
401
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100402 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 data->temp_high[nr] = TEMP_TO_REG(val);
404 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100405 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return count;
407}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200408static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
409 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200411 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
412 int nr = sensor_attr->index;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct i2c_client *client = to_i2c_client(dev);
415 struct it87_data *data = i2c_get_clientdata(client);
416 int val = simple_strtol(buf, NULL, 10);
417
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100418 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 data->temp_low[nr] = TEMP_TO_REG(val);
420 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100421 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return count;
423}
424#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200425static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
426 show_temp, NULL, offset - 1); \
427static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
428 show_temp_max, set_temp_max, offset - 1); \
429static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
430 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432show_temp_offset(1);
433show_temp_offset(2);
434show_temp_offset(3);
435
Jean Delvare20ad93d2005-06-05 11:53:25 +0200436static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
437 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200439 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
440 int nr = sensor_attr->index;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct it87_data *data = it87_update_device(dev);
443 u8 reg = data->sensor; /* In case the value is updated while we use it */
444
445 if (reg & (1 << nr))
446 return sprintf(buf, "3\n"); /* thermal diode */
447 if (reg & (8 << nr))
448 return sprintf(buf, "2\n"); /* thermistor */
449 return sprintf(buf, "0\n"); /* disabled */
450}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200451static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
452 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200454 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
455 int nr = sensor_attr->index;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct i2c_client *client = to_i2c_client(dev);
458 struct it87_data *data = i2c_get_clientdata(client);
459 int val = simple_strtol(buf, NULL, 10);
460
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100461 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 data->sensor &= ~(1 << nr);
464 data->sensor &= ~(8 << nr);
465 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
466 if (val == 3)
467 data->sensor |= 1 << nr;
468 else if (val == 2)
469 data->sensor |= 8 << nr;
470 else if (val != 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100471 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return -EINVAL;
473 }
474 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100475 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return count;
477}
478#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200479static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
480 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482show_sensor_offset(1);
483show_sensor_offset(2);
484show_sensor_offset(3);
485
486/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200487static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
488 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200490 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
491 int nr = sensor_attr->index;
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct it87_data *data = it87_update_device(dev);
494 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
495 DIV_FROM_REG(data->fan_div[nr])));
496}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200497static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
498 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200500 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
501 int nr = sensor_attr->index;
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct it87_data *data = it87_update_device(dev);
504 return sprintf(buf,"%d\n",
505 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
506}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200507static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
508 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200510 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
511 int nr = sensor_attr->index;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct it87_data *data = it87_update_device(dev);
514 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
515}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200516static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
517 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200519 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
520 int nr = sensor_attr->index;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct it87_data *data = it87_update_device(dev);
523 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
524}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200525static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
526 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200528 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
529 int nr = sensor_attr->index;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct it87_data *data = it87_update_device(dev);
532 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
533}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100534static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
535 char *buf)
536{
537 struct it87_data *data = it87_update_device(dev);
538 int index = (data->fan_ctl >> 4) & 0x07;
539
540 return sprintf(buf, "%u\n", pwm_freq[index]);
541}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200542static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
543 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200545 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
546 int nr = sensor_attr->index;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct i2c_client *client = to_i2c_client(dev);
549 struct it87_data *data = i2c_get_clientdata(client);
550 int val = simple_strtol(buf, NULL, 10);
Jean Delvare07eab462005-11-23 15:44:31 -0800551 u8 reg = it87_read_value(client, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100553 mutex_lock(&data->update_lock);
Jean Delvare07eab462005-11-23 15:44:31 -0800554 switch (nr) {
555 case 0: data->fan_div[nr] = reg & 0x07; break;
556 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
557 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
558 }
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
561 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100562 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return count;
564}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200565static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
566 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200568 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
569 int nr = sensor_attr->index;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct i2c_client *client = to_i2c_client(dev);
572 struct it87_data *data = i2c_get_clientdata(client);
Jean Delvareb9e349f2006-08-28 14:26:22 +0200573 unsigned long val = simple_strtoul(buf, NULL, 10);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200574 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 u8 old;
576
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100577 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 old = it87_read_value(client, IT87_REG_FAN_DIV);
579
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200580 /* Save fan min limit */
581 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 switch (nr) {
584 case 0:
585 case 1:
586 data->fan_div[nr] = DIV_TO_REG(val);
587 break;
588 case 2:
589 if (val < 8)
590 data->fan_div[nr] = 1;
591 else
592 data->fan_div[nr] = 3;
593 }
594 val = old & 0x80;
595 val |= (data->fan_div[0] & 0x07);
596 val |= (data->fan_div[1] & 0x07) << 3;
597 if (data->fan_div[2] == 3)
598 val |= 0x1 << 6;
599 it87_write_value(client, IT87_REG_FAN_DIV, val);
600
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200601 /* Restore fan min limit */
602 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
603 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
604
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100605 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return count;
607}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200608static ssize_t set_pwm_enable(struct device *dev,
609 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200611 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
612 int nr = sensor_attr->index;
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct i2c_client *client = to_i2c_client(dev);
615 struct it87_data *data = i2c_get_clientdata(client);
616 int val = simple_strtol(buf, NULL, 10);
617
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100618 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 if (val == 0) {
621 int tmp;
622 /* make sure the fan is on when in on/off mode */
623 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
624 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
625 /* set on/off mode */
626 data->fan_main_ctrl &= ~(1 << nr);
627 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
628 } else if (val == 1) {
629 /* set SmartGuardian mode */
630 data->fan_main_ctrl |= (1 << nr);
631 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
632 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
633 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
634 } else {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100635 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -EINVAL;
637 }
638
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100639 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return count;
641}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200642static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
643 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200645 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
646 int nr = sensor_attr->index;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct i2c_client *client = to_i2c_client(dev);
649 struct it87_data *data = i2c_get_clientdata(client);
650 int val = simple_strtol(buf, NULL, 10);
651
652 if (val < 0 || val > 255)
653 return -EINVAL;
654
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100655 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 data->manual_pwm_ctl[nr] = val;
657 if (data->fan_main_ctrl & (1 << nr))
658 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100659 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return count;
661}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100662static ssize_t set_pwm_freq(struct device *dev,
663 struct device_attribute *attr, const char *buf, size_t count)
664{
665 struct i2c_client *client = to_i2c_client(dev);
666 struct it87_data *data = i2c_get_clientdata(client);
667 unsigned long val = simple_strtoul(buf, NULL, 10);
668 int i;
669
670 /* Search for the nearest available frequency */
671 for (i = 0; i < 7; i++) {
672 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
673 break;
674 }
675
676 mutex_lock(&data->update_lock);
677 data->fan_ctl = it87_read_value(client, IT87_REG_FAN_CTL) & 0x8f;
678 data->fan_ctl |= i << 4;
679 it87_write_value(client, IT87_REG_FAN_CTL, data->fan_ctl);
680 mutex_unlock(&data->update_lock);
681
682 return count;
683}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Jean Delvare20ad93d2005-06-05 11:53:25 +0200685#define show_fan_offset(offset) \
686static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
687 show_fan, NULL, offset - 1); \
688static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
689 show_fan_min, set_fan_min, offset - 1); \
690static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
691 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693show_fan_offset(1);
694show_fan_offset(2);
695show_fan_offset(3);
696
697#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200698static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
699 show_pwm_enable, set_pwm_enable, offset - 1); \
700static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +0100701 show_pwm, set_pwm, offset - 1); \
702static DEVICE_ATTR(pwm##offset##_freq, \
703 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
704 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706show_pwm_offset(1);
707show_pwm_offset(2);
708show_pwm_offset(3);
709
Jean Delvare17d648b2006-08-28 14:23:46 +0200710/* A different set of callbacks for 16-bit fans */
711static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
712 char *buf)
713{
714 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
715 int nr = sensor_attr->index;
716 struct it87_data *data = it87_update_device(dev);
717 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
718}
719
720static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
721 char *buf)
722{
723 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
724 int nr = sensor_attr->index;
725 struct it87_data *data = it87_update_device(dev);
726 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
727}
728
729static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
730 const char *buf, size_t count)
731{
732 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
733 int nr = sensor_attr->index;
734 struct i2c_client *client = to_i2c_client(dev);
735 struct it87_data *data = i2c_get_clientdata(client);
736 int val = simple_strtol(buf, NULL, 10);
737
738 mutex_lock(&data->update_lock);
739 data->fan_min[nr] = FAN16_TO_REG(val);
740 it87_write_value(client, IT87_REG_FAN_MIN(nr),
741 data->fan_min[nr] & 0xff);
742 it87_write_value(client, IT87_REG_FANX_MIN(nr),
743 data->fan_min[nr] >> 8);
744 mutex_unlock(&data->update_lock);
745 return count;
746}
747
748/* We want to use the same sysfs file names as 8-bit fans, but we need
749 different variable names, so we have to use SENSOR_ATTR instead of
750 SENSOR_DEVICE_ATTR. */
751#define show_fan16_offset(offset) \
752static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
753 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
754 show_fan16, NULL, offset - 1); \
755static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
756 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
757 show_fan16_min, set_fan16_min, offset - 1)
758
759show_fan16_offset(1);
760show_fan16_offset(2);
761show_fan16_offset(3);
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400764static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200767 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
Jean Delvare1d66c642005-04-18 21:16:59 -0700769static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400772show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 struct it87_data *data = it87_update_device(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +0100775 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400778store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct i2c_client *client = to_i2c_client(dev);
781 struct it87_data *data = i2c_get_clientdata(client);
782 u32 val;
783
784 val = simple_strtoul(buf, NULL, 10);
785 data->vrm = val;
786
787 return count;
788}
789static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400792show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 struct it87_data *data = it87_update_device(dev);
795 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
796}
797static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +0200798
799static struct attribute *it87_attributes[] = {
800 &sensor_dev_attr_in0_input.dev_attr.attr,
801 &sensor_dev_attr_in1_input.dev_attr.attr,
802 &sensor_dev_attr_in2_input.dev_attr.attr,
803 &sensor_dev_attr_in3_input.dev_attr.attr,
804 &sensor_dev_attr_in4_input.dev_attr.attr,
805 &sensor_dev_attr_in5_input.dev_attr.attr,
806 &sensor_dev_attr_in6_input.dev_attr.attr,
807 &sensor_dev_attr_in7_input.dev_attr.attr,
808 &sensor_dev_attr_in8_input.dev_attr.attr,
809 &sensor_dev_attr_in0_min.dev_attr.attr,
810 &sensor_dev_attr_in1_min.dev_attr.attr,
811 &sensor_dev_attr_in2_min.dev_attr.attr,
812 &sensor_dev_attr_in3_min.dev_attr.attr,
813 &sensor_dev_attr_in4_min.dev_attr.attr,
814 &sensor_dev_attr_in5_min.dev_attr.attr,
815 &sensor_dev_attr_in6_min.dev_attr.attr,
816 &sensor_dev_attr_in7_min.dev_attr.attr,
817 &sensor_dev_attr_in0_max.dev_attr.attr,
818 &sensor_dev_attr_in1_max.dev_attr.attr,
819 &sensor_dev_attr_in2_max.dev_attr.attr,
820 &sensor_dev_attr_in3_max.dev_attr.attr,
821 &sensor_dev_attr_in4_max.dev_attr.attr,
822 &sensor_dev_attr_in5_max.dev_attr.attr,
823 &sensor_dev_attr_in6_max.dev_attr.attr,
824 &sensor_dev_attr_in7_max.dev_attr.attr,
825
826 &sensor_dev_attr_temp1_input.dev_attr.attr,
827 &sensor_dev_attr_temp2_input.dev_attr.attr,
828 &sensor_dev_attr_temp3_input.dev_attr.attr,
829 &sensor_dev_attr_temp1_max.dev_attr.attr,
830 &sensor_dev_attr_temp2_max.dev_attr.attr,
831 &sensor_dev_attr_temp3_max.dev_attr.attr,
832 &sensor_dev_attr_temp1_min.dev_attr.attr,
833 &sensor_dev_attr_temp2_min.dev_attr.attr,
834 &sensor_dev_attr_temp3_min.dev_attr.attr,
835 &sensor_dev_attr_temp1_type.dev_attr.attr,
836 &sensor_dev_attr_temp2_type.dev_attr.attr,
837 &sensor_dev_attr_temp3_type.dev_attr.attr,
838
839 &dev_attr_alarms.attr,
840 NULL
841};
842
843static const struct attribute_group it87_group = {
844 .attrs = it87_attributes,
845};
846
847static struct attribute *it87_attributes_opt[] = {
848 &sensor_dev_attr_fan1_input16.dev_attr.attr,
849 &sensor_dev_attr_fan1_min16.dev_attr.attr,
850 &sensor_dev_attr_fan2_input16.dev_attr.attr,
851 &sensor_dev_attr_fan2_min16.dev_attr.attr,
852 &sensor_dev_attr_fan3_input16.dev_attr.attr,
853 &sensor_dev_attr_fan3_min16.dev_attr.attr,
854
855 &sensor_dev_attr_fan1_input.dev_attr.attr,
856 &sensor_dev_attr_fan1_min.dev_attr.attr,
857 &sensor_dev_attr_fan1_div.dev_attr.attr,
858 &sensor_dev_attr_fan2_input.dev_attr.attr,
859 &sensor_dev_attr_fan2_min.dev_attr.attr,
860 &sensor_dev_attr_fan2_div.dev_attr.attr,
861 &sensor_dev_attr_fan3_input.dev_attr.attr,
862 &sensor_dev_attr_fan3_min.dev_attr.attr,
863 &sensor_dev_attr_fan3_div.dev_attr.attr,
864
865 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
866 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
867 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
868 &sensor_dev_attr_pwm1.dev_attr.attr,
869 &sensor_dev_attr_pwm2.dev_attr.attr,
870 &sensor_dev_attr_pwm3.dev_attr.attr,
871
872 &dev_attr_vrm.attr,
873 &dev_attr_cpu0_vid.attr,
874 NULL
875};
876
877static const struct attribute_group it87_group_opt = {
878 .attrs = it87_attributes_opt,
879};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Jean Delvare2d8672c2005-07-19 23:56:35 +0200881/* SuperIO detection - will change isa_address if a chip is found */
Jean Delvare91749992005-10-08 00:10:00 +0200882static int __init it87_find(unsigned short *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 int err = -ENODEV;
885
886 superio_enter();
887 chip_type = superio_inw(DEVID);
888 if (chip_type != IT8712F_DEVID
Jean Delvare17d648b2006-08-28 14:23:46 +0200889 && chip_type != IT8716F_DEVID
Jean Delvare87673dd2006-08-28 14:37:19 +0200890 && chip_type != IT8718F_DEVID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 && chip_type != IT8705F_DEVID)
892 goto exit;
893
Jean Delvare87673dd2006-08-28 14:37:19 +0200894 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
896 pr_info("it87: Device not activated, skipping\n");
897 goto exit;
898 }
899
900 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
901 if (*address == 0) {
902 pr_info("it87: Base address not set, skipping\n");
903 goto exit;
904 }
905
906 err = 0;
907 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
908 chip_type, *address, superio_inb(DEVREV) & 0x0f);
909
Jean Delvare87673dd2006-08-28 14:37:19 +0200910 /* Read GPIO config and VID value from LDN 7 (GPIO) */
911 if (chip_type != IT8705F_DEVID) {
912 int reg;
913
914 superio_select(GPIO);
915 if (chip_type == it8718)
916 vid_value = superio_inb(IT87_SIO_VID_REG);
917
918 reg = superio_inb(IT87_SIO_PINX2_REG);
919 if (reg & (1 << 0))
920 pr_info("it87: in3 is VCC (+5V)\n");
921 if (reg & (1 << 1))
922 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
923 }
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925exit:
926 superio_exit();
927 return err;
928}
929
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200930/* This function is called by i2c_probe */
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100931static int it87_detect(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 struct i2c_client *new_client;
934 struct it87_data *data;
935 int err = 0;
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100936 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int enable_pwm_interface;
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /* Reserve the ISA region */
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100940 if (!request_region(isa_address, IT87_EXTENT,
941 it87_isa_driver.driver.name)){
942 err = -EBUSY;
943 goto ERROR0;
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200946 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 err = -ENOMEM;
948 goto ERROR1;
949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 new_client = &data->client;
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100952 mutex_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 i2c_set_clientdata(new_client, data);
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100954 new_client->addr = isa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 new_client->adapter = adapter;
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100956 new_client->driver = &it87_isa_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /* Now, we do the remaining detection. */
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100959 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
960 || it87_read_value(new_client, IT87_REG_CHIPID) != 0x90) {
961 err = -ENODEV;
962 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
965 /* Determine the chip type. */
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100966 switch (chip_type) {
967 case IT8712F_DEVID:
968 data->type = it8712;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 name = "it8712";
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100970 break;
971 case IT8716F_DEVID:
972 data->type = it8716;
Jean Delvare17d648b2006-08-28 14:23:46 +0200973 name = "it8716";
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100974 break;
975 case IT8718F_DEVID:
976 data->type = it8718;
Jean Delvare87673dd2006-08-28 14:37:19 +0200977 name = "it8718";
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100978 break;
979 default:
980 data->type = it87;
981 name = "it87";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983
984 /* Fill in the remaining client fields and put it into the global list */
985 strlcpy(new_client->name, name, I2C_NAME_SIZE);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100986 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 /* Tell the I2C layer a new client has arrived */
989 if ((err = i2c_attach_client(new_client)))
990 goto ERROR2;
991
992 /* Check PWM configuration */
993 enable_pwm_interface = it87_check_pwm(new_client);
994
995 /* Initialize the IT87 chip */
996 it87_init_client(new_client, data);
997
998 /* Register sysfs hooks */
Jean Delvare87808be2006-09-24 21:17:13 +0200999 if ((err = sysfs_create_group(&new_client->dev.kobj, &it87_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001000 goto ERROR3;
Jean Delvare17d648b2006-08-28 14:23:46 +02001001
Jean Delvare9060f8b2006-08-28 14:24:17 +02001002 /* Do not create fan files for disabled fans */
Jean Delvare87673dd2006-08-28 14:37:19 +02001003 if (data->type == it8716 || data->type == it8718) {
1004 /* 16-bit tachometers */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001005 if (data->has_fan & (1 << 0)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001006 if ((err = device_create_file(&new_client->dev,
1007 &sensor_dev_attr_fan1_input16.dev_attr))
1008 || (err = device_create_file(&new_client->dev,
1009 &sensor_dev_attr_fan1_min16.dev_attr)))
1010 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001011 }
1012 if (data->has_fan & (1 << 1)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001013 if ((err = device_create_file(&new_client->dev,
1014 &sensor_dev_attr_fan2_input16.dev_attr))
1015 || (err = device_create_file(&new_client->dev,
1016 &sensor_dev_attr_fan2_min16.dev_attr)))
1017 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001018 }
1019 if (data->has_fan & (1 << 2)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001020 if ((err = device_create_file(&new_client->dev,
1021 &sensor_dev_attr_fan3_input16.dev_attr))
1022 || (err = device_create_file(&new_client->dev,
1023 &sensor_dev_attr_fan3_min16.dev_attr)))
1024 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001025 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001026 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001027 /* 8-bit tachometers with clock divider */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001028 if (data->has_fan & (1 << 0)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001029 if ((err = device_create_file(&new_client->dev,
1030 &sensor_dev_attr_fan1_input.dev_attr))
1031 || (err = device_create_file(&new_client->dev,
1032 &sensor_dev_attr_fan1_min.dev_attr))
1033 || (err = device_create_file(&new_client->dev,
1034 &sensor_dev_attr_fan1_div.dev_attr)))
1035 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001036 }
1037 if (data->has_fan & (1 << 1)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001038 if ((err = device_create_file(&new_client->dev,
1039 &sensor_dev_attr_fan2_input.dev_attr))
1040 || (err = device_create_file(&new_client->dev,
1041 &sensor_dev_attr_fan2_min.dev_attr))
1042 || (err = device_create_file(&new_client->dev,
1043 &sensor_dev_attr_fan2_div.dev_attr)))
1044 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001045 }
1046 if (data->has_fan & (1 << 2)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001047 if ((err = device_create_file(&new_client->dev,
1048 &sensor_dev_attr_fan3_input.dev_attr))
1049 || (err = device_create_file(&new_client->dev,
1050 &sensor_dev_attr_fan3_min.dev_attr))
1051 || (err = device_create_file(&new_client->dev,
1052 &sensor_dev_attr_fan3_div.dev_attr)))
1053 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001054 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001055 }
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (enable_pwm_interface) {
Jean Delvare87808be2006-09-24 21:17:13 +02001058 if ((err = device_create_file(&new_client->dev,
1059 &sensor_dev_attr_pwm1_enable.dev_attr))
1060 || (err = device_create_file(&new_client->dev,
1061 &sensor_dev_attr_pwm2_enable.dev_attr))
1062 || (err = device_create_file(&new_client->dev,
1063 &sensor_dev_attr_pwm3_enable.dev_attr))
1064 || (err = device_create_file(&new_client->dev,
1065 &sensor_dev_attr_pwm1.dev_attr))
1066 || (err = device_create_file(&new_client->dev,
1067 &sensor_dev_attr_pwm2.dev_attr))
1068 || (err = device_create_file(&new_client->dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001069 &sensor_dev_attr_pwm3.dev_attr))
1070 || (err = device_create_file(&new_client->dev,
1071 &dev_attr_pwm1_freq))
1072 || (err = device_create_file(&new_client->dev,
1073 &dev_attr_pwm2_freq))
1074 || (err = device_create_file(&new_client->dev,
1075 &dev_attr_pwm3_freq)))
Jean Delvare87808be2006-09-24 21:17:13 +02001076 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
Jean Delvare87673dd2006-08-28 14:37:19 +02001079 if (data->type == it8712 || data->type == it8716
1080 || data->type == it8718) {
Jean Delvare303760b2005-07-31 21:52:01 +02001081 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001082 /* VID reading from Super-I/O config space if available */
1083 data->vid = vid_value;
Jean Delvare87808be2006-09-24 21:17:13 +02001084 if ((err = device_create_file(&new_client->dev,
1085 &dev_attr_vrm))
1086 || (err = device_create_file(&new_client->dev,
1087 &dev_attr_cpu0_vid)))
1088 goto ERROR4;
1089 }
1090
1091 data->class_dev = hwmon_device_register(&new_client->dev);
1092 if (IS_ERR(data->class_dev)) {
1093 err = PTR_ERR(data->class_dev);
1094 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096
1097 return 0;
1098
Jean Delvare87808be2006-09-24 21:17:13 +02001099ERROR4:
1100 sysfs_remove_group(&new_client->dev.kobj, &it87_group);
1101 sysfs_remove_group(&new_client->dev.kobj, &it87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001102ERROR3:
1103 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104ERROR2:
1105 kfree(data);
1106ERROR1:
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001107 release_region(isa_address, IT87_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108ERROR0:
1109 return err;
1110}
1111
1112static int it87_detach_client(struct i2c_client *client)
1113{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001114 struct it87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 int err;
1116
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001117 hwmon_device_unregister(data->class_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001118 sysfs_remove_group(&client->dev.kobj, &it87_group);
1119 sysfs_remove_group(&client->dev.kobj, &it87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001120
Jean Delvare7bef5592005-07-27 22:14:49 +02001121 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001124 release_region(client->addr, IT87_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001125 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 return 0;
1128}
1129
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001130/* ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1132 would slow down the IT87 access and should not be necessary. */
1133static int it87_read_value(struct i2c_client *client, u8 reg)
1134{
1135 struct it87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 int res;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001137
1138 mutex_lock(&data->lock);
1139 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1140 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
1141 mutex_unlock(&data->lock);
1142
1143 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001146/* ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1148 would slow down the IT87 access and should not be necessary. */
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001149static void it87_write_value(struct i2c_client *client, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 struct it87_data *data = i2c_get_clientdata(client);
1152
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001153 mutex_lock(&data->lock);
1154 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1155 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
1156 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159/* Return 1 if and only if the PWM interface is safe to use */
1160static int it87_check_pwm(struct i2c_client *client)
1161{
1162 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1163 * and polarity set to active low is sign that this is the case so we
1164 * disable pwm control to protect the user. */
1165 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1166 if ((tmp & 0x87) == 0) {
1167 if (fix_pwm_polarity) {
1168 /* The user asks us to attempt a chip reconfiguration.
1169 * This means switching to active high polarity and
1170 * inverting all fan speed values. */
1171 int i;
1172 u8 pwm[3];
1173
1174 for (i = 0; i < 3; i++)
1175 pwm[i] = it87_read_value(client,
1176 IT87_REG_PWM(i));
1177
1178 /* If any fan is in automatic pwm mode, the polarity
1179 * might be correct, as suspicious as it seems, so we
1180 * better don't change anything (but still disable the
1181 * PWM interface). */
1182 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1183 dev_info(&client->dev, "Reconfiguring PWM to "
1184 "active high polarity\n");
1185 it87_write_value(client, IT87_REG_FAN_CTL,
1186 tmp | 0x87);
1187 for (i = 0; i < 3; i++)
1188 it87_write_value(client,
1189 IT87_REG_PWM(i),
1190 0x7f & ~pwm[i]);
1191 return 1;
1192 }
1193
1194 dev_info(&client->dev, "PWM configuration is "
1195 "too broken to be fixed\n");
1196 }
1197
1198 dev_info(&client->dev, "Detected broken BIOS "
1199 "defaults, disabling PWM interface\n");
1200 return 0;
1201 } else if (fix_pwm_polarity) {
1202 dev_info(&client->dev, "PWM configuration looks "
1203 "sane, won't touch\n");
1204 }
1205
1206 return 1;
1207}
1208
1209/* Called when we have found a new IT87. */
1210static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1211{
1212 int tmp, i;
1213
1214 /* initialize to sane defaults:
1215 * - if the chip is in manual pwm mode, this will be overwritten with
1216 * the actual settings on the chip (so in this case, initialization
1217 * is not needed)
1218 * - if in automatic or on/off mode, we could switch to manual mode,
1219 * read the registers and set manual_pwm_ctl accordingly, but currently
1220 * this is not implemented, so we initialize to something sane */
1221 for (i = 0; i < 3; i++) {
1222 data->manual_pwm_ctl[i] = 0xff;
1223 }
1224
Jean Delvarec5df9b72006-08-28 14:37:54 +02001225 /* Some chips seem to have default value 0xff for all limit
1226 * registers. For low voltage limits it makes no sense and triggers
1227 * alarms, so change to 0 instead. For high temperature limits, it
1228 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1229 * but is still confusing, so change to 127 degrees C. */
1230 for (i = 0; i < 8; i++) {
1231 tmp = it87_read_value(client, IT87_REG_VIN_MIN(i));
1232 if (tmp == 0xff)
1233 it87_write_value(client, IT87_REG_VIN_MIN(i), 0);
1234 }
1235 for (i = 0; i < 3; i++) {
1236 tmp = it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1237 if (tmp == 0xff)
1238 it87_write_value(client, IT87_REG_TEMP_HIGH(i), 127);
1239 }
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /* Check if temperature channnels are reset manually or by some reason */
1242 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1243 if ((tmp & 0x3f) == 0) {
1244 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1245 tmp = (tmp & 0xc0) | 0x2a;
1246 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1247 }
1248 data->sensor = tmp;
1249
1250 /* Check if voltage monitors are reset manually or by some reason */
1251 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1252 if ((tmp & 0xff) == 0) {
1253 /* Enable all voltage monitors */
1254 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1255 }
1256
1257 /* Check if tachometers are reset manually or by some reason */
1258 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1259 if ((data->fan_main_ctrl & 0x70) == 0) {
1260 /* Enable all fan tachometers */
1261 data->fan_main_ctrl |= 0x70;
1262 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1263 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001264 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Jean Delvare17d648b2006-08-28 14:23:46 +02001266 /* Set tachometers to 16-bit mode if needed */
Jean Delvare87673dd2006-08-28 14:37:19 +02001267 if (data->type == it8716 || data->type == it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001268 tmp = it87_read_value(client, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001269 if (~tmp & 0x07 & data->has_fan) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001270 dev_dbg(&client->dev,
1271 "Setting fan1-3 to 16-bit mode\n");
1272 it87_write_value(client, IT87_REG_FAN_16BIT,
1273 tmp | 0x07);
1274 }
1275 }
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* Set current fan mode registers and the default settings for the
1278 * other mode registers */
1279 for (i = 0; i < 3; i++) {
1280 if (data->fan_main_ctrl & (1 << i)) {
1281 /* pwm mode */
1282 tmp = it87_read_value(client, IT87_REG_PWM(i));
1283 if (tmp & 0x80) {
1284 /* automatic pwm - not yet implemented, but
1285 * leave the settings made by the BIOS alone
1286 * until a change is requested via the sysfs
1287 * interface */
1288 } else {
1289 /* manual pwm */
1290 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1291 }
1292 }
1293 }
1294
1295 /* Start monitoring */
1296 it87_write_value(client, IT87_REG_CONFIG,
1297 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1298 | (update_vbat ? 0x41 : 0x01));
1299}
1300
1301static struct it87_data *it87_update_device(struct device *dev)
1302{
1303 struct i2c_client *client = to_i2c_client(dev);
1304 struct it87_data *data = i2c_get_clientdata(client);
1305 int i;
1306
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001307 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1310 || !data->valid) {
1311
1312 if (update_vbat) {
1313 /* Cleared after each update, so reenable. Value
1314 returned by this read will be previous value */
1315 it87_write_value(client, IT87_REG_CONFIG,
1316 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1317 }
1318 for (i = 0; i <= 7; i++) {
1319 data->in[i] =
1320 it87_read_value(client, IT87_REG_VIN(i));
1321 data->in_min[i] =
1322 it87_read_value(client, IT87_REG_VIN_MIN(i));
1323 data->in_max[i] =
1324 it87_read_value(client, IT87_REG_VIN_MAX(i));
1325 }
Jean Delvare3543a532006-08-28 14:27:25 +02001326 /* in8 (battery) has no limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 data->in[8] =
1328 it87_read_value(client, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 for (i = 0; i < 3; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02001331 /* Skip disabled fans */
1332 if (!(data->has_fan & (1 << i)))
1333 continue;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 data->fan_min[i] =
1336 it87_read_value(client, IT87_REG_FAN_MIN(i));
Jean Delvare17d648b2006-08-28 14:23:46 +02001337 data->fan[i] = it87_read_value(client,
1338 IT87_REG_FAN(i));
1339 /* Add high byte if in 16-bit mode */
Jean Delvare87673dd2006-08-28 14:37:19 +02001340 if (data->type == it8716 || data->type == it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001341 data->fan[i] |= it87_read_value(client,
1342 IT87_REG_FANX(i)) << 8;
1343 data->fan_min[i] |= it87_read_value(client,
1344 IT87_REG_FANX_MIN(i)) << 8;
1345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347 for (i = 0; i < 3; i++) {
1348 data->temp[i] =
1349 it87_read_value(client, IT87_REG_TEMP(i));
1350 data->temp_high[i] =
1351 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1352 data->temp_low[i] =
1353 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1354 }
1355
Jean Delvare17d648b2006-08-28 14:23:46 +02001356 /* Newer chips don't have clock dividers */
Jean Delvare87673dd2006-08-28 14:37:19 +02001357 if ((data->has_fan & 0x07) && data->type != it8716
1358 && data->type != it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001359 i = it87_read_value(client, IT87_REG_FAN_DIV);
1360 data->fan_div[0] = i & 0x07;
1361 data->fan_div[1] = (i >> 3) & 0x07;
1362 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 data->alarms =
1366 it87_read_value(client, IT87_REG_ALARM1) |
1367 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1368 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1369 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001370 data->fan_ctl = it87_read_value(client, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1373 /* The 8705 does not have VID capability */
Jean Delvare17d648b2006-08-28 14:23:46 +02001374 if (data->type == it8712 || data->type == it8716) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 data->vid = it87_read_value(client, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02001376 /* The older IT8712F revisions had only 5 VID pins,
1377 but we assume it is always safe to read 6 bits. */
1378 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380 data->last_updated = jiffies;
1381 data->valid = 1;
1382 }
1383
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001384 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 return data;
1387}
1388
1389static int __init sm_it87_init(void)
1390{
Jean Delvare91749992005-10-08 00:10:00 +02001391 int res;
Jean Delvarefde09502005-07-19 23:51:07 +02001392
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001393 if ((res = it87_find(&isa_address)))
Jean Delvarefde09502005-07-19 23:51:07 +02001394 return res;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001395 return i2c_isa_add_driver(&it87_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
1398static void __exit sm_it87_exit(void)
1399{
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001400 i2c_isa_del_driver(&it87_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
1403
Jean Delvareb19367c2006-08-28 14:39:26 +02001404MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>, "
1405 "Jean Delvare <khali@linux-fr.org>");
Jean Delvare87673dd2006-08-28 14:37:19 +02001406MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407module_param(update_vbat, bool, 0);
1408MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1409module_param(fix_pwm_polarity, bool, 0);
1410MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1411MODULE_LICENSE("GPL");
1412
1413module_init(sm_it87_init);
1414module_exit(sm_it87_exit);