blob: 30cdb0956779646d311476078a9c30ebc6f643ca [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{
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200275 /* IT8705F Datasheet 0.4.1, 3h == Version G.
276 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 7h == Version I.
277 These are the first revisions with 16bit tachometer support. */
278 return (data->type == it87 && data->revision >= 0x03)
279 || (data->type == it8712 && data->revision >= 0x07)
Andrew Paprocki04751692008-08-06 22:41:06 +0200280 || data->type == it8716
281 || data->type == it8718;
282}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200284static int it87_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200285static int __devexit it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200287static int it87_read_value(struct it87_data *data, u8 reg);
288static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200290static int it87_check_pwm(struct device *dev);
291static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200294static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100295 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200296 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200297 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100298 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200299 .probe = it87_probe,
300 .remove = __devexit_p(it87_remove),
Jean Delvarefde09502005-07-19 23:51:07 +0200301};
302
Jean Delvare20ad93d2005-06-05 11:53:25 +0200303static ssize_t show_in(struct device *dev, struct device_attribute *attr,
304 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200306 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
307 int nr = sensor_attr->index;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 struct it87_data *data = it87_update_device(dev);
310 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
311}
312
Jean Delvare20ad93d2005-06-05 11:53:25 +0200313static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
314 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200316 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
317 int nr = sensor_attr->index;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct it87_data *data = it87_update_device(dev);
320 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
321}
322
Jean Delvare20ad93d2005-06-05 11:53:25 +0200323static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
324 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200326 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
327 int nr = sensor_attr->index;
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct it87_data *data = it87_update_device(dev);
330 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
331}
332
Jean Delvare20ad93d2005-06-05 11:53:25 +0200333static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
334 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200336 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
337 int nr = sensor_attr->index;
338
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200339 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 unsigned long val = simple_strtoul(buf, NULL, 10);
341
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100342 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 data->in_min[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200344 it87_write_value(data, IT87_REG_VIN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100346 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return count;
348}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200349static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
350 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200352 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
353 int nr = sensor_attr->index;
354
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200355 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 unsigned long val = simple_strtoul(buf, NULL, 10);
357
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100358 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 data->in_max[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200360 it87_write_value(data, IT87_REG_VIN_MAX(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100362 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return count;
364}
365
366#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200367static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
368 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200371static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
372 show_in_min, set_in_min, offset); \
373static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
374 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376show_in_offset(0);
377limit_in_offset(0);
378show_in_offset(1);
379limit_in_offset(1);
380show_in_offset(2);
381limit_in_offset(2);
382show_in_offset(3);
383limit_in_offset(3);
384show_in_offset(4);
385limit_in_offset(4);
386show_in_offset(5);
387limit_in_offset(5);
388show_in_offset(6);
389limit_in_offset(6);
390show_in_offset(7);
391limit_in_offset(7);
392show_in_offset(8);
393
394/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200395static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
396 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200398 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
399 int nr = sensor_attr->index;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct it87_data *data = it87_update_device(dev);
402 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
403}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200404static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
405 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200407 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
408 int nr = sensor_attr->index;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 struct it87_data *data = it87_update_device(dev);
411 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
412}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200413static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
414 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200416 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
417 int nr = sensor_attr->index;
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct it87_data *data = it87_update_device(dev);
420 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
421}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200422static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
423 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200425 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
426 int nr = sensor_attr->index;
427
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200428 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int val = simple_strtol(buf, NULL, 10);
430
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100431 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 data->temp_high[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200433 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100434 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return count;
436}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200437static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
438 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200440 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
441 int nr = sensor_attr->index;
442
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200443 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 int val = simple_strtol(buf, NULL, 10);
445
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100446 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 data->temp_low[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200448 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100449 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return count;
451}
452#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200453static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
454 show_temp, NULL, offset - 1); \
455static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
456 show_temp_max, set_temp_max, offset - 1); \
457static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
458 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460show_temp_offset(1);
461show_temp_offset(2);
462show_temp_offset(3);
463
Jean Delvare20ad93d2005-06-05 11:53:25 +0200464static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
465 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200467 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
468 int nr = sensor_attr->index;
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct it87_data *data = it87_update_device(dev);
471 u8 reg = data->sensor; /* In case the value is updated while we use it */
472
473 if (reg & (1 << nr))
474 return sprintf(buf, "3\n"); /* thermal diode */
475 if (reg & (8 << nr))
476 return sprintf(buf, "2\n"); /* thermistor */
477 return sprintf(buf, "0\n"); /* disabled */
478}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200479static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
480 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200482 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
483 int nr = sensor_attr->index;
484
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200485 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int val = simple_strtol(buf, NULL, 10);
487
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100488 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 data->sensor &= ~(1 << nr);
491 data->sensor &= ~(8 << nr);
492 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
493 if (val == 3)
494 data->sensor |= 1 << nr;
495 else if (val == 2)
496 data->sensor |= 8 << nr;
497 else if (val != 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100498 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return -EINVAL;
500 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200501 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100502 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return count;
504}
505#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200506static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
507 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509show_sensor_offset(1);
510show_sensor_offset(2);
511show_sensor_offset(3);
512
513/* 3 Fans */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200514static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
515 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200517 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
518 int nr = sensor_attr->index;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct it87_data *data = it87_update_device(dev);
521 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
522 DIV_FROM_REG(data->fan_div[nr])));
523}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200524static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
525 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200527 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
528 int nr = sensor_attr->index;
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct it87_data *data = it87_update_device(dev);
531 return sprintf(buf,"%d\n",
532 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
533}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200534static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
535 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200537 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
538 int nr = sensor_attr->index;
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct it87_data *data = it87_update_device(dev);
541 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
542}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200543static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
544 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200546 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
547 int nr = sensor_attr->index;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct it87_data *data = it87_update_device(dev);
550 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
551}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200552static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
553 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200555 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
556 int nr = sensor_attr->index;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct it87_data *data = it87_update_device(dev);
559 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
560}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100561static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
562 char *buf)
563{
564 struct it87_data *data = it87_update_device(dev);
565 int index = (data->fan_ctl >> 4) & 0x07;
566
567 return sprintf(buf, "%u\n", pwm_freq[index]);
568}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200569static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
570 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200572 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
573 int nr = sensor_attr->index;
574
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200575 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int val = simple_strtol(buf, NULL, 10);
Jean Delvare7f999aa2007-02-14 21:15:03 +0100577 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100579 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200580 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800581 switch (nr) {
582 case 0: data->fan_div[nr] = reg & 0x07; break;
583 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
584 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
585 }
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200588 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100589 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return count;
591}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200592static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
593 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200595 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
596 int nr = sensor_attr->index;
597
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200598 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvareb9e349f2006-08-28 14:26:22 +0200599 unsigned long val = simple_strtoul(buf, NULL, 10);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200600 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 u8 old;
602
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100603 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200604 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200606 /* Save fan min limit */
607 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 switch (nr) {
610 case 0:
611 case 1:
612 data->fan_div[nr] = DIV_TO_REG(val);
613 break;
614 case 2:
615 if (val < 8)
616 data->fan_div[nr] = 1;
617 else
618 data->fan_div[nr] = 3;
619 }
620 val = old & 0x80;
621 val |= (data->fan_div[0] & 0x07);
622 val |= (data->fan_div[1] & 0x07) << 3;
623 if (data->fan_div[2] == 3)
624 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200625 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200627 /* Restore fan min limit */
628 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200629 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200630
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100631 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return count;
633}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200634static ssize_t set_pwm_enable(struct device *dev,
635 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200637 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
638 int nr = sensor_attr->index;
639
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200640 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int val = simple_strtol(buf, NULL, 10);
642
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100643 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (val == 0) {
646 int tmp;
647 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200648 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
649 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* set on/off mode */
651 data->fan_main_ctrl &= ~(1 << nr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200652 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 } else if (val == 1) {
654 /* set SmartGuardian mode */
655 data->fan_main_ctrl |= (1 << nr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200656 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200658 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 } else {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100660 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return -EINVAL;
662 }
663
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100664 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return count;
666}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200667static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
668 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200670 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
671 int nr = sensor_attr->index;
672
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200673 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int val = simple_strtol(buf, NULL, 10);
675
676 if (val < 0 || val > 255)
677 return -EINVAL;
678
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100679 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 data->manual_pwm_ctl[nr] = val;
681 if (data->fan_main_ctrl & (1 << nr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200682 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100683 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return count;
685}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100686static ssize_t set_pwm_freq(struct device *dev,
687 struct device_attribute *attr, const char *buf, size_t count)
688{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200689 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100690 unsigned long val = simple_strtoul(buf, NULL, 10);
691 int i;
692
693 /* Search for the nearest available frequency */
694 for (i = 0; i < 7; i++) {
695 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
696 break;
697 }
698
699 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200700 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100701 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200702 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100703 mutex_unlock(&data->update_lock);
704
705 return count;
706}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Jean Delvare20ad93d2005-06-05 11:53:25 +0200708#define show_fan_offset(offset) \
709static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
710 show_fan, NULL, offset - 1); \
711static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
712 show_fan_min, set_fan_min, offset - 1); \
713static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
714 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716show_fan_offset(1);
717show_fan_offset(2);
718show_fan_offset(3);
719
720#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200721static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
722 show_pwm_enable, set_pwm_enable, offset - 1); \
723static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +0100724 show_pwm, set_pwm, offset - 1); \
725static DEVICE_ATTR(pwm##offset##_freq, \
726 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
727 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729show_pwm_offset(1);
730show_pwm_offset(2);
731show_pwm_offset(3);
732
Jean Delvare17d648b2006-08-28 14:23:46 +0200733/* A different set of callbacks for 16-bit fans */
734static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
735 char *buf)
736{
737 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
738 int nr = sensor_attr->index;
739 struct it87_data *data = it87_update_device(dev);
740 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
741}
742
743static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
744 char *buf)
745{
746 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
747 int nr = sensor_attr->index;
748 struct it87_data *data = it87_update_device(dev);
749 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
750}
751
752static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
753 const char *buf, size_t count)
754{
755 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
756 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200757 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare17d648b2006-08-28 14:23:46 +0200758 int val = simple_strtol(buf, NULL, 10);
759
760 mutex_lock(&data->update_lock);
761 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200762 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +0200763 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200764 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +0200765 data->fan_min[nr] >> 8);
766 mutex_unlock(&data->update_lock);
767 return count;
768}
769
770/* We want to use the same sysfs file names as 8-bit fans, but we need
771 different variable names, so we have to use SENSOR_ATTR instead of
772 SENSOR_DEVICE_ATTR. */
773#define show_fan16_offset(offset) \
774static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
775 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
776 show_fan16, NULL, offset - 1); \
777static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
778 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
779 show_fan16_min, set_fan16_min, offset - 1)
780
781show_fan16_offset(1);
782show_fan16_offset(2);
783show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +0200784show_fan16_offset(4);
785show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +0200786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400788static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200791 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
Jean Delvare1d66c642005-04-18 21:16:59 -0700793static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Jean Delvare0124dd72007-11-25 16:16:41 +0100795static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
796 char *buf)
797{
798 int bitnr = to_sensor_dev_attr(attr)->index;
799 struct it87_data *data = it87_update_device(dev);
800 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
801}
802static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
803static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
804static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
805static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
806static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
807static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
808static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
809static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
810static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
811static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
812static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
813static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
814static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
815static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
816static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
817static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400820show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Jean Delvare90d66192007-10-08 18:24:35 +0200822 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +0100823 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400826store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200828 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 u32 val;
830
831 val = simple_strtoul(buf, NULL, 10);
832 data->vrm = val;
833
834 return count;
835}
836static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838static ssize_t
Yani Ioannou30f74292005-05-17 06:41:35 -0400839show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct it87_data *data = it87_update_device(dev);
842 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
843}
844static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +0200845
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200846static ssize_t show_name(struct device *dev, struct device_attribute
847 *devattr, char *buf)
848{
849 struct it87_data *data = dev_get_drvdata(dev);
850 return sprintf(buf, "%s\n", data->name);
851}
852static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
853
Jean Delvare87808be2006-09-24 21:17:13 +0200854static struct attribute *it87_attributes[] = {
855 &sensor_dev_attr_in0_input.dev_attr.attr,
856 &sensor_dev_attr_in1_input.dev_attr.attr,
857 &sensor_dev_attr_in2_input.dev_attr.attr,
858 &sensor_dev_attr_in3_input.dev_attr.attr,
859 &sensor_dev_attr_in4_input.dev_attr.attr,
860 &sensor_dev_attr_in5_input.dev_attr.attr,
861 &sensor_dev_attr_in6_input.dev_attr.attr,
862 &sensor_dev_attr_in7_input.dev_attr.attr,
863 &sensor_dev_attr_in8_input.dev_attr.attr,
864 &sensor_dev_attr_in0_min.dev_attr.attr,
865 &sensor_dev_attr_in1_min.dev_attr.attr,
866 &sensor_dev_attr_in2_min.dev_attr.attr,
867 &sensor_dev_attr_in3_min.dev_attr.attr,
868 &sensor_dev_attr_in4_min.dev_attr.attr,
869 &sensor_dev_attr_in5_min.dev_attr.attr,
870 &sensor_dev_attr_in6_min.dev_attr.attr,
871 &sensor_dev_attr_in7_min.dev_attr.attr,
872 &sensor_dev_attr_in0_max.dev_attr.attr,
873 &sensor_dev_attr_in1_max.dev_attr.attr,
874 &sensor_dev_attr_in2_max.dev_attr.attr,
875 &sensor_dev_attr_in3_max.dev_attr.attr,
876 &sensor_dev_attr_in4_max.dev_attr.attr,
877 &sensor_dev_attr_in5_max.dev_attr.attr,
878 &sensor_dev_attr_in6_max.dev_attr.attr,
879 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +0100880 &sensor_dev_attr_in0_alarm.dev_attr.attr,
881 &sensor_dev_attr_in1_alarm.dev_attr.attr,
882 &sensor_dev_attr_in2_alarm.dev_attr.attr,
883 &sensor_dev_attr_in3_alarm.dev_attr.attr,
884 &sensor_dev_attr_in4_alarm.dev_attr.attr,
885 &sensor_dev_attr_in5_alarm.dev_attr.attr,
886 &sensor_dev_attr_in6_alarm.dev_attr.attr,
887 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200888
889 &sensor_dev_attr_temp1_input.dev_attr.attr,
890 &sensor_dev_attr_temp2_input.dev_attr.attr,
891 &sensor_dev_attr_temp3_input.dev_attr.attr,
892 &sensor_dev_attr_temp1_max.dev_attr.attr,
893 &sensor_dev_attr_temp2_max.dev_attr.attr,
894 &sensor_dev_attr_temp3_max.dev_attr.attr,
895 &sensor_dev_attr_temp1_min.dev_attr.attr,
896 &sensor_dev_attr_temp2_min.dev_attr.attr,
897 &sensor_dev_attr_temp3_min.dev_attr.attr,
898 &sensor_dev_attr_temp1_type.dev_attr.attr,
899 &sensor_dev_attr_temp2_type.dev_attr.attr,
900 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +0100901 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
902 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
903 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200904
905 &dev_attr_alarms.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200906 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200907 NULL
908};
909
910static const struct attribute_group it87_group = {
911 .attrs = it87_attributes,
912};
913
914static struct attribute *it87_attributes_opt[] = {
915 &sensor_dev_attr_fan1_input16.dev_attr.attr,
916 &sensor_dev_attr_fan1_min16.dev_attr.attr,
917 &sensor_dev_attr_fan2_input16.dev_attr.attr,
918 &sensor_dev_attr_fan2_min16.dev_attr.attr,
919 &sensor_dev_attr_fan3_input16.dev_attr.attr,
920 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvarec7f1f712007-09-03 17:11:46 +0200921 &sensor_dev_attr_fan4_input16.dev_attr.attr,
922 &sensor_dev_attr_fan4_min16.dev_attr.attr,
923 &sensor_dev_attr_fan5_input16.dev_attr.attr,
924 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200925
926 &sensor_dev_attr_fan1_input.dev_attr.attr,
927 &sensor_dev_attr_fan1_min.dev_attr.attr,
928 &sensor_dev_attr_fan1_div.dev_attr.attr,
929 &sensor_dev_attr_fan2_input.dev_attr.attr,
930 &sensor_dev_attr_fan2_min.dev_attr.attr,
931 &sensor_dev_attr_fan2_div.dev_attr.attr,
932 &sensor_dev_attr_fan3_input.dev_attr.attr,
933 &sensor_dev_attr_fan3_min.dev_attr.attr,
934 &sensor_dev_attr_fan3_div.dev_attr.attr,
935
Jean Delvare0124dd72007-11-25 16:16:41 +0100936 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
937 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
938 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
939 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
940 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
941
Jean Delvare87808be2006-09-24 21:17:13 +0200942 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
943 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
944 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
945 &sensor_dev_attr_pwm1.dev_attr.attr,
946 &sensor_dev_attr_pwm2.dev_attr.attr,
947 &sensor_dev_attr_pwm3.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +0100948 &dev_attr_pwm1_freq.attr,
949 &dev_attr_pwm2_freq.attr,
950 &dev_attr_pwm3_freq.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200951
952 &dev_attr_vrm.attr,
953 &dev_attr_cpu0_vid.attr,
954 NULL
955};
956
957static const struct attribute_group it87_group_opt = {
958 .attrs = it87_attributes_opt,
959};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Jean Delvare2d8672c2005-07-19 23:56:35 +0200961/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200962static int __init it87_find(unsigned short *address,
963 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 int err = -ENODEV;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200966 u16 chip_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 superio_enter();
Jean Delvare67b671b2007-12-06 23:13:42 +0100969 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200970
971 switch (chip_type) {
972 case IT8705F_DEVID:
973 sio_data->type = it87;
974 break;
975 case IT8712F_DEVID:
976 sio_data->type = it8712;
977 break;
978 case IT8716F_DEVID:
979 case IT8726F_DEVID:
980 sio_data->type = it8716;
981 break;
982 case IT8718F_DEVID:
983 sio_data->type = it8718;
984 break;
985 case 0xffff: /* No device at all */
986 goto exit;
987 default:
988 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
989 chip_type);
990 goto exit;
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Jean Delvare87673dd2006-08-28 14:37:19 +0200993 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
995 pr_info("it87: Device not activated, skipping\n");
996 goto exit;
997 }
998
999 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1000 if (*address == 0) {
1001 pr_info("it87: Base address not set, skipping\n");
1002 goto exit;
1003 }
1004
1005 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001006 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001008 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Jean Delvare87673dd2006-08-28 14:37:19 +02001010 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1011 if (chip_type != IT8705F_DEVID) {
1012 int reg;
1013
1014 superio_select(GPIO);
1015 if (chip_type == it8718)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001016 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001017
1018 reg = superio_inb(IT87_SIO_PINX2_REG);
1019 if (reg & (1 << 0))
1020 pr_info("it87: in3 is VCC (+5V)\n");
1021 if (reg & (1 << 1))
1022 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
1023 }
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025exit:
1026 superio_exit();
1027 return err;
1028}
1029
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001030static int __devinit it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001033 struct resource *res;
1034 struct device *dev = &pdev->dev;
1035 struct it87_sio_data *sio_data = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 int enable_pwm_interface;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001038 static const char *names[] = {
1039 "it87",
1040 "it8712",
1041 "it8716",
1042 "it8718",
1043 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001045 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001046 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001047 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1048 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001049 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001050 err = -EBUSY;
1051 goto ERROR0;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001054 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 err = -ENOMEM;
1056 goto ERROR1;
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001059 data->addr = res->start;
1060 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001061 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001062 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001065 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1066 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001067 err = -ENODEV;
1068 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001071 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001073 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001076 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001079 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 /* Register sysfs hooks */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001082 if ((err = sysfs_create_group(&dev->kobj, &it87_group)))
1083 goto ERROR2;
Jean Delvare17d648b2006-08-28 14:23:46 +02001084
Jean Delvare9060f8b2006-08-28 14:24:17 +02001085 /* Do not create fan files for disabled fans */
Andrew Paprocki04751692008-08-06 22:41:06 +02001086 if (has_16bit_fans(data)) {
Jean Delvare87673dd2006-08-28 14:37:19 +02001087 /* 16-bit tachometers */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001088 if (data->has_fan & (1 << 0)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001089 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001090 &sensor_dev_attr_fan1_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001091 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001092 &sensor_dev_attr_fan1_min16.dev_attr))
1093 || (err = device_create_file(dev,
1094 &sensor_dev_attr_fan1_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001095 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001096 }
1097 if (data->has_fan & (1 << 1)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001098 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001099 &sensor_dev_attr_fan2_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001100 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001101 &sensor_dev_attr_fan2_min16.dev_attr))
1102 || (err = device_create_file(dev,
1103 &sensor_dev_attr_fan2_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001104 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001105 }
1106 if (data->has_fan & (1 << 2)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001107 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001108 &sensor_dev_attr_fan3_input16.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001109 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001110 &sensor_dev_attr_fan3_min16.dev_attr))
1111 || (err = device_create_file(dev,
1112 &sensor_dev_attr_fan3_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001113 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001114 }
Jean Delvarec7f1f712007-09-03 17:11:46 +02001115 if (data->has_fan & (1 << 3)) {
1116 if ((err = device_create_file(dev,
1117 &sensor_dev_attr_fan4_input16.dev_attr))
1118 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001119 &sensor_dev_attr_fan4_min16.dev_attr))
1120 || (err = device_create_file(dev,
1121 &sensor_dev_attr_fan4_alarm.dev_attr)))
Jean Delvarec7f1f712007-09-03 17:11:46 +02001122 goto ERROR4;
1123 }
1124 if (data->has_fan & (1 << 4)) {
1125 if ((err = device_create_file(dev,
1126 &sensor_dev_attr_fan5_input16.dev_attr))
1127 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001128 &sensor_dev_attr_fan5_min16.dev_attr))
1129 || (err = device_create_file(dev,
1130 &sensor_dev_attr_fan5_alarm.dev_attr)))
Jean Delvarec7f1f712007-09-03 17:11:46 +02001131 goto ERROR4;
1132 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001133 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001134 /* 8-bit tachometers with clock divider */
Jean Delvare9060f8b2006-08-28 14:24:17 +02001135 if (data->has_fan & (1 << 0)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001136 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001137 &sensor_dev_attr_fan1_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001138 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001139 &sensor_dev_attr_fan1_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001140 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001141 &sensor_dev_attr_fan1_div.dev_attr))
1142 || (err = device_create_file(dev,
1143 &sensor_dev_attr_fan1_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001144 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001145 }
1146 if (data->has_fan & (1 << 1)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001147 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001148 &sensor_dev_attr_fan2_input.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001149 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001150 &sensor_dev_attr_fan2_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001151 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001152 &sensor_dev_attr_fan2_div.dev_attr))
1153 || (err = device_create_file(dev,
1154 &sensor_dev_attr_fan2_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001155 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001156 }
1157 if (data->has_fan & (1 << 2)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001158 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001159 &sensor_dev_attr_fan3_input.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_fan3_min.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001162 || (err = device_create_file(dev,
Jean Delvare0124dd72007-11-25 16:16:41 +01001163 &sensor_dev_attr_fan3_div.dev_attr))
1164 || (err = device_create_file(dev,
1165 &sensor_dev_attr_fan3_alarm.dev_attr)))
Jean Delvare87808be2006-09-24 21:17:13 +02001166 goto ERROR4;
Jean Delvare9060f8b2006-08-28 14:24:17 +02001167 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001168 }
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (enable_pwm_interface) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001171 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001172 &sensor_dev_attr_pwm1_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_pwm2_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_pwm3_enable.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_pwm1.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001179 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001180 &sensor_dev_attr_pwm2.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001181 || (err = device_create_file(dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001182 &sensor_dev_attr_pwm3.dev_attr))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001183 || (err = device_create_file(dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001184 &dev_attr_pwm1_freq))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001185 || (err = device_create_file(dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001186 &dev_attr_pwm2_freq))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001187 || (err = device_create_file(dev,
Jean Delvaref8d0c192007-02-14 21:15:02 +01001188 &dev_attr_pwm3_freq)))
Jean Delvare87808be2006-09-24 21:17:13 +02001189 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191
Jean Delvare87673dd2006-08-28 14:37:19 +02001192 if (data->type == it8712 || data->type == it8716
1193 || data->type == it8718) {
Jean Delvare303760b2005-07-31 21:52:01 +02001194 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001195 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001196 data->vid = sio_data->vid_value;
1197 if ((err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001198 &dev_attr_vrm))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001199 || (err = device_create_file(dev,
Jean Delvare87808be2006-09-24 21:17:13 +02001200 &dev_attr_cpu0_vid)))
1201 goto ERROR4;
1202 }
1203
Tony Jones1beeffe2007-08-20 13:46:20 -07001204 data->hwmon_dev = hwmon_device_register(dev);
1205 if (IS_ERR(data->hwmon_dev)) {
1206 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001207 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
1210 return 0;
1211
Jean Delvare87808be2006-09-24 21:17:13 +02001212ERROR4:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001213 sysfs_remove_group(&dev->kobj, &it87_group);
1214 sysfs_remove_group(&dev->kobj, &it87_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215ERROR2:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001216 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 kfree(data);
1218ERROR1:
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001219 release_region(res->start, IT87_EC_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220ERROR0:
1221 return err;
1222}
1223
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001224static int __devexit it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001226 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Tony Jones1beeffe2007-08-20 13:46:20 -07001228 hwmon_device_unregister(data->hwmon_dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001229 sysfs_remove_group(&pdev->dev.kobj, &it87_group);
1230 sysfs_remove_group(&pdev->dev.kobj, &it87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001231
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001232 release_region(data->addr, IT87_EC_EXTENT);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001233 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001234 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 return 0;
1237}
1238
Jean Delvare7f999aa2007-02-14 21:15:03 +01001239/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1241 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001242static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001244 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1245 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Jean Delvare7f999aa2007-02-14 21:15:03 +01001248/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1250 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001251static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001253 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1254 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257/* Return 1 if and only if the PWM interface is safe to use */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001258static int __devinit it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001260 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1262 * and polarity set to active low is sign that this is the case so we
1263 * disable pwm control to protect the user. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001264 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if ((tmp & 0x87) == 0) {
1266 if (fix_pwm_polarity) {
1267 /* The user asks us to attempt a chip reconfiguration.
1268 * This means switching to active high polarity and
1269 * inverting all fan speed values. */
1270 int i;
1271 u8 pwm[3];
1272
1273 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001274 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 IT87_REG_PWM(i));
1276
1277 /* If any fan is in automatic pwm mode, the polarity
1278 * might be correct, as suspicious as it seems, so we
1279 * better don't change anything (but still disable the
1280 * PWM interface). */
1281 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001282 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001284 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 tmp | 0x87);
1286 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001287 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 IT87_REG_PWM(i),
1289 0x7f & ~pwm[i]);
1290 return 1;
1291 }
1292
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001293 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 "too broken to be fixed\n");
1295 }
1296
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001297 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 "defaults, disabling PWM interface\n");
1299 return 0;
1300 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001301 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 "sane, won't touch\n");
1303 }
1304
1305 return 1;
1306}
1307
1308/* Called when we have found a new IT87. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001309static void __devinit it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001311 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int tmp, i;
1313
1314 /* initialize to sane defaults:
1315 * - if the chip is in manual pwm mode, this will be overwritten with
1316 * the actual settings on the chip (so in this case, initialization
1317 * is not needed)
1318 * - if in automatic or on/off mode, we could switch to manual mode,
1319 * read the registers and set manual_pwm_ctl accordingly, but currently
1320 * this is not implemented, so we initialize to something sane */
1321 for (i = 0; i < 3; i++) {
1322 data->manual_pwm_ctl[i] = 0xff;
1323 }
1324
Jean Delvarec5df9b72006-08-28 14:37:54 +02001325 /* Some chips seem to have default value 0xff for all limit
1326 * registers. For low voltage limits it makes no sense and triggers
1327 * alarms, so change to 0 instead. For high temperature limits, it
1328 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1329 * but is still confusing, so change to 127 degrees C. */
1330 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001331 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001332 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001333 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001334 }
1335 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001336 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001337 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001338 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001339 }
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /* Check if temperature channnels are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001342 tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if ((tmp & 0x3f) == 0) {
1344 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1345 tmp = (tmp & 0xc0) | 0x2a;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001346 it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348 data->sensor = tmp;
1349
1350 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001351 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if ((tmp & 0xff) == 0) {
1353 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001354 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356
1357 /* Check if tachometers are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001358 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if ((data->fan_main_ctrl & 0x70) == 0) {
1360 /* Enable all fan tachometers */
1361 data->fan_main_ctrl |= 0x70;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001362 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001364 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Jean Delvare17d648b2006-08-28 14:23:46 +02001366 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02001367 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001368 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001369 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001370 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02001371 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001372 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02001373 tmp | 0x07);
1374 }
Andrew Paprocki816d8c62008-08-06 22:41:06 +02001375 /* IT8705F only supports three fans. */
1376 if (data->type != it87) {
1377 if (tmp & (1 << 4))
1378 data->has_fan |= (1 << 3); /* fan4 enabled */
1379 if (tmp & (1 << 5))
1380 data->has_fan |= (1 << 4); /* fan5 enabled */
1381 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001382 }
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /* Set current fan mode registers and the default settings for the
1385 * other mode registers */
1386 for (i = 0; i < 3; i++) {
1387 if (data->fan_main_ctrl & (1 << i)) {
1388 /* pwm mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001389 tmp = it87_read_value(data, IT87_REG_PWM(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (tmp & 0x80) {
1391 /* automatic pwm - not yet implemented, but
1392 * leave the settings made by the BIOS alone
1393 * until a change is requested via the sysfs
1394 * interface */
1395 } else {
1396 /* manual pwm */
1397 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1398 }
1399 }
1400 }
1401
1402 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001403 it87_write_value(data, IT87_REG_CONFIG,
1404 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 | (update_vbat ? 0x41 : 0x01));
1406}
1407
1408static struct it87_data *it87_update_device(struct device *dev)
1409{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001410 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 int i;
1412
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001413 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1416 || !data->valid) {
1417
1418 if (update_vbat) {
1419 /* Cleared after each update, so reenable. Value
1420 returned by this read will be previous value */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001421 it87_write_value(data, IT87_REG_CONFIG,
1422 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424 for (i = 0; i <= 7; i++) {
1425 data->in[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001426 it87_read_value(data, IT87_REG_VIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 data->in_min[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001428 it87_read_value(data, IT87_REG_VIN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 data->in_max[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001430 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
Jean Delvare3543a532006-08-28 14:27:25 +02001432 /* in8 (battery) has no limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 data->in[8] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001434 it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Jean Delvarec7f1f712007-09-03 17:11:46 +02001436 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02001437 /* Skip disabled fans */
1438 if (!(data->has_fan & (1 << i)))
1439 continue;
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 data->fan_min[i] =
Jean Delvarec7f1f712007-09-03 17:11:46 +02001442 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001443 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001444 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02001445 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02001446 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001447 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001448 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001449 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02001450 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453 for (i = 0; i < 3; i++) {
1454 data->temp[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001455 it87_read_value(data, IT87_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 data->temp_high[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001457 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 data->temp_low[i] =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001459 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461
Jean Delvare17d648b2006-08-28 14:23:46 +02001462 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02001463 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001464 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02001465 data->fan_div[0] = i & 0x07;
1466 data->fan_div[1] = (i >> 3) & 0x07;
1467 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001471 it87_read_value(data, IT87_REG_ALARM1) |
1472 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1473 (it87_read_value(data, IT87_REG_ALARM3) << 16);
1474 data->fan_main_ctrl = it87_read_value(data,
1475 IT87_REG_FAN_MAIN_CTRL);
1476 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001478 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Andrew Paprocki04751692008-08-06 22:41:06 +02001479 /* The 8705 does not have VID capability.
1480 The 8718 does not use IT87_REG_VID for the same purpose. */
Jean Delvare17d648b2006-08-28 14:23:46 +02001481 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001482 data->vid = it87_read_value(data, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02001483 /* The older IT8712F revisions had only 5 VID pins,
1484 but we assume it is always safe to read 6 bits. */
1485 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487 data->last_updated = jiffies;
1488 data->valid = 1;
1489 }
1490
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001491 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 return data;
1494}
1495
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001496static int __init it87_device_add(unsigned short address,
1497 const struct it87_sio_data *sio_data)
1498{
1499 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001500 .start = address + IT87_EC_OFFSET,
1501 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001502 .name = DRVNAME,
1503 .flags = IORESOURCE_IO,
1504 };
1505 int err;
1506
1507 pdev = platform_device_alloc(DRVNAME, address);
1508 if (!pdev) {
1509 err = -ENOMEM;
1510 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1511 goto exit;
1512 }
1513
1514 err = platform_device_add_resources(pdev, &res, 1);
1515 if (err) {
1516 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1517 "(%d)\n", err);
1518 goto exit_device_put;
1519 }
1520
1521 err = platform_device_add_data(pdev, sio_data,
1522 sizeof(struct it87_sio_data));
1523 if (err) {
1524 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1525 goto exit_device_put;
1526 }
1527
1528 err = platform_device_add(pdev);
1529 if (err) {
1530 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1531 err);
1532 goto exit_device_put;
1533 }
1534
1535 return 0;
1536
1537exit_device_put:
1538 platform_device_put(pdev);
1539exit:
1540 return err;
1541}
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543static int __init sm_it87_init(void)
1544{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001545 int err;
1546 unsigned short isa_address=0;
1547 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02001548
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001549 err = it87_find(&isa_address, &sio_data);
1550 if (err)
1551 return err;
1552 err = platform_driver_register(&it87_driver);
1553 if (err)
1554 return err;
1555
1556 err = it87_device_add(isa_address, &sio_data);
1557 if (err){
1558 platform_driver_unregister(&it87_driver);
1559 return err;
1560 }
1561
1562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565static void __exit sm_it87_exit(void)
1566{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001567 platform_device_unregister(pdev);
1568 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571
Jean Delvaref1d8e332007-11-25 16:14:44 +01001572MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02001573 "Jean Delvare <khali@linux-fr.org>");
Rudolf Marek08a8f6e2007-06-09 10:11:16 -04001574MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8726F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575module_param(update_vbat, bool, 0);
1576MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1577module_param(fix_pwm_polarity, bool, 0);
1578MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1579MODULE_LICENSE("GPL");
1580
1581module_init(sm_it87_init);
1582module_exit(sm_it87_exit);