blob: 0b204e4cf51c5303836c9254d64f808fd8364115 [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
Jean Delvare44c1bcd2010-10-28 20:31:51 +020018 * IT8721F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010019 * IT8726F Super I/O chip w/LPC interface
Jean Delvare16b5dda2012-01-16 22:51:48 +010020 * IT8728F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020021 * IT8758E Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010022 * Sis950 A clone of the IT8705F
23 *
24 * Copyright (C) 2001 Chris Gauthron
25 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
26 *
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
31 *
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
40 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Joe Perchesa8ca1032011-01-12 21:55:10 +010042#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/module.h>
45#include <linux/init.h>
46#include <linux/slab.h>
47#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020048#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040049#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020050#include <linux/hwmon-sysfs.h>
51#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040052#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010053#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020054#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020055#include <linux/string.h>
56#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010057#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020058#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
corentin.labbeb74f3fd2007-06-13 20:27:36 +020060#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Jean Delvare16b5dda2012-01-16 22:51:48 +010062enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jean Delvare67b671b2007-12-06 23:13:42 +010064static unsigned short force_id;
65module_param(force_id, ushort, 0);
66MODULE_PARM_DESC(force_id, "Override the detected device ID");
67
corentin.labbeb74f3fd2007-06-13 20:27:36 +020068static struct platform_device *pdev;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define REG 0x2e /* The register to read/write */
71#define DEV 0x07 /* Register: Logical device select */
72#define VAL 0x2f /* The value to read/write */
73#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010074
75/* The device with the IT8718F/IT8720F VID value in it */
76#define GPIO 0x07
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define DEVID 0x20 /* Register: Device ID */
79#define DEVREV 0x22 /* Register: Device Revision */
80
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020081static inline int superio_inb(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 outb(reg, REG);
84 return inb(VAL);
85}
86
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020087static inline void superio_outb(int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +020088{
89 outb(reg, REG);
90 outb(val, VAL);
91}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int superio_inw(int reg)
94{
95 int val;
96 outb(reg++, REG);
97 val = inb(VAL) << 8;
98 outb(reg, REG);
99 val |= inb(VAL);
100 return val;
101}
102
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200103static inline void superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200106 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200109static inline int superio_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200111 /*
112 * Try to reserve REG and REG + 1 for exclusive access.
113 */
114 if (!request_muxed_region(REG, 2, DRVNAME))
115 return -EBUSY;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 outb(0x87, REG);
118 outb(0x01, REG);
119 outb(0x55, REG);
120 outb(0x55, REG);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200124static inline void superio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 outb(0x02, REG);
127 outb(0x02, VAL);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200128 release_region(REG, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Jean Delvare87673dd2006-08-28 14:37:19 +0200131/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#define IT8712F_DEVID 0x8712
133#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200134#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200135#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100136#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200137#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400138#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100139#define IT8728F_DEVID 0x8728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#define IT87_ACT_REG 0x30
141#define IT87_BASE_REG 0x60
142
Jean Delvare87673dd2006-08-28 14:37:19 +0200143/* Logical device 7 registers (IT8712F and later) */
Jean Delvare895ff262009-12-09 20:35:47 +0100144#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100145#define IT87_SIO_GPIO5_REG 0x29
Jean Delvare87673dd2006-08-28 14:37:19 +0200146#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
147#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100148#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030151static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030154static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* Many IT87 constants specified below */
157
158/* Length of ISA address segment */
159#define IT87_EXTENT 8
160
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500161/* Length of ISA address segment for Environmental Controller */
162#define IT87_EC_EXTENT 2
163
164/* Offset of EC registers from ISA base address */
165#define IT87_EC_OFFSET 5
166
167/* Where are the ISA address/data registers relative to the EC base address */
168#define IT87_ADDR_REG_OFFSET 0
169#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/*----- The IT87 registers -----*/
172
173#define IT87_REG_CONFIG 0x00
174
175#define IT87_REG_ALARM1 0x01
176#define IT87_REG_ALARM2 0x02
177#define IT87_REG_ALARM3 0x03
178
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800179/*
180 * The IT8718F and IT8720F have the VID value in a different register, in
181 * Super-I/O configuration space.
182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800184/*
185 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
186 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
187 * mode.
188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200190#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
193
Jean Delvarec7f1f712007-09-03 17:11:46 +0200194static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
195static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
196static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
197static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#define IT87_REG_FAN_MAIN_CTRL 0x13
199#define IT87_REG_FAN_CTL 0x14
200#define IT87_REG_PWM(nr) (0x15 + (nr))
Jean Delvare6229cdb2010-12-08 16:27:22 +0100201#define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203#define IT87_REG_VIN(nr) (0x20 + (nr))
204#define IT87_REG_TEMP(nr) (0x29 + (nr))
205
206#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
207#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
208#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
209#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#define IT87_REG_VIN_ENABLE 0x50
212#define IT87_REG_TEMP_ENABLE 0x51
Jean Delvared9b327c2010-03-05 22:17:17 +0100213#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215#define IT87_REG_CHIPID 0x58
216
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100217#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
218#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200221struct it87_sio_data {
222 enum chips type;
223 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200224 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200225 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100226 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200227 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100228 /* Features skipped based on config or DMI */
Jean Delvare895ff262009-12-09 20:35:47 +0100229 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100230 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200231 u8 skip_pwm;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200232};
233
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800234/*
235 * For each registered chip, we need to keep some data in memory.
236 * The structure is dynamically allocated.
237 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700239 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 enum chips type;
Andrew Paprocki04751692008-08-06 22:41:06 +0200241 u8 revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200243 unsigned short addr;
244 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100245 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 char valid; /* !=0 if following fields are valid */
247 unsigned long last_updated; /* In jiffies */
248
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200249 u16 in_scaled; /* Internal voltage sensors are scaled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200251 u8 in_max[8]; /* Register value */
252 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200253 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvarec7f1f712007-09-03 17:11:46 +0200254 u16 fan[5]; /* Register values, possibly combined */
255 u16 fan_min[5]; /* Register values, possibly combined */
Jean Delvaree267d252009-03-12 13:36:39 +0100256 s8 temp[3]; /* Register value */
257 s8 temp_high[3]; /* Register value */
258 s8 temp_low[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 u8 sensor; /* Register value */
260 u8 fan_div[3]; /* Register encoding, shifted right */
261 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100262 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100264 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100266 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100267
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800268 /*
269 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100270 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
271 * 7, and we want to preserve settings on mode changes, so we have
272 * to track all values separately.
273 * Starting with the IT8721F, the manual PWM duty cycles are stored
274 * in separate registers (8-bit values), so the separate tracking
275 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800276 * simple.
277 */
Jean Delvareb99883d2010-03-05 22:17:15 +0100278 u8 pwm_ctrl[3]; /* Register value */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100279 u8 pwm_duty[3]; /* Manual PWM value set by user */
Jean Delvareb99883d2010-03-05 22:17:15 +0100280 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100281
282 /* Automatic fan speed control registers */
283 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
284 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
Jean Delvare16b5dda2012-01-16 22:51:48 +0100287static inline int has_12mv_adc(const struct it87_data *data)
288{
289 /*
290 * IT8721F and later have a 12 mV ADC, also with internal scaling
291 * on selected inputs.
292 */
293 return data->type == it8721
294 || data->type == it8728;
295}
296
297static inline int has_newer_autopwm(const struct it87_data *data)
298{
299 /*
300 * IT8721F and later have separate registers for the temperature
301 * mapping and the manual duty cycle.
302 */
303 return data->type == it8721
304 || data->type == it8728;
305}
306
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200307static u8 in_to_reg(const struct it87_data *data, int nr, long val)
308{
309 long lsb;
310
Jean Delvare16b5dda2012-01-16 22:51:48 +0100311 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200312 if (data->in_scaled & (1 << nr))
313 lsb = 24;
314 else
315 lsb = 12;
316 } else
317 lsb = 16;
318
319 val = DIV_ROUND_CLOSEST(val, lsb);
320 return SENSORS_LIMIT(val, 0, 255);
321}
322
323static int in_from_reg(const struct it87_data *data, int nr, int val)
324{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100325 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200326 if (data->in_scaled & (1 << nr))
327 return val * 24;
328 else
329 return val * 12;
330 } else
331 return val * 16;
332}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200333
334static inline u8 FAN_TO_REG(long rpm, int div)
335{
336 if (rpm == 0)
337 return 255;
338 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
339 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
340 254);
341}
342
343static inline u16 FAN16_TO_REG(long rpm)
344{
345 if (rpm == 0)
346 return 0xffff;
347 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
348}
349
350#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
351 1350000 / ((val) * (div)))
352/* The divider is fixed to 2 in 16-bit mode */
353#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
354 1350000 / ((val) * 2))
355
356#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
357 ((val) + 500) / 1000), -128, 127))
358#define TEMP_FROM_REG(val) ((val) * 1000)
359
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200360static u8 pwm_to_reg(const struct it87_data *data, long val)
361{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100362 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200363 return val;
364 else
365 return val >> 1;
366}
367
368static int pwm_from_reg(const struct it87_data *data, u8 reg)
369{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100370 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200371 return reg;
372 else
373 return (reg & 0x7f) << 1;
374}
375
Jean Delvare0df6454d2010-10-28 20:31:51 +0200376
377static int DIV_TO_REG(int val)
378{
379 int answer = 0;
380 while (answer < 7 && (val >>= 1))
381 answer++;
382 return answer;
383}
384#define DIV_FROM_REG(val) (1 << (val))
385
386static const unsigned int pwm_freq[8] = {
387 48000000 / 128,
388 24000000 / 128,
389 12000000 / 128,
390 8000000 / 128,
391 6000000 / 128,
392 3000000 / 128,
393 1500000 / 128,
394 750000 / 128,
395};
396
Andrew Paprocki04751692008-08-06 22:41:06 +0200397static inline int has_16bit_fans(const struct it87_data *data)
398{
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800399 /*
400 * IT8705F Datasheet 0.4.1, 3h == Version G.
401 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
402 * These are the first revisions with 16-bit tachometer support.
403 */
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200404 return (data->type == it87 && data->revision >= 0x03)
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200405 || (data->type == it8712 && data->revision >= 0x08)
Andrew Paprocki04751692008-08-06 22:41:06 +0200406 || data->type == it8716
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100407 || data->type == it8718
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200408 || data->type == it8720
Jean Delvare16b5dda2012-01-16 22:51:48 +0100409 || data->type == it8721
410 || data->type == it8728;
Andrew Paprocki04751692008-08-06 22:41:06 +0200411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100413static inline int has_old_autopwm(const struct it87_data *data)
414{
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800415 /*
416 * The old automatic fan speed control interface is implemented
417 * by IT8705F chips up to revision F and IT8712F chips up to
418 * revision G.
419 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100420 return (data->type == it87 && data->revision < 0x03)
421 || (data->type == it8712 && data->revision < 0x08);
422}
423
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200424static int it87_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200425static int __devexit it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200427static int it87_read_value(struct it87_data *data, u8 reg);
428static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200430static int it87_check_pwm(struct device *dev);
431static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200434static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100435 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200436 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200437 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100438 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200439 .probe = it87_probe,
440 .remove = __devexit_p(it87_remove),
Jean Delvarefde09502005-07-19 23:51:07 +0200441};
442
Jean Delvare20ad93d2005-06-05 11:53:25 +0200443static ssize_t show_in(struct device *dev, struct device_attribute *attr,
444 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200446 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
447 int nr = sensor_attr->index;
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200450 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Jean Delvare20ad93d2005-06-05 11:53:25 +0200453static ssize_t show_in_min(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);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200460 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Jean Delvare20ad93d2005-06-05 11:53:25 +0200463static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
464 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200466 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
467 int nr = sensor_attr->index;
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200470 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Jean Delvare20ad93d2005-06-05 11:53:25 +0200473static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
474 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200476 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
477 int nr = sensor_attr->index;
478
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200479 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100480 unsigned long val;
481
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100482 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100483 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100485 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200486 data->in_min[nr] = in_to_reg(data, nr, val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200487 it87_write_value(data, IT87_REG_VIN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100489 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return count;
491}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200492static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
493 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200495 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
496 int nr = sensor_attr->index;
497
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200498 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100499 unsigned long val;
500
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100501 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100504 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200505 data->in_max[nr] = in_to_reg(data, nr, val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200506 it87_write_value(data, IT87_REG_VIN_MAX(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100508 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return count;
510}
511
512#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200513static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
514 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200517static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
518 show_in_min, set_in_min, offset); \
519static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
520 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522show_in_offset(0);
523limit_in_offset(0);
524show_in_offset(1);
525limit_in_offset(1);
526show_in_offset(2);
527limit_in_offset(2);
528show_in_offset(3);
529limit_in_offset(3);
530show_in_offset(4);
531limit_in_offset(4);
532show_in_offset(5);
533limit_in_offset(5);
534show_in_offset(6);
535limit_in_offset(6);
536show_in_offset(7);
537limit_in_offset(7);
538show_in_offset(8);
539
540/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200541static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
542 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200544 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
545 int nr = sensor_attr->index;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct it87_data *data = it87_update_device(dev);
548 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
549}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200550static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
551 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200553 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
554 int nr = sensor_attr->index;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct it87_data *data = it87_update_device(dev);
557 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
558}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200559static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
560 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200562 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
563 int nr = sensor_attr->index;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct it87_data *data = it87_update_device(dev);
566 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
567}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200568static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
569 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200571 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
572 int nr = sensor_attr->index;
573
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200574 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100575 long val;
576
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100577 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100578 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100580 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 data->temp_high[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200582 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100583 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return count;
585}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200586static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
587 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200589 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
590 int nr = sensor_attr->index;
591
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200592 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100593 long val;
594
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100595 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100596 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100598 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 data->temp_low[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200600 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100601 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return count;
603}
604#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200605static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
606 show_temp, NULL, offset - 1); \
607static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
608 show_temp_max, set_temp_max, offset - 1); \
609static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
610 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612show_temp_offset(1);
613show_temp_offset(2);
614show_temp_offset(3);
615
Jean Delvare20ad93d2005-06-05 11:53:25 +0200616static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
617 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200619 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
620 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800622 u8 reg = data->sensor; /* In case value is updated while used */
Jean Delvare5f2dc792010-03-05 22:17:18 +0100623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (reg & (1 << nr))
625 return sprintf(buf, "3\n"); /* thermal diode */
626 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200627 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return sprintf(buf, "0\n"); /* disabled */
629}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200630static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
631 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200633 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
634 int nr = sensor_attr->index;
635
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200636 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100637 long val;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200638 u8 reg;
Jean Delvaref5f64502010-03-05 22:17:19 +0100639
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100640 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100641 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Jean Delvare8acf07c2010-04-14 16:14:09 +0200643 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
644 reg &= ~(1 << nr);
645 reg &= ~(8 << nr);
Jean Delvare4ed10772008-10-17 17:51:16 +0200646 if (val == 2) { /* backwards compatibility */
647 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
648 "instead\n");
649 val = 4;
650 }
651 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200653 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200654 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200655 reg |= 8 << nr;
656 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200658
659 mutex_lock(&data->update_lock);
660 data->sensor = reg;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200661 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200662 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100663 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return count;
665}
666#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200667static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
668 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670show_sensor_offset(1);
671show_sensor_offset(2);
672show_sensor_offset(3);
673
674/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100675
676static int pwm_mode(const struct it87_data *data, int nr)
677{
678 int ctrl = data->fan_main_ctrl & (1 << nr);
679
680 if (ctrl == 0) /* Full speed */
681 return 0;
682 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
683 return 2;
684 else /* Manual mode */
685 return 1;
686}
687
Jean Delvare20ad93d2005-06-05 11:53:25 +0200688static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
689 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200691 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
692 int nr = sensor_attr->index;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100695 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 DIV_FROM_REG(data->fan_div[nr])));
697}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200698static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
699 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200701 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
702 int nr = sensor_attr->index;
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100705 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
706 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200708static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
709 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200711 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
712 int nr = sensor_attr->index;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct it87_data *data = it87_update_device(dev);
715 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
716}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100717static ssize_t show_pwm_enable(struct device *dev,
718 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200720 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
721 int nr = sensor_attr->index;
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100724 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200726static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
727 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200729 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
730 int nr = sensor_attr->index;
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200733 return sprintf(buf, "%d\n",
734 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100736static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
737 char *buf)
738{
739 struct it87_data *data = it87_update_device(dev);
740 int index = (data->fan_ctl >> 4) & 0x07;
741
742 return sprintf(buf, "%u\n", pwm_freq[index]);
743}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200744static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
745 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200747 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
748 int nr = sensor_attr->index;
749
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200750 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100751 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100752 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100754 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100755 return -EINVAL;
756
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100757 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200758 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800759 switch (nr) {
Jean Delvare5f2dc792010-03-05 22:17:18 +0100760 case 0:
761 data->fan_div[nr] = reg & 0x07;
762 break;
763 case 1:
764 data->fan_div[nr] = (reg >> 3) & 0x07;
765 break;
766 case 2:
767 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
768 break;
Jean Delvare07eab462005-11-23 15:44:31 -0800769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200772 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100773 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return count;
775}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200776static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
777 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200779 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
780 int nr = sensor_attr->index;
781
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200782 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100783 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200784 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 u8 old;
786
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100787 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100788 return -EINVAL;
789
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100790 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200791 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200793 /* Save fan min limit */
794 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 switch (nr) {
797 case 0:
798 case 1:
799 data->fan_div[nr] = DIV_TO_REG(val);
800 break;
801 case 2:
802 if (val < 8)
803 data->fan_div[nr] = 1;
804 else
805 data->fan_div[nr] = 3;
806 }
807 val = old & 0x80;
808 val |= (data->fan_div[0] & 0x07);
809 val |= (data->fan_div[1] & 0x07) << 3;
810 if (data->fan_div[2] == 3)
811 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200812 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200814 /* Restore fan min limit */
815 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200816 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200817
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100818 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return count;
820}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100821
822/* Returns 0 if OK, -EINVAL otherwise */
823static int check_trip_points(struct device *dev, int nr)
824{
825 const struct it87_data *data = dev_get_drvdata(dev);
826 int i, err = 0;
827
828 if (has_old_autopwm(data)) {
829 for (i = 0; i < 3; i++) {
830 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
831 err = -EINVAL;
832 }
833 for (i = 0; i < 2; i++) {
834 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
835 err = -EINVAL;
836 }
837 }
838
839 if (err) {
840 dev_err(dev, "Inconsistent trip points, not switching to "
841 "automatic mode\n");
842 dev_err(dev, "Adjust the trip points and try again\n");
843 }
844 return err;
845}
846
Jean Delvare20ad93d2005-06-05 11:53:25 +0200847static ssize_t set_pwm_enable(struct device *dev,
848 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200850 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
851 int nr = sensor_attr->index;
852
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200853 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100854 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100856 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100857 return -EINVAL;
858
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100859 /* Check trip points before switching to automatic mode */
860 if (val == 2) {
861 if (check_trip_points(dev, nr) < 0)
862 return -EINVAL;
863 }
864
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100865 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 if (val == 0) {
868 int tmp;
869 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200870 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
871 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /* set on/off mode */
873 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100874 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
875 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100876 } else {
877 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +0100878 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +0100879 data->pwm_temp_map[nr] :
880 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +0100881 else /* Automatic mode */
882 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
883 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* set SmartGuardian mode */
885 data->fan_main_ctrl |= (1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100886 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
887 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100890 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return count;
892}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200893static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
894 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200896 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
897 int nr = sensor_attr->index;
898
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200899 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100900 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100902 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return -EINVAL;
904
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100905 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +0100906 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800907 /*
908 * If we are in automatic mode, the PWM duty cycle register
909 * is read-only so we can't write the value.
910 */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100911 if (data->pwm_ctrl[nr] & 0x80) {
912 mutex_unlock(&data->update_lock);
913 return -EBUSY;
914 }
915 data->pwm_duty[nr] = pwm_to_reg(data, val);
916 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
917 data->pwm_duty[nr]);
918 } else {
919 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800920 /*
921 * If we are in manual mode, write the duty cycle immediately;
922 * otherwise, just store it for later use.
923 */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100924 if (!(data->pwm_ctrl[nr] & 0x80)) {
925 data->pwm_ctrl[nr] = data->pwm_duty[nr];
926 it87_write_value(data, IT87_REG_PWM(nr),
927 data->pwm_ctrl[nr]);
928 }
Jean Delvareb99883d2010-03-05 22:17:15 +0100929 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100930 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return count;
932}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100933static ssize_t set_pwm_freq(struct device *dev,
934 struct device_attribute *attr, const char *buf, size_t count)
935{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200936 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100937 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100938 int i;
939
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100940 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100941 return -EINVAL;
942
Jean Delvaref8d0c192007-02-14 21:15:02 +0100943 /* Search for the nearest available frequency */
944 for (i = 0; i < 7; i++) {
945 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
946 break;
947 }
948
949 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200950 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100951 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200952 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100953 mutex_unlock(&data->update_lock);
954
955 return count;
956}
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100957static ssize_t show_pwm_temp_map(struct device *dev,
958 struct device_attribute *attr, char *buf)
959{
960 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
961 int nr = sensor_attr->index;
962
963 struct it87_data *data = it87_update_device(dev);
964 int map;
965
966 if (data->pwm_temp_map[nr] < 3)
967 map = 1 << data->pwm_temp_map[nr];
968 else
969 map = 0; /* Should never happen */
970 return sprintf(buf, "%d\n", map);
971}
972static ssize_t set_pwm_temp_map(struct device *dev,
973 struct device_attribute *attr, const char *buf, size_t count)
974{
975 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
976 int nr = sensor_attr->index;
977
978 struct it87_data *data = dev_get_drvdata(dev);
979 long val;
980 u8 reg;
981
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800982 /*
983 * This check can go away if we ever support automatic fan speed
984 * control on newer chips.
985 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100986 if (!has_old_autopwm(data)) {
987 dev_notice(dev, "Mapping change disabled for safety reasons\n");
988 return -EINVAL;
989 }
990
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100991 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100992 return -EINVAL;
993
994 switch (val) {
995 case (1 << 0):
996 reg = 0x00;
997 break;
998 case (1 << 1):
999 reg = 0x01;
1000 break;
1001 case (1 << 2):
1002 reg = 0x02;
1003 break;
1004 default:
1005 return -EINVAL;
1006 }
1007
1008 mutex_lock(&data->update_lock);
1009 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001010 /*
1011 * If we are in automatic mode, write the temp mapping immediately;
1012 * otherwise, just store it for later use.
1013 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001014 if (data->pwm_ctrl[nr] & 0x80) {
1015 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1016 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1017 }
1018 mutex_unlock(&data->update_lock);
1019 return count;
1020}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001022static ssize_t show_auto_pwm(struct device *dev,
1023 struct device_attribute *attr, char *buf)
1024{
1025 struct it87_data *data = it87_update_device(dev);
1026 struct sensor_device_attribute_2 *sensor_attr =
1027 to_sensor_dev_attr_2(attr);
1028 int nr = sensor_attr->nr;
1029 int point = sensor_attr->index;
1030
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001031 return sprintf(buf, "%d\n",
1032 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001033}
1034
1035static ssize_t set_auto_pwm(struct device *dev,
1036 struct device_attribute *attr, const char *buf, size_t count)
1037{
1038 struct it87_data *data = dev_get_drvdata(dev);
1039 struct sensor_device_attribute_2 *sensor_attr =
1040 to_sensor_dev_attr_2(attr);
1041 int nr = sensor_attr->nr;
1042 int point = sensor_attr->index;
1043 long val;
1044
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001045 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001046 return -EINVAL;
1047
1048 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001049 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001050 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1051 data->auto_pwm[nr][point]);
1052 mutex_unlock(&data->update_lock);
1053 return count;
1054}
1055
1056static ssize_t show_auto_temp(struct device *dev,
1057 struct device_attribute *attr, char *buf)
1058{
1059 struct it87_data *data = it87_update_device(dev);
1060 struct sensor_device_attribute_2 *sensor_attr =
1061 to_sensor_dev_attr_2(attr);
1062 int nr = sensor_attr->nr;
1063 int point = sensor_attr->index;
1064
1065 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1066}
1067
1068static ssize_t set_auto_temp(struct device *dev,
1069 struct device_attribute *attr, const char *buf, size_t count)
1070{
1071 struct it87_data *data = dev_get_drvdata(dev);
1072 struct sensor_device_attribute_2 *sensor_attr =
1073 to_sensor_dev_attr_2(attr);
1074 int nr = sensor_attr->nr;
1075 int point = sensor_attr->index;
1076 long val;
1077
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001078 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001079 return -EINVAL;
1080
1081 mutex_lock(&data->update_lock);
1082 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1083 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1084 data->auto_temp[nr][point]);
1085 mutex_unlock(&data->update_lock);
1086 return count;
1087}
1088
Jean Delvare20ad93d2005-06-05 11:53:25 +02001089#define show_fan_offset(offset) \
1090static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1091 show_fan, NULL, offset - 1); \
1092static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1093 show_fan_min, set_fan_min, offset - 1); \
1094static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1095 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097show_fan_offset(1);
1098show_fan_offset(2);
1099show_fan_offset(3);
1100
1101#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +02001102static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1103 show_pwm_enable, set_pwm_enable, offset - 1); \
1104static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +01001105 show_pwm, set_pwm, offset - 1); \
1106static DEVICE_ATTR(pwm##offset##_freq, \
1107 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001108 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1109static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001110 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1111 offset - 1); \
1112static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1113 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1114 offset - 1, 0); \
1115static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1116 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1117 offset - 1, 1); \
1118static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1119 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1120 offset - 1, 2); \
1121static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1122 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1123static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1124 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1125 offset - 1, 1); \
1126static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1127 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1128 offset - 1, 0); \
1129static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1130 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1131 offset - 1, 2); \
1132static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1133 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1134 offset - 1, 3); \
1135static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1136 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1137 offset - 1, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139show_pwm_offset(1);
1140show_pwm_offset(2);
1141show_pwm_offset(3);
1142
Jean Delvare17d648b2006-08-28 14:23:46 +02001143/* A different set of callbacks for 16-bit fans */
1144static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1145 char *buf)
1146{
1147 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1148 int nr = sensor_attr->index;
1149 struct it87_data *data = it87_update_device(dev);
1150 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1151}
1152
1153static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1154 char *buf)
1155{
1156 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1157 int nr = sensor_attr->index;
1158 struct it87_data *data = it87_update_device(dev);
1159 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1160}
1161
1162static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1163 const char *buf, size_t count)
1164{
1165 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1166 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001167 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001168 long val;
1169
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001170 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001171 return -EINVAL;
Jean Delvare17d648b2006-08-28 14:23:46 +02001172
1173 mutex_lock(&data->update_lock);
1174 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001175 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001176 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001177 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001178 data->fan_min[nr] >> 8);
1179 mutex_unlock(&data->update_lock);
1180 return count;
1181}
1182
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001183/*
1184 * We want to use the same sysfs file names as 8-bit fans, but we need
1185 * different variable names, so we have to use SENSOR_ATTR instead of
1186 * SENSOR_DEVICE_ATTR.
1187 */
Jean Delvare17d648b2006-08-28 14:23:46 +02001188#define show_fan16_offset(offset) \
1189static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1190 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1191 show_fan16, NULL, offset - 1); \
1192static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1193 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1194 show_fan16_min, set_fan16_min, offset - 1)
1195
1196show_fan16_offset(1);
1197show_fan16_offset(2);
1198show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001199show_fan16_offset(4);
1200show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +02001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001203static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1204 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001207 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
Jean Delvare1d66c642005-04-18 21:16:59 -07001209static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Jean Delvare0124dd72007-11-25 16:16:41 +01001211static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1212 char *buf)
1213{
1214 int bitnr = to_sensor_dev_attr(attr)->index;
1215 struct it87_data *data = it87_update_device(dev);
1216 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1217}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001218
1219static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1220 *attr, const char *buf, size_t count)
1221{
1222 struct it87_data *data = dev_get_drvdata(dev);
1223 long val;
1224 int config;
1225
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001226 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001227 return -EINVAL;
1228
1229 mutex_lock(&data->update_lock);
1230 config = it87_read_value(data, IT87_REG_CONFIG);
1231 if (config < 0) {
1232 count = config;
1233 } else {
1234 config |= 1 << 5;
1235 it87_write_value(data, IT87_REG_CONFIG, config);
1236 /* Invalidate cache to force re-read */
1237 data->valid = 0;
1238 }
1239 mutex_unlock(&data->update_lock);
1240
1241 return count;
1242}
1243
Jean Delvare0124dd72007-11-25 16:16:41 +01001244static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1245static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1246static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1247static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1248static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1249static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1250static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1251static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1252static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1253static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1254static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1255static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1256static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1257static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1258static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1259static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001260static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1261 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001262
Jean Delvared9b327c2010-03-05 22:17:17 +01001263static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1264 char *buf)
1265{
1266 int bitnr = to_sensor_dev_attr(attr)->index;
1267 struct it87_data *data = it87_update_device(dev);
1268 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1269}
1270static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1271 const char *buf, size_t count)
1272{
1273 int bitnr = to_sensor_dev_attr(attr)->index;
1274 struct it87_data *data = dev_get_drvdata(dev);
1275 long val;
1276
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001277 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001278 || (val != 0 && val != 1))
1279 return -EINVAL;
1280
1281 mutex_lock(&data->update_lock);
1282 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1283 if (val)
1284 data->beeps |= (1 << bitnr);
1285 else
1286 data->beeps &= ~(1 << bitnr);
1287 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1288 mutex_unlock(&data->update_lock);
1289 return count;
1290}
1291
1292static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1293 show_beep, set_beep, 1);
1294static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1295static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1296static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1297static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1298static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1299static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1300static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1301/* fanX_beep writability is set later */
1302static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1303static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1304static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1305static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1306static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1307static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1308 show_beep, set_beep, 2);
1309static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1310static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1311
Jean Delvare5f2dc792010-03-05 22:17:18 +01001312static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1313 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Jean Delvare90d66192007-10-08 18:24:35 +02001315 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001316 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001318static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1319 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001321 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001322 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001324 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001325 return -EINVAL;
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 data->vrm = val;
1328
1329 return count;
1330}
1331static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Jean Delvare5f2dc792010-03-05 22:17:18 +01001333static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1334 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 struct it87_data *data = it87_update_device(dev);
1337 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1338}
1339static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001340
Jean Delvare738e5e02010-08-14 21:08:50 +02001341static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1342 char *buf)
1343{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001344 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001345 "+5V",
1346 "5VSB",
1347 "Vbat",
1348 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001349 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001350 "+3.3V",
1351 "3VSB",
1352 "Vbat",
1353 };
1354 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001355 int nr = to_sensor_dev_attr(attr)->index;
1356
Jean Delvare16b5dda2012-01-16 22:51:48 +01001357 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1358 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001359}
1360static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1361static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1362static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1363
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001364static ssize_t show_name(struct device *dev, struct device_attribute
1365 *devattr, char *buf)
1366{
1367 struct it87_data *data = dev_get_drvdata(dev);
1368 return sprintf(buf, "%s\n", data->name);
1369}
1370static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1371
Jean Delvare87808be2006-09-24 21:17:13 +02001372static struct attribute *it87_attributes[] = {
1373 &sensor_dev_attr_in0_input.dev_attr.attr,
1374 &sensor_dev_attr_in1_input.dev_attr.attr,
1375 &sensor_dev_attr_in2_input.dev_attr.attr,
1376 &sensor_dev_attr_in3_input.dev_attr.attr,
1377 &sensor_dev_attr_in4_input.dev_attr.attr,
1378 &sensor_dev_attr_in5_input.dev_attr.attr,
1379 &sensor_dev_attr_in6_input.dev_attr.attr,
1380 &sensor_dev_attr_in7_input.dev_attr.attr,
1381 &sensor_dev_attr_in8_input.dev_attr.attr,
1382 &sensor_dev_attr_in0_min.dev_attr.attr,
1383 &sensor_dev_attr_in1_min.dev_attr.attr,
1384 &sensor_dev_attr_in2_min.dev_attr.attr,
1385 &sensor_dev_attr_in3_min.dev_attr.attr,
1386 &sensor_dev_attr_in4_min.dev_attr.attr,
1387 &sensor_dev_attr_in5_min.dev_attr.attr,
1388 &sensor_dev_attr_in6_min.dev_attr.attr,
1389 &sensor_dev_attr_in7_min.dev_attr.attr,
1390 &sensor_dev_attr_in0_max.dev_attr.attr,
1391 &sensor_dev_attr_in1_max.dev_attr.attr,
1392 &sensor_dev_attr_in2_max.dev_attr.attr,
1393 &sensor_dev_attr_in3_max.dev_attr.attr,
1394 &sensor_dev_attr_in4_max.dev_attr.attr,
1395 &sensor_dev_attr_in5_max.dev_attr.attr,
1396 &sensor_dev_attr_in6_max.dev_attr.attr,
1397 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001398 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1399 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1400 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1401 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1402 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1403 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1404 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1405 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001406
1407 &sensor_dev_attr_temp1_input.dev_attr.attr,
1408 &sensor_dev_attr_temp2_input.dev_attr.attr,
1409 &sensor_dev_attr_temp3_input.dev_attr.attr,
1410 &sensor_dev_attr_temp1_max.dev_attr.attr,
1411 &sensor_dev_attr_temp2_max.dev_attr.attr,
1412 &sensor_dev_attr_temp3_max.dev_attr.attr,
1413 &sensor_dev_attr_temp1_min.dev_attr.attr,
1414 &sensor_dev_attr_temp2_min.dev_attr.attr,
1415 &sensor_dev_attr_temp3_min.dev_attr.attr,
1416 &sensor_dev_attr_temp1_type.dev_attr.attr,
1417 &sensor_dev_attr_temp2_type.dev_attr.attr,
1418 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001419 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1420 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1421 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001422
1423 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001424 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001425 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001426 NULL
1427};
1428
1429static const struct attribute_group it87_group = {
1430 .attrs = it87_attributes,
1431};
1432
Jean Delvared9b327c2010-03-05 22:17:17 +01001433static struct attribute *it87_attributes_beep[] = {
1434 &sensor_dev_attr_in0_beep.dev_attr.attr,
1435 &sensor_dev_attr_in1_beep.dev_attr.attr,
1436 &sensor_dev_attr_in2_beep.dev_attr.attr,
1437 &sensor_dev_attr_in3_beep.dev_attr.attr,
1438 &sensor_dev_attr_in4_beep.dev_attr.attr,
1439 &sensor_dev_attr_in5_beep.dev_attr.attr,
1440 &sensor_dev_attr_in6_beep.dev_attr.attr,
1441 &sensor_dev_attr_in7_beep.dev_attr.attr,
1442
1443 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1444 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1445 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1446 NULL
1447};
1448
1449static const struct attribute_group it87_group_beep = {
1450 .attrs = it87_attributes_beep,
1451};
1452
Jean Delvare723a0aa2010-03-05 22:17:16 +01001453static struct attribute *it87_attributes_fan16[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001454 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1455 &sensor_dev_attr_fan1_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001456 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1457 NULL
1458}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001459 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1460 &sensor_dev_attr_fan2_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001461 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1462 NULL
1463}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001464 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1465 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001466 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1467 NULL
1468}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001469 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1470 &sensor_dev_attr_fan4_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001471 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1472 NULL
1473}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001474 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1475 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001476 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1477 NULL
1478} };
Jean Delvare87808be2006-09-24 21:17:13 +02001479
Jean Delvare723a0aa2010-03-05 22:17:16 +01001480static const struct attribute_group it87_group_fan16[5] = {
1481 { .attrs = it87_attributes_fan16[0] },
1482 { .attrs = it87_attributes_fan16[1] },
1483 { .attrs = it87_attributes_fan16[2] },
1484 { .attrs = it87_attributes_fan16[3] },
1485 { .attrs = it87_attributes_fan16[4] },
1486};
1487
1488static struct attribute *it87_attributes_fan[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001489 &sensor_dev_attr_fan1_input.dev_attr.attr,
1490 &sensor_dev_attr_fan1_min.dev_attr.attr,
1491 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001492 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1493 NULL
1494}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001495 &sensor_dev_attr_fan2_input.dev_attr.attr,
1496 &sensor_dev_attr_fan2_min.dev_attr.attr,
1497 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001498 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1499 NULL
1500}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001501 &sensor_dev_attr_fan3_input.dev_attr.attr,
1502 &sensor_dev_attr_fan3_min.dev_attr.attr,
1503 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001504 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001505 NULL
1506} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001507
Jean Delvare723a0aa2010-03-05 22:17:16 +01001508static const struct attribute_group it87_group_fan[3] = {
1509 { .attrs = it87_attributes_fan[0] },
1510 { .attrs = it87_attributes_fan[1] },
1511 { .attrs = it87_attributes_fan[2] },
1512};
1513
1514static const struct attribute_group *
1515it87_get_fan_group(const struct it87_data *data)
1516{
1517 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1518}
1519
1520static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001521 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001522 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001523 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001524 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001525 NULL
1526}, {
1527 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1528 &sensor_dev_attr_pwm2.dev_attr.attr,
1529 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001530 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001531 NULL
1532}, {
1533 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1534 &sensor_dev_attr_pwm3.dev_attr.attr,
1535 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001536 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001537 NULL
1538} };
Jean Delvare87808be2006-09-24 21:17:13 +02001539
Jean Delvare723a0aa2010-03-05 22:17:16 +01001540static const struct attribute_group it87_group_pwm[3] = {
1541 { .attrs = it87_attributes_pwm[0] },
1542 { .attrs = it87_attributes_pwm[1] },
1543 { .attrs = it87_attributes_pwm[2] },
1544};
1545
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001546static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1547 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1548 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1549 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1550 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1551 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1552 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1553 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1554 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1555 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1556 NULL
1557}, {
1558 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1559 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1560 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1561 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1562 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1563 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1564 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1565 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1566 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1567 NULL
1568}, {
1569 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1570 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1571 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1572 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1573 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1574 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1575 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1576 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1577 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1578 NULL
1579} };
1580
1581static const struct attribute_group it87_group_autopwm[3] = {
1582 { .attrs = it87_attributes_autopwm[0] },
1583 { .attrs = it87_attributes_autopwm[1] },
1584 { .attrs = it87_attributes_autopwm[2] },
1585};
1586
Jean Delvared9b327c2010-03-05 22:17:17 +01001587static struct attribute *it87_attributes_fan_beep[] = {
1588 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1589 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1590 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1591 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1592 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1593};
1594
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001595static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001596 &dev_attr_vrm.attr,
1597 &dev_attr_cpu0_vid.attr,
1598 NULL
1599};
1600
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001601static const struct attribute_group it87_group_vid = {
1602 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001603};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Jean Delvare738e5e02010-08-14 21:08:50 +02001605static struct attribute *it87_attributes_label[] = {
1606 &sensor_dev_attr_in3_label.dev_attr.attr,
1607 &sensor_dev_attr_in7_label.dev_attr.attr,
1608 &sensor_dev_attr_in8_label.dev_attr.attr,
1609 NULL
1610};
1611
1612static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001613 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001614};
1615
Jean Delvare2d8672c2005-07-19 23:56:35 +02001616/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001617static int __init it87_find(unsigned short *address,
1618 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001620 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001621 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001622 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001624 err = superio_enter();
1625 if (err)
1626 return err;
1627
1628 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001629 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001630
1631 switch (chip_type) {
1632 case IT8705F_DEVID:
1633 sio_data->type = it87;
1634 break;
1635 case IT8712F_DEVID:
1636 sio_data->type = it8712;
1637 break;
1638 case IT8716F_DEVID:
1639 case IT8726F_DEVID:
1640 sio_data->type = it8716;
1641 break;
1642 case IT8718F_DEVID:
1643 sio_data->type = it8718;
1644 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001645 case IT8720F_DEVID:
1646 sio_data->type = it8720;
1647 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001648 case IT8721F_DEVID:
1649 sio_data->type = it8721;
1650 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001651 case IT8728F_DEVID:
1652 sio_data->type = it8728;
1653 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001654 case 0xffff: /* No device at all */
1655 goto exit;
1656 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001657 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001658 goto exit;
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Jean Delvare87673dd2006-08-28 14:37:19 +02001661 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001663 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 goto exit;
1665 }
1666
1667 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1668 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001669 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 goto exit;
1671 }
1672
1673 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001674 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Joe Perchesa8ca1032011-01-12 21:55:10 +01001675 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001676 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Jean Delvare738e5e02010-08-14 21:08:50 +02001678 /* in8 (Vbat) is always internal */
1679 sio_data->internal = (1 << 2);
1680
Jean Delvare87673dd2006-08-28 14:37:19 +02001681 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001682 if (sio_data->type == it87) {
1683 /* The IT8705F doesn't have VID pins at all */
1684 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001685
1686 /* The IT8705F has a different LD number for GPIO */
1687 superio_select(5);
1688 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01001689 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001690 int reg;
1691
1692 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001693
Jean Delvare895ff262009-12-09 20:35:47 +01001694 reg = superio_inb(IT87_SIO_GPIO3_REG);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001695 if (sio_data->type == it8721 || sio_data->type == it8728) {
1696 /*
1697 * The IT8721F/IT8758E doesn't have VID pins at all,
1698 * not sure about the IT8728F.
1699 */
Jean Delvare895ff262009-12-09 20:35:47 +01001700 sio_data->skip_vid = 1;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001701 } else {
1702 /* We need at least 4 VID pins */
1703 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001704 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001705 sio_data->skip_vid = 1;
1706 }
Jean Delvare895ff262009-12-09 20:35:47 +01001707 }
1708
Jean Delvare591ec652009-12-09 20:35:48 +01001709 /* Check if fan3 is there or not */
1710 if (reg & (1 << 6))
1711 sio_data->skip_pwm |= (1 << 2);
1712 if (reg & (1 << 7))
1713 sio_data->skip_fan |= (1 << 2);
1714
1715 /* Check if fan2 is there or not */
1716 reg = superio_inb(IT87_SIO_GPIO5_REG);
1717 if (reg & (1 << 1))
1718 sio_data->skip_pwm |= (1 << 1);
1719 if (reg & (1 << 2))
1720 sio_data->skip_fan |= (1 << 1);
1721
Jean Delvare895ff262009-12-09 20:35:47 +01001722 if ((sio_data->type == it8718 || sio_data->type == it8720)
1723 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001724 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001725
1726 reg = superio_inb(IT87_SIO_PINX2_REG);
Jean Delvare436cad22010-07-09 16:22:48 +02001727 /*
1728 * The IT8720F has no VIN7 pin, so VCCH should always be
1729 * routed internally to VIN7 with an internal divider.
1730 * Curiously, there still is a configuration bit to control
1731 * this, which means it can be set incorrectly. And even
1732 * more curiously, many boards out there are improperly
1733 * configured, even though the IT8720F datasheet claims
1734 * that the internal routing of VCCH to VIN7 is the default
1735 * setting. So we force the internal routing in this case.
1736 */
1737 if (sio_data->type == it8720 && !(reg & (1 << 1))) {
1738 reg |= (1 << 1);
1739 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01001740 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02001741 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001742 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001743 sio_data->internal |= (1 << 0);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001744 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1745 sio_data->type == it8728)
Jean Delvare738e5e02010-08-14 21:08:50 +02001746 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01001747
1748 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02001749 }
Jean Delvared9b327c2010-03-05 22:17:17 +01001750 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01001751 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02001752
Jean Delvare98dd22c2008-10-09 15:33:58 +02001753 /* Disable specific features based on DMI strings */
1754 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1755 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1756 if (board_vendor && board_name) {
1757 if (strcmp(board_vendor, "nVIDIA") == 0
1758 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001759 /*
1760 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1761 * connected to a fan, but to something else. One user
1762 * has reported instant system power-off when changing
1763 * the PWM2 duty cycle, so we disable it.
1764 * I use the board name string as the trigger in case
1765 * the same board is ever used in other systems.
1766 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01001767 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02001768 sio_data->skip_pwm = (1 << 1);
1769 }
1770 }
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772exit:
1773 superio_exit();
1774 return err;
1775}
1776
Jean Delvare723a0aa2010-03-05 22:17:16 +01001777static void it87_remove_files(struct device *dev)
1778{
1779 struct it87_data *data = platform_get_drvdata(pdev);
1780 struct it87_sio_data *sio_data = dev->platform_data;
1781 const struct attribute_group *fan_group = it87_get_fan_group(data);
1782 int i;
1783
1784 sysfs_remove_group(&dev->kobj, &it87_group);
Jean Delvared9b327c2010-03-05 22:17:17 +01001785 if (sio_data->beep_pin)
1786 sysfs_remove_group(&dev->kobj, &it87_group_beep);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001787 for (i = 0; i < 5; i++) {
1788 if (!(data->has_fan & (1 << i)))
1789 continue;
1790 sysfs_remove_group(&dev->kobj, &fan_group[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01001791 if (sio_data->beep_pin)
1792 sysfs_remove_file(&dev->kobj,
1793 it87_attributes_fan_beep[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001794 }
1795 for (i = 0; i < 3; i++) {
1796 if (sio_data->skip_pwm & (1 << 0))
1797 continue;
1798 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001799 if (has_old_autopwm(data))
1800 sysfs_remove_group(&dev->kobj,
1801 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001802 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001803 if (!sio_data->skip_vid)
1804 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02001805 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001806}
1807
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001808static int __devinit it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001811 struct resource *res;
1812 struct device *dev = &pdev->dev;
1813 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001814 const struct attribute_group *fan_group;
1815 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01001817 int fan_beep_need_rw;
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001818 static const char * const names[] = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001819 "it87",
1820 "it8712",
1821 "it8716",
1822 "it8718",
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001823 "it8720",
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001824 "it8721",
Jean Delvare16b5dda2012-01-16 22:51:48 +01001825 "it8728",
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001826 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001828 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001829 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001830 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1831 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001832 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001833 err = -EBUSY;
1834 goto ERROR0;
1835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Jean Delvare5f2dc792010-03-05 22:17:18 +01001837 data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
1838 if (!data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 err = -ENOMEM;
1840 goto ERROR1;
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001843 data->addr = res->start;
1844 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001845 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001846 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001849 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1850 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001851 err = -ENODEV;
1852 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
1854
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001855 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001857 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001860 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001862 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001863 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001864 if (sio_data->internal & (1 << 0))
1865 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1866 if (sio_data->internal & (1 << 1))
1867 data->in_scaled |= (1 << 7); /* in7 is VSB */
1868 if (sio_data->internal & (1 << 2))
1869 data->in_scaled |= (1 << 8); /* in8 is Vbat */
1870 }
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001873 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001876 err = sysfs_create_group(&dev->kobj, &it87_group);
1877 if (err)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001878 goto ERROR2;
Jean Delvare17d648b2006-08-28 14:23:46 +02001879
Jean Delvared9b327c2010-03-05 22:17:17 +01001880 if (sio_data->beep_pin) {
1881 err = sysfs_create_group(&dev->kobj, &it87_group_beep);
1882 if (err)
1883 goto ERROR4;
1884 }
1885
Jean Delvare9060f8b2006-08-28 14:24:17 +02001886 /* Do not create fan files for disabled fans */
Jean Delvare723a0aa2010-03-05 22:17:16 +01001887 fan_group = it87_get_fan_group(data);
Jean Delvared9b327c2010-03-05 22:17:17 +01001888 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001889 for (i = 0; i < 5; i++) {
1890 if (!(data->has_fan & (1 << i)))
1891 continue;
1892 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
1893 if (err)
1894 goto ERROR4;
Jean Delvared9b327c2010-03-05 22:17:17 +01001895
1896 if (sio_data->beep_pin) {
1897 err = sysfs_create_file(&dev->kobj,
1898 it87_attributes_fan_beep[i]);
1899 if (err)
1900 goto ERROR4;
1901 if (!fan_beep_need_rw)
1902 continue;
1903
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001904 /*
1905 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01001906 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001907 * for it.
1908 */
Jean Delvared9b327c2010-03-05 22:17:17 +01001909 if (sysfs_chmod_file(&dev->kobj,
1910 it87_attributes_fan_beep[i],
1911 S_IRUGO | S_IWUSR))
1912 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
1913 i + 1);
1914 fan_beep_need_rw = 0;
1915 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001916 }
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001919 for (i = 0; i < 3; i++) {
1920 if (sio_data->skip_pwm & (1 << i))
1921 continue;
1922 err = sysfs_create_group(&dev->kobj,
1923 &it87_group_pwm[i]);
1924 if (err)
Jean Delvare98dd22c2008-10-09 15:33:58 +02001925 goto ERROR4;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001926
1927 if (!has_old_autopwm(data))
1928 continue;
1929 err = sysfs_create_group(&dev->kobj,
1930 &it87_group_autopwm[i]);
1931 if (err)
1932 goto ERROR4;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
1935
Jean Delvare895ff262009-12-09 20:35:47 +01001936 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02001937 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001938 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001939 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001940 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
1941 if (err)
Jean Delvare87808be2006-09-24 21:17:13 +02001942 goto ERROR4;
1943 }
1944
Jean Delvare738e5e02010-08-14 21:08:50 +02001945 /* Export labels for internal sensors */
1946 for (i = 0; i < 3; i++) {
1947 if (!(sio_data->internal & (1 << i)))
1948 continue;
1949 err = sysfs_create_file(&dev->kobj,
1950 it87_attributes_label[i]);
1951 if (err)
1952 goto ERROR4;
1953 }
1954
Tony Jones1beeffe2007-08-20 13:46:20 -07001955 data->hwmon_dev = hwmon_device_register(dev);
1956 if (IS_ERR(data->hwmon_dev)) {
1957 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001958 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960
1961 return 0;
1962
Jean Delvare87808be2006-09-24 21:17:13 +02001963ERROR4:
Jean Delvare723a0aa2010-03-05 22:17:16 +01001964 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965ERROR2:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001966 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 kfree(data);
1968ERROR1:
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001969 release_region(res->start, IT87_EC_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970ERROR0:
1971 return err;
1972}
1973
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001974static int __devexit it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001976 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Tony Jones1beeffe2007-08-20 13:46:20 -07001978 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001979 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001980
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001981 release_region(data->addr, IT87_EC_EXTENT);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001982 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001983 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 return 0;
1986}
1987
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001988/*
1989 * Must be called with data->update_lock held, except during initialization.
1990 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1991 * would slow down the IT87 access and should not be necessary.
1992 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001993static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001995 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1996 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001999/*
2000 * Must be called with data->update_lock held, except during initialization.
2001 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2002 * would slow down the IT87 access and should not be necessary.
2003 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002004static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002006 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2007 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
2010/* Return 1 if and only if the PWM interface is safe to use */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002011static int __devinit it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002013 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002014 /*
2015 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002017 * disable pwm control to protect the user.
2018 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002019 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if ((tmp & 0x87) == 0) {
2021 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002022 /*
2023 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002025 * inverting all fan speed values.
2026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 int i;
2028 u8 pwm[3];
2029
2030 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002031 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 IT87_REG_PWM(i));
2033
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002034 /*
2035 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 * might be correct, as suspicious as it seems, so we
2037 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002038 * PWM interface).
2039 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002041 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002043 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 tmp | 0x87);
2045 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002046 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 IT87_REG_PWM(i),
2048 0x7f & ~pwm[i]);
2049 return 1;
2050 }
2051
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002052 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 "too broken to be fixed\n");
2054 }
2055
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002056 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 "defaults, disabling PWM interface\n");
2058 return 0;
2059 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002060 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 "sane, won't touch\n");
2062 }
2063
2064 return 1;
2065}
2066
2067/* Called when we have found a new IT87. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002068static void __devinit it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Jean Delvare591ec652009-12-09 20:35:48 +01002070 struct it87_sio_data *sio_data = pdev->dev.platform_data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002071 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002073 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002075 /*
2076 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002077 * - If it is in automatic mode, setting to manual mode should set
2078 * the fan to full speed by default.
2079 * - If it is in manual mode, we need a mapping to temperature
2080 * channels to use when later setting to automatic mode later.
2081 * Use a 1:1 mapping by default (we are clueless.)
2082 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002083 * prior to switching to a different mode.
2084 * Note that this is no longer needed for the IT8721F and later, as
2085 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002086 * manual duty cycle.
2087 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002089 data->pwm_temp_map[i] = i;
2090 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002091 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002094 /*
2095 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002096 * registers. For low voltage limits it makes no sense and triggers
2097 * alarms, so change to 0 instead. For high temperature limits, it
2098 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002099 * but is still confusing, so change to 127 degrees C.
2100 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002101 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002102 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002103 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002104 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002105 }
2106 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002107 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002108 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002109 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002110 }
2111
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002112 /*
2113 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002114 * set to two different sensor types and we can't guess which one
2115 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002116 * run-time through the temp{1-3}_type sysfs accessors if needed.
2117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002120 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if ((tmp & 0xff) == 0) {
2122 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002123 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125
2126 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002127 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002128 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002129 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002131 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002132 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2133 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002135 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Jean Delvare17d648b2006-08-28 14:23:46 +02002137 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02002138 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002139 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002140 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002141 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002142 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002143 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002144 tmp | 0x07);
2145 }
Andrew Paprocki816d8c62008-08-06 22:41:06 +02002146 /* IT8705F only supports three fans. */
2147 if (data->type != it87) {
2148 if (tmp & (1 << 4))
2149 data->has_fan |= (1 << 3); /* fan4 enabled */
2150 if (tmp & (1 << 5))
2151 data->has_fan |= (1 << 4); /* fan5 enabled */
2152 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002153 }
2154
Jean Delvare591ec652009-12-09 20:35:48 +01002155 /* Fan input pins may be used for alternative functions */
2156 data->has_fan &= ~sio_data->skip_fan;
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002159 it87_write_value(data, IT87_REG_CONFIG,
2160 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 | (update_vbat ? 0x41 : 0x01));
2162}
2163
Jean Delvareb99883d2010-03-05 22:17:15 +01002164static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2165{
2166 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002167 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002168 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002169 data->pwm_duty[nr] = it87_read_value(data,
2170 IT87_REG_PWM_DUTY(nr));
2171 } else {
2172 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2173 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2174 else /* Manual mode */
2175 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2176 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002177
2178 if (has_old_autopwm(data)) {
2179 int i;
2180
2181 for (i = 0; i < 5 ; i++)
2182 data->auto_temp[nr][i] = it87_read_value(data,
2183 IT87_REG_AUTO_TEMP(nr, i));
2184 for (i = 0; i < 3 ; i++)
2185 data->auto_pwm[nr][i] = it87_read_value(data,
2186 IT87_REG_AUTO_PWM(nr, i));
2187 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002188}
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190static struct it87_data *it87_update_device(struct device *dev)
2191{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002192 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 int i;
2194
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002195 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2198 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002200 /*
2201 * Cleared after each update, so reenable. Value
2202 * returned by this read will be previous value
2203 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002204 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002205 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
2207 for (i = 0; i <= 7; i++) {
2208 data->in[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002209 it87_read_value(data, IT87_REG_VIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 data->in_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002211 it87_read_value(data, IT87_REG_VIN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 data->in_max[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002213 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
Jean Delvare3543a532006-08-28 14:27:25 +02002215 /* in8 (battery) has no limit registers */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002216 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Jean Delvarec7f1f712007-09-03 17:11:46 +02002218 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002219 /* Skip disabled fans */
2220 if (!(data->has_fan & (1 << i)))
2221 continue;
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 data->fan_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002224 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002225 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002226 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002227 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002228 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002229 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002230 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002231 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002232 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
2235 for (i = 0; i < 3; i++) {
2236 data->temp[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002237 it87_read_value(data, IT87_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 data->temp_high[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002239 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 data->temp_low[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002241 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
2243
Jean Delvare17d648b2006-08-28 14:23:46 +02002244 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002245 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002246 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002247 data->fan_div[0] = i & 0x07;
2248 data->fan_div[1] = (i >> 3) & 0x07;
2249 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002253 it87_read_value(data, IT87_REG_ALARM1) |
2254 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2255 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002256 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002257
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002258 data->fan_main_ctrl = it87_read_value(data,
2259 IT87_REG_FAN_MAIN_CTRL);
2260 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002261 for (i = 0; i < 3; i++)
2262 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002264 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002265 /*
2266 * The IT8705F does not have VID capability.
2267 * The IT8718F and later don't use IT87_REG_VID for the
2268 * same purpose.
2269 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002270 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002271 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002272 /*
2273 * The older IT8712F revisions had only 5 VID pins,
2274 * but we assume it is always safe to read 6 bits.
2275 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002276 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
2278 data->last_updated = jiffies;
2279 data->valid = 1;
2280 }
2281
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002282 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 return data;
2285}
2286
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002287static int __init it87_device_add(unsigned short address,
2288 const struct it87_sio_data *sio_data)
2289{
2290 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002291 .start = address + IT87_EC_OFFSET,
2292 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002293 .name = DRVNAME,
2294 .flags = IORESOURCE_IO,
2295 };
2296 int err;
2297
Jean Delvareb9acb642009-01-07 16:37:35 +01002298 err = acpi_check_resource_conflict(&res);
2299 if (err)
2300 goto exit;
2301
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002302 pdev = platform_device_alloc(DRVNAME, address);
2303 if (!pdev) {
2304 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002305 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002306 goto exit;
2307 }
2308
2309 err = platform_device_add_resources(pdev, &res, 1);
2310 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002311 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002312 goto exit_device_put;
2313 }
2314
2315 err = platform_device_add_data(pdev, sio_data,
2316 sizeof(struct it87_sio_data));
2317 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002318 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002319 goto exit_device_put;
2320 }
2321
2322 err = platform_device_add(pdev);
2323 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002324 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002325 goto exit_device_put;
2326 }
2327
2328 return 0;
2329
2330exit_device_put:
2331 platform_device_put(pdev);
2332exit:
2333 return err;
2334}
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336static int __init sm_it87_init(void)
2337{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002338 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002339 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002340 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002341
Jean Delvare98dd22c2008-10-09 15:33:58 +02002342 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002343 err = it87_find(&isa_address, &sio_data);
2344 if (err)
2345 return err;
2346 err = platform_driver_register(&it87_driver);
2347 if (err)
2348 return err;
2349
2350 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002351 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002352 platform_driver_unregister(&it87_driver);
2353 return err;
2354 }
2355
2356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
2359static void __exit sm_it87_exit(void)
2360{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002361 platform_device_unregister(pdev);
2362 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
2365
Jean Delvaref1d8e332007-11-25 16:14:44 +01002366MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02002367 "Jean Delvare <khali@linux-fr.org>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002368MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369module_param(update_vbat, bool, 0);
2370MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2371module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002372MODULE_PARM_DESC(fix_pwm_polarity,
2373 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374MODULE_LICENSE("GPL");
2375
2376module_init(sm_it87_init);
2377module_exit(sm_it87_exit);