blob: 2a365681f96934444d97ff24b53944f3c64bbf57 [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
Andrew Paprocki04751692008-08-06 22:41:06 +0200154/* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
155 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
156 mode. */
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 */
Andrew Paprocki04751692008-08-06 22:41:06 +0200237 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200238 u8 vid_value;
239};
240
Jean Delvareed6bafb2007-02-14 21:15:03 +0100241/* For each registered chip, we need to keep some data in memory.
242 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700244 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 enum chips type;
Andrew Paprocki04751692008-08-06 22:41:06 +0200246 u8 revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200248 unsigned short addr;
249 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100250 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 char valid; /* !=0 if following fields are valid */
252 unsigned long last_updated; /* In jiffies */
253
254 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200255 u8 in_max[8]; /* Register value */
256 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200257 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvarec7f1f712007-09-03 17:11:46 +0200258 u16 fan[5]; /* Register values, possibly combined */
259 u16 fan_min[5]; /* Register values, possibly combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 u8 temp[3]; /* Register value */
261 u8 temp_high[3]; /* Register value */
262 u8 temp_low[3]; /* Register value */
263 u8 sensor; /* Register value */
264 u8 fan_div[3]; /* Register encoding, shifted right */
265 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100266 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 u32 alarms; /* Register encoding, combined */
268 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100269 u8 fan_ctl; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
271};
272
Andrew Paprocki04751692008-08-06 22:41:06 +0200273static inline int has_16bit_fans(const struct it87_data *data)
274{
275 /* IT8712F Datasheet 0.9.1, section 8.3.5 indicates 7h == Version I.
276 This is the first revision with 16bit tachometer support. */
277 return (data->type == it8712 && data->revision >= 0x07)
278 || data->type == it8716
279 || data->type == it8718;
280}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200282static int it87_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200283static int __devexit it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200285static int it87_read_value(struct it87_data *data, u8 reg);
286static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200288static int it87_check_pwm(struct device *dev);
289static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200292static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100293 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200294 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200295 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100296 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200297 .probe = it87_probe,
298 .remove = __devexit_p(it87_remove),
Jean Delvarefde09502005-07-19 23:51:07 +0200299};
300
Jean Delvare20ad93d2005-06-05 11:53:25 +0200301static ssize_t show_in(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[nr]));
309}
310
Jean Delvare20ad93d2005-06-05 11:53:25 +0200311static ssize_t show_in_min(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_min[nr]));
319}
320
Jean Delvare20ad93d2005-06-05 11:53:25 +0200321static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
322 char *buf)
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct it87_data *data = it87_update_device(dev);
328 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
329}
330
Jean Delvare20ad93d2005-06-05 11:53:25 +0200331static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
332 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200334 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
335 int nr = sensor_attr->index;
336
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200337 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 unsigned long val = simple_strtoul(buf, NULL, 10);
339
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100340 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 data->in_min[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200342 it87_write_value(data, IT87_REG_VIN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100344 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return count;
346}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200347static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
348 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200350 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
351 int nr = sensor_attr->index;
352
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200353 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 unsigned long val = simple_strtoul(buf, NULL, 10);
355
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100356 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 data->in_max[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200358 it87_write_value(data, IT87_REG_VIN_MAX(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100360 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return count;
362}
363
364#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200365static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
366 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200369static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
370 show_in_min, set_in_min, offset); \
371static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
372 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374show_in_offset(0);
375limit_in_offset(0);
376show_in_offset(1);
377limit_in_offset(1);
378show_in_offset(2);
379limit_in_offset(2);
380show_in_offset(3);
381limit_in_offset(3);
382show_in_offset(4);
383limit_in_offset(4);
384show_in_offset(5);
385limit_in_offset(5);
386show_in_offset(6);
387limit_in_offset(6);
388show_in_offset(7);
389limit_in_offset(7);
390show_in_offset(8);
391
392/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200393static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
394 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200396 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
397 int nr = sensor_attr->index;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct it87_data *data = it87_update_device(dev);
400 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
401}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200402static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
403 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200405 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
406 int nr = sensor_attr->index;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct it87_data *data = it87_update_device(dev);
409 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
410}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200411static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
412 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200414 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
415 int nr = sensor_attr->index;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct it87_data *data = it87_update_device(dev);
418 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
419}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200420static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
421 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200423 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
424 int nr = sensor_attr->index;
425
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200426 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 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_high[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200431 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[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}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200435static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
436 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200438 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
439 int nr = sensor_attr->index;
440
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200441 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int val = simple_strtol(buf, NULL, 10);
443
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100444 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 data->temp_low[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200446 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100447 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return count;
449}
450#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200451static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
452 show_temp, NULL, offset - 1); \
453static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
454 show_temp_max, set_temp_max, offset - 1); \
455static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
456 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458show_temp_offset(1);
459show_temp_offset(2);
460show_temp_offset(3);
461
Jean Delvare20ad93d2005-06-05 11:53:25 +0200462static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
463 char *buf)
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 it87_data *data = it87_update_device(dev);
469 u8 reg = data->sensor; /* In case the value is updated while we use it */
470
471 if (reg & (1 << nr))
472 return sprintf(buf, "3\n"); /* thermal diode */
473 if (reg & (8 << nr))
474 return sprintf(buf, "2\n"); /* thermistor */
475 return sprintf(buf, "0\n"); /* disabled */
476}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200477static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
478 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200480 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
481 int nr = sensor_attr->index;
482
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200483 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int val = simple_strtol(buf, NULL, 10);
485
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100486 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 data->sensor &= ~(1 << nr);
489 data->sensor &= ~(8 << nr);
490 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
491 if (val == 3)
492 data->sensor |= 1 << nr;
493 else if (val == 2)
494 data->sensor |= 8 << nr;
495 else if (val != 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100496 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return -EINVAL;
498 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200499 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100500 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return count;
502}
503#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200504static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
505 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507show_sensor_offset(1);
508show_sensor_offset(2);
509show_sensor_offset(3);
510
511/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200512static ssize_t show_fan(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", FAN_FROM_REG(data->fan[nr],
520 DIV_FROM_REG(data->fan_div[nr])));
521}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200522static ssize_t show_fan_min(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",
530 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
531}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200532static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
533 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200535 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
536 int nr = sensor_attr->index;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct it87_data *data = it87_update_device(dev);
539 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
540}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200541static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
542 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200544 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
545 int nr = sensor_attr->index;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct it87_data *data = it87_update_device(dev);
548 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
549}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200550static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
551 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200553 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
554 int nr = sensor_attr->index;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct it87_data *data = it87_update_device(dev);
557 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
558}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100559static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
560 char *buf)
561{
562 struct it87_data *data = it87_update_device(dev);
563 int index = (data->fan_ctl >> 4) & 0x07;
564
565 return sprintf(buf, "%u\n", pwm_freq[index]);
566}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200567static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
568 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200570 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
571 int nr = sensor_attr->index;
572
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200573 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 int val = simple_strtol(buf, NULL, 10);
Jean Delvare7f999aa2007-02-14 21:15:03 +0100575 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100577 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200578 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800579 switch (nr) {
580 case 0: data->fan_div[nr] = reg & 0x07; break;
581 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
582 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
583 }
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200586 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100587 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return count;
589}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200590static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
591 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200593 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
594 int nr = sensor_attr->index;
595
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200596 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvareb9e349f2006-08-28 14:26:22 +0200597 unsigned long val = simple_strtoul(buf, NULL, 10);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200598 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 u8 old;
600
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100601 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200602 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200604 /* Save fan min limit */
605 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 switch (nr) {
608 case 0:
609 case 1:
610 data->fan_div[nr] = DIV_TO_REG(val);
611 break;
612 case 2:
613 if (val < 8)
614 data->fan_div[nr] = 1;
615 else
616 data->fan_div[nr] = 3;
617 }
618 val = old & 0x80;
619 val |= (data->fan_div[0] & 0x07);
620 val |= (data->fan_div[1] & 0x07) << 3;
621 if (data->fan_div[2] == 3)
622 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200623 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200625 /* Restore fan min limit */
626 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200627 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200628
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100629 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return count;
631}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200632static ssize_t set_pwm_enable(struct device *dev,
633 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200635 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
636 int nr = sensor_attr->index;
637
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200638 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int val = simple_strtol(buf, NULL, 10);
640
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100641 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (val == 0) {
644 int tmp;
645 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200646 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
647 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* set on/off mode */
649 data->fan_main_ctrl &= ~(1 << nr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200650 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 } else if (val == 1) {
652 /* set SmartGuardian mode */
653 data->fan_main_ctrl |= (1 << nr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200654 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200656 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 } else {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100658 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return -EINVAL;
660 }
661
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100662 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return count;
664}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200665static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
666 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200668 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
669 int nr = sensor_attr->index;
670
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200671 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int val = simple_strtol(buf, NULL, 10);
673
674 if (val < 0 || val > 255)
675 return -EINVAL;
676
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100677 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 data->manual_pwm_ctl[nr] = val;
679 if (data->fan_main_ctrl & (1 << nr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200680 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100681 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return count;
683}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100684static ssize_t set_pwm_freq(struct device *dev,
685 struct device_attribute *attr, const char *buf, size_t count)
686{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200687 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100688 unsigned long val = simple_strtoul(buf, NULL, 10);
689 int i;
690
691 /* Search for the nearest available frequency */
692 for (i = 0; i < 7; i++) {
693 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
694 break;
695 }
696
697 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200698 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100699 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200700 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100701 mutex_unlock(&data->update_lock);
702
703 return count;
704}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Jean Delvare20ad93d2005-06-05 11:53:25 +0200706#define show_fan_offset(offset) \
707static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
708 show_fan, NULL, offset - 1); \
709static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
710 show_fan_min, set_fan_min, offset - 1); \
711static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
712 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714show_fan_offset(1);
715show_fan_offset(2);
716show_fan_offset(3);
717
718#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200719static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
720 show_pwm_enable, set_pwm_enable, offset - 1); \
721static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +0100722 show_pwm, set_pwm, offset - 1); \
723static DEVICE_ATTR(pwm##offset##_freq, \
724 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
725 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727show_pwm_offset(1);
728show_pwm_offset(2);
729show_pwm_offset(3);
730
Jean Delvare17d648b2006-08-28 14:23:46 +0200731/* A different set of callbacks for 16-bit fans */
732static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
733 char *buf)
734{
735 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
736 int nr = sensor_attr->index;
737 struct it87_data *data = it87_update_device(dev);
738 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
739}
740
741static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
742 char *buf)
743{
744 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
745 int nr = sensor_attr->index;
746 struct it87_data *data = it87_update_device(dev);
747 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
748}
749
750static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
751 const char *buf, size_t count)
752{
753 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
754 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200755 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare17d648b2006-08-28 14:23:46 +0200756 int val = simple_strtol(buf, NULL, 10);
757
758 mutex_lock(&data->update_lock);
759 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200760 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +0200761 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200762 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +0200763 data->fan_min[nr] >> 8);
764 mutex_unlock(&data->update_lock);
765 return count;
766}
767
768/* We want to use the same sysfs file names as 8-bit fans, but we need
769 different variable names, so we have to use SENSOR_ATTR instead of
770 SENSOR_DEVICE_ATTR. */
771#define show_fan16_offset(offset) \
772static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
773 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
774 show_fan16, NULL, offset - 1); \
775static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
776 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
777 show_fan16_min, set_fan16_min, offset - 1)
778
779show_fan16_offset(1);
780show_fan16_offset(2);
781show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200782show_fan16_offset(4);
783show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +0200784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400786static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200789 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
Jean Delvare1d66c642005-04-18 21:16:59 -0700791static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Jean Delvare0124dd72007-11-25 16:16:41 +0100793static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
794 char *buf)
795{
796 int bitnr = to_sensor_dev_attr(attr)->index;
797 struct it87_data *data = it87_update_device(dev);
798 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
799}
800static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
801static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
802static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
803static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
804static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
805static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
806static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
807static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
808static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
809static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
810static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
811static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
812static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
813static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
814static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
815static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400818show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Jean Delvare90d66192007-10-08 18:24:35 +0200820 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +0100821 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400824store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200826 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 u32 val;
828
829 val = simple_strtoul(buf, NULL, 10);
830 data->vrm = val;
831
832 return count;
833}
834static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400837show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 struct it87_data *data = it87_update_device(dev);
840 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
841}
842static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +0200843
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200844static ssize_t show_name(struct device *dev, struct device_attribute
845 *devattr, char *buf)
846{
847 struct it87_data *data = dev_get_drvdata(dev);
848 return sprintf(buf, "%s\n", data->name);
849}
850static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
851
Jean Delvare87808be2006-09-24 21:17:13 +0200852static struct attribute *it87_attributes[] = {
853 &sensor_dev_attr_in0_input.dev_attr.attr,
854 &sensor_dev_attr_in1_input.dev_attr.attr,
855 &sensor_dev_attr_in2_input.dev_attr.attr,
856 &sensor_dev_attr_in3_input.dev_attr.attr,
857 &sensor_dev_attr_in4_input.dev_attr.attr,
858 &sensor_dev_attr_in5_input.dev_attr.attr,
859 &sensor_dev_attr_in6_input.dev_attr.attr,
860 &sensor_dev_attr_in7_input.dev_attr.attr,
861 &sensor_dev_attr_in8_input.dev_attr.attr,
862 &sensor_dev_attr_in0_min.dev_attr.attr,
863 &sensor_dev_attr_in1_min.dev_attr.attr,
864 &sensor_dev_attr_in2_min.dev_attr.attr,
865 &sensor_dev_attr_in3_min.dev_attr.attr,
866 &sensor_dev_attr_in4_min.dev_attr.attr,
867 &sensor_dev_attr_in5_min.dev_attr.attr,
868 &sensor_dev_attr_in6_min.dev_attr.attr,
869 &sensor_dev_attr_in7_min.dev_attr.attr,
870 &sensor_dev_attr_in0_max.dev_attr.attr,
871 &sensor_dev_attr_in1_max.dev_attr.attr,
872 &sensor_dev_attr_in2_max.dev_attr.attr,
873 &sensor_dev_attr_in3_max.dev_attr.attr,
874 &sensor_dev_attr_in4_max.dev_attr.attr,
875 &sensor_dev_attr_in5_max.dev_attr.attr,
876 &sensor_dev_attr_in6_max.dev_attr.attr,
877 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +0100878 &sensor_dev_attr_in0_alarm.dev_attr.attr,
879 &sensor_dev_attr_in1_alarm.dev_attr.attr,
880 &sensor_dev_attr_in2_alarm.dev_attr.attr,
881 &sensor_dev_attr_in3_alarm.dev_attr.attr,
882 &sensor_dev_attr_in4_alarm.dev_attr.attr,
883 &sensor_dev_attr_in5_alarm.dev_attr.attr,
884 &sensor_dev_attr_in6_alarm.dev_attr.attr,
885 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200886
887 &sensor_dev_attr_temp1_input.dev_attr.attr,
888 &sensor_dev_attr_temp2_input.dev_attr.attr,
889 &sensor_dev_attr_temp3_input.dev_attr.attr,
890 &sensor_dev_attr_temp1_max.dev_attr.attr,
891 &sensor_dev_attr_temp2_max.dev_attr.attr,
892 &sensor_dev_attr_temp3_max.dev_attr.attr,
893 &sensor_dev_attr_temp1_min.dev_attr.attr,
894 &sensor_dev_attr_temp2_min.dev_attr.attr,
895 &sensor_dev_attr_temp3_min.dev_attr.attr,
896 &sensor_dev_attr_temp1_type.dev_attr.attr,
897 &sensor_dev_attr_temp2_type.dev_attr.attr,
898 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +0100899 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
900 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
901 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200902
903 &dev_attr_alarms.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200904 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200905 NULL
906};
907
908static const struct attribute_group it87_group = {
909 .attrs = it87_attributes,
910};
911
912static struct attribute *it87_attributes_opt[] = {
913 &sensor_dev_attr_fan1_input16.dev_attr.attr,
914 &sensor_dev_attr_fan1_min16.dev_attr.attr,
915 &sensor_dev_attr_fan2_input16.dev_attr.attr,
916 &sensor_dev_attr_fan2_min16.dev_attr.attr,
917 &sensor_dev_attr_fan3_input16.dev_attr.attr,
918 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvarec7f1f712007-09-03 17:11:46 +0200919 &sensor_dev_attr_fan4_input16.dev_attr.attr,
920 &sensor_dev_attr_fan4_min16.dev_attr.attr,
921 &sensor_dev_attr_fan5_input16.dev_attr.attr,
922 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200923
924 &sensor_dev_attr_fan1_input.dev_attr.attr,
925 &sensor_dev_attr_fan1_min.dev_attr.attr,
926 &sensor_dev_attr_fan1_div.dev_attr.attr,
927 &sensor_dev_attr_fan2_input.dev_attr.attr,
928 &sensor_dev_attr_fan2_min.dev_attr.attr,
929 &sensor_dev_attr_fan2_div.dev_attr.attr,
930 &sensor_dev_attr_fan3_input.dev_attr.attr,
931 &sensor_dev_attr_fan3_min.dev_attr.attr,
932 &sensor_dev_attr_fan3_div.dev_attr.attr,
933
Jean Delvare0124dd72007-11-25 16:16:41 +0100934 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
935 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
936 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
937 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
938 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
939
Jean Delvare87808be2006-09-24 21:17:13 +0200940 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
941 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
942 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
943 &sensor_dev_attr_pwm1.dev_attr.attr,
944 &sensor_dev_attr_pwm2.dev_attr.attr,
945 &sensor_dev_attr_pwm3.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +0100946 &dev_attr_pwm1_freq.attr,
947 &dev_attr_pwm2_freq.attr,
948 &dev_attr_pwm3_freq.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200949
950 &dev_attr_vrm.attr,
951 &dev_attr_cpu0_vid.attr,
952 NULL
953};
954
955static const struct attribute_group it87_group_opt = {
956 .attrs = it87_attributes_opt,
957};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Jean Delvare2d8672c2005-07-19 23:56:35 +0200959/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200960static int __init it87_find(unsigned short *address,
961 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 int err = -ENODEV;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200964 u16 chip_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 superio_enter();
Jean Delvare67b671b2007-12-06 23:13:42 +0100967 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200968
969 switch (chip_type) {
970 case IT8705F_DEVID:
971 sio_data->type = it87;
972 break;
973 case IT8712F_DEVID:
974 sio_data->type = it8712;
975 break;
976 case IT8716F_DEVID:
977 case IT8726F_DEVID:
978 sio_data->type = it8716;
979 break;
980 case IT8718F_DEVID:
981 sio_data->type = it8718;
982 break;
983 case 0xffff: /* No device at all */
984 goto exit;
985 default:
986 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
987 chip_type);
988 goto exit;
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Jean Delvare87673dd2006-08-28 14:37:19 +0200991 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
993 pr_info("it87: Device not activated, skipping\n");
994 goto exit;
995 }
996
997 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
998 if (*address == 0) {
999 pr_info("it87: Base address not set, skipping\n");
1000 goto exit;
1001 }
1002
1003 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001004 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001006 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Jean Delvare87673dd2006-08-28 14:37:19 +02001008 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1009 if (chip_type != IT8705F_DEVID) {
1010 int reg;
1011
1012 superio_select(GPIO);
1013 if (chip_type == it8718)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001014 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001015
1016 reg = superio_inb(IT87_SIO_PINX2_REG);
1017 if (reg & (1 << 0))
1018 pr_info("it87: in3 is VCC (+5V)\n");
1019 if (reg & (1 << 1))
1020 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
1021 }
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023exit:
1024 superio_exit();
1025 return err;
1026}
1027
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001028static int __devinit it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001031 struct resource *res;
1032 struct device *dev = &pdev->dev;
1033 struct it87_sio_data *sio_data = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 int enable_pwm_interface;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001036 static const char *names[] = {
1037 "it87",
1038 "it8712",
1039 "it8716",
1040 "it8718",
1041 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001043 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001044 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001045 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1046 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001047 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001048 err = -EBUSY;
1049 goto ERROR0;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001052 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 err = -ENOMEM;
1054 goto ERROR1;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001057 data->addr = res->start;
1058 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001059 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001060 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001063 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1064 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001065 err = -ENODEV;
1066 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001069 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001071 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001074 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001077 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 /* Register sysfs hooks */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001080 if ((err = sysfs_create_group(&dev->kobj, &it87_group)))
1081 goto ERROR2;
Jean Delvare17d648b2006-08-28 14:23:46 +02001082
Jean Delvare9060f8b2006-08-28 14:24:17 +02001083 /* Do not create fan files for disabled fans */
Andrew Paprocki04751692008-08-06 22:41:06 +02001084 if (has_16bit_fans(data)) {
Jean Delvare87673dd2006-08-28 14:37:19 +02001085 /* 16-bit tachometers */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001086 if (data->has_fan & (1 << 0)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001087 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001088 &sensor_dev_attr_fan1_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001089 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001090 &sensor_dev_attr_fan1_min16.dev_attr))
1091 || (err = device_create_file(dev,
1092 &sensor_dev_attr_fan1_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001093 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001094 }
1095 if (data->has_fan & (1 << 1)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001096 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001097 &sensor_dev_attr_fan2_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001098 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001099 &sensor_dev_attr_fan2_min16.dev_attr))
1100 || (err = device_create_file(dev,
1101 &sensor_dev_attr_fan2_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001102 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001103 }
1104 if (data->has_fan & (1 << 2)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001105 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001106 &sensor_dev_attr_fan3_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001107 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001108 &sensor_dev_attr_fan3_min16.dev_attr))
1109 || (err = device_create_file(dev,
1110 &sensor_dev_attr_fan3_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001111 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001112 }
Jean Delvarec7f1f712007-09-03 17:11:46 +02001113 if (data->has_fan & (1 << 3)) {
1114 if ((err = device_create_file(dev,
1115 &sensor_dev_attr_fan4_input16.dev_attr))
1116 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001117 &sensor_dev_attr_fan4_min16.dev_attr))
1118 || (err = device_create_file(dev,
1119 &sensor_dev_attr_fan4_alarm.dev_attr)))
Jean Delvarec7f1f712007-09-03 17:11:46 +02001120 goto ERROR4;
1121 }
1122 if (data->has_fan & (1 << 4)) {
1123 if ((err = device_create_file(dev,
1124 &sensor_dev_attr_fan5_input16.dev_attr))
1125 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001126 &sensor_dev_attr_fan5_min16.dev_attr))
1127 || (err = device_create_file(dev,
1128 &sensor_dev_attr_fan5_alarm.dev_attr)))
Jean Delvarec7f1f712007-09-03 17:11:46 +02001129 goto ERROR4;
1130 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001131 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001132 /* 8-bit tachometers with clock divider */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001133 if (data->has_fan & (1 << 0)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001134 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001135 &sensor_dev_attr_fan1_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001136 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001137 &sensor_dev_attr_fan1_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001138 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001139 &sensor_dev_attr_fan1_div.dev_attr))
1140 || (err = device_create_file(dev,
1141 &sensor_dev_attr_fan1_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001142 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001143 }
1144 if (data->has_fan & (1 << 1)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001145 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001146 &sensor_dev_attr_fan2_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001147 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001148 &sensor_dev_attr_fan2_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001149 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001150 &sensor_dev_attr_fan2_div.dev_attr))
1151 || (err = device_create_file(dev,
1152 &sensor_dev_attr_fan2_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001153 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001154 }
1155 if (data->has_fan & (1 << 2)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001156 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001157 &sensor_dev_attr_fan3_input.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_fan3_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001160 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001161 &sensor_dev_attr_fan3_div.dev_attr))
1162 || (err = device_create_file(dev,
1163 &sensor_dev_attr_fan3_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001164 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001165 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001166 }
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (enable_pwm_interface) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001169 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001170 &sensor_dev_attr_pwm1_enable.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001171 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001172 &sensor_dev_attr_pwm2_enable.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001173 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001174 &sensor_dev_attr_pwm3_enable.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001175 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001176 &sensor_dev_attr_pwm1.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001177 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001178 &sensor_dev_attr_pwm2.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001179 || (err = device_create_file(dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001180 &sensor_dev_attr_pwm3.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001181 || (err = device_create_file(dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001182 &dev_attr_pwm1_freq))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001183 || (err = device_create_file(dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001184 &dev_attr_pwm2_freq))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001185 || (err = device_create_file(dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001186 &dev_attr_pwm3_freq)))
Jean Delvare87808be2006-09-24 21:17:13 +02001187 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
Jean Delvare87673dd2006-08-28 14:37:19 +02001190 if (data->type == it8712 || data->type == it8716
1191 || data->type == it8718) {
Jean Delvare303760b2005-07-31 21:52:01 +02001192 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001193 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001194 data->vid = sio_data->vid_value;
1195 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001196 &dev_attr_vrm))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001197 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001198 &dev_attr_cpu0_vid)))
1199 goto ERROR4;
1200 }
1201
Tony Jones1beeffe2007-08-20 13:46:20 -07001202 data->hwmon_dev = hwmon_device_register(dev);
1203 if (IS_ERR(data->hwmon_dev)) {
1204 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001205 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207
1208 return 0;
1209
Jean Delvare87808be2006-09-24 21:17:13 +02001210ERROR4:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001211 sysfs_remove_group(&dev->kobj, &it87_group);
1212 sysfs_remove_group(&dev->kobj, &it87_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213ERROR2:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001214 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 kfree(data);
1216ERROR1:
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001217 release_region(res->start, IT87_EC_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218ERROR0:
1219 return err;
1220}
1221
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001222static int __devexit it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001224 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Tony Jones1beeffe2007-08-20 13:46:20 -07001226 hwmon_device_unregister(data->hwmon_dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001227 sysfs_remove_group(&pdev->dev.kobj, &it87_group);
1228 sysfs_remove_group(&pdev->dev.kobj, &it87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001229
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001230 release_region(data->addr, IT87_EC_EXTENT);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001231 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001232 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 return 0;
1235}
1236
Jean Delvare7f999aa2007-02-14 21:15:03 +01001237/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1239 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001240static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001242 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1243 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Jean Delvare7f999aa2007-02-14 21:15:03 +01001246/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1248 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001249static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001251 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1252 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255/* Return 1 if and only if the PWM interface is safe to use */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001256static int __devinit it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001258 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1260 * and polarity set to active low is sign that this is the case so we
1261 * disable pwm control to protect the user. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001262 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if ((tmp & 0x87) == 0) {
1264 if (fix_pwm_polarity) {
1265 /* The user asks us to attempt a chip reconfiguration.
1266 * This means switching to active high polarity and
1267 * inverting all fan speed values. */
1268 int i;
1269 u8 pwm[3];
1270
1271 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001272 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 IT87_REG_PWM(i));
1274
1275 /* If any fan is in automatic pwm mode, the polarity
1276 * might be correct, as suspicious as it seems, so we
1277 * better don't change anything (but still disable the
1278 * PWM interface). */
1279 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001280 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001282 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 tmp | 0x87);
1284 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001285 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 IT87_REG_PWM(i),
1287 0x7f & ~pwm[i]);
1288 return 1;
1289 }
1290
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001291 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 "too broken to be fixed\n");
1293 }
1294
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001295 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 "defaults, disabling PWM interface\n");
1297 return 0;
1298 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001299 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 "sane, won't touch\n");
1301 }
1302
1303 return 1;
1304}
1305
1306/* Called when we have found a new IT87. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001307static void __devinit it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001309 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 int tmp, i;
1311
1312 /* initialize to sane defaults:
1313 * - if the chip is in manual pwm mode, this will be overwritten with
1314 * the actual settings on the chip (so in this case, initialization
1315 * is not needed)
1316 * - if in automatic or on/off mode, we could switch to manual mode,
1317 * read the registers and set manual_pwm_ctl accordingly, but currently
1318 * this is not implemented, so we initialize to something sane */
1319 for (i = 0; i < 3; i++) {
1320 data->manual_pwm_ctl[i] = 0xff;
1321 }
1322
Jean Delvarec5df9b72006-08-28 14:37:54 +02001323 /* Some chips seem to have default value 0xff for all limit
1324 * registers. For low voltage limits it makes no sense and triggers
1325 * alarms, so change to 0 instead. For high temperature limits, it
1326 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1327 * but is still confusing, so change to 127 degrees C. */
1328 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001329 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001330 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001331 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001332 }
1333 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001334 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001335 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001336 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001337 }
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* Check if temperature channnels are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001340 tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if ((tmp & 0x3f) == 0) {
1342 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1343 tmp = (tmp & 0xc0) | 0x2a;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001344 it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 data->sensor = tmp;
1347
1348 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001349 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if ((tmp & 0xff) == 0) {
1351 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001352 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354
1355 /* Check if tachometers are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001356 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if ((data->fan_main_ctrl & 0x70) == 0) {
1358 /* Enable all fan tachometers */
1359 data->fan_main_ctrl |= 0x70;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001360 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001362 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Jean Delvare17d648b2006-08-28 14:23:46 +02001364 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02001365 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001366 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001367 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001368 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02001369 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001370 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02001371 tmp | 0x07);
1372 }
Jean Delvarec7f1f712007-09-03 17:11:46 +02001373 if (tmp & (1 << 4))
1374 data->has_fan |= (1 << 3); /* fan4 enabled */
1375 if (tmp & (1 << 5))
1376 data->has_fan |= (1 << 4); /* fan5 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02001377 }
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /* Set current fan mode registers and the default settings for the
1380 * other mode registers */
1381 for (i = 0; i < 3; i++) {
1382 if (data->fan_main_ctrl & (1 << i)) {
1383 /* pwm mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001384 tmp = it87_read_value(data, IT87_REG_PWM(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (tmp & 0x80) {
1386 /* automatic pwm - not yet implemented, but
1387 * leave the settings made by the BIOS alone
1388 * until a change is requested via the sysfs
1389 * interface */
1390 } else {
1391 /* manual pwm */
1392 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1393 }
1394 }
1395 }
1396
1397 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001398 it87_write_value(data, IT87_REG_CONFIG,
1399 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 | (update_vbat ? 0x41 : 0x01));
1401}
1402
1403static struct it87_data *it87_update_device(struct device *dev)
1404{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001405 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int i;
1407
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001408 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1411 || !data->valid) {
1412
1413 if (update_vbat) {
1414 /* Cleared after each update, so reenable. Value
1415 returned by this read will be previous value */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001416 it87_write_value(data, IT87_REG_CONFIG,
1417 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419 for (i = 0; i <= 7; i++) {
1420 data->in[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001421 it87_read_value(data, IT87_REG_VIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 data->in_min[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001423 it87_read_value(data, IT87_REG_VIN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 data->in_max[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001425 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
Jean Delvare3543a532006-08-28 14:27:25 +02001427 /* in8 (battery) has no limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 data->in[8] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001429 it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Jean Delvarec7f1f712007-09-03 17:11:46 +02001431 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02001432 /* Skip disabled fans */
1433 if (!(data->has_fan & (1 << i)))
1434 continue;
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 data->fan_min[i] =
Jean Delvarec7f1f712007-09-03 17:11:46 +02001437 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001438 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001439 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02001440 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02001441 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001442 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001443 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001444 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001445 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02001446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448 for (i = 0; i < 3; i++) {
1449 data->temp[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001450 it87_read_value(data, IT87_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 data->temp_high[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001452 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 data->temp_low[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001454 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456
Jean Delvare17d648b2006-08-28 14:23:46 +02001457 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02001458 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001459 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02001460 data->fan_div[0] = i & 0x07;
1461 data->fan_div[1] = (i >> 3) & 0x07;
1462 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001466 it87_read_value(data, IT87_REG_ALARM1) |
1467 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1468 (it87_read_value(data, IT87_REG_ALARM3) << 16);
1469 data->fan_main_ctrl = it87_read_value(data,
1470 IT87_REG_FAN_MAIN_CTRL);
1471 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001473 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Andrew Paprocki04751692008-08-06 22:41:06 +02001474 /* The 8705 does not have VID capability.
1475 The 8718 does not use IT87_REG_VID for the same purpose. */
Jean Delvare17d648b2006-08-28 14:23:46 +02001476 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001477 data->vid = it87_read_value(data, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02001478 /* The older IT8712F revisions had only 5 VID pins,
1479 but we assume it is always safe to read 6 bits. */
1480 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482 data->last_updated = jiffies;
1483 data->valid = 1;
1484 }
1485
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001486 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 return data;
1489}
1490
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001491static int __init it87_device_add(unsigned short address,
1492 const struct it87_sio_data *sio_data)
1493{
1494 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001495 .start = address + IT87_EC_OFFSET,
1496 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001497 .name = DRVNAME,
1498 .flags = IORESOURCE_IO,
1499 };
1500 int err;
1501
1502 pdev = platform_device_alloc(DRVNAME, address);
1503 if (!pdev) {
1504 err = -ENOMEM;
1505 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1506 goto exit;
1507 }
1508
1509 err = platform_device_add_resources(pdev, &res, 1);
1510 if (err) {
1511 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1512 "(%d)\n", err);
1513 goto exit_device_put;
1514 }
1515
1516 err = platform_device_add_data(pdev, sio_data,
1517 sizeof(struct it87_sio_data));
1518 if (err) {
1519 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1520 goto exit_device_put;
1521 }
1522
1523 err = platform_device_add(pdev);
1524 if (err) {
1525 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1526 err);
1527 goto exit_device_put;
1528 }
1529
1530 return 0;
1531
1532exit_device_put:
1533 platform_device_put(pdev);
1534exit:
1535 return err;
1536}
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538static int __init sm_it87_init(void)
1539{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001540 int err;
1541 unsigned short isa_address=0;
1542 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02001543
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001544 err = it87_find(&isa_address, &sio_data);
1545 if (err)
1546 return err;
1547 err = platform_driver_register(&it87_driver);
1548 if (err)
1549 return err;
1550
1551 err = it87_device_add(isa_address, &sio_data);
1552 if (err){
1553 platform_driver_unregister(&it87_driver);
1554 return err;
1555 }
1556
1557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
1560static void __exit sm_it87_exit(void)
1561{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001562 platform_device_unregister(pdev);
1563 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566
Jean Delvaref1d8e332007-11-25 16:14:44 +01001567MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02001568 "Jean Delvare <khali@linux-fr.org>");
Rudolf Marek08a8f6e2007-06-09 10:11:16 -04001569MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8726F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570module_param(update_vbat, bool, 0);
1571MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1572module_param(fix_pwm_polarity, bool, 0);
1573MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1574MODULE_LICENSE("GPL");
1575
1576module_init(sm_it87_init);
1577module_exit(sm_it87_exit);