blob: ca0a723cbce526efae9b365dd3da6df1bc738bb6 [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
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05005 The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 addition to an Environment Controller (Enhanced Hardware Monitor and
8 Fan Controller)
9
10 This driver supports only the Environment Controller in the IT8705F and
11 similar parts. The other devices are supported by different drivers.
12
Jean Delvare91749992005-10-08 00:10:00 +020013 Supports: IT8705F Super I/O chip w/LPC interface
Jean Delvare8e9afcb2006-12-12 18:18:28 +010014 IT8712F Super I/O chip w/LPC interface
Jean Delvare17d648b2006-08-28 14:23:46 +020015 IT8716F Super I/O chip w/LPC interface
Jean Delvare87673dd2006-08-28 14:37:19 +020016 IT8718F Super I/O chip w/LPC interface
Rudolf Marek08a8f6e2007-06-09 10:11:16 -040017 IT8726F Super I/O chip w/LPC interface
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 Sis950 A clone of the IT8705F
19
Jean Delvaref1d8e332007-11-25 16:14:44 +010020 Copyright (C) 2001 Chris Gauthron
Jean Delvare0124dd72007-11-25 16:16:41 +010021 Copyright (C) 2005-2007 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23 This program is free software; you can redistribute it and/or modify
24 it under the terms of the GNU General Public License as published by
25 the Free Software Foundation; either version 2 of the License, or
26 (at your option) any later version.
27
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
32
33 You should have received a copy of the GNU General Public License
34 along with this program; if not, write to the Free Software
35 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36*/
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/slab.h>
41#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020042#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040043#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020044#include <linux/hwmon-sysfs.h>
45#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040046#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010047#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020048#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/io.h>
50
corentin.labbeb74f3fd2007-06-13 20:27:36 +020051#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jean Delvare8e9afcb2006-12-12 18:18:28 +010053enum chips { it87, it8712, it8716, it8718 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jean Delvare67b671b2007-12-06 23:13:42 +010055static unsigned short force_id;
56module_param(force_id, ushort, 0);
57MODULE_PARM_DESC(force_id, "Override the detected device ID");
58
corentin.labbeb74f3fd2007-06-13 20:27:36 +020059static struct platform_device *pdev;
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define REG 0x2e /* The register to read/write */
62#define DEV 0x07 /* Register: Logical device select */
63#define VAL 0x2f /* The value to read/write */
64#define PME 0x04 /* The device with the fan registers in it */
Jean Delvare87673dd2006-08-28 14:37:19 +020065#define GPIO 0x07 /* The device with the IT8718F VID value in it */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define DEVID 0x20 /* Register: Device ID */
67#define DEVREV 0x22 /* Register: Device Revision */
68
69static inline int
70superio_inb(int reg)
71{
72 outb(reg, REG);
73 return inb(VAL);
74}
75
76static int superio_inw(int reg)
77{
78 int val;
79 outb(reg++, REG);
80 val = inb(VAL) << 8;
81 outb(reg, REG);
82 val |= inb(VAL);
83 return val;
84}
85
86static inline void
Jean Delvare87673dd2006-08-28 14:37:19 +020087superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +020090 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93static inline void
94superio_enter(void)
95{
96 outb(0x87, REG);
97 outb(0x01, REG);
98 outb(0x55, REG);
99 outb(0x55, REG);
100}
101
102static inline void
103superio_exit(void)
104{
105 outb(0x02, REG);
106 outb(0x02, VAL);
107}
108
Jean Delvare87673dd2006-08-28 14:37:19 +0200109/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define IT8712F_DEVID 0x8712
111#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200112#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200113#define IT8718F_DEVID 0x8718
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400114#define IT8726F_DEVID 0x8726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define IT87_ACT_REG 0x30
116#define IT87_BASE_REG 0x60
117
Jean Delvare87673dd2006-08-28 14:37:19 +0200118/* Logical device 7 registers (IT8712F and later) */
119#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
120#define IT87_SIO_VID_REG 0xfc /* VID value */
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* Update battery voltage after every reading if true */
123static int update_vbat;
124
125/* Not all BIOSes properly configure the PWM registers */
126static int fix_pwm_polarity;
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/* Many IT87 constants specified below */
129
130/* Length of ISA address segment */
131#define IT87_EXTENT 8
132
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500133/* Length of ISA address segment for Environmental Controller */
134#define IT87_EC_EXTENT 2
135
136/* Offset of EC registers from ISA base address */
137#define IT87_EC_OFFSET 5
138
139/* Where are the ISA address/data registers relative to the EC base address */
140#define IT87_ADDR_REG_OFFSET 0
141#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/*----- The IT87 registers -----*/
144
145#define IT87_REG_CONFIG 0x00
146
147#define IT87_REG_ALARM1 0x01
148#define IT87_REG_ALARM2 0x02
149#define IT87_REG_ALARM3 0x03
150
Jean Delvare87673dd2006-08-28 14:37:19 +0200151/* The IT8718F has the VID value in a different register, in Super-I/O
152 configuration space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define IT87_REG_VID 0x0a
Jean Delvare17d648b2006-08-28 14:23:46 +0200154/* Warning: register 0x0b is used for something completely different in
155 new chips/revisions. I suspect only 16-bit tachometer mode will work
156 for these. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200158#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
161
Jean Delvarec7f1f712007-09-03 17:11:46 +0200162static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
163static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
164static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
165static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#define IT87_REG_FAN_MAIN_CTRL 0x13
167#define IT87_REG_FAN_CTL 0x14
168#define IT87_REG_PWM(nr) (0x15 + (nr))
169
170#define IT87_REG_VIN(nr) (0x20 + (nr))
171#define IT87_REG_TEMP(nr) (0x29 + (nr))
172
173#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
174#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
175#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
176#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#define IT87_REG_VIN_ENABLE 0x50
179#define IT87_REG_TEMP_ENABLE 0x51
180
181#define IT87_REG_CHIPID 0x58
182
183#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
184#define IN_FROM_REG(val) ((val) * 16)
185
186static inline u8 FAN_TO_REG(long rpm, int div)
187{
188 if (rpm == 0)
189 return 255;
190 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
191 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
192 254);
193}
194
Jean Delvare17d648b2006-08-28 14:23:46 +0200195static inline u16 FAN16_TO_REG(long rpm)
196{
197 if (rpm == 0)
198 return 0xffff;
199 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
Jean Delvare17d648b2006-08-28 14:23:46 +0200203/* The divider is fixed to 2 in 16-bit mode */
204#define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
207 ((val)+500)/1000),-128,127))
208#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#define PWM_TO_REG(val) ((val) >> 1)
211#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
212
213static int DIV_TO_REG(int val)
214{
215 int answer = 0;
Jean Delvareb9e349f2006-08-28 14:26:22 +0200216 while (answer < 7 && (val >>= 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 answer++;
218 return answer;
219}
220#define DIV_FROM_REG(val) (1 << (val))
221
Jean Delvaref8d0c192007-02-14 21:15:02 +0100222static const unsigned int pwm_freq[8] = {
223 48000000 / 128,
224 24000000 / 128,
225 12000000 / 128,
226 8000000 / 128,
227 6000000 / 128,
228 3000000 / 128,
229 1500000 / 128,
230 750000 / 128,
231};
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200234struct it87_sio_data {
235 enum chips type;
236 /* Values read from Super-I/O config space */
237 u8 vid_value;
238};
239
Jean Delvareed6bafb2007-02-14 21:15:03 +0100240/* For each registered chip, we need to keep some data in memory.
241 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700243 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 enum chips type;
245
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200246 unsigned short addr;
247 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100248 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 char valid; /* !=0 if following fields are valid */
250 unsigned long last_updated; /* In jiffies */
251
252 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200253 u8 in_max[8]; /* Register value */
254 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200255 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvarec7f1f712007-09-03 17:11:46 +0200256 u16 fan[5]; /* Register values, possibly combined */
257 u16 fan_min[5]; /* Register values, possibly combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 u8 temp[3]; /* Register value */
259 u8 temp_high[3]; /* Register value */
260 u8 temp_low[3]; /* Register value */
261 u8 sensor; /* Register value */
262 u8 fan_div[3]; /* Register encoding, shifted right */
263 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100264 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 u32 alarms; /* Register encoding, combined */
266 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100267 u8 fan_ctl; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
269};
270
271
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200272static int it87_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200273static int __devexit it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200275static int it87_read_value(struct it87_data *data, u8 reg);
276static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200278static int it87_check_pwm(struct device *dev);
279static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200282static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100283 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200284 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200285 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100286 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200287 .probe = it87_probe,
288 .remove = __devexit_p(it87_remove),
Jean Delvarefde09502005-07-19 23:51:07 +0200289};
290
Jean Delvare20ad93d2005-06-05 11:53:25 +0200291static ssize_t show_in(struct device *dev, struct device_attribute *attr,
292 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200294 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
295 int nr = sensor_attr->index;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 struct it87_data *data = it87_update_device(dev);
298 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
299}
300
Jean Delvare20ad93d2005-06-05 11:53:25 +0200301static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
302 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200304 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
305 int nr = sensor_attr->index;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct it87_data *data = it87_update_device(dev);
308 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
309}
310
Jean Delvare20ad93d2005-06-05 11:53:25 +0200311static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
312 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200314 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
315 int nr = sensor_attr->index;
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct it87_data *data = it87_update_device(dev);
318 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
319}
320
Jean Delvare20ad93d2005-06-05 11:53:25 +0200321static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
322 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200324 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
325 int nr = sensor_attr->index;
326
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200327 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 unsigned long val = simple_strtoul(buf, NULL, 10);
329
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100330 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 data->in_min[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200332 it87_write_value(data, IT87_REG_VIN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100334 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return count;
336}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200337static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
338 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200340 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
341 int nr = sensor_attr->index;
342
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200343 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 unsigned long val = simple_strtoul(buf, NULL, 10);
345
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100346 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 data->in_max[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200348 it87_write_value(data, IT87_REG_VIN_MAX(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100350 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return count;
352}
353
354#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200355static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
356 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200359static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
360 show_in_min, set_in_min, offset); \
361static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
362 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364show_in_offset(0);
365limit_in_offset(0);
366show_in_offset(1);
367limit_in_offset(1);
368show_in_offset(2);
369limit_in_offset(2);
370show_in_offset(3);
371limit_in_offset(3);
372show_in_offset(4);
373limit_in_offset(4);
374show_in_offset(5);
375limit_in_offset(5);
376show_in_offset(6);
377limit_in_offset(6);
378show_in_offset(7);
379limit_in_offset(7);
380show_in_offset(8);
381
382/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200383static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
384 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200386 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
387 int nr = sensor_attr->index;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct it87_data *data = it87_update_device(dev);
390 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
391}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200392static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
393 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200395 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
396 int nr = sensor_attr->index;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct it87_data *data = it87_update_device(dev);
399 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
400}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200401static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
402 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200404 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
405 int nr = sensor_attr->index;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct it87_data *data = it87_update_device(dev);
408 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
409}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200410static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
411 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200413 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
414 int nr = sensor_attr->index;
415
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200416 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 int val = simple_strtol(buf, NULL, 10);
418
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100419 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 data->temp_high[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200421 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100422 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return count;
424}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200425static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
426 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200428 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
429 int nr = sensor_attr->index;
430
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200431 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int val = simple_strtol(buf, NULL, 10);
433
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100434 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 data->temp_low[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200436 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100437 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return count;
439}
440#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200441static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
442 show_temp, NULL, offset - 1); \
443static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
444 show_temp_max, set_temp_max, offset - 1); \
445static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
446 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448show_temp_offset(1);
449show_temp_offset(2);
450show_temp_offset(3);
451
Jean Delvare20ad93d2005-06-05 11:53:25 +0200452static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
453 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200455 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
456 int nr = sensor_attr->index;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct it87_data *data = it87_update_device(dev);
459 u8 reg = data->sensor; /* In case the value is updated while we use it */
460
461 if (reg & (1 << nr))
462 return sprintf(buf, "3\n"); /* thermal diode */
463 if (reg & (8 << nr))
464 return sprintf(buf, "2\n"); /* thermistor */
465 return sprintf(buf, "0\n"); /* disabled */
466}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200467static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
468 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200470 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
471 int nr = sensor_attr->index;
472
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200473 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 int val = simple_strtol(buf, NULL, 10);
475
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100476 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 data->sensor &= ~(1 << nr);
479 data->sensor &= ~(8 << nr);
480 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
481 if (val == 3)
482 data->sensor |= 1 << nr;
483 else if (val == 2)
484 data->sensor |= 8 << nr;
485 else if (val != 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100486 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return -EINVAL;
488 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200489 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100490 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return count;
492}
493#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200494static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
495 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497show_sensor_offset(1);
498show_sensor_offset(2);
499show_sensor_offset(3);
500
501/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200502static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
503 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200505 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
506 int nr = sensor_attr->index;
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 struct it87_data *data = it87_update_device(dev);
509 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
510 DIV_FROM_REG(data->fan_div[nr])));
511}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200512static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
513 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200515 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
516 int nr = sensor_attr->index;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 struct it87_data *data = it87_update_device(dev);
519 return sprintf(buf,"%d\n",
520 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
521}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200522static ssize_t show_fan_div(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", DIV_FROM_REG(data->fan_div[nr]));
530}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200531static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
532 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200534 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
535 int nr = sensor_attr->index;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct it87_data *data = it87_update_device(dev);
538 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
539}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200540static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
541 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200543 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
544 int nr = sensor_attr->index;
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct it87_data *data = it87_update_device(dev);
547 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
548}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100549static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
550 char *buf)
551{
552 struct it87_data *data = it87_update_device(dev);
553 int index = (data->fan_ctl >> 4) & 0x07;
554
555 return sprintf(buf, "%u\n", pwm_freq[index]);
556}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200557static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
558 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200560 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
561 int nr = sensor_attr->index;
562
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200563 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int val = simple_strtol(buf, NULL, 10);
Jean Delvare7f999aa2007-02-14 21:15:03 +0100565 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100567 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200568 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800569 switch (nr) {
570 case 0: data->fan_div[nr] = reg & 0x07; break;
571 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
572 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
573 }
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200576 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100577 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return count;
579}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200580static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
581 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200583 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
584 int nr = sensor_attr->index;
585
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200586 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvareb9e349f2006-08-28 14:26:22 +0200587 unsigned long val = simple_strtoul(buf, NULL, 10);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200588 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 u8 old;
590
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100591 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200592 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200594 /* Save fan min limit */
595 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 switch (nr) {
598 case 0:
599 case 1:
600 data->fan_div[nr] = DIV_TO_REG(val);
601 break;
602 case 2:
603 if (val < 8)
604 data->fan_div[nr] = 1;
605 else
606 data->fan_div[nr] = 3;
607 }
608 val = old & 0x80;
609 val |= (data->fan_div[0] & 0x07);
610 val |= (data->fan_div[1] & 0x07) << 3;
611 if (data->fan_div[2] == 3)
612 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200613 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200615 /* Restore fan min limit */
616 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200617 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200618
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100619 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return count;
621}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200622static ssize_t set_pwm_enable(struct device *dev,
623 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200625 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
626 int nr = sensor_attr->index;
627
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200628 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 int val = simple_strtol(buf, NULL, 10);
630
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100631 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (val == 0) {
634 int tmp;
635 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200636 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
637 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* set on/off mode */
639 data->fan_main_ctrl &= ~(1 << nr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200640 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 } else if (val == 1) {
642 /* set SmartGuardian mode */
643 data->fan_main_ctrl |= (1 << nr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200644 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200646 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 } else {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100648 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -EINVAL;
650 }
651
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100652 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return count;
654}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200655static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
656 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200658 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
659 int nr = sensor_attr->index;
660
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200661 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 int val = simple_strtol(buf, NULL, 10);
663
664 if (val < 0 || val > 255)
665 return -EINVAL;
666
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100667 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 data->manual_pwm_ctl[nr] = val;
669 if (data->fan_main_ctrl & (1 << nr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200670 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100671 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return count;
673}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100674static ssize_t set_pwm_freq(struct device *dev,
675 struct device_attribute *attr, const char *buf, size_t count)
676{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200677 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100678 unsigned long val = simple_strtoul(buf, NULL, 10);
679 int i;
680
681 /* Search for the nearest available frequency */
682 for (i = 0; i < 7; i++) {
683 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
684 break;
685 }
686
687 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200688 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100689 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200690 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100691 mutex_unlock(&data->update_lock);
692
693 return count;
694}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Jean Delvare20ad93d2005-06-05 11:53:25 +0200696#define show_fan_offset(offset) \
697static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
698 show_fan, NULL, offset - 1); \
699static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
700 show_fan_min, set_fan_min, offset - 1); \
701static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
702 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704show_fan_offset(1);
705show_fan_offset(2);
706show_fan_offset(3);
707
708#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200709static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
710 show_pwm_enable, set_pwm_enable, offset - 1); \
711static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +0100712 show_pwm, set_pwm, offset - 1); \
713static DEVICE_ATTR(pwm##offset##_freq, \
714 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
715 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717show_pwm_offset(1);
718show_pwm_offset(2);
719show_pwm_offset(3);
720
Jean Delvare17d648b2006-08-28 14:23:46 +0200721/* A different set of callbacks for 16-bit fans */
722static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
723 char *buf)
724{
725 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
726 int nr = sensor_attr->index;
727 struct it87_data *data = it87_update_device(dev);
728 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
729}
730
731static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
732 char *buf)
733{
734 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
735 int nr = sensor_attr->index;
736 struct it87_data *data = it87_update_device(dev);
737 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
738}
739
740static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
741 const char *buf, size_t count)
742{
743 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
744 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200745 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare17d648b2006-08-28 14:23:46 +0200746 int val = simple_strtol(buf, NULL, 10);
747
748 mutex_lock(&data->update_lock);
749 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200750 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +0200751 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200752 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +0200753 data->fan_min[nr] >> 8);
754 mutex_unlock(&data->update_lock);
755 return count;
756}
757
758/* We want to use the same sysfs file names as 8-bit fans, but we need
759 different variable names, so we have to use SENSOR_ATTR instead of
760 SENSOR_DEVICE_ATTR. */
761#define show_fan16_offset(offset) \
762static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
763 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
764 show_fan16, NULL, offset - 1); \
765static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
766 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
767 show_fan16_min, set_fan16_min, offset - 1)
768
769show_fan16_offset(1);
770show_fan16_offset(2);
771show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200772show_fan16_offset(4);
773show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +0200774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400776static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200779 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
Jean Delvare1d66c642005-04-18 21:16:59 -0700781static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jean Delvare0124dd72007-11-25 16:16:41 +0100783static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
784 char *buf)
785{
786 int bitnr = to_sensor_dev_attr(attr)->index;
787 struct it87_data *data = it87_update_device(dev);
788 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
789}
790static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
791static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
792static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
793static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
794static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
795static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
796static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
797static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
798static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
799static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
800static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
801static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
802static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
803static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
804static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
805static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400808show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Jean Delvare90d66192007-10-08 18:24:35 +0200810 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +0100811 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400814store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200816 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 u32 val;
818
819 val = simple_strtoul(buf, NULL, 10);
820 data->vrm = val;
821
822 return count;
823}
824static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400827show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 struct it87_data *data = it87_update_device(dev);
830 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
831}
832static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +0200833
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200834static ssize_t show_name(struct device *dev, struct device_attribute
835 *devattr, char *buf)
836{
837 struct it87_data *data = dev_get_drvdata(dev);
838 return sprintf(buf, "%s\n", data->name);
839}
840static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
841
Jean Delvare87808be2006-09-24 21:17:13 +0200842static struct attribute *it87_attributes[] = {
843 &sensor_dev_attr_in0_input.dev_attr.attr,
844 &sensor_dev_attr_in1_input.dev_attr.attr,
845 &sensor_dev_attr_in2_input.dev_attr.attr,
846 &sensor_dev_attr_in3_input.dev_attr.attr,
847 &sensor_dev_attr_in4_input.dev_attr.attr,
848 &sensor_dev_attr_in5_input.dev_attr.attr,
849 &sensor_dev_attr_in6_input.dev_attr.attr,
850 &sensor_dev_attr_in7_input.dev_attr.attr,
851 &sensor_dev_attr_in8_input.dev_attr.attr,
852 &sensor_dev_attr_in0_min.dev_attr.attr,
853 &sensor_dev_attr_in1_min.dev_attr.attr,
854 &sensor_dev_attr_in2_min.dev_attr.attr,
855 &sensor_dev_attr_in3_min.dev_attr.attr,
856 &sensor_dev_attr_in4_min.dev_attr.attr,
857 &sensor_dev_attr_in5_min.dev_attr.attr,
858 &sensor_dev_attr_in6_min.dev_attr.attr,
859 &sensor_dev_attr_in7_min.dev_attr.attr,
860 &sensor_dev_attr_in0_max.dev_attr.attr,
861 &sensor_dev_attr_in1_max.dev_attr.attr,
862 &sensor_dev_attr_in2_max.dev_attr.attr,
863 &sensor_dev_attr_in3_max.dev_attr.attr,
864 &sensor_dev_attr_in4_max.dev_attr.attr,
865 &sensor_dev_attr_in5_max.dev_attr.attr,
866 &sensor_dev_attr_in6_max.dev_attr.attr,
867 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +0100868 &sensor_dev_attr_in0_alarm.dev_attr.attr,
869 &sensor_dev_attr_in1_alarm.dev_attr.attr,
870 &sensor_dev_attr_in2_alarm.dev_attr.attr,
871 &sensor_dev_attr_in3_alarm.dev_attr.attr,
872 &sensor_dev_attr_in4_alarm.dev_attr.attr,
873 &sensor_dev_attr_in5_alarm.dev_attr.attr,
874 &sensor_dev_attr_in6_alarm.dev_attr.attr,
875 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200876
877 &sensor_dev_attr_temp1_input.dev_attr.attr,
878 &sensor_dev_attr_temp2_input.dev_attr.attr,
879 &sensor_dev_attr_temp3_input.dev_attr.attr,
880 &sensor_dev_attr_temp1_max.dev_attr.attr,
881 &sensor_dev_attr_temp2_max.dev_attr.attr,
882 &sensor_dev_attr_temp3_max.dev_attr.attr,
883 &sensor_dev_attr_temp1_min.dev_attr.attr,
884 &sensor_dev_attr_temp2_min.dev_attr.attr,
885 &sensor_dev_attr_temp3_min.dev_attr.attr,
886 &sensor_dev_attr_temp1_type.dev_attr.attr,
887 &sensor_dev_attr_temp2_type.dev_attr.attr,
888 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +0100889 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
890 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
891 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200892
893 &dev_attr_alarms.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200894 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200895 NULL
896};
897
898static const struct attribute_group it87_group = {
899 .attrs = it87_attributes,
900};
901
902static struct attribute *it87_attributes_opt[] = {
903 &sensor_dev_attr_fan1_input16.dev_attr.attr,
904 &sensor_dev_attr_fan1_min16.dev_attr.attr,
905 &sensor_dev_attr_fan2_input16.dev_attr.attr,
906 &sensor_dev_attr_fan2_min16.dev_attr.attr,
907 &sensor_dev_attr_fan3_input16.dev_attr.attr,
908 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvarec7f1f712007-09-03 17:11:46 +0200909 &sensor_dev_attr_fan4_input16.dev_attr.attr,
910 &sensor_dev_attr_fan4_min16.dev_attr.attr,
911 &sensor_dev_attr_fan5_input16.dev_attr.attr,
912 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200913
914 &sensor_dev_attr_fan1_input.dev_attr.attr,
915 &sensor_dev_attr_fan1_min.dev_attr.attr,
916 &sensor_dev_attr_fan1_div.dev_attr.attr,
917 &sensor_dev_attr_fan2_input.dev_attr.attr,
918 &sensor_dev_attr_fan2_min.dev_attr.attr,
919 &sensor_dev_attr_fan2_div.dev_attr.attr,
920 &sensor_dev_attr_fan3_input.dev_attr.attr,
921 &sensor_dev_attr_fan3_min.dev_attr.attr,
922 &sensor_dev_attr_fan3_div.dev_attr.attr,
923
Jean Delvare0124dd72007-11-25 16:16:41 +0100924 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
925 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
926 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
927 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
928 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
929
Jean Delvare87808be2006-09-24 21:17:13 +0200930 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
931 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
932 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
933 &sensor_dev_attr_pwm1.dev_attr.attr,
934 &sensor_dev_attr_pwm2.dev_attr.attr,
935 &sensor_dev_attr_pwm3.dev_attr.attr,
936
937 &dev_attr_vrm.attr,
938 &dev_attr_cpu0_vid.attr,
939 NULL
940};
941
942static const struct attribute_group it87_group_opt = {
943 .attrs = it87_attributes_opt,
944};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Jean Delvare2d8672c2005-07-19 23:56:35 +0200946/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200947static int __init it87_find(unsigned short *address,
948 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 int err = -ENODEV;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200951 u16 chip_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 superio_enter();
Jean Delvare67b671b2007-12-06 23:13:42 +0100954 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200955
956 switch (chip_type) {
957 case IT8705F_DEVID:
958 sio_data->type = it87;
959 break;
960 case IT8712F_DEVID:
961 sio_data->type = it8712;
962 break;
963 case IT8716F_DEVID:
964 case IT8726F_DEVID:
965 sio_data->type = it8716;
966 break;
967 case IT8718F_DEVID:
968 sio_data->type = it8718;
969 break;
970 case 0xffff: /* No device at all */
971 goto exit;
972 default:
973 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
974 chip_type);
975 goto exit;
976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Jean Delvare87673dd2006-08-28 14:37:19 +0200978 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
980 pr_info("it87: Device not activated, skipping\n");
981 goto exit;
982 }
983
984 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
985 if (*address == 0) {
986 pr_info("it87: Base address not set, skipping\n");
987 goto exit;
988 }
989
990 err = 0;
991 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
992 chip_type, *address, superio_inb(DEVREV) & 0x0f);
993
Jean Delvare87673dd2006-08-28 14:37:19 +0200994 /* Read GPIO config and VID value from LDN 7 (GPIO) */
995 if (chip_type != IT8705F_DEVID) {
996 int reg;
997
998 superio_select(GPIO);
999 if (chip_type == it8718)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001000 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001001
1002 reg = superio_inb(IT87_SIO_PINX2_REG);
1003 if (reg & (1 << 0))
1004 pr_info("it87: in3 is VCC (+5V)\n");
1005 if (reg & (1 << 1))
1006 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
1007 }
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009exit:
1010 superio_exit();
1011 return err;
1012}
1013
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001014static int __devinit it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001017 struct resource *res;
1018 struct device *dev = &pdev->dev;
1019 struct it87_sio_data *sio_data = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 int enable_pwm_interface;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001022 static const char *names[] = {
1023 "it87",
1024 "it8712",
1025 "it8716",
1026 "it8718",
1027 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001029 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001030 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001031 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1032 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001033 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001034 err = -EBUSY;
1035 goto ERROR0;
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001038 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 err = -ENOMEM;
1040 goto ERROR1;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001043 data->addr = res->start;
1044 data->type = sio_data->type;
1045 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001048 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1049 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001050 err = -ENODEV;
1051 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001054 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001056 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001059 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001062 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /* Register sysfs hooks */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001065 if ((err = sysfs_create_group(&dev->kobj, &it87_group)))
1066 goto ERROR2;
Jean Delvare17d648b2006-08-28 14:23:46 +02001067
Jean Delvare9060f8b2006-08-28 14:24:17 +02001068 /* Do not create fan files for disabled fans */
Jean Delvare87673dd2006-08-28 14:37:19 +02001069 if (data->type == it8716 || data->type == it8718) {
1070 /* 16-bit tachometers */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001071 if (data->has_fan & (1 << 0)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001072 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001073 &sensor_dev_attr_fan1_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001074 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001075 &sensor_dev_attr_fan1_min16.dev_attr))
1076 || (err = device_create_file(dev,
1077 &sensor_dev_attr_fan1_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001078 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001079 }
1080 if (data->has_fan & (1 << 1)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001081 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001082 &sensor_dev_attr_fan2_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001083 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001084 &sensor_dev_attr_fan2_min16.dev_attr))
1085 || (err = device_create_file(dev,
1086 &sensor_dev_attr_fan2_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001087 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001088 }
1089 if (data->has_fan & (1 << 2)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001090 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001091 &sensor_dev_attr_fan3_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001092 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001093 &sensor_dev_attr_fan3_min16.dev_attr))
1094 || (err = device_create_file(dev,
1095 &sensor_dev_attr_fan3_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001096 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001097 }
Jean Delvarec7f1f712007-09-03 17:11:46 +02001098 if (data->has_fan & (1 << 3)) {
1099 if ((err = device_create_file(dev,
1100 &sensor_dev_attr_fan4_input16.dev_attr))
1101 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001102 &sensor_dev_attr_fan4_min16.dev_attr))
1103 || (err = device_create_file(dev,
1104 &sensor_dev_attr_fan4_alarm.dev_attr)))
Jean Delvarec7f1f712007-09-03 17:11:46 +02001105 goto ERROR4;
1106 }
1107 if (data->has_fan & (1 << 4)) {
1108 if ((err = device_create_file(dev,
1109 &sensor_dev_attr_fan5_input16.dev_attr))
1110 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001111 &sensor_dev_attr_fan5_min16.dev_attr))
1112 || (err = device_create_file(dev,
1113 &sensor_dev_attr_fan5_alarm.dev_attr)))
Jean Delvarec7f1f712007-09-03 17:11:46 +02001114 goto ERROR4;
1115 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001116 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001117 /* 8-bit tachometers with clock divider */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001118 if (data->has_fan & (1 << 0)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001119 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001120 &sensor_dev_attr_fan1_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001121 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001122 &sensor_dev_attr_fan1_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001123 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001124 &sensor_dev_attr_fan1_div.dev_attr))
1125 || (err = device_create_file(dev,
1126 &sensor_dev_attr_fan1_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001127 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001128 }
1129 if (data->has_fan & (1 << 1)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001130 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001131 &sensor_dev_attr_fan2_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001132 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001133 &sensor_dev_attr_fan2_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001134 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001135 &sensor_dev_attr_fan2_div.dev_attr))
1136 || (err = device_create_file(dev,
1137 &sensor_dev_attr_fan2_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001138 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001139 }
1140 if (data->has_fan & (1 << 2)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001141 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001142 &sensor_dev_attr_fan3_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001143 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001144 &sensor_dev_attr_fan3_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001145 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001146 &sensor_dev_attr_fan3_div.dev_attr))
1147 || (err = device_create_file(dev,
1148 &sensor_dev_attr_fan3_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001149 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001150 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001151 }
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (enable_pwm_interface) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001154 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001155 &sensor_dev_attr_pwm1_enable.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001156 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001157 &sensor_dev_attr_pwm2_enable.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001158 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001159 &sensor_dev_attr_pwm3_enable.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001160 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001161 &sensor_dev_attr_pwm1.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001162 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001163 &sensor_dev_attr_pwm2.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001164 || (err = device_create_file(dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001165 &sensor_dev_attr_pwm3.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001166 || (err = device_create_file(dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001167 &dev_attr_pwm1_freq))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001168 || (err = device_create_file(dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001169 &dev_attr_pwm2_freq))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001170 || (err = device_create_file(dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001171 &dev_attr_pwm3_freq)))
Jean Delvare87808be2006-09-24 21:17:13 +02001172 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174
Jean Delvare87673dd2006-08-28 14:37:19 +02001175 if (data->type == it8712 || data->type == it8716
1176 || data->type == it8718) {
Jean Delvare303760b2005-07-31 21:52:01 +02001177 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001178 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001179 data->vid = sio_data->vid_value;
1180 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001181 &dev_attr_vrm))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001182 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001183 &dev_attr_cpu0_vid)))
1184 goto ERROR4;
1185 }
1186
Tony Jones1beeffe2007-08-20 13:46:20 -07001187 data->hwmon_dev = hwmon_device_register(dev);
1188 if (IS_ERR(data->hwmon_dev)) {
1189 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001190 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192
1193 return 0;
1194
Jean Delvare87808be2006-09-24 21:17:13 +02001195ERROR4:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001196 sysfs_remove_group(&dev->kobj, &it87_group);
1197 sysfs_remove_group(&dev->kobj, &it87_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198ERROR2:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001199 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 kfree(data);
1201ERROR1:
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001202 release_region(res->start, IT87_EC_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203ERROR0:
1204 return err;
1205}
1206
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001207static int __devexit it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001209 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Tony Jones1beeffe2007-08-20 13:46:20 -07001211 hwmon_device_unregister(data->hwmon_dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001212 sysfs_remove_group(&pdev->dev.kobj, &it87_group);
1213 sysfs_remove_group(&pdev->dev.kobj, &it87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001214
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001215 release_region(data->addr, IT87_EC_EXTENT);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001216 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001217 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 return 0;
1220}
1221
Jean Delvare7f999aa2007-02-14 21:15:03 +01001222/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1224 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001225static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001227 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1228 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Jean Delvare7f999aa2007-02-14 21:15:03 +01001231/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1233 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001234static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001236 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1237 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
1240/* Return 1 if and only if the PWM interface is safe to use */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001241static int __devinit it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001243 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1245 * and polarity set to active low is sign that this is the case so we
1246 * disable pwm control to protect the user. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001247 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if ((tmp & 0x87) == 0) {
1249 if (fix_pwm_polarity) {
1250 /* The user asks us to attempt a chip reconfiguration.
1251 * This means switching to active high polarity and
1252 * inverting all fan speed values. */
1253 int i;
1254 u8 pwm[3];
1255
1256 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001257 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 IT87_REG_PWM(i));
1259
1260 /* If any fan is in automatic pwm mode, the polarity
1261 * might be correct, as suspicious as it seems, so we
1262 * better don't change anything (but still disable the
1263 * PWM interface). */
1264 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001265 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001267 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 tmp | 0x87);
1269 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001270 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 IT87_REG_PWM(i),
1272 0x7f & ~pwm[i]);
1273 return 1;
1274 }
1275
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001276 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 "too broken to be fixed\n");
1278 }
1279
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001280 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 "defaults, disabling PWM interface\n");
1282 return 0;
1283 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001284 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 "sane, won't touch\n");
1286 }
1287
1288 return 1;
1289}
1290
1291/* Called when we have found a new IT87. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001292static void __devinit it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001294 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 int tmp, i;
1296
1297 /* initialize to sane defaults:
1298 * - if the chip is in manual pwm mode, this will be overwritten with
1299 * the actual settings on the chip (so in this case, initialization
1300 * is not needed)
1301 * - if in automatic or on/off mode, we could switch to manual mode,
1302 * read the registers and set manual_pwm_ctl accordingly, but currently
1303 * this is not implemented, so we initialize to something sane */
1304 for (i = 0; i < 3; i++) {
1305 data->manual_pwm_ctl[i] = 0xff;
1306 }
1307
Jean Delvarec5df9b72006-08-28 14:37:54 +02001308 /* Some chips seem to have default value 0xff for all limit
1309 * registers. For low voltage limits it makes no sense and triggers
1310 * alarms, so change to 0 instead. For high temperature limits, it
1311 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1312 * but is still confusing, so change to 127 degrees C. */
1313 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001314 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001315 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001316 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001317 }
1318 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001319 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001320 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001321 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001322 }
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /* Check if temperature channnels are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001325 tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if ((tmp & 0x3f) == 0) {
1327 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1328 tmp = (tmp & 0xc0) | 0x2a;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001329 it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331 data->sensor = tmp;
1332
1333 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001334 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if ((tmp & 0xff) == 0) {
1336 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001337 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339
1340 /* Check if tachometers are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001341 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if ((data->fan_main_ctrl & 0x70) == 0) {
1343 /* Enable all fan tachometers */
1344 data->fan_main_ctrl |= 0x70;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001345 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001347 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Jean Delvare17d648b2006-08-28 14:23:46 +02001349 /* Set tachometers to 16-bit mode if needed */
Jean Delvare87673dd2006-08-28 14:37:19 +02001350 if (data->type == it8716 || data->type == it8718) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001351 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001352 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001353 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02001354 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001355 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02001356 tmp | 0x07);
1357 }
Jean Delvarec7f1f712007-09-03 17:11:46 +02001358 if (tmp & (1 << 4))
1359 data->has_fan |= (1 << 3); /* fan4 enabled */
1360 if (tmp & (1 << 5))
1361 data->has_fan |= (1 << 4); /* fan5 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02001362 }
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /* Set current fan mode registers and the default settings for the
1365 * other mode registers */
1366 for (i = 0; i < 3; i++) {
1367 if (data->fan_main_ctrl & (1 << i)) {
1368 /* pwm mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001369 tmp = it87_read_value(data, IT87_REG_PWM(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (tmp & 0x80) {
1371 /* automatic pwm - not yet implemented, but
1372 * leave the settings made by the BIOS alone
1373 * until a change is requested via the sysfs
1374 * interface */
1375 } else {
1376 /* manual pwm */
1377 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1378 }
1379 }
1380 }
1381
1382 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001383 it87_write_value(data, IT87_REG_CONFIG,
1384 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 | (update_vbat ? 0x41 : 0x01));
1386}
1387
1388static struct it87_data *it87_update_device(struct device *dev)
1389{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001390 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 int i;
1392
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001393 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1396 || !data->valid) {
1397
1398 if (update_vbat) {
1399 /* Cleared after each update, so reenable. Value
1400 returned by this read will be previous value */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001401 it87_write_value(data, IT87_REG_CONFIG,
1402 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404 for (i = 0; i <= 7; i++) {
1405 data->in[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001406 it87_read_value(data, IT87_REG_VIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 data->in_min[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001408 it87_read_value(data, IT87_REG_VIN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 data->in_max[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001410 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
Jean Delvare3543a532006-08-28 14:27:25 +02001412 /* in8 (battery) has no limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 data->in[8] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001414 it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Jean Delvarec7f1f712007-09-03 17:11:46 +02001416 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02001417 /* Skip disabled fans */
1418 if (!(data->has_fan & (1 << i)))
1419 continue;
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 data->fan_min[i] =
Jean Delvarec7f1f712007-09-03 17:11:46 +02001422 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001423 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001424 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02001425 /* Add high byte if in 16-bit mode */
Jean Delvare87673dd2006-08-28 14:37:19 +02001426 if (data->type == it8716 || data->type == it8718) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001427 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001428 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001429 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001430 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02001431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433 for (i = 0; i < 3; i++) {
1434 data->temp[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001435 it87_read_value(data, IT87_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 data->temp_high[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001437 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 data->temp_low[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001439 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441
Jean Delvare17d648b2006-08-28 14:23:46 +02001442 /* Newer chips don't have clock dividers */
Jean Delvare87673dd2006-08-28 14:37:19 +02001443 if ((data->has_fan & 0x07) && data->type != it8716
1444 && data->type != it8718) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001445 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02001446 data->fan_div[0] = i & 0x07;
1447 data->fan_div[1] = (i >> 3) & 0x07;
1448 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001452 it87_read_value(data, IT87_REG_ALARM1) |
1453 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1454 (it87_read_value(data, IT87_REG_ALARM3) << 16);
1455 data->fan_main_ctrl = it87_read_value(data,
1456 IT87_REG_FAN_MAIN_CTRL);
1457 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001459 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /* The 8705 does not have VID capability */
Jean Delvare17d648b2006-08-28 14:23:46 +02001461 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001462 data->vid = it87_read_value(data, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02001463 /* The older IT8712F revisions had only 5 VID pins,
1464 but we assume it is always safe to read 6 bits. */
1465 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467 data->last_updated = jiffies;
1468 data->valid = 1;
1469 }
1470
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001471 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 return data;
1474}
1475
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001476static int __init it87_device_add(unsigned short address,
1477 const struct it87_sio_data *sio_data)
1478{
1479 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001480 .start = address + IT87_EC_OFFSET,
1481 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001482 .name = DRVNAME,
1483 .flags = IORESOURCE_IO,
1484 };
1485 int err;
1486
1487 pdev = platform_device_alloc(DRVNAME, address);
1488 if (!pdev) {
1489 err = -ENOMEM;
1490 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1491 goto exit;
1492 }
1493
1494 err = platform_device_add_resources(pdev, &res, 1);
1495 if (err) {
1496 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1497 "(%d)\n", err);
1498 goto exit_device_put;
1499 }
1500
1501 err = platform_device_add_data(pdev, sio_data,
1502 sizeof(struct it87_sio_data));
1503 if (err) {
1504 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1505 goto exit_device_put;
1506 }
1507
1508 err = platform_device_add(pdev);
1509 if (err) {
1510 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1511 err);
1512 goto exit_device_put;
1513 }
1514
1515 return 0;
1516
1517exit_device_put:
1518 platform_device_put(pdev);
1519exit:
1520 return err;
1521}
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523static int __init sm_it87_init(void)
1524{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001525 int err;
1526 unsigned short isa_address=0;
1527 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02001528
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001529 err = it87_find(&isa_address, &sio_data);
1530 if (err)
1531 return err;
1532 err = platform_driver_register(&it87_driver);
1533 if (err)
1534 return err;
1535
1536 err = it87_device_add(isa_address, &sio_data);
1537 if (err){
1538 platform_driver_unregister(&it87_driver);
1539 return err;
1540 }
1541
1542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
1545static void __exit sm_it87_exit(void)
1546{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001547 platform_device_unregister(pdev);
1548 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
1551
Jean Delvaref1d8e332007-11-25 16:14:44 +01001552MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02001553 "Jean Delvare <khali@linux-fr.org>");
Rudolf Marek08a8f6e2007-06-09 10:11:16 -04001554MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8726F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555module_param(update_vbat, bool, 0);
1556MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1557module_param(fix_pwm_polarity, bool, 0);
1558MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1559MODULE_LICENSE("GPL");
1560
1561module_init(sm_it87_init);
1562module_exit(sm_it87_exit);