blob: 9ca10a73aee8a7cb0fe83cc8ded8a416364e440b [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 *
Rudolf Marekc145d5c2014-01-29 20:40:08 +010013 * Supports: IT8603E Super I/O chip w/LPC interface
Rudolf Marek574e9bd2014-04-04 18:01:35 +020014 * IT8623E Super I/O chip w/LPC interface
Rudolf Marekc145d5c2014-01-29 20:40:08 +010015 * IT8705F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010016 * IT8712F Super I/O chip w/LPC interface
17 * IT8716F Super I/O chip w/LPC interface
18 * IT8718F Super I/O chip w/LPC interface
19 * IT8720F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020020 * IT8721F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010021 * IT8726F Super I/O chip w/LPC interface
Jean Delvare16b5dda2012-01-16 22:51:48 +010022 * IT8728F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020023 * IT8758E Super I/O chip w/LPC interface
Guenter Roeckb0636702012-09-07 17:34:41 -060024 * IT8771E Super I/O chip w/LPC interface
25 * IT8772E Super I/O chip w/LPC interface
Guenter Roeck7bc32d22015-01-17 14:10:24 -080026 * IT8781F Super I/O chip w/LPC interface
Guenter Roeck0531d982012-03-02 11:46:44 -080027 * IT8782F Super I/O chip w/LPC interface
28 * IT8783E/F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010029 * Sis950 A clone of the IT8705F
30 *
31 * Copyright (C) 2001 Chris Gauthron
Jean Delvare7c81c602014-01-29 20:40:08 +010032 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
Jean Delvare5f2dc792010-03-05 22:17:18 +010033 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License
45 * along with this program; if not, write to the Free Software
46 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
47 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Joe Perchesa8ca1032011-01-12 21:55:10 +010049#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/slab.h>
54#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020055#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040056#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020057#include <linux/hwmon-sysfs.h>
58#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040059#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010060#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020061#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020062#include <linux/string.h>
63#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010064#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020065#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
corentin.labbeb74f3fd2007-06-13 20:27:36 +020067#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Guenter Roeckb0636702012-09-07 17:34:41 -060069enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8771,
Guenter Roeck7bc32d22015-01-17 14:10:24 -080070 it8772, it8781, it8782, it8783, it8603 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jean Delvare67b671b2007-12-06 23:13:42 +010072static unsigned short force_id;
73module_param(force_id, ushort, 0);
74MODULE_PARM_DESC(force_id, "Override the detected device ID");
75
corentin.labbeb74f3fd2007-06-13 20:27:36 +020076static struct platform_device *pdev;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define REG 0x2e /* The register to read/write */
79#define DEV 0x07 /* Register: Logical device select */
80#define VAL 0x2f /* The value to read/write */
81#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010082
83/* The device with the IT8718F/IT8720F VID value in it */
84#define GPIO 0x07
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define DEVID 0x20 /* Register: Device ID */
87#define DEVREV 0x22 /* Register: Device Revision */
88
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020089static inline int superio_inb(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 outb(reg, REG);
92 return inb(VAL);
93}
94
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020095static inline void superio_outb(int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +020096{
97 outb(reg, REG);
98 outb(val, VAL);
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int superio_inw(int reg)
102{
103 int val;
104 outb(reg++, REG);
105 val = inb(VAL) << 8;
106 outb(reg, REG);
107 val |= inb(VAL);
108 return val;
109}
110
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200111static inline void superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200114 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200117static inline int superio_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200119 /*
120 * Try to reserve REG and REG + 1 for exclusive access.
121 */
122 if (!request_muxed_region(REG, 2, DRVNAME))
123 return -EBUSY;
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 outb(0x87, REG);
126 outb(0x01, REG);
127 outb(0x55, REG);
128 outb(0x55, REG);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200132static inline void superio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 outb(0x02, REG);
135 outb(0x02, VAL);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200136 release_region(REG, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Jean Delvare87673dd2006-08-28 14:37:19 +0200139/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#define IT8712F_DEVID 0x8712
141#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200142#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200143#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100144#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200145#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400146#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100147#define IT8728F_DEVID 0x8728
Guenter Roeckb0636702012-09-07 17:34:41 -0600148#define IT8771E_DEVID 0x8771
149#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800150#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800151#define IT8782F_DEVID 0x8782
152#define IT8783E_DEVID 0x8783
Rudolf Marek7183ae82014-04-04 18:01:35 +0200153#define IT8603E_DEVID 0x8603
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200154#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define IT87_ACT_REG 0x30
156#define IT87_BASE_REG 0x60
157
Jean Delvare87673dd2006-08-28 14:37:19 +0200158/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800159#define IT87_SIO_GPIO1_REG 0x25
Jean Delvare895ff262009-12-09 20:35:47 +0100160#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100161#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800162#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200163#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800164#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200165#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100166#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030169static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030172static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/* Many IT87 constants specified below */
175
176/* Length of ISA address segment */
177#define IT87_EXTENT 8
178
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500179/* Length of ISA address segment for Environmental Controller */
180#define IT87_EC_EXTENT 2
181
182/* Offset of EC registers from ISA base address */
183#define IT87_EC_OFFSET 5
184
185/* Where are the ISA address/data registers relative to the EC base address */
186#define IT87_ADDR_REG_OFFSET 0
187#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189/*----- The IT87 registers -----*/
190
191#define IT87_REG_CONFIG 0x00
192
193#define IT87_REG_ALARM1 0x01
194#define IT87_REG_ALARM2 0x02
195#define IT87_REG_ALARM3 0x03
196
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800197/*
198 * The IT8718F and IT8720F have the VID value in a different register, in
199 * Super-I/O configuration space.
200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800202/*
203 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
204 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
205 * mode.
206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200208#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
211
Jean Delvarec7f1f712007-09-03 17:11:46 +0200212static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
213static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
214static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
215static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100216static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#define IT87_REG_FAN_MAIN_CTRL 0x13
219#define IT87_REG_FAN_CTL 0x14
220#define IT87_REG_PWM(nr) (0x15 + (nr))
Jean Delvare6229cdb2010-12-08 16:27:22 +0100221#define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223#define IT87_REG_VIN(nr) (0x20 + (nr))
224#define IT87_REG_TEMP(nr) (0x29 + (nr))
225
226#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
227#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
228#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
229#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#define IT87_REG_VIN_ENABLE 0x50
232#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700233#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100234#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236#define IT87_REG_CHIPID 0x58
237
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100238#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
239#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
240
Guenter Roeck483db432012-12-19 22:17:02 +0100241struct it87_devices {
242 const char *name;
243 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100244 u8 peci_mask;
245 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100246};
247
248#define FEAT_12MV_ADC (1 << 0)
249#define FEAT_NEWER_AUTOPWM (1 << 1)
250#define FEAT_OLD_AUTOPWM (1 << 2)
251#define FEAT_16BIT_FANS (1 << 3)
252#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100253#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100254#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800255#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
256#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800257#define FEAT_VID (1 << 9) /* Set if chip supports VID */
Guenter Roeck483db432012-12-19 22:17:02 +0100258
259static const struct it87_devices it87_devices[] = {
260 [it87] = {
261 .name = "it87",
262 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
263 },
264 [it8712] = {
265 .name = "it8712",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800266 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
267 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100268 },
269 [it8716] = {
270 .name = "it8716",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800271 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800272 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck483db432012-12-19 22:17:02 +0100273 },
274 [it8718] = {
275 .name = "it8718",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800276 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800277 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100278 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100279 },
280 [it8720] = {
281 .name = "it8720",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800282 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800283 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100284 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100285 },
286 [it8721] = {
287 .name = "it8721",
288 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800289 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
290 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100291 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100292 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100293 },
294 [it8728] = {
295 .name = "it8728",
296 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800297 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100298 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100299 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600300 [it8771] = {
301 .name = "it8771",
302 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
303 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800304 /* PECI: guesswork */
305 /* 12mV ADC (OHM) */
306 /* 16 bit fans (OHM) */
307 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600308 .peci_mask = 0x07,
309 },
310 [it8772] = {
311 .name = "it8772",
312 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
313 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800314 /* PECI (coreboot) */
315 /* 12mV ADC (HWSensors4, OHM) */
316 /* 16 bit fans (HWSensors4, OHM) */
317 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600318 .peci_mask = 0x07,
319 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800320 [it8781] = {
321 .name = "it8781",
322 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800323 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800324 .old_peci_mask = 0x4,
325 },
Guenter Roeck483db432012-12-19 22:17:02 +0100326 [it8782] = {
327 .name = "it8782",
Guenter Roeck19529782012-12-19 22:17:02 +0100328 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800329 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100330 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100331 },
332 [it8783] = {
333 .name = "it8783",
Guenter Roeck19529782012-12-19 22:17:02 +0100334 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800335 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100336 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100337 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100338 [it8603] = {
339 .name = "it8603",
340 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
341 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
342 .peci_mask = 0x07,
343 },
Guenter Roeck483db432012-12-19 22:17:02 +0100344};
345
346#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
347#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
348#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
349#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
350#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100351#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
352 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100353#define has_temp_old_peci(data, nr) \
354 (((data)->features & FEAT_TEMP_OLD_PECI) && \
355 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800356#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
357#define has_five_fans(data) ((data)->features & FEAT_FIVE_FANS)
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800358#define has_vid(data) ((data)->features & FEAT_VID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200360struct it87_sio_data {
361 enum chips type;
362 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200363 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200364 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100365 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200366 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100367 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700368 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100369 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100370 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200371 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700372 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200373};
374
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800375/*
376 * For each registered chip, we need to keep some data in memory.
377 * The structure is dynamically allocated.
378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700380 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100382 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100383 u8 peci_mask;
384 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200386 unsigned short addr;
387 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100388 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 char valid; /* !=0 if following fields are valid */
390 unsigned long last_updated; /* In jiffies */
391
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200392 u16 in_scaled; /* Internal voltage sensors are scaled */
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100393 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200394 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100395 u16 fan[5][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700396 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck161d8982012-12-19 22:17:01 +0100397 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100398 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
399 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 u8 fan_div[3]; /* Register encoding, shifted right */
401 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100402 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100404 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100406 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100407
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800408 /*
409 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100410 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
411 * 7, and we want to preserve settings on mode changes, so we have
412 * to track all values separately.
413 * Starting with the IT8721F, the manual PWM duty cycles are stored
414 * in separate registers (8-bit values), so the separate tracking
415 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800416 * simple.
417 */
Jean Delvareb99883d2010-03-05 22:17:15 +0100418 u8 pwm_ctrl[3]; /* Register value */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100419 u8 pwm_duty[3]; /* Manual PWM value set by user */
Jean Delvareb99883d2010-03-05 22:17:15 +0100420 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100421
422 /* Automatic fan speed control registers */
423 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
424 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425};
426
Guenter Roeck0531d982012-03-02 11:46:44 -0800427static int adc_lsb(const struct it87_data *data, int nr)
428{
429 int lsb = has_12mv_adc(data) ? 12 : 16;
430 if (data->in_scaled & (1 << nr))
431 lsb <<= 1;
432 return lsb;
433}
434
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200435static u8 in_to_reg(const struct it87_data *data, int nr, long val)
436{
Guenter Roeck0531d982012-03-02 11:46:44 -0800437 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800438 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200439}
440
441static int in_from_reg(const struct it87_data *data, int nr, int val)
442{
Guenter Roeck0531d982012-03-02 11:46:44 -0800443 return val * adc_lsb(data, nr);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200444}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200445
446static inline u8 FAN_TO_REG(long rpm, int div)
447{
448 if (rpm == 0)
449 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800450 rpm = clamp_val(rpm, 1, 1000000);
451 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200452}
453
454static inline u16 FAN16_TO_REG(long rpm)
455{
456 if (rpm == 0)
457 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800458 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200459}
460
461#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
462 1350000 / ((val) * (div)))
463/* The divider is fixed to 2 in 16-bit mode */
464#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
465 1350000 / ((val) * 2))
466
Guenter Roeck2a844c12013-01-09 08:09:34 -0800467#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
468 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200469#define TEMP_FROM_REG(val) ((val) * 1000)
470
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200471static u8 pwm_to_reg(const struct it87_data *data, long val)
472{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100473 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200474 return val;
475 else
476 return val >> 1;
477}
478
479static int pwm_from_reg(const struct it87_data *data, u8 reg)
480{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100481 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200482 return reg;
483 else
484 return (reg & 0x7f) << 1;
485}
486
Jean Delvare0df6454d2010-10-28 20:31:51 +0200487
488static int DIV_TO_REG(int val)
489{
490 int answer = 0;
491 while (answer < 7 && (val >>= 1))
492 answer++;
493 return answer;
494}
495#define DIV_FROM_REG(val) (1 << (val))
496
497static const unsigned int pwm_freq[8] = {
498 48000000 / 128,
499 24000000 / 128,
500 12000000 / 128,
501 8000000 / 128,
502 6000000 / 128,
503 3000000 / 128,
504 1500000 / 128,
505 750000 / 128,
506};
507
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200508static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500509static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200511static int it87_read_value(struct it87_data *data, u8 reg);
512static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200514static int it87_check_pwm(struct device *dev);
515static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200518static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100519 .driver = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200520 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100521 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200522 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500523 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200524};
525
Jean Delvare20ad93d2005-06-05 11:53:25 +0200526static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100527 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100529 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
530 int nr = sattr->nr;
531 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100534 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Guenter Roeck929c6a52012-12-19 22:17:00 +0100537static ssize_t set_in(struct device *dev, struct device_attribute *attr,
538 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100540 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
541 int nr = sattr->nr;
542 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200543
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200544 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100545 unsigned long val;
546
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100547 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100548 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100550 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100551 data->in[nr][index] = in_to_reg(data, nr, val);
552 it87_write_value(data,
553 index == 1 ? IT87_REG_VIN_MIN(nr)
554 : IT87_REG_VIN_MAX(nr),
555 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100556 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return count;
558}
559
Guenter Roeck929c6a52012-12-19 22:17:00 +0100560static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
561static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
562 0, 1);
563static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
564 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Guenter Roeck929c6a52012-12-19 22:17:00 +0100566static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
567static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
568 1, 1);
569static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
570 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Guenter Roeck929c6a52012-12-19 22:17:00 +0100572static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
573static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
574 2, 1);
575static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
576 2, 2);
577
578static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
579static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
580 3, 1);
581static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
582 3, 2);
583
584static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
585static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
586 4, 1);
587static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
588 4, 2);
589
590static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
591static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
592 5, 1);
593static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
594 5, 2);
595
596static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
597static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
598 6, 1);
599static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
600 6, 2);
601
602static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
603static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
604 7, 1);
605static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
606 7, 2);
607
608static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100609static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200612static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100613 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100615 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
616 int nr = sattr->nr;
617 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200619
Guenter Roeck60ca3852012-12-19 22:17:00 +0100620 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200622
Guenter Roeck60ca3852012-12-19 22:17:00 +0100623static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
624 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100626 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
627 int nr = sattr->nr;
628 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200629 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100630 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100631 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100632
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100633 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100634 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100636 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100637
638 switch (index) {
639 default:
640 case 1:
641 reg = IT87_REG_TEMP_LOW(nr);
642 break;
643 case 2:
644 reg = IT87_REG_TEMP_HIGH(nr);
645 break;
646 case 3:
647 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
648 if (!(regval & 0x80)) {
649 regval |= 0x80;
650 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
651 }
652 data->valid = 0;
653 reg = IT87_REG_TEMP_OFFSET[nr];
654 break;
655 }
656
Guenter Roeck60ca3852012-12-19 22:17:00 +0100657 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100658 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100659 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return count;
661}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200662
Guenter Roeck60ca3852012-12-19 22:17:00 +0100663static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
664static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
665 0, 1);
666static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
667 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100668static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
669 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100670static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
671static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
672 1, 1);
673static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
674 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100675static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
676 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100677static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
678static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
679 2, 1);
680static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
681 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100682static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
683 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Guenter Roeck2cece012012-12-19 22:17:01 +0100685static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
686 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200688 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
689 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800691 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100692 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100693
Guenter Roeck19529782012-12-19 22:17:02 +0100694 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
695 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100696 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (reg & (1 << nr))
698 return sprintf(buf, "3\n"); /* thermal diode */
699 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200700 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return sprintf(buf, "0\n"); /* disabled */
702}
Guenter Roeck2cece012012-12-19 22:17:01 +0100703
704static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
705 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200707 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
708 int nr = sensor_attr->index;
709
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200710 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100711 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100712 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100713
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100714 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100715 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Jean Delvare8acf07c2010-04-14 16:14:09 +0200717 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
718 reg &= ~(1 << nr);
719 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100720 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
721 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100722 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
723 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
724 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200725 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100726 dev_warn(dev,
727 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200728 val = 4;
729 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100730 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200732 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200733 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200734 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100735 else if (has_temp_peci(data, nr) && val == 6)
736 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100737 else if (has_temp_old_peci(data, nr) && val == 6)
738 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200739 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200741
742 mutex_lock(&data->update_lock);
743 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100744 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200745 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100746 if (has_temp_old_peci(data, nr))
747 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200748 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100749 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return count;
751}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Guenter Roeck2cece012012-12-19 22:17:01 +0100753static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
754 set_temp_type, 0);
755static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
756 set_temp_type, 1);
757static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
758 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100761
762static int pwm_mode(const struct it87_data *data, int nr)
763{
764 int ctrl = data->fan_main_ctrl & (1 << nr);
765
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100766 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +0100767 return 0;
768 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
769 return 2;
770 else /* Manual mode */
771 return 1;
772}
773
Jean Delvare20ad93d2005-06-05 11:53:25 +0200774static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +0100775 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100777 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
778 int nr = sattr->nr;
779 int index = sattr->index;
780 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200782
Guenter Roecke1169ba2012-12-19 22:17:01 +0100783 speed = has_16bit_fans(data) ?
784 FAN16_FROM_REG(data->fan[nr][index]) :
785 FAN_FROM_REG(data->fan[nr][index],
786 DIV_FROM_REG(data->fan_div[nr]));
787 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100789
Jean Delvare20ad93d2005-06-05 11:53:25 +0200790static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
791 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200793 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
794 int nr = sensor_attr->index;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 struct it87_data *data = it87_update_device(dev);
797 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
798}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100799static ssize_t show_pwm_enable(struct device *dev,
800 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200802 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
803 int nr = sensor_attr->index;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100806 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200808static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
809 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200811 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
812 int nr = sensor_attr->index;
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200815 return sprintf(buf, "%d\n",
816 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100818static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
819 char *buf)
820{
821 struct it87_data *data = it87_update_device(dev);
822 int index = (data->fan_ctl >> 4) & 0x07;
823
824 return sprintf(buf, "%u\n", pwm_freq[index]);
825}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100826
827static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
828 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100830 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
831 int nr = sattr->nr;
832 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200833
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200834 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100835 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100836 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100838 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100839 return -EINVAL;
840
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100841 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +0100842
843 if (has_16bit_fans(data)) {
844 data->fan[nr][index] = FAN16_TO_REG(val);
845 it87_write_value(data, IT87_REG_FAN_MIN[nr],
846 data->fan[nr][index] & 0xff);
847 it87_write_value(data, IT87_REG_FANX_MIN[nr],
848 data->fan[nr][index] >> 8);
849 } else {
850 reg = it87_read_value(data, IT87_REG_FAN_DIV);
851 switch (nr) {
852 case 0:
853 data->fan_div[nr] = reg & 0x07;
854 break;
855 case 1:
856 data->fan_div[nr] = (reg >> 3) & 0x07;
857 break;
858 case 2:
859 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
860 break;
861 }
862 data->fan[nr][index] =
863 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
864 it87_write_value(data, IT87_REG_FAN_MIN[nr],
865 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -0800866 }
867
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100868 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return count;
870}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100871
Jean Delvare20ad93d2005-06-05 11:53:25 +0200872static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
873 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200875 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
876 int nr = sensor_attr->index;
877
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200878 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100879 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200880 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 u8 old;
882
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100883 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100884 return -EINVAL;
885
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100886 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200887 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200889 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100890 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 switch (nr) {
893 case 0:
894 case 1:
895 data->fan_div[nr] = DIV_TO_REG(val);
896 break;
897 case 2:
898 if (val < 8)
899 data->fan_div[nr] = 1;
900 else
901 data->fan_div[nr] = 3;
902 }
903 val = old & 0x80;
904 val |= (data->fan_div[0] & 0x07);
905 val |= (data->fan_div[1] & 0x07) << 3;
906 if (data->fan_div[2] == 3)
907 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200908 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200910 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100911 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
912 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200913
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100914 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return count;
916}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100917
918/* Returns 0 if OK, -EINVAL otherwise */
919static int check_trip_points(struct device *dev, int nr)
920{
921 const struct it87_data *data = dev_get_drvdata(dev);
922 int i, err = 0;
923
924 if (has_old_autopwm(data)) {
925 for (i = 0; i < 3; i++) {
926 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
927 err = -EINVAL;
928 }
929 for (i = 0; i < 2; i++) {
930 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
931 err = -EINVAL;
932 }
933 }
934
935 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100936 dev_err(dev,
937 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100938 dev_err(dev, "Adjust the trip points and try again\n");
939 }
940 return err;
941}
942
Jean Delvare20ad93d2005-06-05 11:53:25 +0200943static ssize_t set_pwm_enable(struct device *dev,
944 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200946 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
947 int nr = sensor_attr->index;
948
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200949 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100950 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100952 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100953 return -EINVAL;
954
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100955 /* Check trip points before switching to automatic mode */
956 if (val == 2) {
957 if (check_trip_points(dev, nr) < 0)
958 return -EINVAL;
959 }
960
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100961 /* IT8603E does not have on/off mode */
962 if (val == 0 && data->type == it8603)
963 return -EINVAL;
964
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100965 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (val == 0) {
968 int tmp;
969 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200970 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
971 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* set on/off mode */
973 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100974 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
975 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100976 } else {
977 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +0100978 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +0100979 data->pwm_temp_map[nr] :
980 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +0100981 else /* Automatic mode */
982 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
983 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100984
985 if (data->type != it8603) {
986 /* set SmartGuardian mode */
987 data->fan_main_ctrl |= (1 << nr);
988 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
989 data->fan_main_ctrl);
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100993 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return count;
995}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200996static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
997 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200999 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1000 int nr = sensor_attr->index;
1001
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001002 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001003 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001005 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return -EINVAL;
1007
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001008 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001009 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001010 /*
1011 * If we are in automatic mode, the PWM duty cycle register
1012 * is read-only so we can't write the value.
1013 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001014 if (data->pwm_ctrl[nr] & 0x80) {
1015 mutex_unlock(&data->update_lock);
1016 return -EBUSY;
1017 }
1018 data->pwm_duty[nr] = pwm_to_reg(data, val);
1019 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
1020 data->pwm_duty[nr]);
1021 } else {
1022 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001023 /*
1024 * If we are in manual mode, write the duty cycle immediately;
1025 * otherwise, just store it for later use.
1026 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001027 if (!(data->pwm_ctrl[nr] & 0x80)) {
1028 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1029 it87_write_value(data, IT87_REG_PWM(nr),
1030 data->pwm_ctrl[nr]);
1031 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001032 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001033 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return count;
1035}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001036static ssize_t set_pwm_freq(struct device *dev,
1037 struct device_attribute *attr, const char *buf, size_t count)
1038{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001039 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001040 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001041 int i;
1042
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001043 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001044 return -EINVAL;
1045
Jean Delvaref8d0c192007-02-14 21:15:02 +01001046 /* Search for the nearest available frequency */
1047 for (i = 0; i < 7; i++) {
1048 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1049 break;
1050 }
1051
1052 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001053 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001054 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001055 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001056 mutex_unlock(&data->update_lock);
1057
1058 return count;
1059}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001060static ssize_t show_pwm_temp_map(struct device *dev,
1061 struct device_attribute *attr, char *buf)
1062{
1063 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1064 int nr = sensor_attr->index;
1065
1066 struct it87_data *data = it87_update_device(dev);
1067 int map;
1068
1069 if (data->pwm_temp_map[nr] < 3)
1070 map = 1 << data->pwm_temp_map[nr];
1071 else
1072 map = 0; /* Should never happen */
1073 return sprintf(buf, "%d\n", map);
1074}
1075static ssize_t set_pwm_temp_map(struct device *dev,
1076 struct device_attribute *attr, const char *buf, size_t count)
1077{
1078 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1079 int nr = sensor_attr->index;
1080
1081 struct it87_data *data = dev_get_drvdata(dev);
1082 long val;
1083 u8 reg;
1084
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001085 /*
1086 * This check can go away if we ever support automatic fan speed
1087 * control on newer chips.
1088 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001089 if (!has_old_autopwm(data)) {
1090 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1091 return -EINVAL;
1092 }
1093
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001094 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001095 return -EINVAL;
1096
1097 switch (val) {
1098 case (1 << 0):
1099 reg = 0x00;
1100 break;
1101 case (1 << 1):
1102 reg = 0x01;
1103 break;
1104 case (1 << 2):
1105 reg = 0x02;
1106 break;
1107 default:
1108 return -EINVAL;
1109 }
1110
1111 mutex_lock(&data->update_lock);
1112 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001113 /*
1114 * If we are in automatic mode, write the temp mapping immediately;
1115 * otherwise, just store it for later use.
1116 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001117 if (data->pwm_ctrl[nr] & 0x80) {
1118 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1119 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1120 }
1121 mutex_unlock(&data->update_lock);
1122 return count;
1123}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001125static ssize_t show_auto_pwm(struct device *dev,
1126 struct device_attribute *attr, char *buf)
1127{
1128 struct it87_data *data = it87_update_device(dev);
1129 struct sensor_device_attribute_2 *sensor_attr =
1130 to_sensor_dev_attr_2(attr);
1131 int nr = sensor_attr->nr;
1132 int point = sensor_attr->index;
1133
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001134 return sprintf(buf, "%d\n",
1135 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001136}
1137
1138static ssize_t set_auto_pwm(struct device *dev,
1139 struct device_attribute *attr, const char *buf, size_t count)
1140{
1141 struct it87_data *data = dev_get_drvdata(dev);
1142 struct sensor_device_attribute_2 *sensor_attr =
1143 to_sensor_dev_attr_2(attr);
1144 int nr = sensor_attr->nr;
1145 int point = sensor_attr->index;
1146 long val;
1147
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001148 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001149 return -EINVAL;
1150
1151 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001152 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001153 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1154 data->auto_pwm[nr][point]);
1155 mutex_unlock(&data->update_lock);
1156 return count;
1157}
1158
1159static ssize_t show_auto_temp(struct device *dev,
1160 struct device_attribute *attr, char *buf)
1161{
1162 struct it87_data *data = it87_update_device(dev);
1163 struct sensor_device_attribute_2 *sensor_attr =
1164 to_sensor_dev_attr_2(attr);
1165 int nr = sensor_attr->nr;
1166 int point = sensor_attr->index;
1167
1168 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1169}
1170
1171static ssize_t set_auto_temp(struct device *dev,
1172 struct device_attribute *attr, const char *buf, size_t count)
1173{
1174 struct it87_data *data = dev_get_drvdata(dev);
1175 struct sensor_device_attribute_2 *sensor_attr =
1176 to_sensor_dev_attr_2(attr);
1177 int nr = sensor_attr->nr;
1178 int point = sensor_attr->index;
1179 long val;
1180
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001181 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001182 return -EINVAL;
1183
1184 mutex_lock(&data->update_lock);
1185 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1186 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1187 data->auto_temp[nr][point]);
1188 mutex_unlock(&data->update_lock);
1189 return count;
1190}
1191
Guenter Roecke1169ba2012-12-19 22:17:01 +01001192static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1193static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1194 0, 1);
1195static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1196 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Guenter Roecke1169ba2012-12-19 22:17:01 +01001198static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1199static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1200 1, 1);
1201static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1202 set_fan_div, 1);
1203
1204static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1205static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1206 2, 1);
1207static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1208 set_fan_div, 2);
1209
1210static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1211static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1212 3, 1);
1213
1214static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1215static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1216 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Guenter Roeckc4458db2012-12-19 22:17:02 +01001218static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1219 show_pwm_enable, set_pwm_enable, 0);
1220static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1221static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1222static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1223 show_pwm_temp_map, set_pwm_temp_map, 0);
1224static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1225 show_auto_pwm, set_auto_pwm, 0, 0);
1226static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1227 show_auto_pwm, set_auto_pwm, 0, 1);
1228static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1229 show_auto_pwm, set_auto_pwm, 0, 2);
1230static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1231 show_auto_pwm, NULL, 0, 3);
1232static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1233 show_auto_temp, set_auto_temp, 0, 1);
1234static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1235 show_auto_temp, set_auto_temp, 0, 0);
1236static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1237 show_auto_temp, set_auto_temp, 0, 2);
1238static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1239 show_auto_temp, set_auto_temp, 0, 3);
1240static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1241 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Guenter Roeckc4458db2012-12-19 22:17:02 +01001243static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1244 show_pwm_enable, set_pwm_enable, 1);
1245static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1246static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1247static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1248 show_pwm_temp_map, set_pwm_temp_map, 1);
1249static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1250 show_auto_pwm, set_auto_pwm, 1, 0);
1251static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1252 show_auto_pwm, set_auto_pwm, 1, 1);
1253static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1254 show_auto_pwm, set_auto_pwm, 1, 2);
1255static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1256 show_auto_pwm, NULL, 1, 3);
1257static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1258 show_auto_temp, set_auto_temp, 1, 1);
1259static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1260 show_auto_temp, set_auto_temp, 1, 0);
1261static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1262 show_auto_temp, set_auto_temp, 1, 2);
1263static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1264 show_auto_temp, set_auto_temp, 1, 3);
1265static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1266 show_auto_temp, set_auto_temp, 1, 4);
1267
1268static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1269 show_pwm_enable, set_pwm_enable, 2);
1270static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1271static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1272static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1273 show_pwm_temp_map, set_pwm_temp_map, 2);
1274static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1275 show_auto_pwm, set_auto_pwm, 2, 0);
1276static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1277 show_auto_pwm, set_auto_pwm, 2, 1);
1278static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1279 show_auto_pwm, set_auto_pwm, 2, 2);
1280static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1281 show_auto_pwm, NULL, 2, 3);
1282static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1283 show_auto_temp, set_auto_temp, 2, 1);
1284static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1285 show_auto_temp, set_auto_temp, 2, 0);
1286static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1287 show_auto_temp, set_auto_temp, 2, 2);
1288static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1289 show_auto_temp, set_auto_temp, 2, 3);
1290static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1291 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001294static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1295 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001298 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
Jean Delvare1d66c642005-04-18 21:16:59 -07001300static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Jean Delvare0124dd72007-11-25 16:16:41 +01001302static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1303 char *buf)
1304{
1305 int bitnr = to_sensor_dev_attr(attr)->index;
1306 struct it87_data *data = it87_update_device(dev);
1307 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1308}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001309
1310static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1311 *attr, const char *buf, size_t count)
1312{
1313 struct it87_data *data = dev_get_drvdata(dev);
1314 long val;
1315 int config;
1316
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001317 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001318 return -EINVAL;
1319
1320 mutex_lock(&data->update_lock);
1321 config = it87_read_value(data, IT87_REG_CONFIG);
1322 if (config < 0) {
1323 count = config;
1324 } else {
1325 config |= 1 << 5;
1326 it87_write_value(data, IT87_REG_CONFIG, config);
1327 /* Invalidate cache to force re-read */
1328 data->valid = 0;
1329 }
1330 mutex_unlock(&data->update_lock);
1331
1332 return count;
1333}
1334
Jean Delvare0124dd72007-11-25 16:16:41 +01001335static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1336static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1337static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1338static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1339static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1340static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1341static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1342static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1343static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1344static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1345static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1346static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1347static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1348static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1349static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1350static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001351static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1352 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001353
Jean Delvared9b327c2010-03-05 22:17:17 +01001354static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1355 char *buf)
1356{
1357 int bitnr = to_sensor_dev_attr(attr)->index;
1358 struct it87_data *data = it87_update_device(dev);
1359 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1360}
1361static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1362 const char *buf, size_t count)
1363{
1364 int bitnr = to_sensor_dev_attr(attr)->index;
1365 struct it87_data *data = dev_get_drvdata(dev);
1366 long val;
1367
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001368 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001369 || (val != 0 && val != 1))
1370 return -EINVAL;
1371
1372 mutex_lock(&data->update_lock);
1373 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1374 if (val)
1375 data->beeps |= (1 << bitnr);
1376 else
1377 data->beeps &= ~(1 << bitnr);
1378 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1379 mutex_unlock(&data->update_lock);
1380 return count;
1381}
1382
1383static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1384 show_beep, set_beep, 1);
1385static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1386static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1387static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1388static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1389static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1390static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1391static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1392/* fanX_beep writability is set later */
1393static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1394static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1395static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1396static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1397static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1398static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1399 show_beep, set_beep, 2);
1400static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1401static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1402
Jean Delvare5f2dc792010-03-05 22:17:18 +01001403static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1404 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Jean Delvare90d66192007-10-08 18:24:35 +02001406 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001407 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001409static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1410 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001412 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001413 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001415 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001416 return -EINVAL;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 data->vrm = val;
1419
1420 return count;
1421}
1422static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Jean Delvare5f2dc792010-03-05 22:17:18 +01001424static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1425 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 struct it87_data *data = it87_update_device(dev);
1428 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1429}
1430static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001431
Jean Delvare738e5e02010-08-14 21:08:50 +02001432static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1433 char *buf)
1434{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001435 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001436 "+5V",
1437 "5VSB",
1438 "Vbat",
1439 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001440 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001441 "+3.3V",
1442 "3VSB",
1443 "Vbat",
1444 };
1445 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001446 int nr = to_sensor_dev_attr(attr)->index;
1447
Jean Delvare16b5dda2012-01-16 22:51:48 +01001448 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1449 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001450}
1451static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1452static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1453static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Rudolf Marek7183ae82014-04-04 18:01:35 +02001454/* special AVCC3 IT8603E in9 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001455static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001456
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001457static ssize_t show_name(struct device *dev, struct device_attribute
1458 *devattr, char *buf)
1459{
1460 struct it87_data *data = dev_get_drvdata(dev);
1461 return sprintf(buf, "%s\n", data->name);
1462}
1463static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1464
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001465static struct attribute *it87_attributes_in[10][5] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001466{
Jean Delvare87808be2006-09-24 21:17:13 +02001467 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001468 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001469 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001470 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001471 NULL
1472}, {
1473 &sensor_dev_attr_in1_input.dev_attr.attr,
1474 &sensor_dev_attr_in1_min.dev_attr.attr,
1475 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001476 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001477 NULL
1478}, {
1479 &sensor_dev_attr_in2_input.dev_attr.attr,
1480 &sensor_dev_attr_in2_min.dev_attr.attr,
1481 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001482 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001483 NULL
1484}, {
1485 &sensor_dev_attr_in3_input.dev_attr.attr,
1486 &sensor_dev_attr_in3_min.dev_attr.attr,
1487 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001488 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001489 NULL
1490}, {
1491 &sensor_dev_attr_in4_input.dev_attr.attr,
1492 &sensor_dev_attr_in4_min.dev_attr.attr,
1493 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001494 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001495 NULL
1496}, {
1497 &sensor_dev_attr_in5_input.dev_attr.attr,
1498 &sensor_dev_attr_in5_min.dev_attr.attr,
1499 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001500 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001501 NULL
1502}, {
1503 &sensor_dev_attr_in6_input.dev_attr.attr,
1504 &sensor_dev_attr_in6_min.dev_attr.attr,
1505 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001506 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001507 NULL
1508}, {
1509 &sensor_dev_attr_in7_input.dev_attr.attr,
1510 &sensor_dev_attr_in7_min.dev_attr.attr,
1511 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001512 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001513 NULL
1514}, {
1515 &sensor_dev_attr_in8_input.dev_attr.attr,
1516 NULL
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001517}, {
1518 &sensor_dev_attr_in9_input.dev_attr.attr,
1519 NULL
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001520} };
Jean Delvare87808be2006-09-24 21:17:13 +02001521
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001522static const struct attribute_group it87_group_in[10] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001523 { .attrs = it87_attributes_in[0] },
1524 { .attrs = it87_attributes_in[1] },
1525 { .attrs = it87_attributes_in[2] },
1526 { .attrs = it87_attributes_in[3] },
1527 { .attrs = it87_attributes_in[4] },
1528 { .attrs = it87_attributes_in[5] },
1529 { .attrs = it87_attributes_in[6] },
1530 { .attrs = it87_attributes_in[7] },
1531 { .attrs = it87_attributes_in[8] },
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001532 { .attrs = it87_attributes_in[9] },
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001533};
1534
Guenter Roeck4573acb2012-03-26 16:17:41 -07001535static struct attribute *it87_attributes_temp[3][6] = {
1536{
Jean Delvare87808be2006-09-24 21:17:13 +02001537 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001538 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001539 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001540 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001541 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001542 NULL
1543} , {
1544 &sensor_dev_attr_temp2_input.dev_attr.attr,
1545 &sensor_dev_attr_temp2_max.dev_attr.attr,
1546 &sensor_dev_attr_temp2_min.dev_attr.attr,
1547 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001548 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001549 NULL
1550} , {
1551 &sensor_dev_attr_temp3_input.dev_attr.attr,
1552 &sensor_dev_attr_temp3_max.dev_attr.attr,
1553 &sensor_dev_attr_temp3_min.dev_attr.attr,
1554 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001555 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001556 NULL
1557} };
Jean Delvare87808be2006-09-24 21:17:13 +02001558
Guenter Roeck4573acb2012-03-26 16:17:41 -07001559static const struct attribute_group it87_group_temp[3] = {
1560 { .attrs = it87_attributes_temp[0] },
1561 { .attrs = it87_attributes_temp[1] },
1562 { .attrs = it87_attributes_temp[2] },
1563};
1564
Guenter Roeck161d8982012-12-19 22:17:01 +01001565static struct attribute *it87_attributes_temp_offset[] = {
1566 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1567 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1568 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1569};
1570
Guenter Roeck4573acb2012-03-26 16:17:41 -07001571static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001572 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001573 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001574 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001575 NULL
1576};
1577
1578static const struct attribute_group it87_group = {
1579 .attrs = it87_attributes,
1580};
1581
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001582static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001583 &sensor_dev_attr_in0_beep.dev_attr.attr,
1584 &sensor_dev_attr_in1_beep.dev_attr.attr,
1585 &sensor_dev_attr_in2_beep.dev_attr.attr,
1586 &sensor_dev_attr_in3_beep.dev_attr.attr,
1587 &sensor_dev_attr_in4_beep.dev_attr.attr,
1588 &sensor_dev_attr_in5_beep.dev_attr.attr,
1589 &sensor_dev_attr_in6_beep.dev_attr.attr,
1590 &sensor_dev_attr_in7_beep.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001591 NULL,
1592 NULL,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001593};
Jean Delvared9b327c2010-03-05 22:17:17 +01001594
Guenter Roeck4573acb2012-03-26 16:17:41 -07001595static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001596 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1597 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1598 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001599};
1600
Guenter Roecke1169ba2012-12-19 22:17:01 +01001601static struct attribute *it87_attributes_fan[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001602 &sensor_dev_attr_fan1_input.dev_attr.attr,
1603 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001604 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1605 NULL
1606}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001607 &sensor_dev_attr_fan2_input.dev_attr.attr,
1608 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001609 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1610 NULL
1611}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001612 &sensor_dev_attr_fan3_input.dev_attr.attr,
1613 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001614 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001615 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001616}, {
1617 &sensor_dev_attr_fan4_input.dev_attr.attr,
1618 &sensor_dev_attr_fan4_min.dev_attr.attr,
1619 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1620 NULL
1621}, {
1622 &sensor_dev_attr_fan5_input.dev_attr.attr,
1623 &sensor_dev_attr_fan5_min.dev_attr.attr,
1624 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1625 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001626} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001627
Guenter Roecke1169ba2012-12-19 22:17:01 +01001628static const struct attribute_group it87_group_fan[5] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001629 { .attrs = it87_attributes_fan[0] },
1630 { .attrs = it87_attributes_fan[1] },
1631 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001632 { .attrs = it87_attributes_fan[3] },
1633 { .attrs = it87_attributes_fan[4] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001634};
1635
Guenter Roecke1169ba2012-12-19 22:17:01 +01001636static const struct attribute *it87_attributes_fan_div[] = {
1637 &sensor_dev_attr_fan1_div.dev_attr.attr,
1638 &sensor_dev_attr_fan2_div.dev_attr.attr,
1639 &sensor_dev_attr_fan3_div.dev_attr.attr,
1640};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001641
1642static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001643 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001644 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001645 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001646 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001647 NULL
1648}, {
1649 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1650 &sensor_dev_attr_pwm2.dev_attr.attr,
1651 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001652 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001653 NULL
1654}, {
1655 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1656 &sensor_dev_attr_pwm3.dev_attr.attr,
1657 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001658 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001659 NULL
1660} };
Jean Delvare87808be2006-09-24 21:17:13 +02001661
Jean Delvare723a0aa2010-03-05 22:17:16 +01001662static const struct attribute_group it87_group_pwm[3] = {
1663 { .attrs = it87_attributes_pwm[0] },
1664 { .attrs = it87_attributes_pwm[1] },
1665 { .attrs = it87_attributes_pwm[2] },
1666};
1667
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001668static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1669 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1670 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1671 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1672 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1673 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1674 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1675 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1676 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1677 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1678 NULL
1679}, {
1680 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1681 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1682 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1683 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1684 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1685 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1686 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1687 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1688 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1689 NULL
1690}, {
1691 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1692 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1693 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1694 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1695 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1696 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1697 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1698 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1699 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1700 NULL
1701} };
1702
1703static const struct attribute_group it87_group_autopwm[3] = {
1704 { .attrs = it87_attributes_autopwm[0] },
1705 { .attrs = it87_attributes_autopwm[1] },
1706 { .attrs = it87_attributes_autopwm[2] },
1707};
1708
Jean Delvared9b327c2010-03-05 22:17:17 +01001709static struct attribute *it87_attributes_fan_beep[] = {
1710 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1711 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1712 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1713 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1714 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1715};
1716
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001717static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001718 &dev_attr_vrm.attr,
1719 &dev_attr_cpu0_vid.attr,
1720 NULL
1721};
1722
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001723static const struct attribute_group it87_group_vid = {
1724 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001725};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Jean Delvare738e5e02010-08-14 21:08:50 +02001727static struct attribute *it87_attributes_label[] = {
1728 &sensor_dev_attr_in3_label.dev_attr.attr,
1729 &sensor_dev_attr_in7_label.dev_attr.attr,
1730 &sensor_dev_attr_in8_label.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001731 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare738e5e02010-08-14 21:08:50 +02001732 NULL
1733};
1734
1735static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001736 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001737};
1738
Jean Delvare2d8672c2005-07-19 23:56:35 +02001739/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001740static int __init it87_find(unsigned short *address,
1741 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001743 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001744 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001745 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001747 err = superio_enter();
1748 if (err)
1749 return err;
1750
1751 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001752 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001753
1754 switch (chip_type) {
1755 case IT8705F_DEVID:
1756 sio_data->type = it87;
1757 break;
1758 case IT8712F_DEVID:
1759 sio_data->type = it8712;
1760 break;
1761 case IT8716F_DEVID:
1762 case IT8726F_DEVID:
1763 sio_data->type = it8716;
1764 break;
1765 case IT8718F_DEVID:
1766 sio_data->type = it8718;
1767 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001768 case IT8720F_DEVID:
1769 sio_data->type = it8720;
1770 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001771 case IT8721F_DEVID:
1772 sio_data->type = it8721;
1773 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001774 case IT8728F_DEVID:
1775 sio_data->type = it8728;
1776 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06001777 case IT8771E_DEVID:
1778 sio_data->type = it8771;
1779 break;
1780 case IT8772E_DEVID:
1781 sio_data->type = it8772;
1782 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08001783 case IT8781F_DEVID:
1784 sio_data->type = it8781;
1785 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001786 case IT8782F_DEVID:
1787 sio_data->type = it8782;
1788 break;
1789 case IT8783E_DEVID:
1790 sio_data->type = it8783;
1791 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02001792 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02001793 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001794 sio_data->type = it8603;
1795 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001796 case 0xffff: /* No device at all */
1797 goto exit;
1798 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001799 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001800 goto exit;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Jean Delvare87673dd2006-08-28 14:37:19 +02001803 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001805 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto exit;
1807 }
1808
1809 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1810 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001811 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto exit;
1813 }
1814
1815 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001816 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001817 pr_info("Found IT%04x%c chip at 0x%x, revision %d\n", chip_type,
Jean Delvareb523bb72014-01-29 20:40:08 +01001818 chip_type == 0x8771 || chip_type == 0x8772 ||
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001819 chip_type == 0x8603 ? 'E' : 'F', *address,
1820 sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Jean Delvare738e5e02010-08-14 21:08:50 +02001822 /* in8 (Vbat) is always internal */
1823 sio_data->internal = (1 << 2);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001824 /* Only the IT8603E has in9 */
1825 if (sio_data->type != it8603)
1826 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02001827
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001828 if (!(it87_devices[sio_data->type].features & FEAT_VID))
Jean Delvare895ff262009-12-09 20:35:47 +01001829 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001830
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001831 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1832 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01001833 /* The IT8705F has a different LD number for GPIO */
1834 superio_select(5);
1835 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08001836 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001837 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08001838
Guenter Roeck0531d982012-03-02 11:46:44 -08001839 superio_select(GPIO);
1840
1841 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1842 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001843 reg2a = superio_inb(IT87_SIO_PINX1_REG);
1844 reg2c = superio_inb(IT87_SIO_PINX2_REG);
1845 regef = superio_inb(IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001846
Guenter Roeck0531d982012-03-02 11:46:44 -08001847 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001848 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08001849 sio_data->skip_fan |= (1 << 2);
1850 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001851 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08001852 sio_data->skip_pwm |= (1 << 2);
1853
1854 /* Check if fan2 is there or not */
1855 if (reg27 & (1 << 7))
1856 sio_data->skip_fan |= (1 << 1);
1857 if (reg27 & (1 << 3))
1858 sio_data->skip_pwm |= (1 << 1);
1859
1860 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001861 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001862 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001863
1864 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001865 if (reg27 & (1 << 1))
1866 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001867
1868 /*
1869 * VIN7
1870 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1871 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001872 if (reg27 & (1 << 2)) {
1873 /*
1874 * The data sheet is a bit unclear regarding the
1875 * internal voltage divider for VCCH5V. It says
1876 * "This bit enables and switches VIN7 (pin 91) to the
1877 * internal voltage divider for VCCH5V".
1878 * This is different to other chips, where the internal
1879 * voltage divider would connect VIN7 to an internal
1880 * voltage source. Maybe that is the case here as well.
1881 *
1882 * Since we don't know for sure, re-route it if that is
1883 * not the case, and ask the user to report if the
1884 * resulting voltage is sane.
1885 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001886 if (!(reg2c & (1 << 1))) {
1887 reg2c |= (1 << 1);
1888 superio_outb(IT87_SIO_PINX2_REG, reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001889 pr_notice("Routing internal VCCH5V to in7.\n");
1890 }
1891 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1892 pr_notice("Please report if it displays a reasonable voltage.\n");
1893 }
Guenter Roeck0531d982012-03-02 11:46:44 -08001894
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001895 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08001896 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001897 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08001898 sio_data->internal |= (1 << 1);
1899
1900 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001901 } else if (sio_data->type == it8603) {
1902 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08001903
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001904 superio_select(GPIO);
1905
1906 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1907
1908 /* Check if fan3 is there or not */
1909 if (reg27 & (1 << 6))
1910 sio_data->skip_pwm |= (1 << 2);
1911 if (reg27 & (1 << 7))
1912 sio_data->skip_fan |= (1 << 2);
1913
1914 /* Check if fan2 is there or not */
1915 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
1916 if (reg29 & (1 << 1))
1917 sio_data->skip_pwm |= (1 << 1);
1918 if (reg29 & (1 << 2))
1919 sio_data->skip_fan |= (1 << 1);
1920
1921 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1922 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1923
1924 /* no fan4 */
1925 sio_data->skip_pwm |= (1 << 3);
1926 sio_data->skip_fan |= (1 << 3);
1927
1928 sio_data->internal |= (1 << 1); /* in7 is VSB */
1929 sio_data->internal |= (1 << 3); /* in9 is AVCC */
1930
1931 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01001932 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001933 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001934 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02001935
1936 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001937
Jean Delvare895ff262009-12-09 20:35:47 +01001938 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001939 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001940 /* We need at least 4 VID pins */
1941 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001942 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001943 sio_data->skip_vid = 1;
1944 }
Jean Delvare895ff262009-12-09 20:35:47 +01001945 }
1946
Jean Delvare591ec652009-12-09 20:35:48 +01001947 /* Check if fan3 is there or not */
1948 if (reg & (1 << 6))
1949 sio_data->skip_pwm |= (1 << 2);
1950 if (reg & (1 << 7))
1951 sio_data->skip_fan |= (1 << 2);
1952
1953 /* Check if fan2 is there or not */
1954 reg = superio_inb(IT87_SIO_GPIO5_REG);
1955 if (reg & (1 << 1))
1956 sio_data->skip_pwm |= (1 << 1);
1957 if (reg & (1 << 2))
1958 sio_data->skip_fan |= (1 << 1);
1959
Jean Delvare895ff262009-12-09 20:35:47 +01001960 if ((sio_data->type == it8718 || sio_data->type == it8720)
1961 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001962 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001963
1964 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001965
1966 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1967
Jean Delvare436cad22010-07-09 16:22:48 +02001968 /*
1969 * The IT8720F has no VIN7 pin, so VCCH should always be
1970 * routed internally to VIN7 with an internal divider.
1971 * Curiously, there still is a configuration bit to control
1972 * this, which means it can be set incorrectly. And even
1973 * more curiously, many boards out there are improperly
1974 * configured, even though the IT8720F datasheet claims
1975 * that the internal routing of VCCH to VIN7 is the default
1976 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08001977 *
1978 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001979 * If UART6 is enabled, re-route VIN7 to the internal divider
1980 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02001981 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001982 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02001983 reg |= (1 << 1);
1984 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01001985 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02001986 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001987 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001988 sio_data->internal |= (1 << 0);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001989 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
Guenter Roeckb0636702012-09-07 17:34:41 -06001990 sio_data->type == it8728 ||
1991 sio_data->type == it8771 ||
1992 sio_data->type == it8772)
Jean Delvare738e5e02010-08-14 21:08:50 +02001993 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01001994
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001995 /*
1996 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1997 * While VIN7 can be routed to the internal voltage divider,
1998 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07001999 *
2000 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2001 * is the temperature source. Since we can not read the
2002 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002003 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002004 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002005 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002006 sio_data->skip_temp |= (1 << 2);
2007 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002008
Jean Delvared9b327c2010-03-05 22:17:17 +01002009 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002010 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002011 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002012 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002013
Jean Delvare98dd22c2008-10-09 15:33:58 +02002014 /* Disable specific features based on DMI strings */
2015 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2016 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2017 if (board_vendor && board_name) {
2018 if (strcmp(board_vendor, "nVIDIA") == 0
2019 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002020 /*
2021 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2022 * connected to a fan, but to something else. One user
2023 * has reported instant system power-off when changing
2024 * the PWM2 duty cycle, so we disable it.
2025 * I use the board name string as the trigger in case
2026 * the same board is ever used in other systems.
2027 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002028 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002029 sio_data->skip_pwm = (1 << 1);
2030 }
2031 }
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033exit:
2034 superio_exit();
2035 return err;
2036}
2037
Jean Delvare723a0aa2010-03-05 22:17:16 +01002038static void it87_remove_files(struct device *dev)
2039{
2040 struct it87_data *data = platform_get_drvdata(pdev);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002041 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002042 int i;
2043
2044 sysfs_remove_group(&dev->kobj, &it87_group);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002045 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002046 if (sio_data->skip_in & (1 << i))
2047 continue;
2048 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2049 if (it87_attributes_in_beep[i])
2050 sysfs_remove_file(&dev->kobj,
2051 it87_attributes_in_beep[i]);
2052 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002053 for (i = 0; i < 3; i++) {
2054 if (!(data->has_temp & (1 << i)))
2055 continue;
2056 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01002057 if (has_temp_offset(data))
2058 sysfs_remove_file(&dev->kobj,
2059 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002060 if (sio_data->beep_pin)
2061 sysfs_remove_file(&dev->kobj,
2062 it87_attributes_temp_beep[i]);
2063 }
Jean Delvare723a0aa2010-03-05 22:17:16 +01002064 for (i = 0; i < 5; i++) {
2065 if (!(data->has_fan & (1 << i)))
2066 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002067 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002068 if (sio_data->beep_pin)
2069 sysfs_remove_file(&dev->kobj,
2070 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002071 if (i < 3 && !has_16bit_fans(data))
2072 sysfs_remove_file(&dev->kobj,
2073 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002074 }
2075 for (i = 0; i < 3; i++) {
2076 if (sio_data->skip_pwm & (1 << 0))
2077 continue;
2078 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002079 if (has_old_autopwm(data))
2080 sysfs_remove_group(&dev->kobj,
2081 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002082 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002083 if (!sio_data->skip_vid)
2084 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02002085 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002086}
2087
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002088static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002091 struct resource *res;
2092 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002093 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002094 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01002096 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002098 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002099 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2100 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002101 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2102 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002103 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002104 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Guenter Roeck62a1d052012-03-24 21:54:41 -07002107 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2108 if (!data)
2109 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002111 data->addr = res->start;
2112 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002113 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002114 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002115 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002116 data->name = it87_devices[sio_data->type].name;
2117 /*
2118 * IT8705F Datasheet 0.4.1, 3h == Version G.
2119 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2120 * These are the first revisions with 16-bit tachometer support.
2121 */
2122 switch (data->type) {
2123 case it87:
2124 if (sio_data->revision >= 0x03) {
2125 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002126 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002127 }
2128 break;
2129 case it8712:
2130 if (sio_data->revision >= 0x08) {
2131 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002132 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2133 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002134 }
2135 break;
2136 default:
2137 break;
2138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002141 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002142 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2143 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002145 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002147 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002150 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002152 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002153 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002154 if (sio_data->internal & (1 << 0))
2155 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2156 if (sio_data->internal & (1 << 1))
2157 data->in_scaled |= (1 << 7); /* in7 is VSB */
2158 if (sio_data->internal & (1 << 2))
2159 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002160 if (sio_data->internal & (1 << 3))
2161 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002162 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2163 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002164 if (sio_data->internal & (1 << 0))
2165 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2166 if (sio_data->internal & (1 << 1))
2167 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002168 }
2169
Guenter Roeck4573acb2012-03-26 16:17:41 -07002170 data->has_temp = 0x07;
2171 if (sio_data->skip_temp & (1 << 2)) {
2172 if (sio_data->type == it8782
2173 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2174 data->has_temp &= ~(1 << 2);
2175 }
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002178 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002181 err = sysfs_create_group(&dev->kobj, &it87_group);
2182 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002183 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002184
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002185 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002186 if (sio_data->skip_in & (1 << i))
2187 continue;
2188 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2189 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002190 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002191 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2192 err = sysfs_create_file(&dev->kobj,
2193 it87_attributes_in_beep[i]);
2194 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002195 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002196 }
2197 }
2198
Guenter Roeck4573acb2012-03-26 16:17:41 -07002199 for (i = 0; i < 3; i++) {
2200 if (!(data->has_temp & (1 << i)))
2201 continue;
2202 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002203 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002204 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002205 if (has_temp_offset(data)) {
2206 err = sysfs_create_file(&dev->kobj,
2207 it87_attributes_temp_offset[i]);
2208 if (err)
2209 goto error;
2210 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002211 if (sio_data->beep_pin) {
2212 err = sysfs_create_file(&dev->kobj,
2213 it87_attributes_temp_beep[i]);
2214 if (err)
2215 goto error;
2216 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002217 }
2218
Jean Delvare9060f8b2006-08-28 14:24:17 +02002219 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002220 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01002221 for (i = 0; i < 5; i++) {
2222 if (!(data->has_fan & (1 << i)))
2223 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002224 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002225 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002226 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002227
Guenter Roecke1169ba2012-12-19 22:17:01 +01002228 if (i < 3 && !has_16bit_fans(data)) {
2229 err = sysfs_create_file(&dev->kobj,
2230 it87_attributes_fan_div[i]);
2231 if (err)
2232 goto error;
2233 }
2234
Jean Delvared9b327c2010-03-05 22:17:17 +01002235 if (sio_data->beep_pin) {
2236 err = sysfs_create_file(&dev->kobj,
2237 it87_attributes_fan_beep[i]);
2238 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002239 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002240 if (!fan_beep_need_rw)
2241 continue;
2242
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002243 /*
2244 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002245 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002246 * for it.
2247 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002248 if (sysfs_chmod_file(&dev->kobj,
2249 it87_attributes_fan_beep[i],
2250 S_IRUGO | S_IWUSR))
2251 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2252 i + 1);
2253 fan_beep_need_rw = 0;
2254 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002255 }
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002258 for (i = 0; i < 3; i++) {
2259 if (sio_data->skip_pwm & (1 << i))
2260 continue;
2261 err = sysfs_create_group(&dev->kobj,
2262 &it87_group_pwm[i]);
2263 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002264 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002265
2266 if (!has_old_autopwm(data))
2267 continue;
2268 err = sysfs_create_group(&dev->kobj,
2269 &it87_group_autopwm[i]);
2270 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002271 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274
Jean Delvare895ff262009-12-09 20:35:47 +01002275 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002276 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002277 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002278 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002279 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2280 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002281 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002282 }
2283
Jean Delvare738e5e02010-08-14 21:08:50 +02002284 /* Export labels for internal sensors */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002285 for (i = 0; i < 4; i++) {
Jean Delvare738e5e02010-08-14 21:08:50 +02002286 if (!(sio_data->internal & (1 << i)))
2287 continue;
2288 err = sysfs_create_file(&dev->kobj,
2289 it87_attributes_label[i]);
2290 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002291 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002292 }
2293
Tony Jones1beeffe2007-08-20 13:46:20 -07002294 data->hwmon_dev = hwmon_device_register(dev);
2295 if (IS_ERR(data->hwmon_dev)) {
2296 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002297 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299
2300 return 0;
2301
Guenter Roeck62a1d052012-03-24 21:54:41 -07002302error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002303 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return err;
2305}
2306
Bill Pemberton281dfd02012-11-19 13:25:51 -05002307static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002309 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Tony Jones1beeffe2007-08-20 13:46:20 -07002311 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002312 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return 0;
2315}
2316
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002317/*
2318 * Must be called with data->update_lock held, except during initialization.
2319 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2320 * would slow down the IT87 access and should not be necessary.
2321 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002322static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002324 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2325 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002328/*
2329 * Must be called with data->update_lock held, except during initialization.
2330 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2331 * would slow down the IT87 access and should not be necessary.
2332 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002333static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002335 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2336 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
2339/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002340static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002342 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002343 /*
2344 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002346 * disable pwm control to protect the user.
2347 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002348 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if ((tmp & 0x87) == 0) {
2350 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002351 /*
2352 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002354 * inverting all fan speed values.
2355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 int i;
2357 u8 pwm[3];
2358
2359 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002360 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 IT87_REG_PWM(i));
2362
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002363 /*
2364 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 * might be correct, as suspicious as it seems, so we
2366 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002367 * PWM interface).
2368 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002370 dev_info(dev,
2371 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002372 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 tmp | 0x87);
2374 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002375 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 IT87_REG_PWM(i),
2377 0x7f & ~pwm[i]);
2378 return 1;
2379 }
2380
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002381 dev_info(dev,
2382 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002385 dev_info(dev,
2386 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return 0;
2388 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002389 dev_info(dev,
2390 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
2392
2393 return 1;
2394}
2395
2396/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002397static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002399 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002400 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002402 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002404 /*
2405 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002406 * - If it is in automatic mode, setting to manual mode should set
2407 * the fan to full speed by default.
2408 * - If it is in manual mode, we need a mapping to temperature
2409 * channels to use when later setting to automatic mode later.
2410 * Use a 1:1 mapping by default (we are clueless.)
2411 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002412 * prior to switching to a different mode.
2413 * Note that this is no longer needed for the IT8721F and later, as
2414 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002415 * manual duty cycle.
2416 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002418 data->pwm_temp_map[i] = i;
2419 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002420 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002423 /*
2424 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002425 * registers. For low voltage limits it makes no sense and triggers
2426 * alarms, so change to 0 instead. For high temperature limits, it
2427 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002428 * but is still confusing, so change to 127 degrees C.
2429 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002430 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002431 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002432 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002433 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002434 }
2435 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002436 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002437 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002438 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002439 }
2440
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002441 /*
2442 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002443 * set to two different sensor types and we can't guess which one
2444 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002445 * run-time through the temp{1-3}_type sysfs accessors if needed.
2446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002449 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if ((tmp & 0xff) == 0) {
2451 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002452 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
2454
2455 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002456 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002457 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002458 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002460 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002461 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2462 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002464 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002466 /* Set tachometers to 16-bit mode if needed */
2467 if (has_fan16_config(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002468 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002469 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002470 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002471 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002472 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002473 tmp | 0x07);
2474 }
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002475 }
2476
2477 /* Check for additional fans */
2478 if (has_five_fans(data)) {
2479 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2480 if (tmp & (1 << 4))
2481 data->has_fan |= (1 << 3); /* fan4 enabled */
2482 if (tmp & (1 << 5))
2483 data->has_fan |= (1 << 4); /* fan5 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02002484 }
2485
Jean Delvare591ec652009-12-09 20:35:48 +01002486 /* Fan input pins may be used for alternative functions */
2487 data->has_fan &= ~sio_data->skip_fan;
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002490 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002491 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 | (update_vbat ? 0x41 : 0x01));
2493}
2494
Jean Delvareb99883d2010-03-05 22:17:15 +01002495static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2496{
2497 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002498 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002499 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002500 data->pwm_duty[nr] = it87_read_value(data,
2501 IT87_REG_PWM_DUTY(nr));
2502 } else {
2503 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2504 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2505 else /* Manual mode */
2506 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2507 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002508
2509 if (has_old_autopwm(data)) {
2510 int i;
2511
2512 for (i = 0; i < 5 ; i++)
2513 data->auto_temp[nr][i] = it87_read_value(data,
2514 IT87_REG_AUTO_TEMP(nr, i));
2515 for (i = 0; i < 3 ; i++)
2516 data->auto_pwm[nr][i] = it87_read_value(data,
2517 IT87_REG_AUTO_PWM(nr, i));
2518 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002519}
2520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521static struct it87_data *it87_update_device(struct device *dev)
2522{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002523 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 int i;
2525
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002526 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2529 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002531 /*
2532 * Cleared after each update, so reenable. Value
2533 * returned by this read will be previous value
2534 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002535 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002536 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
2538 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002539 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002540 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002541 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002542 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002543 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002544 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
Jean Delvare3543a532006-08-28 14:27:25 +02002546 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002547 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002548 if (data->type == it8603)
2549 data->in[9][0] = it87_read_value(data, 0x2f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Jean Delvarec7f1f712007-09-03 17:11:46 +02002551 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002552 /* Skip disabled fans */
2553 if (!(data->has_fan & (1 << i)))
2554 continue;
2555
Guenter Roecke1169ba2012-12-19 22:17:01 +01002556 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002557 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002558 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002559 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002560 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002561 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002562 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002563 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002564 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002565 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
2568 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002569 if (!(data->has_temp & (1 << i)))
2570 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002571 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002572 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002573 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002574 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002575 data->temp[i][2] =
2576 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002577 if (has_temp_offset(data))
2578 data->temp[i][3] =
2579 it87_read_value(data,
2580 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582
Jean Delvare17d648b2006-08-28 14:23:46 +02002583 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002584 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002585 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002586 data->fan_div[0] = i & 0x07;
2587 data->fan_div[1] = (i >> 3) & 0x07;
2588 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002592 it87_read_value(data, IT87_REG_ALARM1) |
2593 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2594 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002595 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002596
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002597 data->fan_main_ctrl = it87_read_value(data,
2598 IT87_REG_FAN_MAIN_CTRL);
2599 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002600 for (i = 0; i < 3; i++)
2601 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002603 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002604 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002605 /*
2606 * The IT8705F does not have VID capability.
2607 * The IT8718F and later don't use IT87_REG_VID for the
2608 * same purpose.
2609 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002610 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002611 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002612 /*
2613 * The older IT8712F revisions had only 5 VID pins,
2614 * but we assume it is always safe to read 6 bits.
2615 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002616 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618 data->last_updated = jiffies;
2619 data->valid = 1;
2620 }
2621
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002622 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 return data;
2625}
2626
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002627static int __init it87_device_add(unsigned short address,
2628 const struct it87_sio_data *sio_data)
2629{
2630 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002631 .start = address + IT87_EC_OFFSET,
2632 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002633 .name = DRVNAME,
2634 .flags = IORESOURCE_IO,
2635 };
2636 int err;
2637
Jean Delvareb9acb642009-01-07 16:37:35 +01002638 err = acpi_check_resource_conflict(&res);
2639 if (err)
2640 goto exit;
2641
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002642 pdev = platform_device_alloc(DRVNAME, address);
2643 if (!pdev) {
2644 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002645 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002646 goto exit;
2647 }
2648
2649 err = platform_device_add_resources(pdev, &res, 1);
2650 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002651 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002652 goto exit_device_put;
2653 }
2654
2655 err = platform_device_add_data(pdev, sio_data,
2656 sizeof(struct it87_sio_data));
2657 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002658 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002659 goto exit_device_put;
2660 }
2661
2662 err = platform_device_add(pdev);
2663 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002664 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002665 goto exit_device_put;
2666 }
2667
2668 return 0;
2669
2670exit_device_put:
2671 platform_device_put(pdev);
2672exit:
2673 return err;
2674}
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676static int __init sm_it87_init(void)
2677{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002678 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002679 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002680 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002681
Jean Delvare98dd22c2008-10-09 15:33:58 +02002682 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002683 err = it87_find(&isa_address, &sio_data);
2684 if (err)
2685 return err;
2686 err = platform_driver_register(&it87_driver);
2687 if (err)
2688 return err;
2689
2690 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002691 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002692 platform_driver_unregister(&it87_driver);
2693 return err;
2694 }
2695
2696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
2699static void __exit sm_it87_exit(void)
2700{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002701 platform_device_unregister(pdev);
2702 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
2705
Jean Delvare7c81c602014-01-29 20:40:08 +01002706MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002707MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708module_param(update_vbat, bool, 0);
2709MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2710module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002711MODULE_PARM_DESC(fix_pwm_polarity,
2712 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713MODULE_LICENSE("GPL");
2714
2715module_init(sm_it87_init);
2716module_exit(sm_it87_exit);