blob: 146f93584cdea764be582489d2e0356f3a8017ac [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
Guenter Roeck3ba9d972015-02-13 20:13:20 -080014 * IT8620E Super I/O chip w/LPC interface
Rudolf Marek574e9bd2014-04-04 18:01:35 +020015 * IT8623E Super I/O chip w/LPC interface
Rudolf Marekc145d5c2014-01-29 20:40:08 +010016 * IT8705F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010017 * IT8712F Super I/O chip w/LPC interface
18 * IT8716F Super I/O chip w/LPC interface
19 * IT8718F Super I/O chip w/LPC interface
20 * IT8720F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020021 * IT8721F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010022 * IT8726F Super I/O chip w/LPC interface
Jean Delvare16b5dda2012-01-16 22:51:48 +010023 * IT8728F Super I/O chip w/LPC interface
Justin Maggardead80802015-08-05 12:53:08 -070024 * IT8732F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020025 * IT8758E Super I/O chip w/LPC interface
Guenter Roeckb0636702012-09-07 17:34:41 -060026 * IT8771E Super I/O chip w/LPC interface
27 * IT8772E Super I/O chip w/LPC interface
Guenter Roeck7bc32d22015-01-17 14:10:24 -080028 * IT8781F Super I/O chip w/LPC interface
Guenter Roeck0531d982012-03-02 11:46:44 -080029 * IT8782F Super I/O chip w/LPC interface
30 * IT8783E/F Super I/O chip w/LPC interface
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +010031 * IT8786E Super I/O chip w/LPC interface
Guenter Roeck4ee07152015-03-25 23:26:28 -070032 * IT8790E Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010033 * Sis950 A clone of the IT8705F
34 *
35 * Copyright (C) 2001 Chris Gauthron
Jean Delvare7c81c602014-01-29 20:40:08 +010036 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
Jean Delvare5f2dc792010-03-05 22:17:18 +010037 *
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version.
42 *
43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
47 *
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
51 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Joe Perchesa8ca1032011-01-12 21:55:10 +010053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/module.h>
56#include <linux/init.h>
57#include <linux/slab.h>
58#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020059#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040060#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020061#include <linux/hwmon-sysfs.h>
62#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040063#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010064#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020065#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020066#include <linux/string.h>
67#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010068#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020069#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
corentin.labbeb74f3fd2007-06-13 20:27:36 +020071#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Justin Maggardead80802015-08-05 12:53:08 -070073enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
74 it8771, it8772, it8781, it8782, it8783, it8786, it8790, it8603,
75 it8620 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jean Delvare67b671b2007-12-06 23:13:42 +010077static unsigned short force_id;
78module_param(force_id, ushort, 0);
79MODULE_PARM_DESC(force_id, "Override the detected device ID");
80
corentin.labbeb74f3fd2007-06-13 20:27:36 +020081static struct platform_device *pdev;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define REG 0x2e /* The register to read/write */
84#define DEV 0x07 /* Register: Logical device select */
85#define VAL 0x2f /* The value to read/write */
86#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010087
88/* The device with the IT8718F/IT8720F VID value in it */
89#define GPIO 0x07
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define DEVID 0x20 /* Register: Device ID */
92#define DEVREV 0x22 /* Register: Device Revision */
93
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020094static inline int superio_inb(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 outb(reg, REG);
97 return inb(VAL);
98}
99
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200100static inline void superio_outb(int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +0200101{
102 outb(reg, REG);
103 outb(val, VAL);
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static int superio_inw(int reg)
107{
108 int val;
109 outb(reg++, REG);
110 val = inb(VAL) << 8;
111 outb(reg, REG);
112 val |= inb(VAL);
113 return val;
114}
115
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200116static inline void superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200119 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200122static inline int superio_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200124 /*
125 * Try to reserve REG and REG + 1 for exclusive access.
126 */
127 if (!request_muxed_region(REG, 2, DRVNAME))
128 return -EBUSY;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 outb(0x87, REG);
131 outb(0x01, REG);
132 outb(0x55, REG);
133 outb(0x55, REG);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200137static inline void superio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 outb(0x02, REG);
140 outb(0x02, VAL);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200141 release_region(REG, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Jean Delvare87673dd2006-08-28 14:37:19 +0200144/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define IT8712F_DEVID 0x8712
146#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200147#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200148#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100149#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200150#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400151#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100152#define IT8728F_DEVID 0x8728
Justin Maggardead80802015-08-05 12:53:08 -0700153#define IT8732F_DEVID 0x8732
Guenter Roeckb0636702012-09-07 17:34:41 -0600154#define IT8771E_DEVID 0x8771
155#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800156#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800157#define IT8782F_DEVID 0x8782
158#define IT8783E_DEVID 0x8783
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100159#define IT8786E_DEVID 0x8786
Guenter Roeck4ee07152015-03-25 23:26:28 -0700160#define IT8790E_DEVID 0x8790
Rudolf Marek7183ae82014-04-04 18:01:35 +0200161#define IT8603E_DEVID 0x8603
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800162#define IT8620E_DEVID 0x8620
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200163#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define IT87_ACT_REG 0x30
165#define IT87_BASE_REG 0x60
166
Jean Delvare87673dd2006-08-28 14:37:19 +0200167/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800168#define IT87_SIO_GPIO1_REG 0x25
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800169#define IT87_SIO_GPIO2_REG 0x26
Jean Delvare895ff262009-12-09 20:35:47 +0100170#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100171#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800172#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200173#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800174#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200175#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100176#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030179static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030182static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/* Many IT87 constants specified below */
185
186/* Length of ISA address segment */
187#define IT87_EXTENT 8
188
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500189/* Length of ISA address segment for Environmental Controller */
190#define IT87_EC_EXTENT 2
191
192/* Offset of EC registers from ISA base address */
193#define IT87_EC_OFFSET 5
194
195/* Where are the ISA address/data registers relative to the EC base address */
196#define IT87_ADDR_REG_OFFSET 0
197#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199/*----- The IT87 registers -----*/
200
201#define IT87_REG_CONFIG 0x00
202
203#define IT87_REG_ALARM1 0x01
204#define IT87_REG_ALARM2 0x02
205#define IT87_REG_ALARM3 0x03
206
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800207/*
208 * The IT8718F and IT8720F have the VID value in a different register, in
209 * Super-I/O configuration space.
210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800212/*
213 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
214 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
215 * mode.
216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200218#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
221
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700222static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
223static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
224static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
225static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
226static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228#define IT87_REG_FAN_MAIN_CTRL 0x13
229#define IT87_REG_FAN_CTL 0x14
230#define IT87_REG_PWM(nr) (0x15 + (nr))
Jean Delvare6229cdb2010-12-08 16:27:22 +0100231#define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233#define IT87_REG_VIN(nr) (0x20 + (nr))
234#define IT87_REG_TEMP(nr) (0x29 + (nr))
235
Guenter Roeck73055402015-03-26 09:16:32 -0700236#define IT87_REG_AVCC3 0x2f
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
239#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
240#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
241#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#define IT87_REG_VIN_ENABLE 0x50
244#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700245#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100246#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248#define IT87_REG_CHIPID 0x58
249
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100250#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
251#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
252
Guenter Roeck483db432012-12-19 22:17:02 +0100253struct it87_devices {
254 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700255 const char * const suffix;
Guenter Roeck483db432012-12-19 22:17:02 +0100256 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100257 u8 peci_mask;
258 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100259};
260
261#define FEAT_12MV_ADC (1 << 0)
262#define FEAT_NEWER_AUTOPWM (1 << 1)
263#define FEAT_OLD_AUTOPWM (1 << 2)
264#define FEAT_16BIT_FANS (1 << 3)
265#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100266#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100267#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800268#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
269#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800270#define FEAT_VID (1 << 9) /* Set if chip supports VID */
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700271#define FEAT_IN7_INTERNAL (1 << 10) /* Set if in7 is internal */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700272#define FEAT_SIX_FANS (1 << 11) /* Supports six fans */
Justin Maggardead80802015-08-05 12:53:08 -0700273#define FEAT_10_9MV_ADC (1 << 12)
Guenter Roeck73055402015-03-26 09:16:32 -0700274#define FEAT_AVCC3 (1 << 13) /* Chip supports in9/AVCC3 */
Guenter Roeck483db432012-12-19 22:17:02 +0100275
276static const struct it87_devices it87_devices[] = {
277 [it87] = {
278 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700279 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100280 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
281 },
282 [it8712] = {
283 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700284 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800285 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
286 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100287 },
288 [it8716] = {
289 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700290 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800291 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800292 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck483db432012-12-19 22:17:02 +0100293 },
294 [it8718] = {
295 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700296 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800297 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800298 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100299 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100300 },
301 [it8720] = {
302 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700303 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800304 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800305 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100306 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100307 },
308 [it8721] = {
309 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700310 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100311 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800312 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700313 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100314 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100315 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100316 },
317 [it8728] = {
318 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700319 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100320 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700321 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
322 | FEAT_IN7_INTERNAL,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100323 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100324 },
Justin Maggardead80802015-08-05 12:53:08 -0700325 [it8732] = {
326 .name = "it8732",
327 .suffix = "F",
328 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
329 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
330 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
331 .peci_mask = 0x07,
332 .old_peci_mask = 0x02, /* Actually reports PCH */
333 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600334 [it8771] = {
335 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700336 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600337 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700338 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800339 /* PECI: guesswork */
340 /* 12mV ADC (OHM) */
341 /* 16 bit fans (OHM) */
342 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600343 .peci_mask = 0x07,
344 },
345 [it8772] = {
346 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700347 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600348 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700349 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800350 /* PECI (coreboot) */
351 /* 12mV ADC (HWSensors4, OHM) */
352 /* 16 bit fans (HWSensors4, OHM) */
353 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600354 .peci_mask = 0x07,
355 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800356 [it8781] = {
357 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700358 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800359 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800360 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800361 .old_peci_mask = 0x4,
362 },
Guenter Roeck483db432012-12-19 22:17:02 +0100363 [it8782] = {
364 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700365 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100366 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800367 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100368 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100369 },
370 [it8783] = {
371 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700372 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100373 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800374 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100375 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100376 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100377 [it8786] = {
378 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700379 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100380 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700381 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100382 .peci_mask = 0x07,
383 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700384 [it8790] = {
385 .name = "it8790",
386 .suffix = "E",
387 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
388 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
389 .peci_mask = 0x07,
390 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100391 [it8603] = {
392 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700393 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100394 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck73055402015-03-26 09:16:32 -0700395 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
396 | FEAT_AVCC3,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100397 .peci_mask = 0x07,
398 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800399 [it8620] = {
400 .name = "it8620",
401 .suffix = "E",
402 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700403 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800404 | FEAT_IN7_INTERNAL,
405 .peci_mask = 0x07,
406 },
Guenter Roeck483db432012-12-19 22:17:02 +0100407};
408
409#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
410#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
Justin Maggardead80802015-08-05 12:53:08 -0700411#define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
Guenter Roeck483db432012-12-19 22:17:02 +0100412#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
413#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
414#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100415#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
416 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100417#define has_temp_old_peci(data, nr) \
418 (((data)->features & FEAT_TEMP_OLD_PECI) && \
419 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800420#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700421#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
422 FEAT_SIX_FANS))
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800423#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700424#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700425#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Guenter Roeck73055402015-03-26 09:16:32 -0700426#define has_avcc3(data) ((data)->features & FEAT_AVCC3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200428struct it87_sio_data {
429 enum chips type;
430 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200431 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200432 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100433 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200434 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100435 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700436 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100437 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100438 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200439 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700440 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200441};
442
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800443/*
444 * For each registered chip, we need to keep some data in memory.
445 * The structure is dynamically allocated.
446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700448 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100450 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100451 u8 peci_mask;
452 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200454 unsigned short addr;
455 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100456 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 char valid; /* !=0 if following fields are valid */
458 unsigned long last_updated; /* In jiffies */
459
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200460 u16 in_scaled; /* Internal voltage sensors are scaled */
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100461 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200462 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700463 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700464 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck161d8982012-12-19 22:17:01 +0100465 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100466 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
467 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 u8 fan_div[3]; /* Register encoding, shifted right */
469 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100470 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100472 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100474 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100475
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800476 /*
477 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100478 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
479 * 7, and we want to preserve settings on mode changes, so we have
480 * to track all values separately.
481 * Starting with the IT8721F, the manual PWM duty cycles are stored
482 * in separate registers (8-bit values), so the separate tracking
483 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800484 * simple.
485 */
Jean Delvareb99883d2010-03-05 22:17:15 +0100486 u8 pwm_ctrl[3]; /* Register value */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100487 u8 pwm_duty[3]; /* Manual PWM value set by user */
Jean Delvareb99883d2010-03-05 22:17:15 +0100488 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100489
490 /* Automatic fan speed control registers */
491 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
492 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493};
494
Guenter Roeck0531d982012-03-02 11:46:44 -0800495static int adc_lsb(const struct it87_data *data, int nr)
496{
Justin Maggardead80802015-08-05 12:53:08 -0700497 int lsb;
498
499 if (has_12mv_adc(data))
500 lsb = 120;
501 else if (has_10_9mv_adc(data))
502 lsb = 109;
503 else
504 lsb = 160;
Guenter Roeck0531d982012-03-02 11:46:44 -0800505 if (data->in_scaled & (1 << nr))
506 lsb <<= 1;
507 return lsb;
508}
509
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200510static u8 in_to_reg(const struct it87_data *data, int nr, long val)
511{
Justin Maggardead80802015-08-05 12:53:08 -0700512 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800513 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200514}
515
516static int in_from_reg(const struct it87_data *data, int nr, int val)
517{
Justin Maggardead80802015-08-05 12:53:08 -0700518 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200519}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200520
521static inline u8 FAN_TO_REG(long rpm, int div)
522{
523 if (rpm == 0)
524 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800525 rpm = clamp_val(rpm, 1, 1000000);
526 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200527}
528
529static inline u16 FAN16_TO_REG(long rpm)
530{
531 if (rpm == 0)
532 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800533 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200534}
535
536#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
537 1350000 / ((val) * (div)))
538/* The divider is fixed to 2 in 16-bit mode */
539#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
540 1350000 / ((val) * 2))
541
Guenter Roeck2a844c12013-01-09 08:09:34 -0800542#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
543 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200544#define TEMP_FROM_REG(val) ((val) * 1000)
545
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200546static u8 pwm_to_reg(const struct it87_data *data, long val)
547{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100548 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200549 return val;
550 else
551 return val >> 1;
552}
553
554static int pwm_from_reg(const struct it87_data *data, u8 reg)
555{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100556 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200557 return reg;
558 else
559 return (reg & 0x7f) << 1;
560}
561
Jean Delvare0df6454d2010-10-28 20:31:51 +0200562
563static int DIV_TO_REG(int val)
564{
565 int answer = 0;
566 while (answer < 7 && (val >>= 1))
567 answer++;
568 return answer;
569}
570#define DIV_FROM_REG(val) (1 << (val))
571
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700572/*
573 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
574 * depending on the chip type, to calculate the actual PWM frequency.
575 *
576 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
577 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
578 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
579 * sometimes just one. It is unknown if this is a datasheet error or real,
580 * so this is ignored for now.
581 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200582static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700583 48000000,
584 24000000,
585 12000000,
586 8000000,
587 6000000,
588 3000000,
589 1500000,
590 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200591};
592
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200593static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500594static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200596static int it87_read_value(struct it87_data *data, u8 reg);
597static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200599static int it87_check_pwm(struct device *dev);
600static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200603static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100604 .driver = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200605 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100606 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200607 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500608 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200609};
610
Jean Delvare20ad93d2005-06-05 11:53:25 +0200611static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100612 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100614 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
615 int nr = sattr->nr;
616 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100619 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Guenter Roeck929c6a52012-12-19 22:17:00 +0100622static ssize_t set_in(struct device *dev, struct device_attribute *attr,
623 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100625 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
626 int nr = sattr->nr;
627 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200628
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200629 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100630 unsigned long val;
631
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100632 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100633 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100635 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100636 data->in[nr][index] = in_to_reg(data, nr, val);
637 it87_write_value(data,
638 index == 1 ? IT87_REG_VIN_MIN(nr)
639 : IT87_REG_VIN_MAX(nr),
640 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100641 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return count;
643}
644
Guenter Roeck929c6a52012-12-19 22:17:00 +0100645static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
646static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
647 0, 1);
648static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
649 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Guenter Roeck929c6a52012-12-19 22:17:00 +0100651static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
652static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
653 1, 1);
654static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
655 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Guenter Roeck929c6a52012-12-19 22:17:00 +0100657static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
658static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
659 2, 1);
660static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
661 2, 2);
662
663static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
664static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
665 3, 1);
666static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
667 3, 2);
668
669static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
670static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
671 4, 1);
672static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
673 4, 2);
674
675static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
676static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
677 5, 1);
678static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
679 5, 2);
680
681static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
682static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
683 6, 1);
684static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
685 6, 2);
686
687static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
688static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
689 7, 1);
690static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
691 7, 2);
692
693static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100694static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200697static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100698 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100700 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
701 int nr = sattr->nr;
702 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200704
Guenter Roeck60ca3852012-12-19 22:17:00 +0100705 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200707
Guenter Roeck60ca3852012-12-19 22:17:00 +0100708static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
709 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100711 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
712 int nr = sattr->nr;
713 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200714 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100715 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100716 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100717
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100718 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100719 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100721 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100722
723 switch (index) {
724 default:
725 case 1:
726 reg = IT87_REG_TEMP_LOW(nr);
727 break;
728 case 2:
729 reg = IT87_REG_TEMP_HIGH(nr);
730 break;
731 case 3:
732 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
733 if (!(regval & 0x80)) {
734 regval |= 0x80;
735 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
736 }
737 data->valid = 0;
738 reg = IT87_REG_TEMP_OFFSET[nr];
739 break;
740 }
741
Guenter Roeck60ca3852012-12-19 22:17:00 +0100742 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100743 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100744 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return count;
746}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200747
Guenter Roeck60ca3852012-12-19 22:17:00 +0100748static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
749static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
750 0, 1);
751static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
752 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100753static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
754 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100755static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
756static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
757 1, 1);
758static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
759 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100760static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
761 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100762static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
763static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
764 2, 1);
765static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
766 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100767static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
768 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Guenter Roeck2cece012012-12-19 22:17:01 +0100770static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
771 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200773 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
774 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800776 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100777 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100778
Guenter Roeck19529782012-12-19 22:17:02 +0100779 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
780 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100781 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (reg & (1 << nr))
783 return sprintf(buf, "3\n"); /* thermal diode */
784 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200785 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return sprintf(buf, "0\n"); /* disabled */
787}
Guenter Roeck2cece012012-12-19 22:17:01 +0100788
789static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
790 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200792 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
793 int nr = sensor_attr->index;
794
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200795 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100796 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100797 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100798
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100799 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100800 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Jean Delvare8acf07c2010-04-14 16:14:09 +0200802 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
803 reg &= ~(1 << nr);
804 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100805 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
806 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100807 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
808 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
809 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200810 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100811 dev_warn(dev,
812 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200813 val = 4;
814 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100815 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200817 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200818 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200819 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100820 else if (has_temp_peci(data, nr) && val == 6)
821 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100822 else if (has_temp_old_peci(data, nr) && val == 6)
823 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200824 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200826
827 mutex_lock(&data->update_lock);
828 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100829 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200830 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100831 if (has_temp_old_peci(data, nr))
832 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200833 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100834 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return count;
836}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Guenter Roeck2cece012012-12-19 22:17:01 +0100838static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
839 set_temp_type, 0);
840static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
841 set_temp_type, 1);
842static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
843 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100846
847static int pwm_mode(const struct it87_data *data, int nr)
848{
849 int ctrl = data->fan_main_ctrl & (1 << nr);
850
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100851 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +0100852 return 0;
853 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
854 return 2;
855 else /* Manual mode */
856 return 1;
857}
858
Jean Delvare20ad93d2005-06-05 11:53:25 +0200859static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +0100860 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100862 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
863 int nr = sattr->nr;
864 int index = sattr->index;
865 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200867
Guenter Roecke1169ba2012-12-19 22:17:01 +0100868 speed = has_16bit_fans(data) ?
869 FAN16_FROM_REG(data->fan[nr][index]) :
870 FAN_FROM_REG(data->fan[nr][index],
871 DIV_FROM_REG(data->fan_div[nr]));
872 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100874
Jean Delvare20ad93d2005-06-05 11:53:25 +0200875static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
876 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200878 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
879 int nr = sensor_attr->index;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct it87_data *data = it87_update_device(dev);
882 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
883}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100884static ssize_t show_pwm_enable(struct device *dev,
885 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200887 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
888 int nr = sensor_attr->index;
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100891 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200893static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
894 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200896 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
897 int nr = sensor_attr->index;
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200900 return sprintf(buf, "%d\n",
901 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100903static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
904 char *buf)
905{
906 struct it87_data *data = it87_update_device(dev);
907 int index = (data->fan_ctl >> 4) & 0x07;
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700908 unsigned int freq;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100909
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700910 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
911
912 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100913}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100914
915static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
916 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100918 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
919 int nr = sattr->nr;
920 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200921
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200922 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100923 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100924 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100926 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100927 return -EINVAL;
928
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100929 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +0100930
931 if (has_16bit_fans(data)) {
932 data->fan[nr][index] = FAN16_TO_REG(val);
933 it87_write_value(data, IT87_REG_FAN_MIN[nr],
934 data->fan[nr][index] & 0xff);
935 it87_write_value(data, IT87_REG_FANX_MIN[nr],
936 data->fan[nr][index] >> 8);
937 } else {
938 reg = it87_read_value(data, IT87_REG_FAN_DIV);
939 switch (nr) {
940 case 0:
941 data->fan_div[nr] = reg & 0x07;
942 break;
943 case 1:
944 data->fan_div[nr] = (reg >> 3) & 0x07;
945 break;
946 case 2:
947 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
948 break;
949 }
950 data->fan[nr][index] =
951 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
952 it87_write_value(data, IT87_REG_FAN_MIN[nr],
953 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -0800954 }
955
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100956 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return count;
958}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100959
Jean Delvare20ad93d2005-06-05 11:53:25 +0200960static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
961 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200963 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
964 int nr = sensor_attr->index;
965
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200966 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100967 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200968 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 u8 old;
970
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100971 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100972 return -EINVAL;
973
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100974 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200975 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200977 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100978 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 switch (nr) {
981 case 0:
982 case 1:
983 data->fan_div[nr] = DIV_TO_REG(val);
984 break;
985 case 2:
986 if (val < 8)
987 data->fan_div[nr] = 1;
988 else
989 data->fan_div[nr] = 3;
990 }
991 val = old & 0x80;
992 val |= (data->fan_div[0] & 0x07);
993 val |= (data->fan_div[1] & 0x07) << 3;
994 if (data->fan_div[2] == 3)
995 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200996 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200998 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100999 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1000 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001001
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001002 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return count;
1004}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001005
1006/* Returns 0 if OK, -EINVAL otherwise */
1007static int check_trip_points(struct device *dev, int nr)
1008{
1009 const struct it87_data *data = dev_get_drvdata(dev);
1010 int i, err = 0;
1011
1012 if (has_old_autopwm(data)) {
1013 for (i = 0; i < 3; i++) {
1014 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1015 err = -EINVAL;
1016 }
1017 for (i = 0; i < 2; i++) {
1018 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1019 err = -EINVAL;
1020 }
1021 }
1022
1023 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001024 dev_err(dev,
1025 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001026 dev_err(dev, "Adjust the trip points and try again\n");
1027 }
1028 return err;
1029}
1030
Jean Delvare20ad93d2005-06-05 11:53:25 +02001031static ssize_t set_pwm_enable(struct device *dev,
1032 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001034 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1035 int nr = sensor_attr->index;
1036
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001037 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001038 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001040 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001041 return -EINVAL;
1042
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001043 /* Check trip points before switching to automatic mode */
1044 if (val == 2) {
1045 if (check_trip_points(dev, nr) < 0)
1046 return -EINVAL;
1047 }
1048
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001049 /* IT8603E does not have on/off mode */
1050 if (val == 0 && data->type == it8603)
1051 return -EINVAL;
1052
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001053 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 if (val == 0) {
1056 int tmp;
1057 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001058 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1059 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /* set on/off mode */
1061 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +01001062 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1063 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +01001064 } else {
1065 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001066 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001067 data->pwm_temp_map[nr] :
1068 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001069 else /* Automatic mode */
1070 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1071 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001072
1073 if (data->type != it8603) {
1074 /* set SmartGuardian mode */
1075 data->fan_main_ctrl |= (1 << nr);
1076 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1077 data->fan_main_ctrl);
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001081 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return count;
1083}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001084static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1085 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001087 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1088 int nr = sensor_attr->index;
1089
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001090 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001091 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001093 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return -EINVAL;
1095
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001096 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001097 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001098 /*
1099 * If we are in automatic mode, the PWM duty cycle register
1100 * is read-only so we can't write the value.
1101 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001102 if (data->pwm_ctrl[nr] & 0x80) {
1103 mutex_unlock(&data->update_lock);
1104 return -EBUSY;
1105 }
1106 data->pwm_duty[nr] = pwm_to_reg(data, val);
1107 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
1108 data->pwm_duty[nr]);
1109 } else {
1110 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001111 /*
1112 * If we are in manual mode, write the duty cycle immediately;
1113 * otherwise, just store it for later use.
1114 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001115 if (!(data->pwm_ctrl[nr] & 0x80)) {
1116 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1117 it87_write_value(data, IT87_REG_PWM(nr),
1118 data->pwm_ctrl[nr]);
1119 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001120 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001121 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return count;
1123}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001124static ssize_t set_pwm_freq(struct device *dev,
1125 struct device_attribute *attr, const char *buf, size_t count)
1126{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001127 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001128 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001129 int i;
1130
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001131 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001132 return -EINVAL;
1133
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001134 val = clamp_val(val, 0, 1000000);
1135 val *= has_newer_autopwm(data) ? 256 : 128;
1136
Jean Delvaref8d0c192007-02-14 21:15:02 +01001137 /* Search for the nearest available frequency */
1138 for (i = 0; i < 7; i++) {
1139 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1140 break;
1141 }
1142
1143 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001144 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001145 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001146 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001147 mutex_unlock(&data->update_lock);
1148
1149 return count;
1150}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001151static ssize_t show_pwm_temp_map(struct device *dev,
1152 struct device_attribute *attr, char *buf)
1153{
1154 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1155 int nr = sensor_attr->index;
1156
1157 struct it87_data *data = it87_update_device(dev);
1158 int map;
1159
1160 if (data->pwm_temp_map[nr] < 3)
1161 map = 1 << data->pwm_temp_map[nr];
1162 else
1163 map = 0; /* Should never happen */
1164 return sprintf(buf, "%d\n", map);
1165}
1166static ssize_t set_pwm_temp_map(struct device *dev,
1167 struct device_attribute *attr, const char *buf, size_t count)
1168{
1169 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1170 int nr = sensor_attr->index;
1171
1172 struct it87_data *data = dev_get_drvdata(dev);
1173 long val;
1174 u8 reg;
1175
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001176 /*
1177 * This check can go away if we ever support automatic fan speed
1178 * control on newer chips.
1179 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001180 if (!has_old_autopwm(data)) {
1181 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1182 return -EINVAL;
1183 }
1184
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001185 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001186 return -EINVAL;
1187
1188 switch (val) {
1189 case (1 << 0):
1190 reg = 0x00;
1191 break;
1192 case (1 << 1):
1193 reg = 0x01;
1194 break;
1195 case (1 << 2):
1196 reg = 0x02;
1197 break;
1198 default:
1199 return -EINVAL;
1200 }
1201
1202 mutex_lock(&data->update_lock);
1203 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001204 /*
1205 * If we are in automatic mode, write the temp mapping immediately;
1206 * otherwise, just store it for later use.
1207 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001208 if (data->pwm_ctrl[nr] & 0x80) {
1209 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1210 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1211 }
1212 mutex_unlock(&data->update_lock);
1213 return count;
1214}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001216static ssize_t show_auto_pwm(struct device *dev,
1217 struct device_attribute *attr, char *buf)
1218{
1219 struct it87_data *data = it87_update_device(dev);
1220 struct sensor_device_attribute_2 *sensor_attr =
1221 to_sensor_dev_attr_2(attr);
1222 int nr = sensor_attr->nr;
1223 int point = sensor_attr->index;
1224
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001225 return sprintf(buf, "%d\n",
1226 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001227}
1228
1229static ssize_t set_auto_pwm(struct device *dev,
1230 struct device_attribute *attr, const char *buf, size_t count)
1231{
1232 struct it87_data *data = dev_get_drvdata(dev);
1233 struct sensor_device_attribute_2 *sensor_attr =
1234 to_sensor_dev_attr_2(attr);
1235 int nr = sensor_attr->nr;
1236 int point = sensor_attr->index;
1237 long val;
1238
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001239 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001240 return -EINVAL;
1241
1242 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001243 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001244 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1245 data->auto_pwm[nr][point]);
1246 mutex_unlock(&data->update_lock);
1247 return count;
1248}
1249
1250static ssize_t show_auto_temp(struct device *dev,
1251 struct device_attribute *attr, char *buf)
1252{
1253 struct it87_data *data = it87_update_device(dev);
1254 struct sensor_device_attribute_2 *sensor_attr =
1255 to_sensor_dev_attr_2(attr);
1256 int nr = sensor_attr->nr;
1257 int point = sensor_attr->index;
1258
1259 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1260}
1261
1262static ssize_t set_auto_temp(struct device *dev,
1263 struct device_attribute *attr, const char *buf, size_t count)
1264{
1265 struct it87_data *data = dev_get_drvdata(dev);
1266 struct sensor_device_attribute_2 *sensor_attr =
1267 to_sensor_dev_attr_2(attr);
1268 int nr = sensor_attr->nr;
1269 int point = sensor_attr->index;
1270 long val;
1271
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001272 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001273 return -EINVAL;
1274
1275 mutex_lock(&data->update_lock);
1276 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1277 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1278 data->auto_temp[nr][point]);
1279 mutex_unlock(&data->update_lock);
1280 return count;
1281}
1282
Guenter Roecke1169ba2012-12-19 22:17:01 +01001283static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1284static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1285 0, 1);
1286static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1287 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Guenter Roecke1169ba2012-12-19 22:17:01 +01001289static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1290static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1291 1, 1);
1292static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1293 set_fan_div, 1);
1294
1295static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1296static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1297 2, 1);
1298static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1299 set_fan_div, 2);
1300
1301static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1302static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1303 3, 1);
1304
1305static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1306static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1307 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001309static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1310static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1311 5, 1);
1312
Guenter Roeckc4458db2012-12-19 22:17:02 +01001313static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1314 show_pwm_enable, set_pwm_enable, 0);
1315static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1316static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1317static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1318 show_pwm_temp_map, set_pwm_temp_map, 0);
1319static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1320 show_auto_pwm, set_auto_pwm, 0, 0);
1321static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1322 show_auto_pwm, set_auto_pwm, 0, 1);
1323static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1324 show_auto_pwm, set_auto_pwm, 0, 2);
1325static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1326 show_auto_pwm, NULL, 0, 3);
1327static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1328 show_auto_temp, set_auto_temp, 0, 1);
1329static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1330 show_auto_temp, set_auto_temp, 0, 0);
1331static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1332 show_auto_temp, set_auto_temp, 0, 2);
1333static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1334 show_auto_temp, set_auto_temp, 0, 3);
1335static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1336 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Guenter Roeckc4458db2012-12-19 22:17:02 +01001338static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1339 show_pwm_enable, set_pwm_enable, 1);
1340static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1341static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1342static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1343 show_pwm_temp_map, set_pwm_temp_map, 1);
1344static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1345 show_auto_pwm, set_auto_pwm, 1, 0);
1346static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1347 show_auto_pwm, set_auto_pwm, 1, 1);
1348static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1349 show_auto_pwm, set_auto_pwm, 1, 2);
1350static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1351 show_auto_pwm, NULL, 1, 3);
1352static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1353 show_auto_temp, set_auto_temp, 1, 1);
1354static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1355 show_auto_temp, set_auto_temp, 1, 0);
1356static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1357 show_auto_temp, set_auto_temp, 1, 2);
1358static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1359 show_auto_temp, set_auto_temp, 1, 3);
1360static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1361 show_auto_temp, set_auto_temp, 1, 4);
1362
1363static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1364 show_pwm_enable, set_pwm_enable, 2);
1365static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1366static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1367static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1368 show_pwm_temp_map, set_pwm_temp_map, 2);
1369static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1370 show_auto_pwm, set_auto_pwm, 2, 0);
1371static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1372 show_auto_pwm, set_auto_pwm, 2, 1);
1373static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1374 show_auto_pwm, set_auto_pwm, 2, 2);
1375static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1376 show_auto_pwm, NULL, 2, 3);
1377static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1378 show_auto_temp, set_auto_temp, 2, 1);
1379static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1380 show_auto_temp, set_auto_temp, 2, 0);
1381static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1382 show_auto_temp, set_auto_temp, 2, 2);
1383static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1384 show_auto_temp, set_auto_temp, 2, 3);
1385static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1386 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001389static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1390 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001393 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
Jean Delvare1d66c642005-04-18 21:16:59 -07001395static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Jean Delvare0124dd72007-11-25 16:16:41 +01001397static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1398 char *buf)
1399{
1400 int bitnr = to_sensor_dev_attr(attr)->index;
1401 struct it87_data *data = it87_update_device(dev);
1402 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1403}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001404
1405static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1406 *attr, const char *buf, size_t count)
1407{
1408 struct it87_data *data = dev_get_drvdata(dev);
1409 long val;
1410 int config;
1411
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001412 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001413 return -EINVAL;
1414
1415 mutex_lock(&data->update_lock);
1416 config = it87_read_value(data, IT87_REG_CONFIG);
1417 if (config < 0) {
1418 count = config;
1419 } else {
1420 config |= 1 << 5;
1421 it87_write_value(data, IT87_REG_CONFIG, config);
1422 /* Invalidate cache to force re-read */
1423 data->valid = 0;
1424 }
1425 mutex_unlock(&data->update_lock);
1426
1427 return count;
1428}
1429
Jean Delvare0124dd72007-11-25 16:16:41 +01001430static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1431static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1432static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1433static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1434static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1435static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1436static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1437static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1438static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1439static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1440static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1441static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1442static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001443static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001444static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1445static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1446static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001447static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1448 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001449
Jean Delvared9b327c2010-03-05 22:17:17 +01001450static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1451 char *buf)
1452{
1453 int bitnr = to_sensor_dev_attr(attr)->index;
1454 struct it87_data *data = it87_update_device(dev);
1455 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1456}
1457static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1458 const char *buf, size_t count)
1459{
1460 int bitnr = to_sensor_dev_attr(attr)->index;
1461 struct it87_data *data = dev_get_drvdata(dev);
1462 long val;
1463
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001464 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001465 || (val != 0 && val != 1))
1466 return -EINVAL;
1467
1468 mutex_lock(&data->update_lock);
1469 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1470 if (val)
1471 data->beeps |= (1 << bitnr);
1472 else
1473 data->beeps &= ~(1 << bitnr);
1474 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1475 mutex_unlock(&data->update_lock);
1476 return count;
1477}
1478
1479static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1480 show_beep, set_beep, 1);
1481static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1482static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1483static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1484static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1485static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1486static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1487static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1488/* fanX_beep writability is set later */
1489static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1490static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1491static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1492static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1493static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001494static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001495static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1496 show_beep, set_beep, 2);
1497static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1498static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1499
Jean Delvare5f2dc792010-03-05 22:17:18 +01001500static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1501 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Jean Delvare90d66192007-10-08 18:24:35 +02001503 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001504 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001506static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1507 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001509 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001510 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001512 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001513 return -EINVAL;
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 data->vrm = val;
1516
1517 return count;
1518}
1519static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Jean Delvare5f2dc792010-03-05 22:17:18 +01001521static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1522 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 struct it87_data *data = it87_update_device(dev);
1525 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1526}
1527static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001528
Jean Delvare738e5e02010-08-14 21:08:50 +02001529static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1530 char *buf)
1531{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001532 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001533 "+5V",
1534 "5VSB",
1535 "Vbat",
1536 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001537 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001538 "+3.3V",
1539 "3VSB",
1540 "Vbat",
1541 };
1542 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001543 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001544 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001545
Justin Maggardead80802015-08-05 12:53:08 -07001546 if (has_12mv_adc(data) || has_10_9mv_adc(data))
1547 label = labels_it8721[nr];
1548 else
1549 label = labels[nr];
1550
1551 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001552}
1553static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1554static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1555static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001556/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001557static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001558
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001559static ssize_t show_name(struct device *dev, struct device_attribute
1560 *devattr, char *buf)
1561{
1562 struct it87_data *data = dev_get_drvdata(dev);
1563 return sprintf(buf, "%s\n", data->name);
1564}
1565static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1566
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001567static struct attribute *it87_attributes_in[10][5] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001568{
Jean Delvare87808be2006-09-24 21:17:13 +02001569 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001570 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001571 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001572 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001573 NULL
1574}, {
1575 &sensor_dev_attr_in1_input.dev_attr.attr,
1576 &sensor_dev_attr_in1_min.dev_attr.attr,
1577 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001578 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001579 NULL
1580}, {
1581 &sensor_dev_attr_in2_input.dev_attr.attr,
1582 &sensor_dev_attr_in2_min.dev_attr.attr,
1583 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001584 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001585 NULL
1586}, {
1587 &sensor_dev_attr_in3_input.dev_attr.attr,
1588 &sensor_dev_attr_in3_min.dev_attr.attr,
1589 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001590 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001591 NULL
1592}, {
1593 &sensor_dev_attr_in4_input.dev_attr.attr,
1594 &sensor_dev_attr_in4_min.dev_attr.attr,
1595 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001596 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001597 NULL
1598}, {
1599 &sensor_dev_attr_in5_input.dev_attr.attr,
1600 &sensor_dev_attr_in5_min.dev_attr.attr,
1601 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001602 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001603 NULL
1604}, {
1605 &sensor_dev_attr_in6_input.dev_attr.attr,
1606 &sensor_dev_attr_in6_min.dev_attr.attr,
1607 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001608 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001609 NULL
1610}, {
1611 &sensor_dev_attr_in7_input.dev_attr.attr,
1612 &sensor_dev_attr_in7_min.dev_attr.attr,
1613 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001614 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001615 NULL
1616}, {
1617 &sensor_dev_attr_in8_input.dev_attr.attr,
1618 NULL
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001619}, {
1620 &sensor_dev_attr_in9_input.dev_attr.attr,
1621 NULL
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001622} };
Jean Delvare87808be2006-09-24 21:17:13 +02001623
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001624static const struct attribute_group it87_group_in[10] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001625 { .attrs = it87_attributes_in[0] },
1626 { .attrs = it87_attributes_in[1] },
1627 { .attrs = it87_attributes_in[2] },
1628 { .attrs = it87_attributes_in[3] },
1629 { .attrs = it87_attributes_in[4] },
1630 { .attrs = it87_attributes_in[5] },
1631 { .attrs = it87_attributes_in[6] },
1632 { .attrs = it87_attributes_in[7] },
1633 { .attrs = it87_attributes_in[8] },
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001634 { .attrs = it87_attributes_in[9] },
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001635};
1636
Guenter Roeck4573acb2012-03-26 16:17:41 -07001637static struct attribute *it87_attributes_temp[3][6] = {
1638{
Jean Delvare87808be2006-09-24 21:17:13 +02001639 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001640 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001641 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001642 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001643 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001644 NULL
1645} , {
1646 &sensor_dev_attr_temp2_input.dev_attr.attr,
1647 &sensor_dev_attr_temp2_max.dev_attr.attr,
1648 &sensor_dev_attr_temp2_min.dev_attr.attr,
1649 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001650 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001651 NULL
1652} , {
1653 &sensor_dev_attr_temp3_input.dev_attr.attr,
1654 &sensor_dev_attr_temp3_max.dev_attr.attr,
1655 &sensor_dev_attr_temp3_min.dev_attr.attr,
1656 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001657 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001658 NULL
1659} };
Jean Delvare87808be2006-09-24 21:17:13 +02001660
Guenter Roeck4573acb2012-03-26 16:17:41 -07001661static const struct attribute_group it87_group_temp[3] = {
1662 { .attrs = it87_attributes_temp[0] },
1663 { .attrs = it87_attributes_temp[1] },
1664 { .attrs = it87_attributes_temp[2] },
1665};
1666
Guenter Roeck161d8982012-12-19 22:17:01 +01001667static struct attribute *it87_attributes_temp_offset[] = {
1668 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1669 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1670 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1671};
1672
Guenter Roeck4573acb2012-03-26 16:17:41 -07001673static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001674 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001675 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001676 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001677 NULL
1678};
1679
1680static const struct attribute_group it87_group = {
1681 .attrs = it87_attributes,
1682};
1683
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001684static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001685 &sensor_dev_attr_in0_beep.dev_attr.attr,
1686 &sensor_dev_attr_in1_beep.dev_attr.attr,
1687 &sensor_dev_attr_in2_beep.dev_attr.attr,
1688 &sensor_dev_attr_in3_beep.dev_attr.attr,
1689 &sensor_dev_attr_in4_beep.dev_attr.attr,
1690 &sensor_dev_attr_in5_beep.dev_attr.attr,
1691 &sensor_dev_attr_in6_beep.dev_attr.attr,
1692 &sensor_dev_attr_in7_beep.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001693 NULL,
1694 NULL,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001695};
Jean Delvared9b327c2010-03-05 22:17:17 +01001696
Guenter Roeck4573acb2012-03-26 16:17:41 -07001697static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001698 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1699 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1700 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001701};
1702
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001703static struct attribute *it87_attributes_fan[6][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001704 &sensor_dev_attr_fan1_input.dev_attr.attr,
1705 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001706 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1707 NULL
1708}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001709 &sensor_dev_attr_fan2_input.dev_attr.attr,
1710 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001711 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1712 NULL
1713}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001714 &sensor_dev_attr_fan3_input.dev_attr.attr,
1715 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001716 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001717 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001718}, {
1719 &sensor_dev_attr_fan4_input.dev_attr.attr,
1720 &sensor_dev_attr_fan4_min.dev_attr.attr,
1721 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1722 NULL
1723}, {
1724 &sensor_dev_attr_fan5_input.dev_attr.attr,
1725 &sensor_dev_attr_fan5_min.dev_attr.attr,
1726 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1727 NULL
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001728}, {
1729 &sensor_dev_attr_fan6_input.dev_attr.attr,
1730 &sensor_dev_attr_fan6_min.dev_attr.attr,
1731 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1732 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001733} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001734
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001735static const struct attribute_group it87_group_fan[6] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001736 { .attrs = it87_attributes_fan[0] },
1737 { .attrs = it87_attributes_fan[1] },
1738 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001739 { .attrs = it87_attributes_fan[3] },
1740 { .attrs = it87_attributes_fan[4] },
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001741 { .attrs = it87_attributes_fan[5] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001742};
1743
Guenter Roecke1169ba2012-12-19 22:17:01 +01001744static const struct attribute *it87_attributes_fan_div[] = {
1745 &sensor_dev_attr_fan1_div.dev_attr.attr,
1746 &sensor_dev_attr_fan2_div.dev_attr.attr,
1747 &sensor_dev_attr_fan3_div.dev_attr.attr,
1748};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001749
1750static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001751 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001752 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001753 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001754 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001755 NULL
1756}, {
1757 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1758 &sensor_dev_attr_pwm2.dev_attr.attr,
1759 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001760 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001761 NULL
1762}, {
1763 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1764 &sensor_dev_attr_pwm3.dev_attr.attr,
1765 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001766 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001767 NULL
1768} };
Jean Delvare87808be2006-09-24 21:17:13 +02001769
Jean Delvare723a0aa2010-03-05 22:17:16 +01001770static const struct attribute_group it87_group_pwm[3] = {
1771 { .attrs = it87_attributes_pwm[0] },
1772 { .attrs = it87_attributes_pwm[1] },
1773 { .attrs = it87_attributes_pwm[2] },
1774};
1775
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001776static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1777 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1778 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1779 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1780 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1781 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1782 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1783 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1784 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1785 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1786 NULL
1787}, {
1788 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1789 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1790 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1791 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1792 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1793 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1794 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1795 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1796 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1797 NULL
1798}, {
1799 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1800 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1801 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1802 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1803 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1804 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1805 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1806 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1807 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1808 NULL
1809} };
1810
1811static const struct attribute_group it87_group_autopwm[3] = {
1812 { .attrs = it87_attributes_autopwm[0] },
1813 { .attrs = it87_attributes_autopwm[1] },
1814 { .attrs = it87_attributes_autopwm[2] },
1815};
1816
Jean Delvared9b327c2010-03-05 22:17:17 +01001817static struct attribute *it87_attributes_fan_beep[] = {
1818 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1819 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1820 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1821 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1822 &sensor_dev_attr_fan5_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001823 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001824};
1825
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001826static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001827 &dev_attr_vrm.attr,
1828 &dev_attr_cpu0_vid.attr,
1829 NULL
1830};
1831
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001832static const struct attribute_group it87_group_vid = {
1833 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001834};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Jean Delvare738e5e02010-08-14 21:08:50 +02001836static struct attribute *it87_attributes_label[] = {
1837 &sensor_dev_attr_in3_label.dev_attr.attr,
1838 &sensor_dev_attr_in7_label.dev_attr.attr,
1839 &sensor_dev_attr_in8_label.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001840 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare738e5e02010-08-14 21:08:50 +02001841 NULL
1842};
1843
1844static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001845 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001846};
1847
Jean Delvare2d8672c2005-07-19 23:56:35 +02001848/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001849static int __init it87_find(unsigned short *address,
1850 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001852 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001853 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001854 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001855 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001857 err = superio_enter();
1858 if (err)
1859 return err;
1860
1861 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001862 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001863
1864 switch (chip_type) {
1865 case IT8705F_DEVID:
1866 sio_data->type = it87;
1867 break;
1868 case IT8712F_DEVID:
1869 sio_data->type = it8712;
1870 break;
1871 case IT8716F_DEVID:
1872 case IT8726F_DEVID:
1873 sio_data->type = it8716;
1874 break;
1875 case IT8718F_DEVID:
1876 sio_data->type = it8718;
1877 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001878 case IT8720F_DEVID:
1879 sio_data->type = it8720;
1880 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001881 case IT8721F_DEVID:
1882 sio_data->type = it8721;
1883 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001884 case IT8728F_DEVID:
1885 sio_data->type = it8728;
1886 break;
Justin Maggardead80802015-08-05 12:53:08 -07001887 case IT8732F_DEVID:
1888 sio_data->type = it8732;
1889 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06001890 case IT8771E_DEVID:
1891 sio_data->type = it8771;
1892 break;
1893 case IT8772E_DEVID:
1894 sio_data->type = it8772;
1895 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08001896 case IT8781F_DEVID:
1897 sio_data->type = it8781;
1898 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001899 case IT8782F_DEVID:
1900 sio_data->type = it8782;
1901 break;
1902 case IT8783E_DEVID:
1903 sio_data->type = it8783;
1904 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001905 case IT8786E_DEVID:
1906 sio_data->type = it8786;
1907 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07001908 case IT8790E_DEVID:
1909 sio_data->type = it8790;
1910 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02001911 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02001912 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001913 sio_data->type = it8603;
1914 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08001915 case IT8620E_DEVID:
1916 sio_data->type = it8620;
1917 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001918 case 0xffff: /* No device at all */
1919 goto exit;
1920 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001921 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001922 goto exit;
1923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Jean Delvare87673dd2006-08-28 14:37:19 +02001925 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001927 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 goto exit;
1929 }
1930
1931 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1932 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001933 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 goto exit;
1935 }
1936
1937 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001938 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07001939 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
1940 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001941 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001943 config = &it87_devices[sio_data->type];
1944
Guenter Roeck7f5726c2015-03-26 08:49:18 -07001945 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001946 if (has_in7_internal(config))
Guenter Roeck7f5726c2015-03-26 08:49:18 -07001947 sio_data->internal |= (1 << 1);
1948
Jean Delvare738e5e02010-08-14 21:08:50 +02001949 /* in8 (Vbat) is always internal */
Guenter Roeck7f5726c2015-03-26 08:49:18 -07001950 sio_data->internal |= (1 << 2);
1951
Guenter Roeck73055402015-03-26 09:16:32 -07001952 /* in9 (AVCC3), always internal if supported */
1953 if (has_avcc3(config))
1954 sio_data->internal |= (1 << 3); /* in9 is AVCC */
1955 else
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001956 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02001957
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001958 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01001959 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001960
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001961 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1962 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01001963 /* The IT8705F has a different LD number for GPIO */
1964 superio_select(5);
1965 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08001966 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001967 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08001968
Guenter Roeck0531d982012-03-02 11:46:44 -08001969 superio_select(GPIO);
1970
1971 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1972 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001973 reg2a = superio_inb(IT87_SIO_PINX1_REG);
1974 reg2c = superio_inb(IT87_SIO_PINX2_REG);
1975 regef = superio_inb(IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001976
Guenter Roeck0531d982012-03-02 11:46:44 -08001977 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001978 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08001979 sio_data->skip_fan |= (1 << 2);
1980 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001981 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08001982 sio_data->skip_pwm |= (1 << 2);
1983
1984 /* Check if fan2 is there or not */
1985 if (reg27 & (1 << 7))
1986 sio_data->skip_fan |= (1 << 1);
1987 if (reg27 & (1 << 3))
1988 sio_data->skip_pwm |= (1 << 1);
1989
1990 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001991 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001992 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001993
1994 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001995 if (reg27 & (1 << 1))
1996 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001997
1998 /*
1999 * VIN7
2000 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2001 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002002 if (reg27 & (1 << 2)) {
2003 /*
2004 * The data sheet is a bit unclear regarding the
2005 * internal voltage divider for VCCH5V. It says
2006 * "This bit enables and switches VIN7 (pin 91) to the
2007 * internal voltage divider for VCCH5V".
2008 * This is different to other chips, where the internal
2009 * voltage divider would connect VIN7 to an internal
2010 * voltage source. Maybe that is the case here as well.
2011 *
2012 * Since we don't know for sure, re-route it if that is
2013 * not the case, and ask the user to report if the
2014 * resulting voltage is sane.
2015 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002016 if (!(reg2c & (1 << 1))) {
2017 reg2c |= (1 << 1);
2018 superio_outb(IT87_SIO_PINX2_REG, reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002019 pr_notice("Routing internal VCCH5V to in7.\n");
2020 }
2021 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2022 pr_notice("Please report if it displays a reasonable voltage.\n");
2023 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002024
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002025 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08002026 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002027 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08002028 sio_data->internal |= (1 << 1);
2029
2030 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002031 } else if (sio_data->type == it8603) {
2032 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002033
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002034 superio_select(GPIO);
2035
2036 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
2037
2038 /* Check if fan3 is there or not */
2039 if (reg27 & (1 << 6))
2040 sio_data->skip_pwm |= (1 << 2);
2041 if (reg27 & (1 << 7))
2042 sio_data->skip_fan |= (1 << 2);
2043
2044 /* Check if fan2 is there or not */
2045 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
2046 if (reg29 & (1 << 1))
2047 sio_data->skip_pwm |= (1 << 1);
2048 if (reg29 & (1 << 2))
2049 sio_data->skip_fan |= (1 << 1);
2050
2051 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2052 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2053
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002054 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002055 } else if (sio_data->type == it8620) {
2056 int reg;
2057
2058 superio_select(GPIO);
2059
2060 /* Check for fan4, fan5 */
2061 reg = superio_inb(IT87_SIO_GPIO2_REG);
2062 if (!(reg & (1 << 5)))
2063 sio_data->skip_fan |= (1 << 3);
2064 if (!(reg & (1 << 4)))
2065 sio_data->skip_fan |= (1 << 4);
2066
2067 /* Check for pwm3, fan3 */
2068 reg = superio_inb(IT87_SIO_GPIO3_REG);
2069 if (reg & (1 << 6))
2070 sio_data->skip_pwm |= (1 << 2);
2071 if (reg & (1 << 7))
2072 sio_data->skip_fan |= (1 << 2);
2073
2074 /* Check for pwm2, fan2 */
2075 reg = superio_inb(IT87_SIO_GPIO5_REG);
2076 if (reg & (1 << 1))
2077 sio_data->skip_pwm |= (1 << 1);
2078 if (reg & (1 << 2))
2079 sio_data->skip_fan |= (1 << 1);
2080
2081 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002082 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002083 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002084 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002085
2086 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002087
Jean Delvare895ff262009-12-09 20:35:47 +01002088 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002089 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002090 /* We need at least 4 VID pins */
2091 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002092 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002093 sio_data->skip_vid = 1;
2094 }
Jean Delvare895ff262009-12-09 20:35:47 +01002095 }
2096
Jean Delvare591ec652009-12-09 20:35:48 +01002097 /* Check if fan3 is there or not */
2098 if (reg & (1 << 6))
2099 sio_data->skip_pwm |= (1 << 2);
2100 if (reg & (1 << 7))
2101 sio_data->skip_fan |= (1 << 2);
2102
2103 /* Check if fan2 is there or not */
2104 reg = superio_inb(IT87_SIO_GPIO5_REG);
2105 if (reg & (1 << 1))
2106 sio_data->skip_pwm |= (1 << 1);
2107 if (reg & (1 << 2))
2108 sio_data->skip_fan |= (1 << 1);
2109
Jean Delvare895ff262009-12-09 20:35:47 +01002110 if ((sio_data->type == it8718 || sio_data->type == it8720)
2111 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002112 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002113
2114 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002115
2116 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2117
Jean Delvare436cad22010-07-09 16:22:48 +02002118 /*
2119 * The IT8720F has no VIN7 pin, so VCCH should always be
2120 * routed internally to VIN7 with an internal divider.
2121 * Curiously, there still is a configuration bit to control
2122 * this, which means it can be set incorrectly. And even
2123 * more curiously, many boards out there are improperly
2124 * configured, even though the IT8720F datasheet claims
2125 * that the internal routing of VCCH to VIN7 is the default
2126 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002127 *
2128 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002129 * If UART6 is enabled, re-route VIN7 to the internal divider
2130 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002131 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002132 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02002133 reg |= (1 << 1);
2134 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002135 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002136 }
Jean Delvare87673dd2006-08-28 14:37:19 +02002137 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02002138 sio_data->internal |= (1 << 0);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002139 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02002140 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002141
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002142 /*
2143 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2144 * While VIN7 can be routed to the internal voltage divider,
2145 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002146 *
2147 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2148 * is the temperature source. Since we can not read the
2149 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002150 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002151 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002152 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002153 sio_data->skip_temp |= (1 << 2);
2154 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002155
Jean Delvared9b327c2010-03-05 22:17:17 +01002156 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002157 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002158 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002159 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002160
Jean Delvare98dd22c2008-10-09 15:33:58 +02002161 /* Disable specific features based on DMI strings */
2162 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2163 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2164 if (board_vendor && board_name) {
2165 if (strcmp(board_vendor, "nVIDIA") == 0
2166 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002167 /*
2168 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2169 * connected to a fan, but to something else. One user
2170 * has reported instant system power-off when changing
2171 * the PWM2 duty cycle, so we disable it.
2172 * I use the board name string as the trigger in case
2173 * the same board is ever used in other systems.
2174 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002175 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002176 sio_data->skip_pwm = (1 << 1);
2177 }
2178 }
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180exit:
2181 superio_exit();
2182 return err;
2183}
2184
Jean Delvare723a0aa2010-03-05 22:17:16 +01002185static void it87_remove_files(struct device *dev)
2186{
2187 struct it87_data *data = platform_get_drvdata(pdev);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002188 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002189 int i;
2190
2191 sysfs_remove_group(&dev->kobj, &it87_group);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002192 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002193 if (sio_data->skip_in & (1 << i))
2194 continue;
2195 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2196 if (it87_attributes_in_beep[i])
2197 sysfs_remove_file(&dev->kobj,
2198 it87_attributes_in_beep[i]);
2199 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002200 for (i = 0; i < 3; i++) {
2201 if (!(data->has_temp & (1 << i)))
2202 continue;
2203 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01002204 if (has_temp_offset(data))
2205 sysfs_remove_file(&dev->kobj,
2206 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002207 if (sio_data->beep_pin)
2208 sysfs_remove_file(&dev->kobj,
2209 it87_attributes_temp_beep[i]);
2210 }
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002211 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002212 if (!(data->has_fan & (1 << i)))
2213 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002214 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002215 if (sio_data->beep_pin)
2216 sysfs_remove_file(&dev->kobj,
2217 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002218 if (i < 3 && !has_16bit_fans(data))
2219 sysfs_remove_file(&dev->kobj,
2220 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002221 }
2222 for (i = 0; i < 3; i++) {
Guenter Roeck1696d1d2015-03-27 06:03:41 -07002223 if (sio_data->skip_pwm & (1 << i))
Jean Delvare723a0aa2010-03-05 22:17:16 +01002224 continue;
2225 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002226 if (has_old_autopwm(data))
2227 sysfs_remove_group(&dev->kobj,
2228 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002229 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002230 if (!sio_data->skip_vid)
2231 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02002232 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002233}
2234
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002235static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002238 struct resource *res;
2239 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002240 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002241 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01002243 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002245 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002246 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2247 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002248 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2249 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002250 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002251 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Guenter Roeck62a1d052012-03-24 21:54:41 -07002254 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2255 if (!data)
2256 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002258 data->addr = res->start;
2259 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002260 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002261 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002262 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002263 data->name = it87_devices[sio_data->type].name;
2264 /*
2265 * IT8705F Datasheet 0.4.1, 3h == Version G.
2266 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2267 * These are the first revisions with 16-bit tachometer support.
2268 */
2269 switch (data->type) {
2270 case it87:
2271 if (sio_data->revision >= 0x03) {
2272 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002273 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002274 }
2275 break;
2276 case it8712:
2277 if (sio_data->revision >= 0x08) {
2278 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002279 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2280 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002281 }
2282 break;
2283 default:
2284 break;
2285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002288 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002289 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2290 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002292 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002294 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002297 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002299 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002300 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002301 if (sio_data->internal & (1 << 0))
2302 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2303 if (sio_data->internal & (1 << 1))
2304 data->in_scaled |= (1 << 7); /* in7 is VSB */
2305 if (sio_data->internal & (1 << 2))
2306 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002307 if (sio_data->internal & (1 << 3))
2308 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002309 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2310 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002311 if (sio_data->internal & (1 << 0))
2312 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2313 if (sio_data->internal & (1 << 1))
2314 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002315 }
2316
Guenter Roeck4573acb2012-03-26 16:17:41 -07002317 data->has_temp = 0x07;
2318 if (sio_data->skip_temp & (1 << 2)) {
2319 if (sio_data->type == it8782
2320 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2321 data->has_temp &= ~(1 << 2);
2322 }
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002325 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002328 err = sysfs_create_group(&dev->kobj, &it87_group);
2329 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002330 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002331
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002332 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002333 if (sio_data->skip_in & (1 << i))
2334 continue;
2335 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2336 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002337 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002338 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2339 err = sysfs_create_file(&dev->kobj,
2340 it87_attributes_in_beep[i]);
2341 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002342 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002343 }
2344 }
2345
Guenter Roeck4573acb2012-03-26 16:17:41 -07002346 for (i = 0; i < 3; i++) {
2347 if (!(data->has_temp & (1 << i)))
2348 continue;
2349 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002350 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002351 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002352 if (has_temp_offset(data)) {
2353 err = sysfs_create_file(&dev->kobj,
2354 it87_attributes_temp_offset[i]);
2355 if (err)
2356 goto error;
2357 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002358 if (sio_data->beep_pin) {
2359 err = sysfs_create_file(&dev->kobj,
2360 it87_attributes_temp_beep[i]);
2361 if (err)
2362 goto error;
2363 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002364 }
2365
Jean Delvare9060f8b2006-08-28 14:24:17 +02002366 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002367 fan_beep_need_rw = 1;
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002368 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002369 if (!(data->has_fan & (1 << i)))
2370 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002371 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002372 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002373 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002374
Guenter Roecke1169ba2012-12-19 22:17:01 +01002375 if (i < 3 && !has_16bit_fans(data)) {
2376 err = sysfs_create_file(&dev->kobj,
2377 it87_attributes_fan_div[i]);
2378 if (err)
2379 goto error;
2380 }
2381
Jean Delvared9b327c2010-03-05 22:17:17 +01002382 if (sio_data->beep_pin) {
2383 err = sysfs_create_file(&dev->kobj,
2384 it87_attributes_fan_beep[i]);
2385 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002386 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002387 if (!fan_beep_need_rw)
2388 continue;
2389
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002390 /*
2391 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002392 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002393 * for it.
2394 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002395 if (sysfs_chmod_file(&dev->kobj,
2396 it87_attributes_fan_beep[i],
2397 S_IRUGO | S_IWUSR))
2398 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2399 i + 1);
2400 fan_beep_need_rw = 0;
2401 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002402 }
2403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002405 for (i = 0; i < 3; i++) {
2406 if (sio_data->skip_pwm & (1 << i))
2407 continue;
2408 err = sysfs_create_group(&dev->kobj,
2409 &it87_group_pwm[i]);
2410 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002411 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002412
2413 if (!has_old_autopwm(data))
2414 continue;
2415 err = sysfs_create_group(&dev->kobj,
2416 &it87_group_autopwm[i]);
2417 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002418 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421
Jean Delvare895ff262009-12-09 20:35:47 +01002422 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002423 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002424 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002425 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002426 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2427 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002428 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002429 }
2430
Jean Delvare738e5e02010-08-14 21:08:50 +02002431 /* Export labels for internal sensors */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002432 for (i = 0; i < 4; i++) {
Jean Delvare738e5e02010-08-14 21:08:50 +02002433 if (!(sio_data->internal & (1 << i)))
2434 continue;
2435 err = sysfs_create_file(&dev->kobj,
2436 it87_attributes_label[i]);
2437 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002438 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002439 }
2440
Tony Jones1beeffe2007-08-20 13:46:20 -07002441 data->hwmon_dev = hwmon_device_register(dev);
2442 if (IS_ERR(data->hwmon_dev)) {
2443 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002444 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446
2447 return 0;
2448
Guenter Roeck62a1d052012-03-24 21:54:41 -07002449error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002450 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return err;
2452}
2453
Bill Pemberton281dfd02012-11-19 13:25:51 -05002454static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002456 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Tony Jones1beeffe2007-08-20 13:46:20 -07002458 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002459 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return 0;
2462}
2463
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002464/*
2465 * Must be called with data->update_lock held, except during initialization.
2466 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2467 * would slow down the IT87 access and should not be necessary.
2468 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002469static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002471 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2472 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002475/*
2476 * Must be called with data->update_lock held, except during initialization.
2477 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2478 * would slow down the IT87 access and should not be necessary.
2479 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002480static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002482 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2483 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
2486/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002487static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002489 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002490 /*
2491 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002493 * disable pwm control to protect the user.
2494 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002495 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if ((tmp & 0x87) == 0) {
2497 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002498 /*
2499 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002501 * inverting all fan speed values.
2502 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 int i;
2504 u8 pwm[3];
2505
2506 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002507 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 IT87_REG_PWM(i));
2509
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002510 /*
2511 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 * might be correct, as suspicious as it seems, so we
2513 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002514 * PWM interface).
2515 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002517 dev_info(dev,
2518 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002519 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 tmp | 0x87);
2521 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002522 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 IT87_REG_PWM(i),
2524 0x7f & ~pwm[i]);
2525 return 1;
2526 }
2527
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002528 dev_info(dev,
2529 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002532 dev_info(dev,
2533 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return 0;
2535 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002536 dev_info(dev,
2537 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539
2540 return 1;
2541}
2542
2543/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002544static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002546 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002547 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002549 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002551 /*
2552 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002553 * - If it is in automatic mode, setting to manual mode should set
2554 * the fan to full speed by default.
2555 * - If it is in manual mode, we need a mapping to temperature
2556 * channels to use when later setting to automatic mode later.
2557 * Use a 1:1 mapping by default (we are clueless.)
2558 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002559 * prior to switching to a different mode.
2560 * Note that this is no longer needed for the IT8721F and later, as
2561 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002562 * manual duty cycle.
2563 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002565 data->pwm_temp_map[i] = i;
2566 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002567 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
2569
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002570 /*
2571 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002572 * registers. For low voltage limits it makes no sense and triggers
2573 * alarms, so change to 0 instead. For high temperature limits, it
2574 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002575 * but is still confusing, so change to 127 degrees C.
2576 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002577 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002578 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002579 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002580 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002581 }
2582 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002583 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002584 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002585 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002586 }
2587
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002588 /*
2589 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002590 * set to two different sensor types and we can't guess which one
2591 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002592 * run-time through the temp{1-3}_type sysfs accessors if needed.
2593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002596 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 if ((tmp & 0xff) == 0) {
2598 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002599 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 }
2601
2602 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002603 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002604 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002605 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002607 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002608 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2609 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002611 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002613 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2614
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002615 /* Set tachometers to 16-bit mode if needed */
2616 if (has_fan16_config(data)) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002617 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002618 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002619 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002620 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002621 tmp | 0x07);
2622 }
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002623 }
2624
2625 /* Check for additional fans */
2626 if (has_five_fans(data)) {
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002627 if (tmp & (1 << 4))
2628 data->has_fan |= (1 << 3); /* fan4 enabled */
2629 if (tmp & (1 << 5))
2630 data->has_fan |= (1 << 4); /* fan5 enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002631 if (has_six_fans(data) && (tmp & (1 << 2)))
2632 data->has_fan |= (1 << 5); /* fan6 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02002633 }
2634
Jean Delvare591ec652009-12-09 20:35:48 +01002635 /* Fan input pins may be used for alternative functions */
2636 data->has_fan &= ~sio_data->skip_fan;
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002639 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002640 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 | (update_vbat ? 0x41 : 0x01));
2642}
2643
Jean Delvareb99883d2010-03-05 22:17:15 +01002644static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2645{
2646 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002647 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002648 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002649 data->pwm_duty[nr] = it87_read_value(data,
2650 IT87_REG_PWM_DUTY(nr));
2651 } else {
2652 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2653 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2654 else /* Manual mode */
2655 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2656 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002657
2658 if (has_old_autopwm(data)) {
2659 int i;
2660
2661 for (i = 0; i < 5 ; i++)
2662 data->auto_temp[nr][i] = it87_read_value(data,
2663 IT87_REG_AUTO_TEMP(nr, i));
2664 for (i = 0; i < 3 ; i++)
2665 data->auto_pwm[nr][i] = it87_read_value(data,
2666 IT87_REG_AUTO_PWM(nr, i));
2667 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002668}
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670static struct it87_data *it87_update_device(struct device *dev)
2671{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002672 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 int i;
2674
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002675 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
2677 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2678 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002680 /*
2681 * Cleared after each update, so reenable. Value
2682 * returned by this read will be previous value
2683 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002684 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002685 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
2687 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002688 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002689 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002690 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002691 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002692 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002693 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 }
Jean Delvare3543a532006-08-28 14:27:25 +02002695 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002696 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Guenter Roeck73055402015-03-26 09:16:32 -07002697 if (has_avcc3(data))
2698 data->in[9][0] = it87_read_value(data, IT87_REG_AVCC3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002700 for (i = 0; i < 6; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002701 /* Skip disabled fans */
2702 if (!(data->has_fan & (1 << i)))
2703 continue;
2704
Guenter Roecke1169ba2012-12-19 22:17:01 +01002705 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002706 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002707 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002708 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002709 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002710 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002711 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002712 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002713 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002714 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 }
2717 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002718 if (!(data->has_temp & (1 << i)))
2719 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002720 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002721 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002722 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002723 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002724 data->temp[i][2] =
2725 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002726 if (has_temp_offset(data))
2727 data->temp[i][3] =
2728 it87_read_value(data,
2729 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
2731
Jean Delvare17d648b2006-08-28 14:23:46 +02002732 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002733 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002734 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002735 data->fan_div[0] = i & 0x07;
2736 data->fan_div[1] = (i >> 3) & 0x07;
2737 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002741 it87_read_value(data, IT87_REG_ALARM1) |
2742 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2743 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002744 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002745
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002746 data->fan_main_ctrl = it87_read_value(data,
2747 IT87_REG_FAN_MAIN_CTRL);
2748 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002749 for (i = 0; i < 3; i++)
2750 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002752 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002753 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002754 /*
2755 * The IT8705F does not have VID capability.
2756 * The IT8718F and later don't use IT87_REG_VID for the
2757 * same purpose.
2758 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002759 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002760 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002761 /*
2762 * The older IT8712F revisions had only 5 VID pins,
2763 * but we assume it is always safe to read 6 bits.
2764 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002765 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
2767 data->last_updated = jiffies;
2768 data->valid = 1;
2769 }
2770
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002771 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 return data;
2774}
2775
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002776static int __init it87_device_add(unsigned short address,
2777 const struct it87_sio_data *sio_data)
2778{
2779 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002780 .start = address + IT87_EC_OFFSET,
2781 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002782 .name = DRVNAME,
2783 .flags = IORESOURCE_IO,
2784 };
2785 int err;
2786
Jean Delvareb9acb642009-01-07 16:37:35 +01002787 err = acpi_check_resource_conflict(&res);
2788 if (err)
2789 goto exit;
2790
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002791 pdev = platform_device_alloc(DRVNAME, address);
2792 if (!pdev) {
2793 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002794 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002795 goto exit;
2796 }
2797
2798 err = platform_device_add_resources(pdev, &res, 1);
2799 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002800 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002801 goto exit_device_put;
2802 }
2803
2804 err = platform_device_add_data(pdev, sio_data,
2805 sizeof(struct it87_sio_data));
2806 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002807 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002808 goto exit_device_put;
2809 }
2810
2811 err = platform_device_add(pdev);
2812 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002813 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002814 goto exit_device_put;
2815 }
2816
2817 return 0;
2818
2819exit_device_put:
2820 platform_device_put(pdev);
2821exit:
2822 return err;
2823}
2824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825static int __init sm_it87_init(void)
2826{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002827 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002828 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002829 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002830
Jean Delvare98dd22c2008-10-09 15:33:58 +02002831 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002832 err = it87_find(&isa_address, &sio_data);
2833 if (err)
2834 return err;
2835 err = platform_driver_register(&it87_driver);
2836 if (err)
2837 return err;
2838
2839 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002840 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002841 platform_driver_unregister(&it87_driver);
2842 return err;
2843 }
2844
2845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
2848static void __exit sm_it87_exit(void)
2849{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002850 platform_device_unregister(pdev);
2851 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
2854
Jean Delvare7c81c602014-01-29 20:40:08 +01002855MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002856MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857module_param(update_vbat, bool, 0);
2858MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2859module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002860MODULE_PARM_DESC(fix_pwm_polarity,
2861 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862MODULE_LICENSE("GPL");
2863
2864module_init(sm_it87_init);
2865module_exit(sm_it87_exit);