blob: de62f60bcaf15b9ab66f0c0b48c56cd3673b2537 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring.
4
Jean Delvare91749992005-10-08 00:10:00 +02005 Supports: IT8705F Super I/O chip w/LPC interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 IT8712F Super I/O chip w/LPC interface & SMBus
Jean Delvare17d648b2006-08-28 14:23:46 +02007 IT8716F Super I/O chip w/LPC interface
Jean Delvare87673dd2006-08-28 14:37:19 +02008 IT8718F Super I/O chip w/LPC interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 Sis950 A clone of the IT8705F
10
11 Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
12 Largely inspired by lm78.c of the same package
13
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
29/*
30 djg@pdp8.net David Gesswein 7/18/01
31 Modified to fix bug with not all alarms enabled.
32 Added ability to read battery voltage and select temperature sensor
33 type at module load time.
34*/
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/jiffies.h>
40#include <linux/i2c.h>
Jean Delvarefde09502005-07-19 23:51:07 +020041#include <linux/i2c-isa.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040042#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020043#include <linux/hwmon-sysfs.h>
44#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040045#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010046#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/io.h>
48
49
50/* Addresses to scan */
Jean Delvarec5e3fbf2006-01-18 22:39:48 +010051static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
Jean Delvare91749992005-10-08 00:10:00 +020052static unsigned short isa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* Insmod parameters */
Jean Delvare87673dd2006-08-28 14:37:19 +020055I2C_CLIENT_INSMOD_4(it87, it8712, it8716, it8718);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define REG 0x2e /* The register to read/write */
58#define DEV 0x07 /* Register: Logical device select */
59#define VAL 0x2f /* The value to read/write */
60#define PME 0x04 /* The device with the fan registers in it */
Jean Delvare87673dd2006-08-28 14:37:19 +020061#define GPIO 0x07 /* The device with the IT8718F VID value in it */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define DEVID 0x20 /* Register: Device ID */
63#define DEVREV 0x22 /* Register: Device Revision */
64
65static inline int
66superio_inb(int reg)
67{
68 outb(reg, REG);
69 return inb(VAL);
70}
71
72static int superio_inw(int reg)
73{
74 int val;
75 outb(reg++, REG);
76 val = inb(VAL) << 8;
77 outb(reg, REG);
78 val |= inb(VAL);
79 return val;
80}
81
82static inline void
Jean Delvare87673dd2006-08-28 14:37:19 +020083superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +020086 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89static inline void
90superio_enter(void)
91{
92 outb(0x87, REG);
93 outb(0x01, REG);
94 outb(0x55, REG);
95 outb(0x55, REG);
96}
97
98static inline void
99superio_exit(void)
100{
101 outb(0x02, REG);
102 outb(0x02, VAL);
103}
104
Jean Delvare87673dd2006-08-28 14:37:19 +0200105/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define IT8712F_DEVID 0x8712
107#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200108#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200109#define IT8718F_DEVID 0x8718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define IT87_ACT_REG 0x30
111#define IT87_BASE_REG 0x60
112
Jean Delvare87673dd2006-08-28 14:37:19 +0200113/* Logical device 7 registers (IT8712F and later) */
114#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
115#define IT87_SIO_VID_REG 0xfc /* VID value */
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* Update battery voltage after every reading if true */
118static int update_vbat;
119
120/* Not all BIOSes properly configure the PWM registers */
121static int fix_pwm_polarity;
122
Jean Delvare87673dd2006-08-28 14:37:19 +0200123/* Values read from Super-I/O config space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static u16 chip_type;
Jean Delvare87673dd2006-08-28 14:37:19 +0200125static u8 vid_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* Many IT87 constants specified below */
128
129/* Length of ISA address segment */
130#define IT87_EXTENT 8
131
132/* Where are the ISA address/data registers relative to the base address */
133#define IT87_ADDR_REG_OFFSET 5
134#define IT87_DATA_REG_OFFSET 6
135
136/*----- The IT87 registers -----*/
137
138#define IT87_REG_CONFIG 0x00
139
140#define IT87_REG_ALARM1 0x01
141#define IT87_REG_ALARM2 0x02
142#define IT87_REG_ALARM3 0x03
143
Jean Delvare87673dd2006-08-28 14:37:19 +0200144/* The IT8718F has the VID value in a different register, in Super-I/O
145 configuration space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define IT87_REG_VID 0x0a
Jean Delvare17d648b2006-08-28 14:23:46 +0200147/* Warning: register 0x0b is used for something completely different in
148 new chips/revisions. I suspect only 16-bit tachometer mode will work
149 for these. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200151#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
154
155#define IT87_REG_FAN(nr) (0x0d + (nr))
156#define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
Jean Delvare17d648b2006-08-28 14:23:46 +0200157#define IT87_REG_FANX(nr) (0x18 + (nr))
158#define IT87_REG_FANX_MIN(nr) (0x1b + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define IT87_REG_FAN_MAIN_CTRL 0x13
160#define IT87_REG_FAN_CTL 0x14
161#define IT87_REG_PWM(nr) (0x15 + (nr))
162
163#define IT87_REG_VIN(nr) (0x20 + (nr))
164#define IT87_REG_TEMP(nr) (0x29 + (nr))
165
166#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
167#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
168#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
169#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
170
171#define IT87_REG_I2C_ADDR 0x48
172
173#define IT87_REG_VIN_ENABLE 0x50
174#define IT87_REG_TEMP_ENABLE 0x51
175
176#define IT87_REG_CHIPID 0x58
177
178#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
179#define IN_FROM_REG(val) ((val) * 16)
180
181static inline u8 FAN_TO_REG(long rpm, int div)
182{
183 if (rpm == 0)
184 return 255;
185 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
186 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
187 254);
188}
189
Jean Delvare17d648b2006-08-28 14:23:46 +0200190static inline u16 FAN16_TO_REG(long rpm)
191{
192 if (rpm == 0)
193 return 0xffff;
194 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
Jean Delvare17d648b2006-08-28 14:23:46 +0200198/* The divider is fixed to 2 in 16-bit mode */
199#define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
202 ((val)+500)/1000),-128,127))
203#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205#define PWM_TO_REG(val) ((val) >> 1)
206#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
207
208static int DIV_TO_REG(int val)
209{
210 int answer = 0;
Jean Delvareb9e349f2006-08-28 14:26:22 +0200211 while (answer < 7 && (val >>= 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 answer++;
213 return answer;
214}
215#define DIV_FROM_REG(val) (1 << (val))
216
217
218/* For each registered IT87, we need to keep some data in memory. That
219 data is pointed to by it87_list[NR]->data. The structure itself is
220 dynamically allocated, at the same time when a new it87 client is
221 allocated. */
222struct it87_data {
223 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400224 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100225 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 enum chips type;
227
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100228 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 char valid; /* !=0 if following fields are valid */
230 unsigned long last_updated; /* In jiffies */
231
232 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200233 u8 in_max[8]; /* Register value */
234 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200235 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +0200236 u16 fan[3]; /* Register values, possibly combined */
237 u16 fan_min[3]; /* Register values, possibly combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 u8 temp[3]; /* Register value */
239 u8 temp_high[3]; /* Register value */
240 u8 temp_low[3]; /* Register value */
241 u8 sensor; /* Register value */
242 u8 fan_div[3]; /* Register encoding, shifted right */
243 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100244 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 u32 alarms; /* Register encoding, combined */
246 u8 fan_main_ctrl; /* Register value */
247 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
248};
249
250
251static int it87_attach_adapter(struct i2c_adapter *adapter);
Jean Delvare2d8672c2005-07-19 23:56:35 +0200252static int it87_isa_attach_adapter(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
254static int it87_detach_client(struct i2c_client *client);
255
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100256static int it87_read_value(struct i2c_client *client, u8 reg);
257static int it87_write_value(struct i2c_client *client, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static struct it87_data *it87_update_device(struct device *dev);
259static int it87_check_pwm(struct i2c_client *client);
260static void it87_init_client(struct i2c_client *client, struct it87_data *data);
261
262
263static struct i2c_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100264 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100265 .name = "it87",
266 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 .id = I2C_DRIVERID_IT87,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 .attach_adapter = it87_attach_adapter,
269 .detach_client = it87_detach_client,
270};
271
Jean Delvarefde09502005-07-19 23:51:07 +0200272static struct i2c_driver it87_isa_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100273 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200274 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100275 .name = "it87-isa",
276 },
Jean Delvare2d8672c2005-07-19 23:56:35 +0200277 .attach_adapter = it87_isa_attach_adapter,
Jean Delvarefde09502005-07-19 23:51:07 +0200278 .detach_client = it87_detach_client,
279};
280
281
Jean Delvare20ad93d2005-06-05 11:53:25 +0200282static ssize_t show_in(struct device *dev, struct device_attribute *attr,
283 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200285 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
286 int nr = sensor_attr->index;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct it87_data *data = it87_update_device(dev);
289 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
290}
291
Jean Delvare20ad93d2005-06-05 11:53:25 +0200292static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
293 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200295 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
296 int nr = sensor_attr->index;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct it87_data *data = it87_update_device(dev);
299 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
300}
301
Jean Delvare20ad93d2005-06-05 11:53:25 +0200302static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
303 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200305 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
306 int nr = sensor_attr->index;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct it87_data *data = it87_update_device(dev);
309 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
310}
311
Jean Delvare20ad93d2005-06-05 11:53:25 +0200312static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
313 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200315 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
316 int nr = sensor_attr->index;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct i2c_client *client = to_i2c_client(dev);
319 struct it87_data *data = i2c_get_clientdata(client);
320 unsigned long val = simple_strtoul(buf, NULL, 10);
321
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100322 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 data->in_min[nr] = IN_TO_REG(val);
324 it87_write_value(client, IT87_REG_VIN_MIN(nr),
325 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100326 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return count;
328}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200329static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
330 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200332 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
333 int nr = sensor_attr->index;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct i2c_client *client = to_i2c_client(dev);
336 struct it87_data *data = i2c_get_clientdata(client);
337 unsigned long val = simple_strtoul(buf, NULL, 10);
338
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100339 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 data->in_max[nr] = IN_TO_REG(val);
341 it87_write_value(client, IT87_REG_VIN_MAX(nr),
342 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100343 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return count;
345}
346
347#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200348static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
349 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200352static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
353 show_in_min, set_in_min, offset); \
354static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
355 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357show_in_offset(0);
358limit_in_offset(0);
359show_in_offset(1);
360limit_in_offset(1);
361show_in_offset(2);
362limit_in_offset(2);
363show_in_offset(3);
364limit_in_offset(3);
365show_in_offset(4);
366limit_in_offset(4);
367show_in_offset(5);
368limit_in_offset(5);
369show_in_offset(6);
370limit_in_offset(6);
371show_in_offset(7);
372limit_in_offset(7);
373show_in_offset(8);
374
375/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200376static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
377 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200379 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
380 int nr = sensor_attr->index;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct it87_data *data = it87_update_device(dev);
383 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
384}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200385static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
386 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200388 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
389 int nr = sensor_attr->index;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct it87_data *data = it87_update_device(dev);
392 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
393}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200394static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
395 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200397 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
398 int nr = sensor_attr->index;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct it87_data *data = it87_update_device(dev);
401 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
402}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200403static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
404 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200406 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
407 int nr = sensor_attr->index;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct i2c_client *client = to_i2c_client(dev);
410 struct it87_data *data = i2c_get_clientdata(client);
411 int val = simple_strtol(buf, NULL, 10);
412
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100413 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 data->temp_high[nr] = TEMP_TO_REG(val);
415 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100416 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return count;
418}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200419static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
420 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200422 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
423 int nr = sensor_attr->index;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 struct i2c_client *client = to_i2c_client(dev);
426 struct it87_data *data = i2c_get_clientdata(client);
427 int val = simple_strtol(buf, NULL, 10);
428
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100429 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 data->temp_low[nr] = TEMP_TO_REG(val);
431 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100432 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return count;
434}
435#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200436static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
437 show_temp, NULL, offset - 1); \
438static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
439 show_temp_max, set_temp_max, offset - 1); \
440static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
441 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443show_temp_offset(1);
444show_temp_offset(2);
445show_temp_offset(3);
446
Jean Delvare20ad93d2005-06-05 11:53:25 +0200447static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
448 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200450 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
451 int nr = sensor_attr->index;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct it87_data *data = it87_update_device(dev);
454 u8 reg = data->sensor; /* In case the value is updated while we use it */
455
456 if (reg & (1 << nr))
457 return sprintf(buf, "3\n"); /* thermal diode */
458 if (reg & (8 << nr))
459 return sprintf(buf, "2\n"); /* thermistor */
460 return sprintf(buf, "0\n"); /* disabled */
461}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200462static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
463 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200465 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
466 int nr = sensor_attr->index;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct i2c_client *client = to_i2c_client(dev);
469 struct it87_data *data = i2c_get_clientdata(client);
470 int val = simple_strtol(buf, NULL, 10);
471
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100472 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 data->sensor &= ~(1 << nr);
475 data->sensor &= ~(8 << nr);
476 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
477 if (val == 3)
478 data->sensor |= 1 << nr;
479 else if (val == 2)
480 data->sensor |= 8 << nr;
481 else if (val != 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100482 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -EINVAL;
484 }
485 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100486 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return count;
488}
489#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200490static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
491 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493show_sensor_offset(1);
494show_sensor_offset(2);
495show_sensor_offset(3);
496
497/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200498static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
499 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200501 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
502 int nr = sensor_attr->index;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct it87_data *data = it87_update_device(dev);
505 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
506 DIV_FROM_REG(data->fan_div[nr])));
507}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200508static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
509 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200511 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
512 int nr = sensor_attr->index;
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct it87_data *data = it87_update_device(dev);
515 return sprintf(buf,"%d\n",
516 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
517}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200518static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
519 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200521 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
522 int nr = sensor_attr->index;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct it87_data *data = it87_update_device(dev);
525 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
526}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200527static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
528 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200530 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
531 int nr = sensor_attr->index;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 struct it87_data *data = it87_update_device(dev);
534 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
535}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200536static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
537 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200539 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
540 int nr = sensor_attr->index;
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct it87_data *data = it87_update_device(dev);
543 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
544}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200545static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
546 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200548 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
549 int nr = sensor_attr->index;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct i2c_client *client = to_i2c_client(dev);
552 struct it87_data *data = i2c_get_clientdata(client);
553 int val = simple_strtol(buf, NULL, 10);
Jean Delvare07eab462005-11-23 15:44:31 -0800554 u8 reg = it87_read_value(client, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100556 mutex_lock(&data->update_lock);
Jean Delvare07eab462005-11-23 15:44:31 -0800557 switch (nr) {
558 case 0: data->fan_div[nr] = reg & 0x07; break;
559 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
560 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
561 }
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
564 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100565 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return count;
567}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200568static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
569 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200571 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
572 int nr = sensor_attr->index;
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 struct i2c_client *client = to_i2c_client(dev);
575 struct it87_data *data = i2c_get_clientdata(client);
Jean Delvareb9e349f2006-08-28 14:26:22 +0200576 unsigned long val = simple_strtoul(buf, NULL, 10);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200577 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 u8 old;
579
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100580 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 old = it87_read_value(client, IT87_REG_FAN_DIV);
582
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200583 /* Save fan min limit */
584 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 switch (nr) {
587 case 0:
588 case 1:
589 data->fan_div[nr] = DIV_TO_REG(val);
590 break;
591 case 2:
592 if (val < 8)
593 data->fan_div[nr] = 1;
594 else
595 data->fan_div[nr] = 3;
596 }
597 val = old & 0x80;
598 val |= (data->fan_div[0] & 0x07);
599 val |= (data->fan_div[1] & 0x07) << 3;
600 if (data->fan_div[2] == 3)
601 val |= 0x1 << 6;
602 it87_write_value(client, IT87_REG_FAN_DIV, val);
603
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200604 /* Restore fan min limit */
605 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
606 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
607
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100608 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return count;
610}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200611static ssize_t set_pwm_enable(struct device *dev,
612 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200614 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
615 int nr = sensor_attr->index;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct i2c_client *client = to_i2c_client(dev);
618 struct it87_data *data = i2c_get_clientdata(client);
619 int val = simple_strtol(buf, NULL, 10);
620
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100621 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (val == 0) {
624 int tmp;
625 /* make sure the fan is on when in on/off mode */
626 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
627 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
628 /* set on/off mode */
629 data->fan_main_ctrl &= ~(1 << nr);
630 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
631 } else if (val == 1) {
632 /* set SmartGuardian mode */
633 data->fan_main_ctrl |= (1 << nr);
634 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
635 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
636 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
637 } else {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100638 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return -EINVAL;
640 }
641
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100642 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return count;
644}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200645static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
646 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200648 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
649 int nr = sensor_attr->index;
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct i2c_client *client = to_i2c_client(dev);
652 struct it87_data *data = i2c_get_clientdata(client);
653 int val = simple_strtol(buf, NULL, 10);
654
655 if (val < 0 || val > 255)
656 return -EINVAL;
657
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100658 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 data->manual_pwm_ctl[nr] = val;
660 if (data->fan_main_ctrl & (1 << nr))
661 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100662 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return count;
664}
665
Jean Delvare20ad93d2005-06-05 11:53:25 +0200666#define show_fan_offset(offset) \
667static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
668 show_fan, NULL, offset - 1); \
669static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
670 show_fan_min, set_fan_min, offset - 1); \
671static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
672 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674show_fan_offset(1);
675show_fan_offset(2);
676show_fan_offset(3);
677
678#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200679static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
680 show_pwm_enable, set_pwm_enable, offset - 1); \
681static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
682 show_pwm, set_pwm, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684show_pwm_offset(1);
685show_pwm_offset(2);
686show_pwm_offset(3);
687
Jean Delvare17d648b2006-08-28 14:23:46 +0200688/* A different set of callbacks for 16-bit fans */
689static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
690 char *buf)
691{
692 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
693 int nr = sensor_attr->index;
694 struct it87_data *data = it87_update_device(dev);
695 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
696}
697
698static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
699 char *buf)
700{
701 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
702 int nr = sensor_attr->index;
703 struct it87_data *data = it87_update_device(dev);
704 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
705}
706
707static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
708 const char *buf, size_t count)
709{
710 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
711 int nr = sensor_attr->index;
712 struct i2c_client *client = to_i2c_client(dev);
713 struct it87_data *data = i2c_get_clientdata(client);
714 int val = simple_strtol(buf, NULL, 10);
715
716 mutex_lock(&data->update_lock);
717 data->fan_min[nr] = FAN16_TO_REG(val);
718 it87_write_value(client, IT87_REG_FAN_MIN(nr),
719 data->fan_min[nr] & 0xff);
720 it87_write_value(client, IT87_REG_FANX_MIN(nr),
721 data->fan_min[nr] >> 8);
722 mutex_unlock(&data->update_lock);
723 return count;
724}
725
726/* We want to use the same sysfs file names as 8-bit fans, but we need
727 different variable names, so we have to use SENSOR_ATTR instead of
728 SENSOR_DEVICE_ATTR. */
729#define show_fan16_offset(offset) \
730static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
731 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
732 show_fan16, NULL, offset - 1); \
733static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
734 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
735 show_fan16_min, set_fan16_min, offset - 1)
736
737show_fan16_offset(1);
738show_fan16_offset(2);
739show_fan16_offset(3);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400742static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200745 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
Jean Delvare1d66c642005-04-18 21:16:59 -0700747static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400750show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 struct it87_data *data = it87_update_device(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +0100753 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400756store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct i2c_client *client = to_i2c_client(dev);
759 struct it87_data *data = i2c_get_clientdata(client);
760 u32 val;
761
762 val = simple_strtoul(buf, NULL, 10);
763 data->vrm = val;
764
765 return count;
766}
767static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
768#define device_create_file_vrm(client) \
769device_create_file(&client->dev, &dev_attr_vrm)
770
771static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400772show_vid_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);
775 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
776}
777static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
778#define device_create_file_vid(client) \
779device_create_file(&client->dev, &dev_attr_cpu0_vid)
780
781/* This function is called when:
782 * it87_driver is inserted (when this module is loaded), for each
783 available adapter
784 * when a new adapter is inserted (and it87_driver is still present) */
785static int it87_attach_adapter(struct i2c_adapter *adapter)
786{
787 if (!(adapter->class & I2C_CLASS_HWMON))
788 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200789 return i2c_probe(adapter, &addr_data, it87_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Jean Delvare2d8672c2005-07-19 23:56:35 +0200792static int it87_isa_attach_adapter(struct i2c_adapter *adapter)
793{
794 return it87_detect(adapter, isa_address, -1);
795}
796
797/* SuperIO detection - will change isa_address if a chip is found */
Jean Delvare91749992005-10-08 00:10:00 +0200798static int __init it87_find(unsigned short *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 int err = -ENODEV;
801
802 superio_enter();
803 chip_type = superio_inw(DEVID);
804 if (chip_type != IT8712F_DEVID
Jean Delvare17d648b2006-08-28 14:23:46 +0200805 && chip_type != IT8716F_DEVID
Jean Delvare87673dd2006-08-28 14:37:19 +0200806 && chip_type != IT8718F_DEVID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 && chip_type != IT8705F_DEVID)
808 goto exit;
809
Jean Delvare87673dd2006-08-28 14:37:19 +0200810 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
812 pr_info("it87: Device not activated, skipping\n");
813 goto exit;
814 }
815
816 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
817 if (*address == 0) {
818 pr_info("it87: Base address not set, skipping\n");
819 goto exit;
820 }
821
822 err = 0;
823 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
824 chip_type, *address, superio_inb(DEVREV) & 0x0f);
825
Jean Delvare87673dd2006-08-28 14:37:19 +0200826 /* Read GPIO config and VID value from LDN 7 (GPIO) */
827 if (chip_type != IT8705F_DEVID) {
828 int reg;
829
830 superio_select(GPIO);
831 if (chip_type == it8718)
832 vid_value = superio_inb(IT87_SIO_VID_REG);
833
834 reg = superio_inb(IT87_SIO_PINX2_REG);
835 if (reg & (1 << 0))
836 pr_info("it87: in3 is VCC (+5V)\n");
837 if (reg & (1 << 1))
838 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
839 }
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841exit:
842 superio_exit();
843 return err;
844}
845
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200846/* This function is called by i2c_probe */
Ben Dooksc49efce2005-10-26 21:07:25 +0200847static int it87_detect(struct i2c_adapter *adapter, int address, int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 int i;
850 struct i2c_client *new_client;
851 struct it87_data *data;
852 int err = 0;
853 const char *name = "";
854 int is_isa = i2c_is_isa_adapter(adapter);
855 int enable_pwm_interface;
856
857 if (!is_isa &&
858 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
859 goto ERROR0;
860
861 /* Reserve the ISA region */
862 if (is_isa)
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100863 if (!request_region(address, IT87_EXTENT,
864 it87_isa_driver.driver.name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 goto ERROR0;
866
Jean Delvare91749992005-10-08 00:10:00 +0200867 /* For now, we presume we have a valid client. We create the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 client structure, even though we cannot fill it completely yet.
869 But it allows us to access it87_{read,write}_value. */
870
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200871 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 err = -ENOMEM;
873 goto ERROR1;
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 new_client = &data->client;
877 if (is_isa)
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100878 mutex_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 i2c_set_clientdata(new_client, data);
880 new_client->addr = address;
881 new_client->adapter = adapter;
Jean Delvarefde09502005-07-19 23:51:07 +0200882 new_client->driver = is_isa ? &it87_isa_driver : &it87_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 new_client->flags = 0;
884
885 /* Now, we do the remaining detection. */
886
887 if (kind < 0) {
888 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
889 || (!is_isa
890 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
891 err = -ENODEV;
892 goto ERROR2;
893 }
894 }
895
896 /* Determine the chip type. */
897 if (kind <= 0) {
898 i = it87_read_value(new_client, IT87_REG_CHIPID);
899 if (i == 0x90) {
900 kind = it87;
Jean Delvare17d648b2006-08-28 14:23:46 +0200901 if (is_isa) {
902 switch (chip_type) {
903 case IT8712F_DEVID:
904 kind = it8712;
905 break;
906 case IT8716F_DEVID:
907 kind = it8716;
908 break;
Jean Delvare87673dd2006-08-28 14:37:19 +0200909 case IT8718F_DEVID:
910 kind = it8718;
911 break;
Jean Delvare17d648b2006-08-28 14:23:46 +0200912 }
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915 else {
916 if (kind == 0)
917 dev_info(&adapter->dev,
918 "Ignoring 'force' parameter for unknown chip at "
919 "adapter %d, address 0x%02x\n",
920 i2c_adapter_id(adapter), address);
921 err = -ENODEV;
922 goto ERROR2;
923 }
924 }
925
926 if (kind == it87) {
927 name = "it87";
928 } else if (kind == it8712) {
929 name = "it8712";
Jean Delvare17d648b2006-08-28 14:23:46 +0200930 } else if (kind == it8716) {
931 name = "it8716";
Jean Delvare87673dd2006-08-28 14:37:19 +0200932 } else if (kind == it8718) {
933 name = "it8718";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
936 /* Fill in the remaining client fields and put it into the global list */
937 strlcpy(new_client->name, name, I2C_NAME_SIZE);
938 data->type = kind;
939 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100940 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 /* Tell the I2C layer a new client has arrived */
943 if ((err = i2c_attach_client(new_client)))
944 goto ERROR2;
945
Jean Delvarec5e3fbf2006-01-18 22:39:48 +0100946 if (!is_isa)
947 dev_info(&new_client->dev, "The I2C interface to IT87xxF "
948 "hardware monitoring chips is deprecated. Please "
949 "report if you still rely on it.\n");
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 /* Check PWM configuration */
952 enable_pwm_interface = it87_check_pwm(new_client);
953
954 /* Initialize the IT87 chip */
955 it87_init_client(new_client, data);
956
957 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400958 data->class_dev = hwmon_device_register(&new_client->dev);
959 if (IS_ERR(data->class_dev)) {
960 err = PTR_ERR(data->class_dev);
961 goto ERROR3;
962 }
963
Jean Delvare20ad93d2005-06-05 11:53:25 +0200964 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
965 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
966 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
967 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
968 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
969 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
970 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
971 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
972 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
973 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
974 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
975 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
976 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
977 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
978 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
979 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
980 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
981 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
982 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
983 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
984 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
985 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
986 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
987 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
988 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
989 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
990 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
991 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
992 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
993 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
994 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
995 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
996 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
997 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
998 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
999 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
1000 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
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)) {
1006 device_create_file(&new_client->dev,
1007 &sensor_dev_attr_fan1_input16.dev_attr);
1008 device_create_file(&new_client->dev,
1009 &sensor_dev_attr_fan1_min16.dev_attr);
1010 }
1011 if (data->has_fan & (1 << 1)) {
1012 device_create_file(&new_client->dev,
1013 &sensor_dev_attr_fan2_input16.dev_attr);
1014 device_create_file(&new_client->dev,
1015 &sensor_dev_attr_fan2_min16.dev_attr);
1016 }
1017 if (data->has_fan & (1 << 2)) {
1018 device_create_file(&new_client->dev,
1019 &sensor_dev_attr_fan3_input16.dev_attr);
1020 device_create_file(&new_client->dev,
1021 &sensor_dev_attr_fan3_min16.dev_attr);
1022 }
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)) {
1026 device_create_file(&new_client->dev,
1027 &sensor_dev_attr_fan1_input.dev_attr);
1028 device_create_file(&new_client->dev,
1029 &sensor_dev_attr_fan1_min.dev_attr);
1030 device_create_file(&new_client->dev,
1031 &sensor_dev_attr_fan1_div.dev_attr);
1032 }
1033 if (data->has_fan & (1 << 1)) {
1034 device_create_file(&new_client->dev,
1035 &sensor_dev_attr_fan2_input.dev_attr);
1036 device_create_file(&new_client->dev,
1037 &sensor_dev_attr_fan2_min.dev_attr);
1038 device_create_file(&new_client->dev,
1039 &sensor_dev_attr_fan2_div.dev_attr);
1040 }
1041 if (data->has_fan & (1 << 2)) {
1042 device_create_file(&new_client->dev,
1043 &sensor_dev_attr_fan3_input.dev_attr);
1044 device_create_file(&new_client->dev,
1045 &sensor_dev_attr_fan3_min.dev_attr);
1046 device_create_file(&new_client->dev,
1047 &sensor_dev_attr_fan3_div.dev_attr);
1048 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001049 }
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 device_create_file(&new_client->dev, &dev_attr_alarms);
1052 if (enable_pwm_interface) {
Jean Delvare20ad93d2005-06-05 11:53:25 +02001053 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
1054 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
1055 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
1056 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
1057 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
1058 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060
Jean Delvare87673dd2006-08-28 14:37:19 +02001061 if (data->type == it8712 || data->type == it8716
1062 || data->type == it8718) {
Jean Delvare303760b2005-07-31 21:52:01 +02001063 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001064 /* VID reading from Super-I/O config space if available */
1065 data->vid = vid_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 device_create_file_vrm(new_client);
1067 device_create_file_vid(new_client);
1068 }
1069
1070 return 0;
1071
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001072ERROR3:
1073 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074ERROR2:
1075 kfree(data);
1076ERROR1:
1077 if (is_isa)
1078 release_region(address, IT87_EXTENT);
1079ERROR0:
1080 return err;
1081}
1082
1083static int it87_detach_client(struct i2c_client *client)
1084{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001085 struct it87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int err;
1087
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001088 hwmon_device_unregister(data->class_dev);
1089
Jean Delvare7bef5592005-07-27 22:14:49 +02001090 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if(i2c_is_isa_client(client))
1094 release_region(client->addr, IT87_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001095 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 return 0;
1098}
1099
Steven Cole44bbe872005-05-03 18:21:25 -06001100/* The SMBus locks itself, but ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 We don't want to lock the whole ISA bus, so we lock each client
1102 separately.
1103 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1104 would slow down the IT87 access and should not be necessary. */
1105static int it87_read_value(struct i2c_client *client, u8 reg)
1106{
1107 struct it87_data *data = i2c_get_clientdata(client);
1108
1109 int res;
1110 if (i2c_is_isa_client(client)) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001111 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1113 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001114 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return res;
1116 } else
1117 return i2c_smbus_read_byte_data(client, reg);
1118}
1119
Steven Cole44bbe872005-05-03 18:21:25 -06001120/* The SMBus locks itself, but ISA access muse be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 We don't want to lock the whole ISA bus, so we lock each client
1122 separately.
1123 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1124 would slow down the IT87 access and should not be necessary. */
1125static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
1126{
1127 struct it87_data *data = i2c_get_clientdata(client);
1128
1129 if (i2c_is_isa_client(client)) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001130 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1132 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001133 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return 0;
1135 } else
1136 return i2c_smbus_write_byte_data(client, reg, value);
1137}
1138
1139/* Return 1 if and only if the PWM interface is safe to use */
1140static int it87_check_pwm(struct i2c_client *client)
1141{
1142 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1143 * and polarity set to active low is sign that this is the case so we
1144 * disable pwm control to protect the user. */
1145 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1146 if ((tmp & 0x87) == 0) {
1147 if (fix_pwm_polarity) {
1148 /* The user asks us to attempt a chip reconfiguration.
1149 * This means switching to active high polarity and
1150 * inverting all fan speed values. */
1151 int i;
1152 u8 pwm[3];
1153
1154 for (i = 0; i < 3; i++)
1155 pwm[i] = it87_read_value(client,
1156 IT87_REG_PWM(i));
1157
1158 /* If any fan is in automatic pwm mode, the polarity
1159 * might be correct, as suspicious as it seems, so we
1160 * better don't change anything (but still disable the
1161 * PWM interface). */
1162 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1163 dev_info(&client->dev, "Reconfiguring PWM to "
1164 "active high polarity\n");
1165 it87_write_value(client, IT87_REG_FAN_CTL,
1166 tmp | 0x87);
1167 for (i = 0; i < 3; i++)
1168 it87_write_value(client,
1169 IT87_REG_PWM(i),
1170 0x7f & ~pwm[i]);
1171 return 1;
1172 }
1173
1174 dev_info(&client->dev, "PWM configuration is "
1175 "too broken to be fixed\n");
1176 }
1177
1178 dev_info(&client->dev, "Detected broken BIOS "
1179 "defaults, disabling PWM interface\n");
1180 return 0;
1181 } else if (fix_pwm_polarity) {
1182 dev_info(&client->dev, "PWM configuration looks "
1183 "sane, won't touch\n");
1184 }
1185
1186 return 1;
1187}
1188
1189/* Called when we have found a new IT87. */
1190static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1191{
1192 int tmp, i;
1193
1194 /* initialize to sane defaults:
1195 * - if the chip is in manual pwm mode, this will be overwritten with
1196 * the actual settings on the chip (so in this case, initialization
1197 * is not needed)
1198 * - if in automatic or on/off mode, we could switch to manual mode,
1199 * read the registers and set manual_pwm_ctl accordingly, but currently
1200 * this is not implemented, so we initialize to something sane */
1201 for (i = 0; i < 3; i++) {
1202 data->manual_pwm_ctl[i] = 0xff;
1203 }
1204
Jean Delvarec5df9b72006-08-28 14:37:54 +02001205 /* Some chips seem to have default value 0xff for all limit
1206 * registers. For low voltage limits it makes no sense and triggers
1207 * alarms, so change to 0 instead. For high temperature limits, it
1208 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1209 * but is still confusing, so change to 127 degrees C. */
1210 for (i = 0; i < 8; i++) {
1211 tmp = it87_read_value(client, IT87_REG_VIN_MIN(i));
1212 if (tmp == 0xff)
1213 it87_write_value(client, IT87_REG_VIN_MIN(i), 0);
1214 }
1215 for (i = 0; i < 3; i++) {
1216 tmp = it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1217 if (tmp == 0xff)
1218 it87_write_value(client, IT87_REG_TEMP_HIGH(i), 127);
1219 }
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /* Check if temperature channnels are reset manually or by some reason */
1222 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1223 if ((tmp & 0x3f) == 0) {
1224 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1225 tmp = (tmp & 0xc0) | 0x2a;
1226 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1227 }
1228 data->sensor = tmp;
1229
1230 /* Check if voltage monitors are reset manually or by some reason */
1231 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1232 if ((tmp & 0xff) == 0) {
1233 /* Enable all voltage monitors */
1234 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1235 }
1236
1237 /* Check if tachometers are reset manually or by some reason */
1238 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1239 if ((data->fan_main_ctrl & 0x70) == 0) {
1240 /* Enable all fan tachometers */
1241 data->fan_main_ctrl |= 0x70;
1242 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1243 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001244 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Jean Delvare17d648b2006-08-28 14:23:46 +02001246 /* Set tachometers to 16-bit mode if needed */
Jean Delvare87673dd2006-08-28 14:37:19 +02001247 if (data->type == it8716 || data->type == it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001248 tmp = it87_read_value(client, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001249 if (~tmp & 0x07 & data->has_fan) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001250 dev_dbg(&client->dev,
1251 "Setting fan1-3 to 16-bit mode\n");
1252 it87_write_value(client, IT87_REG_FAN_16BIT,
1253 tmp | 0x07);
1254 }
1255 }
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /* Set current fan mode registers and the default settings for the
1258 * other mode registers */
1259 for (i = 0; i < 3; i++) {
1260 if (data->fan_main_ctrl & (1 << i)) {
1261 /* pwm mode */
1262 tmp = it87_read_value(client, IT87_REG_PWM(i));
1263 if (tmp & 0x80) {
1264 /* automatic pwm - not yet implemented, but
1265 * leave the settings made by the BIOS alone
1266 * until a change is requested via the sysfs
1267 * interface */
1268 } else {
1269 /* manual pwm */
1270 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1271 }
1272 }
1273 }
1274
1275 /* Start monitoring */
1276 it87_write_value(client, IT87_REG_CONFIG,
1277 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1278 | (update_vbat ? 0x41 : 0x01));
1279}
1280
1281static struct it87_data *it87_update_device(struct device *dev)
1282{
1283 struct i2c_client *client = to_i2c_client(dev);
1284 struct it87_data *data = i2c_get_clientdata(client);
1285 int i;
1286
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001287 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1290 || !data->valid) {
1291
1292 if (update_vbat) {
1293 /* Cleared after each update, so reenable. Value
1294 returned by this read will be previous value */
1295 it87_write_value(client, IT87_REG_CONFIG,
1296 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1297 }
1298 for (i = 0; i <= 7; i++) {
1299 data->in[i] =
1300 it87_read_value(client, IT87_REG_VIN(i));
1301 data->in_min[i] =
1302 it87_read_value(client, IT87_REG_VIN_MIN(i));
1303 data->in_max[i] =
1304 it87_read_value(client, IT87_REG_VIN_MAX(i));
1305 }
Jean Delvare3543a532006-08-28 14:27:25 +02001306 /* in8 (battery) has no limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 data->in[8] =
1308 it87_read_value(client, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 for (i = 0; i < 3; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02001311 /* Skip disabled fans */
1312 if (!(data->has_fan & (1 << i)))
1313 continue;
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 data->fan_min[i] =
1316 it87_read_value(client, IT87_REG_FAN_MIN(i));
Jean Delvare17d648b2006-08-28 14:23:46 +02001317 data->fan[i] = it87_read_value(client,
1318 IT87_REG_FAN(i));
1319 /* Add high byte if in 16-bit mode */
Jean Delvare87673dd2006-08-28 14:37:19 +02001320 if (data->type == it8716 || data->type == it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001321 data->fan[i] |= it87_read_value(client,
1322 IT87_REG_FANX(i)) << 8;
1323 data->fan_min[i] |= it87_read_value(client,
1324 IT87_REG_FANX_MIN(i)) << 8;
1325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327 for (i = 0; i < 3; i++) {
1328 data->temp[i] =
1329 it87_read_value(client, IT87_REG_TEMP(i));
1330 data->temp_high[i] =
1331 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1332 data->temp_low[i] =
1333 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1334 }
1335
Jean Delvare17d648b2006-08-28 14:23:46 +02001336 /* Newer chips don't have clock dividers */
Jean Delvare87673dd2006-08-28 14:37:19 +02001337 if ((data->has_fan & 0x07) && data->type != it8716
1338 && data->type != it8718) {
Jean Delvare17d648b2006-08-28 14:23:46 +02001339 i = it87_read_value(client, IT87_REG_FAN_DIV);
1340 data->fan_div[0] = i & 0x07;
1341 data->fan_div[1] = (i >> 3) & 0x07;
1342 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 data->alarms =
1346 it87_read_value(client, IT87_REG_ALARM1) |
1347 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1348 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1349 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1350
1351 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1352 /* The 8705 does not have VID capability */
Jean Delvare17d648b2006-08-28 14:23:46 +02001353 if (data->type == it8712 || data->type == it8716) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 data->vid = it87_read_value(client, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02001355 /* The older IT8712F revisions had only 5 VID pins,
1356 but we assume it is always safe to read 6 bits. */
1357 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 data->last_updated = jiffies;
1360 data->valid = 1;
1361 }
1362
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001363 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 return data;
1366}
1367
1368static int __init sm_it87_init(void)
1369{
Jean Delvare91749992005-10-08 00:10:00 +02001370 int res;
Jean Delvarefde09502005-07-19 23:51:07 +02001371
1372 res = i2c_add_driver(&it87_driver);
1373 if (res)
1374 return res;
1375
Jean Delvare91749992005-10-08 00:10:00 +02001376 if (!it87_find(&isa_address)) {
1377 res = i2c_isa_add_driver(&it87_isa_driver);
1378 if (res) {
1379 i2c_del_driver(&it87_driver);
1380 return res;
1381 }
Jean Delvarefde09502005-07-19 23:51:07 +02001382 }
1383
1384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
1387static void __exit sm_it87_exit(void)
1388{
Jean Delvarebe79c382006-02-07 17:53:32 +01001389 if (isa_address)
1390 i2c_isa_del_driver(&it87_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 i2c_del_driver(&it87_driver);
1392}
1393
1394
1395MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
Jean Delvare87673dd2006-08-28 14:37:19 +02001396MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397module_param(update_vbat, bool, 0);
1398MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1399module_param(fix_pwm_polarity, bool, 0);
1400MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1401MODULE_LICENSE("GPL");
1402
1403module_init(sm_it87_init);
1404module_exit(sm_it87_exit);