blob: 62afc63708a5c85dec58aae278b2f2047c476e3e [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
Jean Delvareed6bafb2007-02-14 21:15:03 +0100217/* For each registered chip, we need to keep some data in memory.
218 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219struct it87_data {
220 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400221 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 enum chips type;
223
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100224 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 char valid; /* !=0 if following fields are valid */
226 unsigned long last_updated; /* In jiffies */
227
228 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200229 u8 in_max[8]; /* Register value */
230 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200231 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +0200232 u16 fan[3]; /* Register values, possibly combined */
233 u16 fan_min[3]; /* Register values, possibly combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 u8 temp[3]; /* Register value */
235 u8 temp_high[3]; /* Register value */
236 u8 temp_low[3]; /* Register value */
237 u8 sensor; /* Register value */
238 u8 fan_div[3]; /* Register encoding, shifted right */
239 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100240 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 u32 alarms; /* Register encoding, combined */
242 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100243 u8 fan_ctl; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
245};
246
247
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100248static int it87_detect(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static int it87_detach_client(struct i2c_client *client);
250
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100251static int it87_read_value(struct i2c_client *client, u8 reg);
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100252static void it87_write_value(struct i2c_client *client, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static struct it87_data *it87_update_device(struct device *dev);
254static int it87_check_pwm(struct i2c_client *client);
255static void it87_init_client(struct i2c_client *client, struct it87_data *data);
256
257
Jean Delvarefde09502005-07-19 23:51:07 +0200258static struct i2c_driver it87_isa_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100259 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200260 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100261 .name = "it87-isa",
262 },
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100263 .attach_adapter = it87_detect,
Jean Delvarefde09502005-07-19 23:51:07 +0200264 .detach_client = it87_detach_client,
265};
266
267
Jean Delvare20ad93d2005-06-05 11:53:25 +0200268static ssize_t show_in(struct device *dev, struct device_attribute *attr,
269 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200271 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
272 int nr = sensor_attr->index;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct it87_data *data = it87_update_device(dev);
275 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
276}
277
Jean Delvare20ad93d2005-06-05 11:53:25 +0200278static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
279 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200281 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
282 int nr = sensor_attr->index;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct it87_data *data = it87_update_device(dev);
285 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
286}
287
Jean Delvare20ad93d2005-06-05 11:53:25 +0200288static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
289 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200291 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
292 int nr = sensor_attr->index;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 struct it87_data *data = it87_update_device(dev);
295 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
296}
297
Jean Delvare20ad93d2005-06-05 11:53:25 +0200298static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
299 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200301 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
302 int nr = sensor_attr->index;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct i2c_client *client = to_i2c_client(dev);
305 struct it87_data *data = i2c_get_clientdata(client);
306 unsigned long val = simple_strtoul(buf, NULL, 10);
307
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100308 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 data->in_min[nr] = IN_TO_REG(val);
310 it87_write_value(client, IT87_REG_VIN_MIN(nr),
311 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100312 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return count;
314}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200315static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
316 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200318 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
319 int nr = sensor_attr->index;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct i2c_client *client = to_i2c_client(dev);
322 struct it87_data *data = i2c_get_clientdata(client);
323 unsigned long val = simple_strtoul(buf, NULL, 10);
324
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100325 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 data->in_max[nr] = IN_TO_REG(val);
327 it87_write_value(client, IT87_REG_VIN_MAX(nr),
328 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100329 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return count;
331}
332
333#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200334static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
335 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200338static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
339 show_in_min, set_in_min, offset); \
340static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
341 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343show_in_offset(0);
344limit_in_offset(0);
345show_in_offset(1);
346limit_in_offset(1);
347show_in_offset(2);
348limit_in_offset(2);
349show_in_offset(3);
350limit_in_offset(3);
351show_in_offset(4);
352limit_in_offset(4);
353show_in_offset(5);
354limit_in_offset(5);
355show_in_offset(6);
356limit_in_offset(6);
357show_in_offset(7);
358limit_in_offset(7);
359show_in_offset(8);
360
361/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200362static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
363 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200365 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
366 int nr = sensor_attr->index;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct it87_data *data = it87_update_device(dev);
369 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
370}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200371static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
372 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200374 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
375 int nr = sensor_attr->index;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct it87_data *data = it87_update_device(dev);
378 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
379}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200380static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
381 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200383 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
384 int nr = sensor_attr->index;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct it87_data *data = it87_update_device(dev);
387 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
388}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200389static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
390 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200392 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
393 int nr = sensor_attr->index;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct i2c_client *client = to_i2c_client(dev);
396 struct it87_data *data = i2c_get_clientdata(client);
397 int val = simple_strtol(buf, NULL, 10);
398
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100399 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 data->temp_high[nr] = TEMP_TO_REG(val);
401 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100402 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return count;
404}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200405static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
406 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200408 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
409 int nr = sensor_attr->index;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct i2c_client *client = to_i2c_client(dev);
412 struct it87_data *data = i2c_get_clientdata(client);
413 int val = simple_strtol(buf, NULL, 10);
414
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100415 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 data->temp_low[nr] = TEMP_TO_REG(val);
417 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100418 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return count;
420}
421#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200422static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
423 show_temp, NULL, offset - 1); \
424static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
425 show_temp_max, set_temp_max, offset - 1); \
426static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
427 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429show_temp_offset(1);
430show_temp_offset(2);
431show_temp_offset(3);
432
Jean Delvare20ad93d2005-06-05 11:53:25 +0200433static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
434 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200436 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
437 int nr = sensor_attr->index;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct it87_data *data = it87_update_device(dev);
440 u8 reg = data->sensor; /* In case the value is updated while we use it */
441
442 if (reg & (1 << nr))
443 return sprintf(buf, "3\n"); /* thermal diode */
444 if (reg & (8 << nr))
445 return sprintf(buf, "2\n"); /* thermistor */
446 return sprintf(buf, "0\n"); /* disabled */
447}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200448static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
449 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200451 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
452 int nr = sensor_attr->index;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct i2c_client *client = to_i2c_client(dev);
455 struct it87_data *data = i2c_get_clientdata(client);
456 int val = simple_strtol(buf, NULL, 10);
457
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100458 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 data->sensor &= ~(1 << nr);
461 data->sensor &= ~(8 << nr);
462 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
463 if (val == 3)
464 data->sensor |= 1 << nr;
465 else if (val == 2)
466 data->sensor |= 8 << nr;
467 else if (val != 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100468 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -EINVAL;
470 }
471 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100472 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return count;
474}
475#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200476static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
477 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479show_sensor_offset(1);
480show_sensor_offset(2);
481show_sensor_offset(3);
482
483/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200484static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
485 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200487 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
488 int nr = sensor_attr->index;
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct it87_data *data = it87_update_device(dev);
491 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
492 DIV_FROM_REG(data->fan_div[nr])));
493}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200494static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
495 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200497 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
498 int nr = sensor_attr->index;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 struct it87_data *data = it87_update_device(dev);
501 return sprintf(buf,"%d\n",
502 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
503}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200504static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
505 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200507 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
508 int nr = sensor_attr->index;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct it87_data *data = it87_update_device(dev);
511 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
512}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200513static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
514 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200516 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
517 int nr = sensor_attr->index;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct it87_data *data = it87_update_device(dev);
520 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
521}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200522static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
523 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200525 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
526 int nr = sensor_attr->index;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct it87_data *data = it87_update_device(dev);
529 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
530}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100531static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
532 char *buf)
533{
534 struct it87_data *data = it87_update_device(dev);
535 int index = (data->fan_ctl >> 4) & 0x07;
536
537 return sprintf(buf, "%u\n", pwm_freq[index]);
538}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200539static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
540 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200542 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
543 int nr = sensor_attr->index;
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct i2c_client *client = to_i2c_client(dev);
546 struct it87_data *data = i2c_get_clientdata(client);
547 int val = simple_strtol(buf, NULL, 10);
Jean Delvare7f999aa2007-02-14 21:15:03 +0100548 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100550 mutex_lock(&data->update_lock);
Jean Delvare7f999aa2007-02-14 21:15:03 +0100551 reg = it87_read_value(client, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800552 switch (nr) {
553 case 0: data->fan_div[nr] = reg & 0x07; break;
554 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
555 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
556 }
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
559 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100560 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return count;
562}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200563static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
564 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200566 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
567 int nr = sensor_attr->index;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct i2c_client *client = to_i2c_client(dev);
570 struct it87_data *data = i2c_get_clientdata(client);
Jean Delvareb9e349f2006-08-28 14:26:22 +0200571 unsigned long val = simple_strtoul(buf, NULL, 10);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200572 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 u8 old;
574
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100575 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 old = it87_read_value(client, IT87_REG_FAN_DIV);
577
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200578 /* Save fan min limit */
579 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 switch (nr) {
582 case 0:
583 case 1:
584 data->fan_div[nr] = DIV_TO_REG(val);
585 break;
586 case 2:
587 if (val < 8)
588 data->fan_div[nr] = 1;
589 else
590 data->fan_div[nr] = 3;
591 }
592 val = old & 0x80;
593 val |= (data->fan_div[0] & 0x07);
594 val |= (data->fan_div[1] & 0x07) << 3;
595 if (data->fan_div[2] == 3)
596 val |= 0x1 << 6;
597 it87_write_value(client, IT87_REG_FAN_DIV, val);
598
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200599 /* Restore fan min limit */
600 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
601 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
602
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100603 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return count;
605}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200606static ssize_t set_pwm_enable(struct device *dev,
607 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200609 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
610 int nr = sensor_attr->index;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct i2c_client *client = to_i2c_client(dev);
613 struct it87_data *data = i2c_get_clientdata(client);
614 int val = simple_strtol(buf, NULL, 10);
615
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100616 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 if (val == 0) {
619 int tmp;
620 /* make sure the fan is on when in on/off mode */
621 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
622 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
623 /* set on/off mode */
624 data->fan_main_ctrl &= ~(1 << nr);
625 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
626 } else if (val == 1) {
627 /* set SmartGuardian mode */
628 data->fan_main_ctrl |= (1 << nr);
629 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
630 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
631 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
632 } else {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100633 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return -EINVAL;
635 }
636
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100637 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return count;
639}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200640static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
641 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200643 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
644 int nr = sensor_attr->index;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct i2c_client *client = to_i2c_client(dev);
647 struct it87_data *data = i2c_get_clientdata(client);
648 int val = simple_strtol(buf, NULL, 10);
649
650 if (val < 0 || val > 255)
651 return -EINVAL;
652
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100653 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 data->manual_pwm_ctl[nr] = val;
655 if (data->fan_main_ctrl & (1 << nr))
656 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100657 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return count;
659}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100660static ssize_t set_pwm_freq(struct device *dev,
661 struct device_attribute *attr, const char *buf, size_t count)
662{
663 struct i2c_client *client = to_i2c_client(dev);
664 struct it87_data *data = i2c_get_clientdata(client);
665 unsigned long val = simple_strtoul(buf, NULL, 10);
666 int i;
667
668 /* Search for the nearest available frequency */
669 for (i = 0; i < 7; i++) {
670 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
671 break;
672 }
673
674 mutex_lock(&data->update_lock);
675 data->fan_ctl = it87_read_value(client, IT87_REG_FAN_CTL) & 0x8f;
676 data->fan_ctl |= i << 4;
677 it87_write_value(client, IT87_REG_FAN_CTL, data->fan_ctl);
678 mutex_unlock(&data->update_lock);
679
680 return count;
681}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Jean Delvare20ad93d2005-06-05 11:53:25 +0200683#define show_fan_offset(offset) \
684static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
685 show_fan, NULL, offset - 1); \
686static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
687 show_fan_min, set_fan_min, offset - 1); \
688static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
689 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691show_fan_offset(1);
692show_fan_offset(2);
693show_fan_offset(3);
694
695#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200696static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
697 show_pwm_enable, set_pwm_enable, offset - 1); \
698static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +0100699 show_pwm, set_pwm, offset - 1); \
700static DEVICE_ATTR(pwm##offset##_freq, \
701 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
702 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704show_pwm_offset(1);
705show_pwm_offset(2);
706show_pwm_offset(3);
707
Jean Delvare17d648b2006-08-28 14:23:46 +0200708/* A different set of callbacks for 16-bit fans */
709static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
710 char *buf)
711{
712 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
713 int nr = sensor_attr->index;
714 struct it87_data *data = it87_update_device(dev);
715 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
716}
717
718static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
719 char *buf)
720{
721 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
722 int nr = sensor_attr->index;
723 struct it87_data *data = it87_update_device(dev);
724 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
725}
726
727static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
728 const char *buf, size_t count)
729{
730 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
731 int nr = sensor_attr->index;
732 struct i2c_client *client = to_i2c_client(dev);
733 struct it87_data *data = i2c_get_clientdata(client);
734 int val = simple_strtol(buf, NULL, 10);
735
736 mutex_lock(&data->update_lock);
737 data->fan_min[nr] = FAN16_TO_REG(val);
738 it87_write_value(client, IT87_REG_FAN_MIN(nr),
739 data->fan_min[nr] & 0xff);
740 it87_write_value(client, IT87_REG_FANX_MIN(nr),
741 data->fan_min[nr] >> 8);
742 mutex_unlock(&data->update_lock);
743 return count;
744}
745
746/* We want to use the same sysfs file names as 8-bit fans, but we need
747 different variable names, so we have to use SENSOR_ATTR instead of
748 SENSOR_DEVICE_ATTR. */
749#define show_fan16_offset(offset) \
750static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
751 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
752 show_fan16, NULL, offset - 1); \
753static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
754 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
755 show_fan16_min, set_fan16_min, offset - 1)
756
757show_fan16_offset(1);
758show_fan16_offset(2);
759show_fan16_offset(3);
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400762static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200765 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
Jean Delvare1d66c642005-04-18 21:16:59 -0700767static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400770show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct it87_data *data = it87_update_device(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +0100773 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400776store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct i2c_client *client = to_i2c_client(dev);
779 struct it87_data *data = i2c_get_clientdata(client);
780 u32 val;
781
782 val = simple_strtoul(buf, NULL, 10);
783 data->vrm = val;
784
785 return count;
786}
787static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400790show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 struct it87_data *data = it87_update_device(dev);
793 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
794}
795static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +0200796
797static struct attribute *it87_attributes[] = {
798 &sensor_dev_attr_in0_input.dev_attr.attr,
799 &sensor_dev_attr_in1_input.dev_attr.attr,
800 &sensor_dev_attr_in2_input.dev_attr.attr,
801 &sensor_dev_attr_in3_input.dev_attr.attr,
802 &sensor_dev_attr_in4_input.dev_attr.attr,
803 &sensor_dev_attr_in5_input.dev_attr.attr,
804 &sensor_dev_attr_in6_input.dev_attr.attr,
805 &sensor_dev_attr_in7_input.dev_attr.attr,
806 &sensor_dev_attr_in8_input.dev_attr.attr,
807 &sensor_dev_attr_in0_min.dev_attr.attr,
808 &sensor_dev_attr_in1_min.dev_attr.attr,
809 &sensor_dev_attr_in2_min.dev_attr.attr,
810 &sensor_dev_attr_in3_min.dev_attr.attr,
811 &sensor_dev_attr_in4_min.dev_attr.attr,
812 &sensor_dev_attr_in5_min.dev_attr.attr,
813 &sensor_dev_attr_in6_min.dev_attr.attr,
814 &sensor_dev_attr_in7_min.dev_attr.attr,
815 &sensor_dev_attr_in0_max.dev_attr.attr,
816 &sensor_dev_attr_in1_max.dev_attr.attr,
817 &sensor_dev_attr_in2_max.dev_attr.attr,
818 &sensor_dev_attr_in3_max.dev_attr.attr,
819 &sensor_dev_attr_in4_max.dev_attr.attr,
820 &sensor_dev_attr_in5_max.dev_attr.attr,
821 &sensor_dev_attr_in6_max.dev_attr.attr,
822 &sensor_dev_attr_in7_max.dev_attr.attr,
823
824 &sensor_dev_attr_temp1_input.dev_attr.attr,
825 &sensor_dev_attr_temp2_input.dev_attr.attr,
826 &sensor_dev_attr_temp3_input.dev_attr.attr,
827 &sensor_dev_attr_temp1_max.dev_attr.attr,
828 &sensor_dev_attr_temp2_max.dev_attr.attr,
829 &sensor_dev_attr_temp3_max.dev_attr.attr,
830 &sensor_dev_attr_temp1_min.dev_attr.attr,
831 &sensor_dev_attr_temp2_min.dev_attr.attr,
832 &sensor_dev_attr_temp3_min.dev_attr.attr,
833 &sensor_dev_attr_temp1_type.dev_attr.attr,
834 &sensor_dev_attr_temp2_type.dev_attr.attr,
835 &sensor_dev_attr_temp3_type.dev_attr.attr,
836
837 &dev_attr_alarms.attr,
838 NULL
839};
840
841static const struct attribute_group it87_group = {
842 .attrs = it87_attributes,
843};
844
845static struct attribute *it87_attributes_opt[] = {
846 &sensor_dev_attr_fan1_input16.dev_attr.attr,
847 &sensor_dev_attr_fan1_min16.dev_attr.attr,
848 &sensor_dev_attr_fan2_input16.dev_attr.attr,
849 &sensor_dev_attr_fan2_min16.dev_attr.attr,
850 &sensor_dev_attr_fan3_input16.dev_attr.attr,
851 &sensor_dev_attr_fan3_min16.dev_attr.attr,
852
853 &sensor_dev_attr_fan1_input.dev_attr.attr,
854 &sensor_dev_attr_fan1_min.dev_attr.attr,
855 &sensor_dev_attr_fan1_div.dev_attr.attr,
856 &sensor_dev_attr_fan2_input.dev_attr.attr,
857 &sensor_dev_attr_fan2_min.dev_attr.attr,
858 &sensor_dev_attr_fan2_div.dev_attr.attr,
859 &sensor_dev_attr_fan3_input.dev_attr.attr,
860 &sensor_dev_attr_fan3_min.dev_attr.attr,
861 &sensor_dev_attr_fan3_div.dev_attr.attr,
862
863 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
864 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
865 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
866 &sensor_dev_attr_pwm1.dev_attr.attr,
867 &sensor_dev_attr_pwm2.dev_attr.attr,
868 &sensor_dev_attr_pwm3.dev_attr.attr,
869
870 &dev_attr_vrm.attr,
871 &dev_attr_cpu0_vid.attr,
872 NULL
873};
874
875static const struct attribute_group it87_group_opt = {
876 .attrs = it87_attributes_opt,
877};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Jean Delvare2d8672c2005-07-19 23:56:35 +0200879/* SuperIO detection - will change isa_address if a chip is found */
Jean Delvare91749992005-10-08 00:10:00 +0200880static int __init it87_find(unsigned short *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 int err = -ENODEV;
883
884 superio_enter();
885 chip_type = superio_inw(DEVID);
886 if (chip_type != IT8712F_DEVID
Jean Delvare17d648b2006-08-28 14:23:46 +0200887 && chip_type != IT8716F_DEVID
Jean Delvare87673dd2006-08-28 14:37:19 +0200888 && chip_type != IT8718F_DEVID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 && chip_type != IT8705F_DEVID)
890 goto exit;
891
Jean Delvare87673dd2006-08-28 14:37:19 +0200892 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
894 pr_info("it87: Device not activated, skipping\n");
895 goto exit;
896 }
897
898 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
899 if (*address == 0) {
900 pr_info("it87: Base address not set, skipping\n");
901 goto exit;
902 }
903
904 err = 0;
905 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
906 chip_type, *address, superio_inb(DEVREV) & 0x0f);
907
Jean Delvare87673dd2006-08-28 14:37:19 +0200908 /* Read GPIO config and VID value from LDN 7 (GPIO) */
909 if (chip_type != IT8705F_DEVID) {
910 int reg;
911
912 superio_select(GPIO);
913 if (chip_type == it8718)
914 vid_value = superio_inb(IT87_SIO_VID_REG);
915
916 reg = superio_inb(IT87_SIO_PINX2_REG);
917 if (reg & (1 << 0))
918 pr_info("it87: in3 is VCC (+5V)\n");
919 if (reg & (1 << 1))
920 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
921 }
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923exit:
924 superio_exit();
925 return err;
926}
927
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200928/* This function is called by i2c_probe */
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100929static int it87_detect(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 struct i2c_client *new_client;
932 struct it87_data *data;
933 int err = 0;
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100934 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 int enable_pwm_interface;
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /* Reserve the ISA region */
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100938 if (!request_region(isa_address, IT87_EXTENT,
939 it87_isa_driver.driver.name)){
940 err = -EBUSY;
941 goto ERROR0;
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200944 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 err = -ENOMEM;
946 goto ERROR1;
947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 new_client = &data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 i2c_set_clientdata(new_client, data);
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100951 new_client->addr = isa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 new_client->adapter = adapter;
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100953 new_client->driver = &it87_isa_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 /* Now, we do the remaining detection. */
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100956 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
957 || it87_read_value(new_client, IT87_REG_CHIPID) != 0x90) {
958 err = -ENODEV;
959 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
962 /* Determine the chip type. */
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100963 switch (chip_type) {
964 case IT8712F_DEVID:
965 data->type = it8712;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 name = "it8712";
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100967 break;
968 case IT8716F_DEVID:
969 data->type = it8716;
Jean Delvare17d648b2006-08-28 14:23:46 +0200970 name = "it8716";
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100971 break;
972 case IT8718F_DEVID:
973 data->type = it8718;
Jean Delvare87673dd2006-08-28 14:37:19 +0200974 name = "it8718";
Jean Delvare8e9afcb2006-12-12 18:18:28 +0100975 break;
976 default:
977 data->type = it87;
978 name = "it87";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980
981 /* Fill in the remaining client fields and put it into the global list */
982 strlcpy(new_client->name, name, I2C_NAME_SIZE);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100983 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 /* Tell the I2C layer a new client has arrived */
986 if ((err = i2c_attach_client(new_client)))
987 goto ERROR2;
988
989 /* Check PWM configuration */
990 enable_pwm_interface = it87_check_pwm(new_client);
991
992 /* Initialize the IT87 chip */
993 it87_init_client(new_client, data);
994
995 /* Register sysfs hooks */
Jean Delvare87808be2006-09-24 21:17:13 +0200996 if ((err = sysfs_create_group(&new_client->dev.kobj, &it87_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400997 goto ERROR3;
Jean Delvare17d648b2006-08-28 14:23:46 +0200998
Jean Delvare9060f8b2006-08-28 14:24:17 +0200999 /* Do not create fan files for disabled fans */
Jean Delvare87673dd2006-08-28 14:37:19 +02001000 if (data->type == it8716 || data->type == it8718) {
1001 /* 16-bit tachometers */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001002 if (data->has_fan & (1 << 0)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001003 if ((err = device_create_file(&new_client->dev,
1004 &sensor_dev_attr_fan1_input16.dev_attr))
1005 || (err = device_create_file(&new_client->dev,
1006 &sensor_dev_attr_fan1_min16.dev_attr)))
1007 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001008 }
1009 if (data->has_fan & (1 << 1)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001010 if ((err = device_create_file(&new_client->dev,
1011 &sensor_dev_attr_fan2_input16.dev_attr))
1012 || (err = device_create_file(&new_client->dev,
1013 &sensor_dev_attr_fan2_min16.dev_attr)))
1014 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001015 }
1016 if (data->has_fan & (1 << 2)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001017 if ((err = device_create_file(&new_client->dev,
1018 &sensor_dev_attr_fan3_input16.dev_attr))
1019 || (err = device_create_file(&new_client->dev,
1020 &sensor_dev_attr_fan3_min16.dev_attr)))
1021 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001022 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001023 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001024 /* 8-bit tachometers with clock divider */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001025 if (data->has_fan & (1 << 0)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001026 if ((err = device_create_file(&new_client->dev,
1027 &sensor_dev_attr_fan1_input.dev_attr))
1028 || (err = device_create_file(&new_client->dev,
1029 &sensor_dev_attr_fan1_min.dev_attr))
1030 || (err = device_create_file(&new_client->dev,
1031 &sensor_dev_attr_fan1_div.dev_attr)))
1032 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001033 }
1034 if (data->has_fan & (1 << 1)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001035 if ((err = device_create_file(&new_client->dev,
1036 &sensor_dev_attr_fan2_input.dev_attr))
1037 || (err = device_create_file(&new_client->dev,
1038 &sensor_dev_attr_fan2_min.dev_attr))
1039 || (err = device_create_file(&new_client->dev,
1040 &sensor_dev_attr_fan2_div.dev_attr)))
1041 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001042 }
1043 if (data->has_fan & (1 << 2)) {
Jean Delvare87808be2006-09-24 21:17:13 +02001044 if ((err = device_create_file(&new_client->dev,
1045 &sensor_dev_attr_fan3_input.dev_attr))
1046 || (err = device_create_file(&new_client->dev,
1047 &sensor_dev_attr_fan3_min.dev_attr))
1048 || (err = device_create_file(&new_client->dev,
1049 &sensor_dev_attr_fan3_div.dev_attr)))
1050 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001051 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001052 }
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (enable_pwm_interface) {
Jean Delvare87808be2006-09-24 21:17:13 +02001055 if ((err = device_create_file(&new_client->dev,
1056 &sensor_dev_attr_pwm1_enable.dev_attr))
1057 || (err = device_create_file(&new_client->dev,
1058 &sensor_dev_attr_pwm2_enable.dev_attr))
1059 || (err = device_create_file(&new_client->dev,
1060 &sensor_dev_attr_pwm3_enable.dev_attr))
1061 || (err = device_create_file(&new_client->dev,
1062 &sensor_dev_attr_pwm1.dev_attr))
1063 || (err = device_create_file(&new_client->dev,
1064 &sensor_dev_attr_pwm2.dev_attr))
1065 || (err = device_create_file(&new_client->dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001066 &sensor_dev_attr_pwm3.dev_attr))
1067 || (err = device_create_file(&new_client->dev,
1068 &dev_attr_pwm1_freq))
1069 || (err = device_create_file(&new_client->dev,
1070 &dev_attr_pwm2_freq))
1071 || (err = device_create_file(&new_client->dev,
1072 &dev_attr_pwm3_freq)))
Jean Delvare87808be2006-09-24 21:17:13 +02001073 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075
Jean Delvare87673dd2006-08-28 14:37:19 +02001076 if (data->type == it8712 || data->type == it8716
1077 || data->type == it8718) {
Jean Delvare303760b2005-07-31 21:52:01 +02001078 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001079 /* VID reading from Super-I/O config space if available */
1080 data->vid = vid_value;
Jean Delvare87808be2006-09-24 21:17:13 +02001081 if ((err = device_create_file(&new_client->dev,
1082 &dev_attr_vrm))
1083 || (err = device_create_file(&new_client->dev,
1084 &dev_attr_cpu0_vid)))
1085 goto ERROR4;
1086 }
1087
1088 data->class_dev = hwmon_device_register(&new_client->dev);
1089 if (IS_ERR(data->class_dev)) {
1090 err = PTR_ERR(data->class_dev);
1091 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
1094 return 0;
1095
Jean Delvare87808be2006-09-24 21:17:13 +02001096ERROR4:
1097 sysfs_remove_group(&new_client->dev.kobj, &it87_group);
1098 sysfs_remove_group(&new_client->dev.kobj, &it87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001099ERROR3:
1100 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101ERROR2:
1102 kfree(data);
1103ERROR1:
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001104 release_region(isa_address, IT87_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105ERROR0:
1106 return err;
1107}
1108
1109static int it87_detach_client(struct i2c_client *client)
1110{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001111 struct it87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 int err;
1113
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001114 hwmon_device_unregister(data->class_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001115 sysfs_remove_group(&client->dev.kobj, &it87_group);
1116 sysfs_remove_group(&client->dev.kobj, &it87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001117
Jean Delvare7bef5592005-07-27 22:14:49 +02001118 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001121 release_region(client->addr, IT87_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001122 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 return 0;
1125}
1126
Jean Delvare7f999aa2007-02-14 21:15:03 +01001127/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1129 would slow down the IT87 access and should not be necessary. */
1130static int it87_read_value(struct i2c_client *client, u8 reg)
1131{
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001132 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
Jean Delvare7f999aa2007-02-14 21:15:03 +01001133 return inb_p(client->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
Jean Delvare7f999aa2007-02-14 21:15:03 +01001136/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1138 would slow down the IT87 access and should not be necessary. */
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001139static void it87_write_value(struct i2c_client *client, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001141 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1142 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145/* Return 1 if and only if the PWM interface is safe to use */
1146static int it87_check_pwm(struct i2c_client *client)
1147{
1148 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1149 * and polarity set to active low is sign that this is the case so we
1150 * disable pwm control to protect the user. */
1151 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1152 if ((tmp & 0x87) == 0) {
1153 if (fix_pwm_polarity) {
1154 /* The user asks us to attempt a chip reconfiguration.
1155 * This means switching to active high polarity and
1156 * inverting all fan speed values. */
1157 int i;
1158 u8 pwm[3];
1159
1160 for (i = 0; i < 3; i++)
1161 pwm[i] = it87_read_value(client,
1162 IT87_REG_PWM(i));
1163
1164 /* If any fan is in automatic pwm mode, the polarity
1165 * might be correct, as suspicious as it seems, so we
1166 * better don't change anything (but still disable the
1167 * PWM interface). */
1168 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1169 dev_info(&client->dev, "Reconfiguring PWM to "
1170 "active high polarity\n");
1171 it87_write_value(client, IT87_REG_FAN_CTL,
1172 tmp | 0x87);
1173 for (i = 0; i < 3; i++)
1174 it87_write_value(client,
1175 IT87_REG_PWM(i),
1176 0x7f & ~pwm[i]);
1177 return 1;
1178 }
1179
1180 dev_info(&client->dev, "PWM configuration is "
1181 "too broken to be fixed\n");
1182 }
1183
1184 dev_info(&client->dev, "Detected broken BIOS "
1185 "defaults, disabling PWM interface\n");
1186 return 0;
1187 } else if (fix_pwm_polarity) {
1188 dev_info(&client->dev, "PWM configuration looks "
1189 "sane, won't touch\n");
1190 }
1191
1192 return 1;
1193}
1194
1195/* Called when we have found a new IT87. */
1196static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1197{
1198 int tmp, i;
1199
1200 /* initialize to sane defaults:
1201 * - if the chip is in manual pwm mode, this will be overwritten with
1202 * the actual settings on the chip (so in this case, initialization
1203 * is not needed)
1204 * - if in automatic or on/off mode, we could switch to manual mode,
1205 * read the registers and set manual_pwm_ctl accordingly, but currently
1206 * this is not implemented, so we initialize to something sane */
1207 for (i = 0; i < 3; i++) {
1208 data->manual_pwm_ctl[i] = 0xff;
1209 }
1210
Jean Delvarec5df9b72006-08-28 14:37:54 +02001211 /* Some chips seem to have default value 0xff for all limit
1212 * registers. For low voltage limits it makes no sense and triggers
1213 * alarms, so change to 0 instead. For high temperature limits, it
1214 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1215 * but is still confusing, so change to 127 degrees C. */
1216 for (i = 0; i < 8; i++) {
1217 tmp = it87_read_value(client, IT87_REG_VIN_MIN(i));
1218 if (tmp == 0xff)
1219 it87_write_value(client, IT87_REG_VIN_MIN(i), 0);
1220 }
1221 for (i = 0; i < 3; i++) {
1222 tmp = it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1223 if (tmp == 0xff)
1224 it87_write_value(client, IT87_REG_TEMP_HIGH(i), 127);
1225 }
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /* Check if temperature channnels are reset manually or by some reason */
1228 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1229 if ((tmp & 0x3f) == 0) {
1230 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1231 tmp = (tmp & 0xc0) | 0x2a;
1232 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1233 }
1234 data->sensor = tmp;
1235
1236 /* Check if voltage monitors are reset manually or by some reason */
1237 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1238 if ((tmp & 0xff) == 0) {
1239 /* Enable all voltage monitors */
1240 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1241 }
1242
1243 /* Check if tachometers are reset manually or by some reason */
1244 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1245 if ((data->fan_main_ctrl & 0x70) == 0) {
1246 /* Enable all fan tachometers */
1247 data->fan_main_ctrl |= 0x70;
1248 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1249 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001250 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Jean Delvare17d648b2006-08-28 14:23:46 +02001252 /* Set tachometers to 16-bit mode if needed */
Jean Delvare87673dd2006-08-28 14:37:19 +02001253 if (data->type == it8716 || data->type == it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001254 tmp = it87_read_value(client, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001255 if (~tmp & 0x07 & data->has_fan) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001256 dev_dbg(&client->dev,
1257 "Setting fan1-3 to 16-bit mode\n");
1258 it87_write_value(client, IT87_REG_FAN_16BIT,
1259 tmp | 0x07);
1260 }
1261 }
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /* Set current fan mode registers and the default settings for the
1264 * other mode registers */
1265 for (i = 0; i < 3; i++) {
1266 if (data->fan_main_ctrl & (1 << i)) {
1267 /* pwm mode */
1268 tmp = it87_read_value(client, IT87_REG_PWM(i));
1269 if (tmp & 0x80) {
1270 /* automatic pwm - not yet implemented, but
1271 * leave the settings made by the BIOS alone
1272 * until a change is requested via the sysfs
1273 * interface */
1274 } else {
1275 /* manual pwm */
1276 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1277 }
1278 }
1279 }
1280
1281 /* Start monitoring */
1282 it87_write_value(client, IT87_REG_CONFIG,
1283 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1284 | (update_vbat ? 0x41 : 0x01));
1285}
1286
1287static struct it87_data *it87_update_device(struct device *dev)
1288{
1289 struct i2c_client *client = to_i2c_client(dev);
1290 struct it87_data *data = i2c_get_clientdata(client);
1291 int i;
1292
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001293 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1296 || !data->valid) {
1297
1298 if (update_vbat) {
1299 /* Cleared after each update, so reenable. Value
1300 returned by this read will be previous value */
1301 it87_write_value(client, IT87_REG_CONFIG,
1302 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1303 }
1304 for (i = 0; i <= 7; i++) {
1305 data->in[i] =
1306 it87_read_value(client, IT87_REG_VIN(i));
1307 data->in_min[i] =
1308 it87_read_value(client, IT87_REG_VIN_MIN(i));
1309 data->in_max[i] =
1310 it87_read_value(client, IT87_REG_VIN_MAX(i));
1311 }
Jean Delvare3543a532006-08-28 14:27:25 +02001312 /* in8 (battery) has no limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 data->in[8] =
1314 it87_read_value(client, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 for (i = 0; i < 3; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02001317 /* Skip disabled fans */
1318 if (!(data->has_fan & (1 << i)))
1319 continue;
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 data->fan_min[i] =
1322 it87_read_value(client, IT87_REG_FAN_MIN(i));
Jean Delvare17d648b2006-08-28 14:23:46 +02001323 data->fan[i] = it87_read_value(client,
1324 IT87_REG_FAN(i));
1325 /* Add high byte if in 16-bit mode */
Jean Delvare87673dd2006-08-28 14:37:19 +02001326 if (data->type == it8716 || data->type == it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001327 data->fan[i] |= it87_read_value(client,
1328 IT87_REG_FANX(i)) << 8;
1329 data->fan_min[i] |= it87_read_value(client,
1330 IT87_REG_FANX_MIN(i)) << 8;
1331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333 for (i = 0; i < 3; i++) {
1334 data->temp[i] =
1335 it87_read_value(client, IT87_REG_TEMP(i));
1336 data->temp_high[i] =
1337 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1338 data->temp_low[i] =
1339 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1340 }
1341
Jean Delvare17d648b2006-08-28 14:23:46 +02001342 /* Newer chips don't have clock dividers */
Jean Delvare87673dd2006-08-28 14:37:19 +02001343 if ((data->has_fan & 0x07) && data->type != it8716
1344 && data->type != it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001345 i = it87_read_value(client, IT87_REG_FAN_DIV);
1346 data->fan_div[0] = i & 0x07;
1347 data->fan_div[1] = (i >> 3) & 0x07;
1348 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 data->alarms =
1352 it87_read_value(client, IT87_REG_ALARM1) |
1353 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1354 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1355 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001356 data->fan_ctl = it87_read_value(client, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1359 /* The 8705 does not have VID capability */
Jean Delvare17d648b2006-08-28 14:23:46 +02001360 if (data->type == it8712 || data->type == it8716) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 data->vid = it87_read_value(client, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02001362 /* The older IT8712F revisions had only 5 VID pins,
1363 but we assume it is always safe to read 6 bits. */
1364 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366 data->last_updated = jiffies;
1367 data->valid = 1;
1368 }
1369
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001370 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 return data;
1373}
1374
1375static int __init sm_it87_init(void)
1376{
Jean Delvare91749992005-10-08 00:10:00 +02001377 int res;
Jean Delvarefde09502005-07-19 23:51:07 +02001378
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001379 if ((res = it87_find(&isa_address)))
Jean Delvarefde09502005-07-19 23:51:07 +02001380 return res;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001381 return i2c_isa_add_driver(&it87_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
1384static void __exit sm_it87_exit(void)
1385{
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001386 i2c_isa_del_driver(&it87_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389
Jean Delvareb19367c2006-08-28 14:39:26 +02001390MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>, "
1391 "Jean Delvare <khali@linux-fr.org>");
Jean Delvare87673dd2006-08-28 14:37:19 +02001392MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393module_param(update_vbat, bool, 0);
1394MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1395module_param(fix_pwm_polarity, bool, 0);
1396MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1397MODULE_LICENSE("GPL");
1398
1399module_init(sm_it87_init);
1400module_exit(sm_it87_exit);