blob: 7a3616ccbf05798637991c25bf326f9e1e9899e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jean Delvare5f2dc792010-03-05 22:17:18 +01002 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring.
4 *
5 * 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 *
13 * Supports: IT8705F Super I/O chip w/LPC interface
14 * IT8712F Super I/O chip w/LPC interface
15 * IT8716F Super I/O chip w/LPC interface
16 * IT8718F Super I/O chip w/LPC interface
17 * IT8720F Super I/O chip w/LPC interface
18 * IT8726F Super I/O chip w/LPC interface
19 * Sis950 A clone of the IT8705F
20 *
21 * Copyright (C) 2001 Chris Gauthron
22 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/slab.h>
42#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020043#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040044#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020045#include <linux/hwmon-sysfs.h>
46#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040047#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010048#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020049#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020050#include <linux/string.h>
51#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010052#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020053#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
corentin.labbeb74f3fd2007-06-13 20:27:36 +020055#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010057enum chips { it87, it8712, it8716, it8718, it8720 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jean Delvare67b671b2007-12-06 23:13:42 +010059static unsigned short force_id;
60module_param(force_id, ushort, 0);
61MODULE_PARM_DESC(force_id, "Override the detected device ID");
62
corentin.labbeb74f3fd2007-06-13 20:27:36 +020063static struct platform_device *pdev;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define REG 0x2e /* The register to read/write */
66#define DEV 0x07 /* Register: Logical device select */
67#define VAL 0x2f /* The value to read/write */
68#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010069
70/* The device with the IT8718F/IT8720F VID value in it */
71#define GPIO 0x07
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define DEVID 0x20 /* Register: Device ID */
74#define DEVREV 0x22 /* Register: Device Revision */
75
76static inline int
77superio_inb(int reg)
78{
79 outb(reg, REG);
80 return inb(VAL);
81}
82
Jean Delvare436cad22010-07-09 16:22:48 +020083static inline void
84superio_outb(int reg, int val)
85{
86 outb(reg, REG);
87 outb(val, VAL);
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int superio_inw(int reg)
91{
92 int val;
93 outb(reg++, REG);
94 val = inb(VAL) << 8;
95 outb(reg, REG);
96 val |= inb(VAL);
97 return val;
98}
99
100static inline void
Jean Delvare87673dd2006-08-28 14:37:19 +0200101superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200104 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107static inline void
108superio_enter(void)
109{
110 outb(0x87, REG);
111 outb(0x01, REG);
112 outb(0x55, REG);
113 outb(0x55, REG);
114}
115
116static inline void
117superio_exit(void)
118{
119 outb(0x02, REG);
120 outb(0x02, VAL);
121}
122
Jean Delvare87673dd2006-08-28 14:37:19 +0200123/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define IT8712F_DEVID 0x8712
125#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200126#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200127#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100128#define IT8720F_DEVID 0x8720
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400129#define IT8726F_DEVID 0x8726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define IT87_ACT_REG 0x30
131#define IT87_BASE_REG 0x60
132
Jean Delvare87673dd2006-08-28 14:37:19 +0200133/* Logical device 7 registers (IT8712F and later) */
Jean Delvare895ff262009-12-09 20:35:47 +0100134#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100135#define IT87_SIO_GPIO5_REG 0x29
Jean Delvare87673dd2006-08-28 14:37:19 +0200136#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
137#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100138#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/* Update battery voltage after every reading if true */
141static int update_vbat;
142
143/* Not all BIOSes properly configure the PWM registers */
144static int fix_pwm_polarity;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* Many IT87 constants specified below */
147
148/* Length of ISA address segment */
149#define IT87_EXTENT 8
150
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500151/* Length of ISA address segment for Environmental Controller */
152#define IT87_EC_EXTENT 2
153
154/* Offset of EC registers from ISA base address */
155#define IT87_EC_OFFSET 5
156
157/* Where are the ISA address/data registers relative to the EC base address */
158#define IT87_ADDR_REG_OFFSET 0
159#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/*----- The IT87 registers -----*/
162
163#define IT87_REG_CONFIG 0x00
164
165#define IT87_REG_ALARM1 0x01
166#define IT87_REG_ALARM2 0x02
167#define IT87_REG_ALARM3 0x03
168
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100169/* The IT8718F and IT8720F have the VID value in a different register, in
170 Super-I/O configuration space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#define IT87_REG_VID 0x0a
Andrew Paprocki04751692008-08-06 22:41:06 +0200172/* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
173 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
174 mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200176#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
179
Jean Delvarec7f1f712007-09-03 17:11:46 +0200180static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
181static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
182static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
183static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#define IT87_REG_FAN_MAIN_CTRL 0x13
185#define IT87_REG_FAN_CTL 0x14
186#define IT87_REG_PWM(nr) (0x15 + (nr))
187
188#define IT87_REG_VIN(nr) (0x20 + (nr))
189#define IT87_REG_TEMP(nr) (0x29 + (nr))
190
191#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
192#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
193#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
194#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196#define IT87_REG_VIN_ENABLE 0x50
197#define IT87_REG_TEMP_ENABLE 0x51
Jean Delvared9b327c2010-03-05 22:17:17 +0100198#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200#define IT87_REG_CHIPID 0x58
201
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100202#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
203#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200206struct it87_sio_data {
207 enum chips type;
208 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200209 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200210 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100211 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200212 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100213 /* Features skipped based on config or DMI */
Jean Delvare895ff262009-12-09 20:35:47 +0100214 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100215 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200216 u8 skip_pwm;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200217};
218
Jean Delvareed6bafb2007-02-14 21:15:03 +0100219/* For each registered chip, we need to keep some data in memory.
220 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700222 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 enum chips type;
Andrew Paprocki04751692008-08-06 22:41:06 +0200224 u8 revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200226 unsigned short addr;
227 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100228 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 char valid; /* !=0 if following fields are valid */
230 unsigned long last_updated; /* In jiffies */
231
232 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200233 u8 in_max[8]; /* Register value */
234 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200235 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvarec7f1f712007-09-03 17:11:46 +0200236 u16 fan[5]; /* Register values, possibly combined */
237 u16 fan_min[5]; /* Register values, possibly combined */
Jean Delvaree267d252009-03-12 13:36:39 +0100238 s8 temp[3]; /* Register value */
239 s8 temp_high[3]; /* Register value */
240 s8 temp_low[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 u8 sensor; /* Register value */
242 u8 fan_div[3]; /* Register encoding, shifted right */
243 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100244 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100246 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100248 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100249
250 /* The following 3 arrays correspond to the same registers. The
251 * meaning of bits 6-0 depends on the value of bit 7, and we want
252 * to preserve settings on mode changes, so we have to track all
253 * values separately. */
254 u8 pwm_ctrl[3]; /* Register value */
255 u8 pwm_duty[3]; /* Manual PWM value set by user (bit 6-0) */
256 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100257
258 /* Automatic fan speed control registers */
259 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
260 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
Jean Delvare0df6454d2010-10-28 20:31:51 +0200263#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8) / 16), 0, 255))
264#define IN_FROM_REG(val) ((val) * 16)
265
266static inline u8 FAN_TO_REG(long rpm, int div)
267{
268 if (rpm == 0)
269 return 255;
270 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
271 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
272 254);
273}
274
275static inline u16 FAN16_TO_REG(long rpm)
276{
277 if (rpm == 0)
278 return 0xffff;
279 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
280}
281
282#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
283 1350000 / ((val) * (div)))
284/* The divider is fixed to 2 in 16-bit mode */
285#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
286 1350000 / ((val) * 2))
287
288#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
289 ((val) + 500) / 1000), -128, 127))
290#define TEMP_FROM_REG(val) ((val) * 1000)
291
292#define PWM_TO_REG(val) ((val) >> 1)
293#define PWM_FROM_REG(val) (((val) & 0x7f) << 1)
294
295static int DIV_TO_REG(int val)
296{
297 int answer = 0;
298 while (answer < 7 && (val >>= 1))
299 answer++;
300 return answer;
301}
302#define DIV_FROM_REG(val) (1 << (val))
303
304static const unsigned int pwm_freq[8] = {
305 48000000 / 128,
306 24000000 / 128,
307 12000000 / 128,
308 8000000 / 128,
309 6000000 / 128,
310 3000000 / 128,
311 1500000 / 128,
312 750000 / 128,
313};
314
Andrew Paprocki04751692008-08-06 22:41:06 +0200315static inline int has_16bit_fans(const struct it87_data *data)
316{
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200317 /* IT8705F Datasheet 0.4.1, 3h == Version G.
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200318 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200319 These are the first revisions with 16bit tachometer support. */
320 return (data->type == it87 && data->revision >= 0x03)
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200321 || (data->type == it8712 && data->revision >= 0x08)
Andrew Paprocki04751692008-08-06 22:41:06 +0200322 || data->type == it8716
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100323 || data->type == it8718
324 || data->type == it8720;
Andrew Paprocki04751692008-08-06 22:41:06 +0200325}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100327static inline int has_old_autopwm(const struct it87_data *data)
328{
329 /* The old automatic fan speed control interface is implemented
330 by IT8705F chips up to revision F and IT8712F chips up to
331 revision G. */
332 return (data->type == it87 && data->revision < 0x03)
333 || (data->type == it8712 && data->revision < 0x08);
334}
335
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200336static int it87_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200337static int __devexit it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200339static int it87_read_value(struct it87_data *data, u8 reg);
340static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200342static int it87_check_pwm(struct device *dev);
343static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200346static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100347 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200348 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200349 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100350 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200351 .probe = it87_probe,
352 .remove = __devexit_p(it87_remove),
Jean Delvarefde09502005-07-19 23:51:07 +0200353};
354
Jean Delvare20ad93d2005-06-05 11:53:25 +0200355static ssize_t show_in(struct device *dev, struct device_attribute *attr,
356 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200358 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
359 int nr = sensor_attr->index;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct it87_data *data = it87_update_device(dev);
362 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
363}
364
Jean Delvare20ad93d2005-06-05 11:53:25 +0200365static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
366 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200368 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
369 int nr = sensor_attr->index;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct it87_data *data = it87_update_device(dev);
372 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
373}
374
Jean Delvare20ad93d2005-06-05 11:53:25 +0200375static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
376 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200378 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
379 int nr = sensor_attr->index;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct it87_data *data = it87_update_device(dev);
382 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
383}
384
Jean Delvare20ad93d2005-06-05 11:53:25 +0200385static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
386 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200388 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
389 int nr = sensor_attr->index;
390
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200391 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100392 unsigned long val;
393
394 if (strict_strtoul(buf, 10, &val) < 0)
395 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100397 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 data->in_min[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200399 it87_write_value(data, IT87_REG_VIN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100401 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return count;
403}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200404static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
405 const char *buf, size_t count)
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
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200410 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100411 unsigned long val;
412
413 if (strict_strtoul(buf, 10, &val) < 0)
414 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100416 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 data->in_max[nr] = IN_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200418 it87_write_value(data, IT87_REG_VIN_MAX(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100420 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return count;
422}
423
424#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200425static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
426 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200429static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
430 show_in_min, set_in_min, offset); \
431static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
432 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434show_in_offset(0);
435limit_in_offset(0);
436show_in_offset(1);
437limit_in_offset(1);
438show_in_offset(2);
439limit_in_offset(2);
440show_in_offset(3);
441limit_in_offset(3);
442show_in_offset(4);
443limit_in_offset(4);
444show_in_offset(5);
445limit_in_offset(5);
446show_in_offset(6);
447limit_in_offset(6);
448show_in_offset(7);
449limit_in_offset(7);
450show_in_offset(8);
451
452/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200453static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
454 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200456 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
457 int nr = sensor_attr->index;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct it87_data *data = it87_update_device(dev);
460 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
461}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200462static ssize_t show_temp_max(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 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
470}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200471static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
472 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200474 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
475 int nr = sensor_attr->index;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 struct it87_data *data = it87_update_device(dev);
478 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
479}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200480static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
481 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200483 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
484 int nr = sensor_attr->index;
485
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200486 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100487 long val;
488
489 if (strict_strtol(buf, 10, &val) < 0)
490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100492 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 data->temp_high[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200494 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100495 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return count;
497}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200498static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
499 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200501 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
502 int nr = sensor_attr->index;
503
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200504 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100505 long val;
506
507 if (strict_strtol(buf, 10, &val) < 0)
508 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100510 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 data->temp_low[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200512 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100513 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return count;
515}
516#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200517static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
518 show_temp, NULL, offset - 1); \
519static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
520 show_temp_max, set_temp_max, offset - 1); \
521static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
522 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524show_temp_offset(1);
525show_temp_offset(2);
526show_temp_offset(3);
527
Jean Delvare20ad93d2005-06-05 11:53:25 +0200528static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
529 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200531 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
532 int nr = sensor_attr->index;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100535 u8 reg = data->sensor; /* In case the value is updated while
536 we use it */
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (reg & (1 << nr))
539 return sprintf(buf, "3\n"); /* thermal diode */
540 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200541 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return sprintf(buf, "0\n"); /* disabled */
543}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200544static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
545 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200547 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
548 int nr = sensor_attr->index;
549
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200550 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100551 long val;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200552 u8 reg;
Jean Delvaref5f64502010-03-05 22:17:19 +0100553
554 if (strict_strtol(buf, 10, &val) < 0)
555 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Jean Delvare8acf07c2010-04-14 16:14:09 +0200557 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
558 reg &= ~(1 << nr);
559 reg &= ~(8 << nr);
Jean Delvare4ed10772008-10-17 17:51:16 +0200560 if (val == 2) { /* backwards compatibility */
561 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
562 "instead\n");
563 val = 4;
564 }
565 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200567 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200568 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200569 reg |= 8 << nr;
570 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200572
573 mutex_lock(&data->update_lock);
574 data->sensor = reg;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200575 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200576 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100577 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return count;
579}
580#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200581static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
582 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584show_sensor_offset(1);
585show_sensor_offset(2);
586show_sensor_offset(3);
587
588/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100589
590static int pwm_mode(const struct it87_data *data, int nr)
591{
592 int ctrl = data->fan_main_ctrl & (1 << nr);
593
594 if (ctrl == 0) /* Full speed */
595 return 0;
596 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
597 return 2;
598 else /* Manual mode */
599 return 1;
600}
601
Jean Delvare20ad93d2005-06-05 11:53:25 +0200602static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
603 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200605 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
606 int nr = sensor_attr->index;
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100609 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 DIV_FROM_REG(data->fan_div[nr])));
611}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200612static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
613 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200615 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
616 int nr = sensor_attr->index;
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100619 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
620 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200622static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
623 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200625 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
626 int nr = sensor_attr->index;
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct it87_data *data = it87_update_device(dev);
629 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
630}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100631static ssize_t show_pwm_enable(struct device *dev,
632 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200634 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
635 int nr = sensor_attr->index;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100638 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200640static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
641 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200643 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
644 int nr = sensor_attr->index;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100647 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100649static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
650 char *buf)
651{
652 struct it87_data *data = it87_update_device(dev);
653 int index = (data->fan_ctl >> 4) & 0x07;
654
655 return sprintf(buf, "%u\n", pwm_freq[index]);
656}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200657static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
658 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200660 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
661 int nr = sensor_attr->index;
662
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200663 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100664 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100665 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Jean Delvaref5f64502010-03-05 22:17:19 +0100667 if (strict_strtol(buf, 10, &val) < 0)
668 return -EINVAL;
669
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100670 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200671 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800672 switch (nr) {
Jean Delvare5f2dc792010-03-05 22:17:18 +0100673 case 0:
674 data->fan_div[nr] = reg & 0x07;
675 break;
676 case 1:
677 data->fan_div[nr] = (reg >> 3) & 0x07;
678 break;
679 case 2:
680 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
681 break;
Jean Delvare07eab462005-11-23 15:44:31 -0800682 }
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200685 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100686 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return count;
688}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200689static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
690 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200692 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
693 int nr = sensor_attr->index;
694
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200695 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100696 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200697 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 u8 old;
699
Jean Delvaref5f64502010-03-05 22:17:19 +0100700 if (strict_strtoul(buf, 10, &val) < 0)
701 return -EINVAL;
702
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100703 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200704 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200706 /* Save fan min limit */
707 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 switch (nr) {
710 case 0:
711 case 1:
712 data->fan_div[nr] = DIV_TO_REG(val);
713 break;
714 case 2:
715 if (val < 8)
716 data->fan_div[nr] = 1;
717 else
718 data->fan_div[nr] = 3;
719 }
720 val = old & 0x80;
721 val |= (data->fan_div[0] & 0x07);
722 val |= (data->fan_div[1] & 0x07) << 3;
723 if (data->fan_div[2] == 3)
724 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200725 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200727 /* Restore fan min limit */
728 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200729 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200730
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100731 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return count;
733}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100734
735/* Returns 0 if OK, -EINVAL otherwise */
736static int check_trip_points(struct device *dev, int nr)
737{
738 const struct it87_data *data = dev_get_drvdata(dev);
739 int i, err = 0;
740
741 if (has_old_autopwm(data)) {
742 for (i = 0; i < 3; i++) {
743 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
744 err = -EINVAL;
745 }
746 for (i = 0; i < 2; i++) {
747 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
748 err = -EINVAL;
749 }
750 }
751
752 if (err) {
753 dev_err(dev, "Inconsistent trip points, not switching to "
754 "automatic mode\n");
755 dev_err(dev, "Adjust the trip points and try again\n");
756 }
757 return err;
758}
759
Jean Delvare20ad93d2005-06-05 11:53:25 +0200760static ssize_t set_pwm_enable(struct device *dev,
761 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200763 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
764 int nr = sensor_attr->index;
765
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200766 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100767 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Jean Delvaref5f64502010-03-05 22:17:19 +0100769 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100770 return -EINVAL;
771
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100772 /* Check trip points before switching to automatic mode */
773 if (val == 2) {
774 if (check_trip_points(dev, nr) < 0)
775 return -EINVAL;
776 }
777
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100778 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (val == 0) {
781 int tmp;
782 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200783 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
784 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* set on/off mode */
786 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100787 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
788 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100789 } else {
790 if (val == 1) /* Manual mode */
791 data->pwm_ctrl[nr] = data->pwm_duty[nr];
792 else /* Automatic mode */
793 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
794 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* set SmartGuardian mode */
796 data->fan_main_ctrl |= (1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100797 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
798 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100801 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return count;
803}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200804static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
805 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200807 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
808 int nr = sensor_attr->index;
809
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200810 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100811 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Jean Delvaref5f64502010-03-05 22:17:19 +0100813 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -EINVAL;
815
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100816 mutex_lock(&data->update_lock);
Jean Delvareb99883d2010-03-05 22:17:15 +0100817 data->pwm_duty[nr] = PWM_TO_REG(val);
818 /* If we are in manual mode, write the duty cycle immediately;
819 * otherwise, just store it for later use. */
820 if (!(data->pwm_ctrl[nr] & 0x80)) {
821 data->pwm_ctrl[nr] = data->pwm_duty[nr];
822 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
823 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100824 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return count;
826}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100827static ssize_t set_pwm_freq(struct device *dev,
828 struct device_attribute *attr, const char *buf, size_t count)
829{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200830 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100831 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100832 int i;
833
Jean Delvaref5f64502010-03-05 22:17:19 +0100834 if (strict_strtoul(buf, 10, &val) < 0)
835 return -EINVAL;
836
Jean Delvaref8d0c192007-02-14 21:15:02 +0100837 /* Search for the nearest available frequency */
838 for (i = 0; i < 7; i++) {
839 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
840 break;
841 }
842
843 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200844 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100845 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200846 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100847 mutex_unlock(&data->update_lock);
848
849 return count;
850}
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100851static ssize_t show_pwm_temp_map(struct device *dev,
852 struct device_attribute *attr, char *buf)
853{
854 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
855 int nr = sensor_attr->index;
856
857 struct it87_data *data = it87_update_device(dev);
858 int map;
859
860 if (data->pwm_temp_map[nr] < 3)
861 map = 1 << data->pwm_temp_map[nr];
862 else
863 map = 0; /* Should never happen */
864 return sprintf(buf, "%d\n", map);
865}
866static ssize_t set_pwm_temp_map(struct device *dev,
867 struct device_attribute *attr, const char *buf, size_t count)
868{
869 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
870 int nr = sensor_attr->index;
871
872 struct it87_data *data = dev_get_drvdata(dev);
873 long val;
874 u8 reg;
875
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100876 /* This check can go away if we ever support automatic fan speed
877 control on newer chips. */
878 if (!has_old_autopwm(data)) {
879 dev_notice(dev, "Mapping change disabled for safety reasons\n");
880 return -EINVAL;
881 }
882
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100883 if (strict_strtol(buf, 10, &val) < 0)
884 return -EINVAL;
885
886 switch (val) {
887 case (1 << 0):
888 reg = 0x00;
889 break;
890 case (1 << 1):
891 reg = 0x01;
892 break;
893 case (1 << 2):
894 reg = 0x02;
895 break;
896 default:
897 return -EINVAL;
898 }
899
900 mutex_lock(&data->update_lock);
901 data->pwm_temp_map[nr] = reg;
902 /* If we are in automatic mode, write the temp mapping immediately;
903 * otherwise, just store it for later use. */
904 if (data->pwm_ctrl[nr] & 0x80) {
905 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
906 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
907 }
908 mutex_unlock(&data->update_lock);
909 return count;
910}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100912static ssize_t show_auto_pwm(struct device *dev,
913 struct device_attribute *attr, char *buf)
914{
915 struct it87_data *data = it87_update_device(dev);
916 struct sensor_device_attribute_2 *sensor_attr =
917 to_sensor_dev_attr_2(attr);
918 int nr = sensor_attr->nr;
919 int point = sensor_attr->index;
920
921 return sprintf(buf, "%d\n", PWM_FROM_REG(data->auto_pwm[nr][point]));
922}
923
924static ssize_t set_auto_pwm(struct device *dev,
925 struct device_attribute *attr, const char *buf, size_t count)
926{
927 struct it87_data *data = dev_get_drvdata(dev);
928 struct sensor_device_attribute_2 *sensor_attr =
929 to_sensor_dev_attr_2(attr);
930 int nr = sensor_attr->nr;
931 int point = sensor_attr->index;
932 long val;
933
934 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
935 return -EINVAL;
936
937 mutex_lock(&data->update_lock);
938 data->auto_pwm[nr][point] = PWM_TO_REG(val);
939 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
940 data->auto_pwm[nr][point]);
941 mutex_unlock(&data->update_lock);
942 return count;
943}
944
945static ssize_t show_auto_temp(struct device *dev,
946 struct device_attribute *attr, char *buf)
947{
948 struct it87_data *data = it87_update_device(dev);
949 struct sensor_device_attribute_2 *sensor_attr =
950 to_sensor_dev_attr_2(attr);
951 int nr = sensor_attr->nr;
952 int point = sensor_attr->index;
953
954 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
955}
956
957static ssize_t set_auto_temp(struct device *dev,
958 struct device_attribute *attr, const char *buf, size_t count)
959{
960 struct it87_data *data = dev_get_drvdata(dev);
961 struct sensor_device_attribute_2 *sensor_attr =
962 to_sensor_dev_attr_2(attr);
963 int nr = sensor_attr->nr;
964 int point = sensor_attr->index;
965 long val;
966
967 if (strict_strtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
968 return -EINVAL;
969
970 mutex_lock(&data->update_lock);
971 data->auto_temp[nr][point] = TEMP_TO_REG(val);
972 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
973 data->auto_temp[nr][point]);
974 mutex_unlock(&data->update_lock);
975 return count;
976}
977
Jean Delvare20ad93d2005-06-05 11:53:25 +0200978#define show_fan_offset(offset) \
979static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
980 show_fan, NULL, offset - 1); \
981static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
982 show_fan_min, set_fan_min, offset - 1); \
983static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
984 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986show_fan_offset(1);
987show_fan_offset(2);
988show_fan_offset(3);
989
990#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200991static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
992 show_pwm_enable, set_pwm_enable, offset - 1); \
993static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +0100994 show_pwm, set_pwm, offset - 1); \
995static DEVICE_ATTR(pwm##offset##_freq, \
996 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100997 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
998static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100999 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1000 offset - 1); \
1001static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1002 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1003 offset - 1, 0); \
1004static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1005 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1006 offset - 1, 1); \
1007static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1008 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1009 offset - 1, 2); \
1010static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1011 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1012static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1013 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1014 offset - 1, 1); \
1015static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1016 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1017 offset - 1, 0); \
1018static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1019 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1020 offset - 1, 2); \
1021static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1022 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1023 offset - 1, 3); \
1024static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1025 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1026 offset - 1, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028show_pwm_offset(1);
1029show_pwm_offset(2);
1030show_pwm_offset(3);
1031
Jean Delvare17d648b2006-08-28 14:23:46 +02001032/* A different set of callbacks for 16-bit fans */
1033static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1034 char *buf)
1035{
1036 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1037 int nr = sensor_attr->index;
1038 struct it87_data *data = it87_update_device(dev);
1039 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1040}
1041
1042static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1043 char *buf)
1044{
1045 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1046 int nr = sensor_attr->index;
1047 struct it87_data *data = it87_update_device(dev);
1048 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1049}
1050
1051static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1052 const char *buf, size_t count)
1053{
1054 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1055 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001056 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001057 long val;
1058
1059 if (strict_strtol(buf, 10, &val) < 0)
1060 return -EINVAL;
Jean Delvare17d648b2006-08-28 14:23:46 +02001061
1062 mutex_lock(&data->update_lock);
1063 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001064 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001065 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001066 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001067 data->fan_min[nr] >> 8);
1068 mutex_unlock(&data->update_lock);
1069 return count;
1070}
1071
1072/* We want to use the same sysfs file names as 8-bit fans, but we need
1073 different variable names, so we have to use SENSOR_ATTR instead of
1074 SENSOR_DEVICE_ATTR. */
1075#define show_fan16_offset(offset) \
1076static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1077 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1078 show_fan16, NULL, offset - 1); \
1079static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1080 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1081 show_fan16_min, set_fan16_min, offset - 1)
1082
1083show_fan16_offset(1);
1084show_fan16_offset(2);
1085show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001086show_fan16_offset(4);
1087show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +02001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001090static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1091 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001094 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
Jean Delvare1d66c642005-04-18 21:16:59 -07001096static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Jean Delvare0124dd72007-11-25 16:16:41 +01001098static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1099 char *buf)
1100{
1101 int bitnr = to_sensor_dev_attr(attr)->index;
1102 struct it87_data *data = it87_update_device(dev);
1103 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1104}
1105static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1106static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1107static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1108static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1109static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1110static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1111static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1112static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1113static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1114static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1115static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1116static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1117static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1118static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1119static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1120static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1121
Jean Delvared9b327c2010-03-05 22:17:17 +01001122static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1123 char *buf)
1124{
1125 int bitnr = to_sensor_dev_attr(attr)->index;
1126 struct it87_data *data = it87_update_device(dev);
1127 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1128}
1129static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1130 const char *buf, size_t count)
1131{
1132 int bitnr = to_sensor_dev_attr(attr)->index;
1133 struct it87_data *data = dev_get_drvdata(dev);
1134 long val;
1135
1136 if (strict_strtol(buf, 10, &val) < 0
1137 || (val != 0 && val != 1))
1138 return -EINVAL;
1139
1140 mutex_lock(&data->update_lock);
1141 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1142 if (val)
1143 data->beeps |= (1 << bitnr);
1144 else
1145 data->beeps &= ~(1 << bitnr);
1146 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1147 mutex_unlock(&data->update_lock);
1148 return count;
1149}
1150
1151static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1152 show_beep, set_beep, 1);
1153static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1154static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1155static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1156static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1157static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1158static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1159static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1160/* fanX_beep writability is set later */
1161static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1162static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1163static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1164static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1165static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1166static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1167 show_beep, set_beep, 2);
1168static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1169static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1170
Jean Delvare5f2dc792010-03-05 22:17:18 +01001171static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1172 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Jean Delvare90d66192007-10-08 18:24:35 +02001174 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001175 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001177static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1178 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001180 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001181 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Jean Delvaref5f64502010-03-05 22:17:19 +01001183 if (strict_strtoul(buf, 10, &val) < 0)
1184 return -EINVAL;
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 data->vrm = val;
1187
1188 return count;
1189}
1190static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Jean Delvare5f2dc792010-03-05 22:17:18 +01001192static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1193 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 struct it87_data *data = it87_update_device(dev);
1196 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1197}
1198static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001199
Jean Delvare738e5e02010-08-14 21:08:50 +02001200static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1201 char *buf)
1202{
1203 static const char *labels[] = {
1204 "+5V",
1205 "5VSB",
1206 "Vbat",
1207 };
1208 int nr = to_sensor_dev_attr(attr)->index;
1209
1210 return sprintf(buf, "%s\n", labels[nr]);
1211}
1212static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1213static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1214static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1215
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001216static ssize_t show_name(struct device *dev, struct device_attribute
1217 *devattr, char *buf)
1218{
1219 struct it87_data *data = dev_get_drvdata(dev);
1220 return sprintf(buf, "%s\n", data->name);
1221}
1222static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1223
Jean Delvare87808be2006-09-24 21:17:13 +02001224static struct attribute *it87_attributes[] = {
1225 &sensor_dev_attr_in0_input.dev_attr.attr,
1226 &sensor_dev_attr_in1_input.dev_attr.attr,
1227 &sensor_dev_attr_in2_input.dev_attr.attr,
1228 &sensor_dev_attr_in3_input.dev_attr.attr,
1229 &sensor_dev_attr_in4_input.dev_attr.attr,
1230 &sensor_dev_attr_in5_input.dev_attr.attr,
1231 &sensor_dev_attr_in6_input.dev_attr.attr,
1232 &sensor_dev_attr_in7_input.dev_attr.attr,
1233 &sensor_dev_attr_in8_input.dev_attr.attr,
1234 &sensor_dev_attr_in0_min.dev_attr.attr,
1235 &sensor_dev_attr_in1_min.dev_attr.attr,
1236 &sensor_dev_attr_in2_min.dev_attr.attr,
1237 &sensor_dev_attr_in3_min.dev_attr.attr,
1238 &sensor_dev_attr_in4_min.dev_attr.attr,
1239 &sensor_dev_attr_in5_min.dev_attr.attr,
1240 &sensor_dev_attr_in6_min.dev_attr.attr,
1241 &sensor_dev_attr_in7_min.dev_attr.attr,
1242 &sensor_dev_attr_in0_max.dev_attr.attr,
1243 &sensor_dev_attr_in1_max.dev_attr.attr,
1244 &sensor_dev_attr_in2_max.dev_attr.attr,
1245 &sensor_dev_attr_in3_max.dev_attr.attr,
1246 &sensor_dev_attr_in4_max.dev_attr.attr,
1247 &sensor_dev_attr_in5_max.dev_attr.attr,
1248 &sensor_dev_attr_in6_max.dev_attr.attr,
1249 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001250 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1251 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1252 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1253 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1254 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1255 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1256 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1257 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001258
1259 &sensor_dev_attr_temp1_input.dev_attr.attr,
1260 &sensor_dev_attr_temp2_input.dev_attr.attr,
1261 &sensor_dev_attr_temp3_input.dev_attr.attr,
1262 &sensor_dev_attr_temp1_max.dev_attr.attr,
1263 &sensor_dev_attr_temp2_max.dev_attr.attr,
1264 &sensor_dev_attr_temp3_max.dev_attr.attr,
1265 &sensor_dev_attr_temp1_min.dev_attr.attr,
1266 &sensor_dev_attr_temp2_min.dev_attr.attr,
1267 &sensor_dev_attr_temp3_min.dev_attr.attr,
1268 &sensor_dev_attr_temp1_type.dev_attr.attr,
1269 &sensor_dev_attr_temp2_type.dev_attr.attr,
1270 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001271 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1272 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1273 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001274
1275 &dev_attr_alarms.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001276 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001277 NULL
1278};
1279
1280static const struct attribute_group it87_group = {
1281 .attrs = it87_attributes,
1282};
1283
Jean Delvared9b327c2010-03-05 22:17:17 +01001284static struct attribute *it87_attributes_beep[] = {
1285 &sensor_dev_attr_in0_beep.dev_attr.attr,
1286 &sensor_dev_attr_in1_beep.dev_attr.attr,
1287 &sensor_dev_attr_in2_beep.dev_attr.attr,
1288 &sensor_dev_attr_in3_beep.dev_attr.attr,
1289 &sensor_dev_attr_in4_beep.dev_attr.attr,
1290 &sensor_dev_attr_in5_beep.dev_attr.attr,
1291 &sensor_dev_attr_in6_beep.dev_attr.attr,
1292 &sensor_dev_attr_in7_beep.dev_attr.attr,
1293
1294 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1295 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1296 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1297 NULL
1298};
1299
1300static const struct attribute_group it87_group_beep = {
1301 .attrs = it87_attributes_beep,
1302};
1303
Jean Delvare723a0aa2010-03-05 22:17:16 +01001304static struct attribute *it87_attributes_fan16[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001305 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1306 &sensor_dev_attr_fan1_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001307 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1308 NULL
1309}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001310 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1311 &sensor_dev_attr_fan2_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001312 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1313 NULL
1314}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001315 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1316 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001317 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1318 NULL
1319}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001320 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1321 &sensor_dev_attr_fan4_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001322 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1323 NULL
1324}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001325 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1326 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001327 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1328 NULL
1329} };
Jean Delvare87808be2006-09-24 21:17:13 +02001330
Jean Delvare723a0aa2010-03-05 22:17:16 +01001331static const struct attribute_group it87_group_fan16[5] = {
1332 { .attrs = it87_attributes_fan16[0] },
1333 { .attrs = it87_attributes_fan16[1] },
1334 { .attrs = it87_attributes_fan16[2] },
1335 { .attrs = it87_attributes_fan16[3] },
1336 { .attrs = it87_attributes_fan16[4] },
1337};
1338
1339static struct attribute *it87_attributes_fan[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001340 &sensor_dev_attr_fan1_input.dev_attr.attr,
1341 &sensor_dev_attr_fan1_min.dev_attr.attr,
1342 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001343 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1344 NULL
1345}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001346 &sensor_dev_attr_fan2_input.dev_attr.attr,
1347 &sensor_dev_attr_fan2_min.dev_attr.attr,
1348 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001349 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1350 NULL
1351}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001352 &sensor_dev_attr_fan3_input.dev_attr.attr,
1353 &sensor_dev_attr_fan3_min.dev_attr.attr,
1354 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001355 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001356 NULL
1357} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001358
Jean Delvare723a0aa2010-03-05 22:17:16 +01001359static const struct attribute_group it87_group_fan[3] = {
1360 { .attrs = it87_attributes_fan[0] },
1361 { .attrs = it87_attributes_fan[1] },
1362 { .attrs = it87_attributes_fan[2] },
1363};
1364
1365static const struct attribute_group *
1366it87_get_fan_group(const struct it87_data *data)
1367{
1368 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1369}
1370
1371static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001372 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001373 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001374 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001375 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001376 NULL
1377}, {
1378 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1379 &sensor_dev_attr_pwm2.dev_attr.attr,
1380 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001381 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001382 NULL
1383}, {
1384 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1385 &sensor_dev_attr_pwm3.dev_attr.attr,
1386 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001387 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001388 NULL
1389} };
Jean Delvare87808be2006-09-24 21:17:13 +02001390
Jean Delvare723a0aa2010-03-05 22:17:16 +01001391static const struct attribute_group it87_group_pwm[3] = {
1392 { .attrs = it87_attributes_pwm[0] },
1393 { .attrs = it87_attributes_pwm[1] },
1394 { .attrs = it87_attributes_pwm[2] },
1395};
1396
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001397static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1398 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1399 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1400 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1401 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1402 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1403 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1404 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1405 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1406 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1407 NULL
1408}, {
1409 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1410 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1411 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1412 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1413 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1414 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1415 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1416 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1417 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1418 NULL
1419}, {
1420 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1421 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1422 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1423 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1424 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1425 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1426 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1427 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1428 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1429 NULL
1430} };
1431
1432static const struct attribute_group it87_group_autopwm[3] = {
1433 { .attrs = it87_attributes_autopwm[0] },
1434 { .attrs = it87_attributes_autopwm[1] },
1435 { .attrs = it87_attributes_autopwm[2] },
1436};
1437
Jean Delvared9b327c2010-03-05 22:17:17 +01001438static struct attribute *it87_attributes_fan_beep[] = {
1439 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1440 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1441 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1442 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1443 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1444};
1445
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001446static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001447 &dev_attr_vrm.attr,
1448 &dev_attr_cpu0_vid.attr,
1449 NULL
1450};
1451
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001452static const struct attribute_group it87_group_vid = {
1453 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001454};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Jean Delvare738e5e02010-08-14 21:08:50 +02001456static struct attribute *it87_attributes_label[] = {
1457 &sensor_dev_attr_in3_label.dev_attr.attr,
1458 &sensor_dev_attr_in7_label.dev_attr.attr,
1459 &sensor_dev_attr_in8_label.dev_attr.attr,
1460 NULL
1461};
1462
1463static const struct attribute_group it87_group_label = {
1464 .attrs = it87_attributes_vid,
1465};
1466
Jean Delvare2d8672c2005-07-19 23:56:35 +02001467/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001468static int __init it87_find(unsigned short *address,
1469 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 int err = -ENODEV;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001472 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001473 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 superio_enter();
Jean Delvare67b671b2007-12-06 23:13:42 +01001476 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001477
1478 switch (chip_type) {
1479 case IT8705F_DEVID:
1480 sio_data->type = it87;
1481 break;
1482 case IT8712F_DEVID:
1483 sio_data->type = it8712;
1484 break;
1485 case IT8716F_DEVID:
1486 case IT8726F_DEVID:
1487 sio_data->type = it8716;
1488 break;
1489 case IT8718F_DEVID:
1490 sio_data->type = it8718;
1491 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001492 case IT8720F_DEVID:
1493 sio_data->type = it8720;
1494 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001495 case 0xffff: /* No device at all */
1496 goto exit;
1497 default:
1498 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
1499 chip_type);
1500 goto exit;
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Jean Delvare87673dd2006-08-28 14:37:19 +02001503 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1505 pr_info("it87: Device not activated, skipping\n");
1506 goto exit;
1507 }
1508
1509 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1510 if (*address == 0) {
1511 pr_info("it87: Base address not set, skipping\n");
1512 goto exit;
1513 }
1514
1515 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001516 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001518 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Jean Delvare738e5e02010-08-14 21:08:50 +02001520 /* in8 (Vbat) is always internal */
1521 sio_data->internal = (1 << 2);
1522
Jean Delvare87673dd2006-08-28 14:37:19 +02001523 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001524 if (sio_data->type == it87) {
1525 /* The IT8705F doesn't have VID pins at all */
1526 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001527
1528 /* The IT8705F has a different LD number for GPIO */
1529 superio_select(5);
1530 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01001531 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001532 int reg;
1533
1534 superio_select(GPIO);
Jean Delvare895ff262009-12-09 20:35:47 +01001535 /* We need at least 4 VID pins */
1536 reg = superio_inb(IT87_SIO_GPIO3_REG);
1537 if (reg & 0x0f) {
1538 pr_info("it87: VID is disabled (pins used for GPIO)\n");
1539 sio_data->skip_vid = 1;
1540 }
1541
Jean Delvare591ec652009-12-09 20:35:48 +01001542 /* Check if fan3 is there or not */
1543 if (reg & (1 << 6))
1544 sio_data->skip_pwm |= (1 << 2);
1545 if (reg & (1 << 7))
1546 sio_data->skip_fan |= (1 << 2);
1547
1548 /* Check if fan2 is there or not */
1549 reg = superio_inb(IT87_SIO_GPIO5_REG);
1550 if (reg & (1 << 1))
1551 sio_data->skip_pwm |= (1 << 1);
1552 if (reg & (1 << 2))
1553 sio_data->skip_fan |= (1 << 1);
1554
Jean Delvare895ff262009-12-09 20:35:47 +01001555 if ((sio_data->type == it8718 || sio_data->type == it8720)
1556 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001557 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001558
1559 reg = superio_inb(IT87_SIO_PINX2_REG);
Jean Delvare436cad22010-07-09 16:22:48 +02001560 /*
1561 * The IT8720F has no VIN7 pin, so VCCH should always be
1562 * routed internally to VIN7 with an internal divider.
1563 * Curiously, there still is a configuration bit to control
1564 * this, which means it can be set incorrectly. And even
1565 * more curiously, many boards out there are improperly
1566 * configured, even though the IT8720F datasheet claims
1567 * that the internal routing of VCCH to VIN7 is the default
1568 * setting. So we force the internal routing in this case.
1569 */
1570 if (sio_data->type == it8720 && !(reg & (1 << 1))) {
1571 reg |= (1 << 1);
1572 superio_outb(IT87_SIO_PINX2_REG, reg);
1573 pr_notice("it87: Routing internal VCCH to in7\n");
1574 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001575 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001576 sio_data->internal |= (1 << 0);
Jean Delvare87673dd2006-08-28 14:37:19 +02001577 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02001578 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01001579
1580 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02001581 }
Jean Delvared9b327c2010-03-05 22:17:17 +01001582 if (sio_data->beep_pin)
1583 pr_info("it87: Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02001584
Jean Delvare98dd22c2008-10-09 15:33:58 +02001585 /* Disable specific features based on DMI strings */
1586 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1587 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1588 if (board_vendor && board_name) {
1589 if (strcmp(board_vendor, "nVIDIA") == 0
1590 && strcmp(board_name, "FN68PT") == 0) {
1591 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1592 connected to a fan, but to something else. One user
1593 has reported instant system power-off when changing
1594 the PWM2 duty cycle, so we disable it.
1595 I use the board name string as the trigger in case
1596 the same board is ever used in other systems. */
1597 pr_info("it87: Disabling pwm2 due to "
1598 "hardware constraints\n");
1599 sio_data->skip_pwm = (1 << 1);
1600 }
1601 }
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603exit:
1604 superio_exit();
1605 return err;
1606}
1607
Jean Delvare723a0aa2010-03-05 22:17:16 +01001608static void it87_remove_files(struct device *dev)
1609{
1610 struct it87_data *data = platform_get_drvdata(pdev);
1611 struct it87_sio_data *sio_data = dev->platform_data;
1612 const struct attribute_group *fan_group = it87_get_fan_group(data);
1613 int i;
1614
1615 sysfs_remove_group(&dev->kobj, &it87_group);
Jean Delvared9b327c2010-03-05 22:17:17 +01001616 if (sio_data->beep_pin)
1617 sysfs_remove_group(&dev->kobj, &it87_group_beep);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001618 for (i = 0; i < 5; i++) {
1619 if (!(data->has_fan & (1 << i)))
1620 continue;
1621 sysfs_remove_group(&dev->kobj, &fan_group[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01001622 if (sio_data->beep_pin)
1623 sysfs_remove_file(&dev->kobj,
1624 it87_attributes_fan_beep[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001625 }
1626 for (i = 0; i < 3; i++) {
1627 if (sio_data->skip_pwm & (1 << 0))
1628 continue;
1629 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001630 if (has_old_autopwm(data))
1631 sysfs_remove_group(&dev->kobj,
1632 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001633 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001634 if (!sio_data->skip_vid)
1635 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02001636 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001637}
1638
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001639static int __devinit it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001642 struct resource *res;
1643 struct device *dev = &pdev->dev;
1644 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001645 const struct attribute_group *fan_group;
1646 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01001648 int fan_beep_need_rw;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001649 static const char *names[] = {
1650 "it87",
1651 "it8712",
1652 "it8716",
1653 "it8718",
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001654 "it8720",
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001655 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001657 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001658 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001659 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1660 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001661 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001662 err = -EBUSY;
1663 goto ERROR0;
1664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Jean Delvare5f2dc792010-03-05 22:17:18 +01001666 data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
1667 if (!data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 err = -ENOMEM;
1669 goto ERROR1;
1670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001672 data->addr = res->start;
1673 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001674 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001675 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001678 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1679 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001680 err = -ENODEV;
1681 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001684 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001686 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001689 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001692 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001695 err = sysfs_create_group(&dev->kobj, &it87_group);
1696 if (err)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001697 goto ERROR2;
Jean Delvare17d648b2006-08-28 14:23:46 +02001698
Jean Delvared9b327c2010-03-05 22:17:17 +01001699 if (sio_data->beep_pin) {
1700 err = sysfs_create_group(&dev->kobj, &it87_group_beep);
1701 if (err)
1702 goto ERROR4;
1703 }
1704
Jean Delvare9060f8b2006-08-28 14:24:17 +02001705 /* Do not create fan files for disabled fans */
Jean Delvare723a0aa2010-03-05 22:17:16 +01001706 fan_group = it87_get_fan_group(data);
Jean Delvared9b327c2010-03-05 22:17:17 +01001707 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001708 for (i = 0; i < 5; i++) {
1709 if (!(data->has_fan & (1 << i)))
1710 continue;
1711 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
1712 if (err)
1713 goto ERROR4;
Jean Delvared9b327c2010-03-05 22:17:17 +01001714
1715 if (sio_data->beep_pin) {
1716 err = sysfs_create_file(&dev->kobj,
1717 it87_attributes_fan_beep[i]);
1718 if (err)
1719 goto ERROR4;
1720 if (!fan_beep_need_rw)
1721 continue;
1722
1723 /* As we have a single beep enable bit for all fans,
1724 * only the first enabled fan has a writable attribute
1725 * for it. */
1726 if (sysfs_chmod_file(&dev->kobj,
1727 it87_attributes_fan_beep[i],
1728 S_IRUGO | S_IWUSR))
1729 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
1730 i + 1);
1731 fan_beep_need_rw = 0;
1732 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001733 }
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001736 for (i = 0; i < 3; i++) {
1737 if (sio_data->skip_pwm & (1 << i))
1738 continue;
1739 err = sysfs_create_group(&dev->kobj,
1740 &it87_group_pwm[i]);
1741 if (err)
Jean Delvare98dd22c2008-10-09 15:33:58 +02001742 goto ERROR4;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001743
1744 if (!has_old_autopwm(data))
1745 continue;
1746 err = sysfs_create_group(&dev->kobj,
1747 &it87_group_autopwm[i]);
1748 if (err)
1749 goto ERROR4;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752
Jean Delvare895ff262009-12-09 20:35:47 +01001753 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02001754 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001755 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001756 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001757 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
1758 if (err)
Jean Delvare87808be2006-09-24 21:17:13 +02001759 goto ERROR4;
1760 }
1761
Jean Delvare738e5e02010-08-14 21:08:50 +02001762 /* Export labels for internal sensors */
1763 for (i = 0; i < 3; i++) {
1764 if (!(sio_data->internal & (1 << i)))
1765 continue;
1766 err = sysfs_create_file(&dev->kobj,
1767 it87_attributes_label[i]);
1768 if (err)
1769 goto ERROR4;
1770 }
1771
Tony Jones1beeffe2007-08-20 13:46:20 -07001772 data->hwmon_dev = hwmon_device_register(dev);
1773 if (IS_ERR(data->hwmon_dev)) {
1774 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001775 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777
1778 return 0;
1779
Jean Delvare87808be2006-09-24 21:17:13 +02001780ERROR4:
Jean Delvare723a0aa2010-03-05 22:17:16 +01001781 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782ERROR2:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001783 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 kfree(data);
1785ERROR1:
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001786 release_region(res->start, IT87_EC_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787ERROR0:
1788 return err;
1789}
1790
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001791static int __devexit it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001793 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Tony Jones1beeffe2007-08-20 13:46:20 -07001795 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001796 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001797
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001798 release_region(data->addr, IT87_EC_EXTENT);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001799 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001800 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 return 0;
1803}
1804
Jean Delvare7f999aa2007-02-14 21:15:03 +01001805/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1807 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001808static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001810 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1811 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
Jean Delvare7f999aa2007-02-14 21:15:03 +01001814/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1816 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001817static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001819 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1820 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
1823/* Return 1 if and only if the PWM interface is safe to use */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001824static int __devinit it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001826 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1828 * and polarity set to active low is sign that this is the case so we
1829 * disable pwm control to protect the user. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001830 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if ((tmp & 0x87) == 0) {
1832 if (fix_pwm_polarity) {
1833 /* The user asks us to attempt a chip reconfiguration.
1834 * This means switching to active high polarity and
1835 * inverting all fan speed values. */
1836 int i;
1837 u8 pwm[3];
1838
1839 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001840 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 IT87_REG_PWM(i));
1842
1843 /* If any fan is in automatic pwm mode, the polarity
1844 * might be correct, as suspicious as it seems, so we
1845 * better don't change anything (but still disable the
1846 * PWM interface). */
1847 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001848 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001850 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 tmp | 0x87);
1852 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001853 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 IT87_REG_PWM(i),
1855 0x7f & ~pwm[i]);
1856 return 1;
1857 }
1858
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001859 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 "too broken to be fixed\n");
1861 }
1862
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001863 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 "defaults, disabling PWM interface\n");
1865 return 0;
1866 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001867 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 "sane, won't touch\n");
1869 }
1870
1871 return 1;
1872}
1873
1874/* Called when we have found a new IT87. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001875static void __devinit it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Jean Delvare591ec652009-12-09 20:35:48 +01001877 struct it87_sio_data *sio_data = pdev->dev.platform_data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001878 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01001880 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Jean Delvareb99883d2010-03-05 22:17:15 +01001882 /* For each PWM channel:
1883 * - If it is in automatic mode, setting to manual mode should set
1884 * the fan to full speed by default.
1885 * - If it is in manual mode, we need a mapping to temperature
1886 * channels to use when later setting to automatic mode later.
1887 * Use a 1:1 mapping by default (we are clueless.)
1888 * In both cases, the value can (and should) be changed by the user
1889 * prior to switching to a different mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01001891 data->pwm_temp_map[i] = i;
1892 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001893 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895
Jean Delvarec5df9b72006-08-28 14:37:54 +02001896 /* Some chips seem to have default value 0xff for all limit
1897 * registers. For low voltage limits it makes no sense and triggers
1898 * alarms, so change to 0 instead. For high temperature limits, it
1899 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1900 * but is still confusing, so change to 127 degrees C. */
1901 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001902 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001903 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001904 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001905 }
1906 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001907 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02001908 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001909 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02001910 }
1911
Jean Delvarea00afb92010-04-14 16:14:09 +02001912 /* Temperature channels are not forcibly enabled, as they can be
1913 * set to two different sensor types and we can't guess which one
1914 * is correct for a given system. These channels can be enabled at
1915 * run-time through the temp{1-3}_type sysfs accessors if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001918 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if ((tmp & 0xff) == 0) {
1920 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001921 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923
1924 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01001925 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001926 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01001927 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01001929 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01001930 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1931 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02001933 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Jean Delvare17d648b2006-08-28 14:23:46 +02001935 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02001936 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001937 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02001938 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001939 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02001940 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001941 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02001942 tmp | 0x07);
1943 }
Andrew Paprocki816d8c62008-08-06 22:41:06 +02001944 /* IT8705F only supports three fans. */
1945 if (data->type != it87) {
1946 if (tmp & (1 << 4))
1947 data->has_fan |= (1 << 3); /* fan4 enabled */
1948 if (tmp & (1 << 5))
1949 data->has_fan |= (1 << 4); /* fan5 enabled */
1950 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001951 }
1952
Jean Delvare591ec652009-12-09 20:35:48 +01001953 /* Fan input pins may be used for alternative functions */
1954 data->has_fan &= ~sio_data->skip_fan;
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001957 it87_write_value(data, IT87_REG_CONFIG,
1958 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 | (update_vbat ? 0x41 : 0x01));
1960}
1961
Jean Delvareb99883d2010-03-05 22:17:15 +01001962static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
1963{
1964 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
1965 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
1966 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
1967 else /* Manual mode */
1968 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001969
1970 if (has_old_autopwm(data)) {
1971 int i;
1972
1973 for (i = 0; i < 5 ; i++)
1974 data->auto_temp[nr][i] = it87_read_value(data,
1975 IT87_REG_AUTO_TEMP(nr, i));
1976 for (i = 0; i < 3 ; i++)
1977 data->auto_pwm[nr][i] = it87_read_value(data,
1978 IT87_REG_AUTO_PWM(nr, i));
1979 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001980}
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982static struct it87_data *it87_update_device(struct device *dev)
1983{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001984 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 int i;
1986
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001987 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1990 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (update_vbat) {
1992 /* Cleared after each update, so reenable. Value
Jean Delvare5f2dc792010-03-05 22:17:18 +01001993 returned by this read will be previous value */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001994 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01001995 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 for (i = 0; i <= 7; i++) {
1998 data->in[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01001999 it87_read_value(data, IT87_REG_VIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 data->in_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002001 it87_read_value(data, IT87_REG_VIN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 data->in_max[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002003 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
Jean Delvare3543a532006-08-28 14:27:25 +02002005 /* in8 (battery) has no limit registers */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002006 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Jean Delvarec7f1f712007-09-03 17:11:46 +02002008 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002009 /* Skip disabled fans */
2010 if (!(data->has_fan & (1 << i)))
2011 continue;
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 data->fan_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002014 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002015 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002016 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002017 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002018 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002019 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002020 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002021 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002022 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
2025 for (i = 0; i < 3; i++) {
2026 data->temp[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002027 it87_read_value(data, IT87_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 data->temp_high[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002029 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 data->temp_low[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002031 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033
Jean Delvare17d648b2006-08-28 14:23:46 +02002034 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002035 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002036 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002037 data->fan_div[0] = i & 0x07;
2038 data->fan_div[1] = (i >> 3) & 0x07;
2039 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002043 it87_read_value(data, IT87_REG_ALARM1) |
2044 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2045 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002046 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002047
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002048 data->fan_main_ctrl = it87_read_value(data,
2049 IT87_REG_FAN_MAIN_CTRL);
2050 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002051 for (i = 0; i < 3; i++)
2052 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002054 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Andrew Paprocki04751692008-08-06 22:41:06 +02002055 /* The 8705 does not have VID capability.
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002056 The 8718 and the 8720 don't use IT87_REG_VID for the
2057 same purpose. */
Jean Delvare17d648b2006-08-28 14:23:46 +02002058 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002059 data->vid = it87_read_value(data, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02002060 /* The older IT8712F revisions had only 5 VID pins,
2061 but we assume it is always safe to read 6 bits. */
2062 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064 data->last_updated = jiffies;
2065 data->valid = 1;
2066 }
2067
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002068 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 return data;
2071}
2072
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002073static int __init it87_device_add(unsigned short address,
2074 const struct it87_sio_data *sio_data)
2075{
2076 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002077 .start = address + IT87_EC_OFFSET,
2078 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002079 .name = DRVNAME,
2080 .flags = IORESOURCE_IO,
2081 };
2082 int err;
2083
Jean Delvareb9acb642009-01-07 16:37:35 +01002084 err = acpi_check_resource_conflict(&res);
2085 if (err)
2086 goto exit;
2087
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002088 pdev = platform_device_alloc(DRVNAME, address);
2089 if (!pdev) {
2090 err = -ENOMEM;
2091 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
2092 goto exit;
2093 }
2094
2095 err = platform_device_add_resources(pdev, &res, 1);
2096 if (err) {
2097 printk(KERN_ERR DRVNAME ": Device resource addition failed "
2098 "(%d)\n", err);
2099 goto exit_device_put;
2100 }
2101
2102 err = platform_device_add_data(pdev, sio_data,
2103 sizeof(struct it87_sio_data));
2104 if (err) {
2105 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
2106 goto exit_device_put;
2107 }
2108
2109 err = platform_device_add(pdev);
2110 if (err) {
2111 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
2112 err);
2113 goto exit_device_put;
2114 }
2115
2116 return 0;
2117
2118exit_device_put:
2119 platform_device_put(pdev);
2120exit:
2121 return err;
2122}
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124static int __init sm_it87_init(void)
2125{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002126 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002127 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002128 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002129
Jean Delvare98dd22c2008-10-09 15:33:58 +02002130 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002131 err = it87_find(&isa_address, &sio_data);
2132 if (err)
2133 return err;
2134 err = platform_driver_register(&it87_driver);
2135 if (err)
2136 return err;
2137
2138 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002139 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002140 platform_driver_unregister(&it87_driver);
2141 return err;
2142 }
2143
2144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
2147static void __exit sm_it87_exit(void)
2148{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002149 platform_device_unregister(pdev);
2150 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
2153
Jean Delvaref1d8e332007-11-25 16:14:44 +01002154MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02002155 "Jean Delvare <khali@linux-fr.org>");
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002156MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8720F/8726F, SiS950 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157module_param(update_vbat, bool, 0);
2158MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2159module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002160MODULE_PARM_DESC(fix_pwm_polarity,
2161 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162MODULE_LICENSE("GPL");
2163
2164module_init(sm_it87_init);
2165module_exit(sm_it87_exit);