blob: 916d7363022475d6f36d0a16da43e5bb92ee9cb8 [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
Guenter Roeck36c4d982015-03-26 18:18:19 -0700171#define IT87_SIO_GPIO4_REG 0x28
Jean Delvare591ec652009-12-09 20:35:48 +0100172#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800173#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200174#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800175#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200176#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100177#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030180static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030183static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* Many IT87 constants specified below */
186
187/* Length of ISA address segment */
188#define IT87_EXTENT 8
189
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500190/* Length of ISA address segment for Environmental Controller */
191#define IT87_EC_EXTENT 2
192
193/* Offset of EC registers from ISA base address */
194#define IT87_EC_OFFSET 5
195
196/* Where are the ISA address/data registers relative to the EC base address */
197#define IT87_ADDR_REG_OFFSET 0
198#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/*----- The IT87 registers -----*/
201
202#define IT87_REG_CONFIG 0x00
203
204#define IT87_REG_ALARM1 0x01
205#define IT87_REG_ALARM2 0x02
206#define IT87_REG_ALARM3 0x03
207
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800208/*
209 * The IT8718F and IT8720F have the VID value in a different register, in
210 * Super-I/O configuration space.
211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800213/*
214 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
215 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
216 * mode.
217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200219#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
222
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700223static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
224static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
225static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
226static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
227static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#define IT87_REG_FAN_MAIN_CTRL 0x13
230#define IT87_REG_FAN_CTL 0x14
Guenter Roeck36c4d982015-03-26 18:18:19 -0700231static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
232static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234#define IT87_REG_VIN(nr) (0x20 + (nr))
235#define IT87_REG_TEMP(nr) (0x29 + (nr))
236
Guenter Roeck73055402015-03-26 09:16:32 -0700237#define IT87_REG_AVCC3 0x2f
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
240#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
241#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
242#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244#define IT87_REG_VIN_ENABLE 0x50
245#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700246#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100247#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249#define IT87_REG_CHIPID 0x58
250
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100251#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
252#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
253
Guenter Roeck483db432012-12-19 22:17:02 +0100254struct it87_devices {
255 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700256 const char * const suffix;
Guenter Roeck483db432012-12-19 22:17:02 +0100257 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100258 u8 peci_mask;
259 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100260};
261
262#define FEAT_12MV_ADC (1 << 0)
263#define FEAT_NEWER_AUTOPWM (1 << 1)
264#define FEAT_OLD_AUTOPWM (1 << 2)
265#define FEAT_16BIT_FANS (1 << 3)
266#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100267#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100268#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800269#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
270#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800271#define FEAT_VID (1 << 9) /* Set if chip supports VID */
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700272#define FEAT_IN7_INTERNAL (1 << 10) /* Set if in7 is internal */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700273#define FEAT_SIX_FANS (1 << 11) /* Supports six fans */
Justin Maggardead80802015-08-05 12:53:08 -0700274#define FEAT_10_9MV_ADC (1 << 12)
Guenter Roeck73055402015-03-26 09:16:32 -0700275#define FEAT_AVCC3 (1 << 13) /* Chip supports in9/AVCC3 */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700276#define FEAT_SIX_PWM (1 << 14) /* Chip supports 6 pwm chn */
Guenter Roeck483db432012-12-19 22:17:02 +0100277
278static const struct it87_devices it87_devices[] = {
279 [it87] = {
280 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700281 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100282 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
283 },
284 [it8712] = {
285 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700286 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800287 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
288 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100289 },
290 [it8716] = {
291 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700292 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800293 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800294 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck483db432012-12-19 22:17:02 +0100295 },
296 [it8718] = {
297 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700298 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800299 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800300 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100301 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100302 },
303 [it8720] = {
304 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700305 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800306 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800307 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100308 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100309 },
310 [it8721] = {
311 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700312 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100313 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800314 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700315 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100316 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100317 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100318 },
319 [it8728] = {
320 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700321 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100322 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700323 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
324 | FEAT_IN7_INTERNAL,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100325 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100326 },
Justin Maggardead80802015-08-05 12:53:08 -0700327 [it8732] = {
328 .name = "it8732",
329 .suffix = "F",
330 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
331 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
332 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
333 .peci_mask = 0x07,
334 .old_peci_mask = 0x02, /* Actually reports PCH */
335 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600336 [it8771] = {
337 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700338 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600339 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700340 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800341 /* PECI: guesswork */
342 /* 12mV ADC (OHM) */
343 /* 16 bit fans (OHM) */
344 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600345 .peci_mask = 0x07,
346 },
347 [it8772] = {
348 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700349 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600350 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700351 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800352 /* PECI (coreboot) */
353 /* 12mV ADC (HWSensors4, OHM) */
354 /* 16 bit fans (HWSensors4, OHM) */
355 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600356 .peci_mask = 0x07,
357 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800358 [it8781] = {
359 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700360 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800361 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800362 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800363 .old_peci_mask = 0x4,
364 },
Guenter Roeck483db432012-12-19 22:17:02 +0100365 [it8782] = {
366 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700367 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100368 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800369 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100370 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100371 },
372 [it8783] = {
373 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700374 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100375 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800376 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100377 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100378 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100379 [it8786] = {
380 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700381 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100382 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700383 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100384 .peci_mask = 0x07,
385 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700386 [it8790] = {
387 .name = "it8790",
388 .suffix = "E",
389 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
390 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
391 .peci_mask = 0x07,
392 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100393 [it8603] = {
394 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700395 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100396 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck73055402015-03-26 09:16:32 -0700397 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
398 | FEAT_AVCC3,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100399 .peci_mask = 0x07,
400 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800401 [it8620] = {
402 .name = "it8620",
403 .suffix = "E",
404 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700405 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeck36c4d982015-03-26 18:18:19 -0700406 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM,
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800407 .peci_mask = 0x07,
408 },
Guenter Roeck483db432012-12-19 22:17:02 +0100409};
410
411#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
412#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
Justin Maggardead80802015-08-05 12:53:08 -0700413#define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
Guenter Roeck483db432012-12-19 22:17:02 +0100414#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
415#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
416#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100417#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
418 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100419#define has_temp_old_peci(data, nr) \
420 (((data)->features & FEAT_TEMP_OLD_PECI) && \
421 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800422#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700423#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
424 FEAT_SIX_FANS))
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800425#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700426#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700427#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Guenter Roeck73055402015-03-26 09:16:32 -0700428#define has_avcc3(data) ((data)->features & FEAT_AVCC3)
Guenter Roeck36c4d982015-03-26 18:18:19 -0700429#define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200431struct it87_sio_data {
432 enum chips type;
433 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200434 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200435 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100436 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200437 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100438 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700439 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100440 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100441 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200442 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700443 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200444};
445
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800446/*
447 * For each registered chip, we need to keep some data in memory.
448 * The structure is dynamically allocated.
449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700451 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100453 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100454 u8 peci_mask;
455 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200457 unsigned short addr;
458 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100459 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 char valid; /* !=0 if following fields are valid */
461 unsigned long last_updated; /* In jiffies */
462
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200463 u16 in_scaled; /* Internal voltage sensors are scaled */
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100464 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200465 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700466 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700467 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck161d8982012-12-19 22:17:01 +0100468 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100469 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
470 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 u8 fan_div[3]; /* Register encoding, shifted right */
472 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100473 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100475 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100477 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100478
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800479 /*
480 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100481 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
482 * 7, and we want to preserve settings on mode changes, so we have
483 * to track all values separately.
484 * Starting with the IT8721F, the manual PWM duty cycles are stored
485 * in separate registers (8-bit values), so the separate tracking
486 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800487 * simple.
488 */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700489 u8 pwm_ctrl[6]; /* Register value */
490 u8 pwm_duty[6]; /* Manual PWM value set by user */
491 u8 pwm_temp_map[6]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100492
493 /* Automatic fan speed control registers */
494 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
495 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496};
497
Guenter Roeck0531d982012-03-02 11:46:44 -0800498static int adc_lsb(const struct it87_data *data, int nr)
499{
Justin Maggardead80802015-08-05 12:53:08 -0700500 int lsb;
501
502 if (has_12mv_adc(data))
503 lsb = 120;
504 else if (has_10_9mv_adc(data))
505 lsb = 109;
506 else
507 lsb = 160;
Guenter Roeck0531d982012-03-02 11:46:44 -0800508 if (data->in_scaled & (1 << nr))
509 lsb <<= 1;
510 return lsb;
511}
512
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200513static u8 in_to_reg(const struct it87_data *data, int nr, long val)
514{
Justin Maggardead80802015-08-05 12:53:08 -0700515 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800516 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200517}
518
519static int in_from_reg(const struct it87_data *data, int nr, int val)
520{
Justin Maggardead80802015-08-05 12:53:08 -0700521 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200522}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200523
524static inline u8 FAN_TO_REG(long rpm, int div)
525{
526 if (rpm == 0)
527 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800528 rpm = clamp_val(rpm, 1, 1000000);
529 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200530}
531
532static inline u16 FAN16_TO_REG(long rpm)
533{
534 if (rpm == 0)
535 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800536 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200537}
538
539#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
540 1350000 / ((val) * (div)))
541/* The divider is fixed to 2 in 16-bit mode */
542#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
543 1350000 / ((val) * 2))
544
Guenter Roeck2a844c12013-01-09 08:09:34 -0800545#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
546 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200547#define TEMP_FROM_REG(val) ((val) * 1000)
548
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200549static u8 pwm_to_reg(const struct it87_data *data, long val)
550{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100551 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200552 return val;
553 else
554 return val >> 1;
555}
556
557static int pwm_from_reg(const struct it87_data *data, u8 reg)
558{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100559 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200560 return reg;
561 else
562 return (reg & 0x7f) << 1;
563}
564
Jean Delvare0df6454d2010-10-28 20:31:51 +0200565
566static int DIV_TO_REG(int val)
567{
568 int answer = 0;
569 while (answer < 7 && (val >>= 1))
570 answer++;
571 return answer;
572}
573#define DIV_FROM_REG(val) (1 << (val))
574
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700575/*
576 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
577 * depending on the chip type, to calculate the actual PWM frequency.
578 *
579 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
580 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
581 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
582 * sometimes just one. It is unknown if this is a datasheet error or real,
583 * so this is ignored for now.
584 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200585static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700586 48000000,
587 24000000,
588 12000000,
589 8000000,
590 6000000,
591 3000000,
592 1500000,
593 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200594};
595
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200596static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500597static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200599static int it87_read_value(struct it87_data *data, u8 reg);
600static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200602static int it87_check_pwm(struct device *dev);
603static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200606static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100607 .driver = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200608 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100609 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200610 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500611 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200612};
613
Jean Delvare20ad93d2005-06-05 11:53:25 +0200614static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100615 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100617 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
618 int nr = sattr->nr;
619 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100622 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Guenter Roeck929c6a52012-12-19 22:17:00 +0100625static ssize_t set_in(struct device *dev, struct device_attribute *attr,
626 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100628 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
629 int nr = sattr->nr;
630 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200631
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200632 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100633 unsigned long val;
634
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100635 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100636 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100638 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100639 data->in[nr][index] = in_to_reg(data, nr, val);
640 it87_write_value(data,
641 index == 1 ? IT87_REG_VIN_MIN(nr)
642 : IT87_REG_VIN_MAX(nr),
643 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100644 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return count;
646}
647
Guenter Roeck929c6a52012-12-19 22:17:00 +0100648static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
649static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
650 0, 1);
651static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
652 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Guenter Roeck929c6a52012-12-19 22:17:00 +0100654static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
655static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
656 1, 1);
657static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
658 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Guenter Roeck929c6a52012-12-19 22:17:00 +0100660static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
661static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
662 2, 1);
663static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
664 2, 2);
665
666static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
667static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
668 3, 1);
669static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
670 3, 2);
671
672static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
673static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
674 4, 1);
675static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
676 4, 2);
677
678static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
679static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
680 5, 1);
681static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
682 5, 2);
683
684static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
685static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
686 6, 1);
687static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
688 6, 2);
689
690static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
691static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
692 7, 1);
693static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
694 7, 2);
695
696static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100697static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200700static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100701 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100703 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
704 int nr = sattr->nr;
705 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200707
Guenter Roeck60ca3852012-12-19 22:17:00 +0100708 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200710
Guenter Roeck60ca3852012-12-19 22:17:00 +0100711static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
712 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100714 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
715 int nr = sattr->nr;
716 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200717 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100718 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100719 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100720
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100721 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100722 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100724 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100725
726 switch (index) {
727 default:
728 case 1:
729 reg = IT87_REG_TEMP_LOW(nr);
730 break;
731 case 2:
732 reg = IT87_REG_TEMP_HIGH(nr);
733 break;
734 case 3:
735 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
736 if (!(regval & 0x80)) {
737 regval |= 0x80;
738 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
739 }
740 data->valid = 0;
741 reg = IT87_REG_TEMP_OFFSET[nr];
742 break;
743 }
744
Guenter Roeck60ca3852012-12-19 22:17:00 +0100745 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100746 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100747 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return count;
749}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200750
Guenter Roeck60ca3852012-12-19 22:17:00 +0100751static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
752static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
753 0, 1);
754static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
755 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100756static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
757 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100758static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
759static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
760 1, 1);
761static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
762 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100763static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
764 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100765static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
766static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
767 2, 1);
768static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
769 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100770static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
771 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Guenter Roeck2cece012012-12-19 22:17:01 +0100773static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
774 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200776 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
777 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800779 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100780 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100781
Guenter Roeck19529782012-12-19 22:17:02 +0100782 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
783 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100784 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (reg & (1 << nr))
786 return sprintf(buf, "3\n"); /* thermal diode */
787 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200788 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return sprintf(buf, "0\n"); /* disabled */
790}
Guenter Roeck2cece012012-12-19 22:17:01 +0100791
792static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
793 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200795 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
796 int nr = sensor_attr->index;
797
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200798 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100799 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100800 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100801
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100802 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100803 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Jean Delvare8acf07c2010-04-14 16:14:09 +0200805 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
806 reg &= ~(1 << nr);
807 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100808 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
809 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100810 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
811 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
812 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200813 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100814 dev_warn(dev,
815 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200816 val = 4;
817 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100818 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200820 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200821 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200822 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100823 else if (has_temp_peci(data, nr) && val == 6)
824 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100825 else if (has_temp_old_peci(data, nr) && val == 6)
826 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200827 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200829
830 mutex_lock(&data->update_lock);
831 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100832 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200833 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100834 if (has_temp_old_peci(data, nr))
835 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200836 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100837 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return count;
839}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Guenter Roeck2cece012012-12-19 22:17:01 +0100841static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
842 set_temp_type, 0);
843static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
844 set_temp_type, 1);
845static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
846 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100849
850static int pwm_mode(const struct it87_data *data, int nr)
851{
852 int ctrl = data->fan_main_ctrl & (1 << nr);
853
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100854 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +0100855 return 0;
856 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
857 return 2;
858 else /* Manual mode */
859 return 1;
860}
861
Jean Delvare20ad93d2005-06-05 11:53:25 +0200862static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +0100863 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100865 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
866 int nr = sattr->nr;
867 int index = sattr->index;
868 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200870
Guenter Roecke1169ba2012-12-19 22:17:01 +0100871 speed = has_16bit_fans(data) ?
872 FAN16_FROM_REG(data->fan[nr][index]) :
873 FAN_FROM_REG(data->fan[nr][index],
874 DIV_FROM_REG(data->fan_div[nr]));
875 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100877
Jean Delvare20ad93d2005-06-05 11:53:25 +0200878static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
879 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200881 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
882 int nr = sensor_attr->index;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct it87_data *data = it87_update_device(dev);
885 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
886}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100887static ssize_t show_pwm_enable(struct device *dev,
888 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200890 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
891 int nr = sensor_attr->index;
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100894 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200896static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
897 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200899 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
900 int nr = sensor_attr->index;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200903 return sprintf(buf, "%d\n",
904 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100906static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
907 char *buf)
908{
909 struct it87_data *data = it87_update_device(dev);
910 int index = (data->fan_ctl >> 4) & 0x07;
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700911 unsigned int freq;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100912
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700913 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
914
915 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100916}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100917
918static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
919 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100921 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
922 int nr = sattr->nr;
923 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200924
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200925 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100926 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100927 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100929 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100930 return -EINVAL;
931
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100932 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +0100933
934 if (has_16bit_fans(data)) {
935 data->fan[nr][index] = FAN16_TO_REG(val);
936 it87_write_value(data, IT87_REG_FAN_MIN[nr],
937 data->fan[nr][index] & 0xff);
938 it87_write_value(data, IT87_REG_FANX_MIN[nr],
939 data->fan[nr][index] >> 8);
940 } else {
941 reg = it87_read_value(data, IT87_REG_FAN_DIV);
942 switch (nr) {
943 case 0:
944 data->fan_div[nr] = reg & 0x07;
945 break;
946 case 1:
947 data->fan_div[nr] = (reg >> 3) & 0x07;
948 break;
949 case 2:
950 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
951 break;
952 }
953 data->fan[nr][index] =
954 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
955 it87_write_value(data, IT87_REG_FAN_MIN[nr],
956 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -0800957 }
958
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100959 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return count;
961}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100962
Jean Delvare20ad93d2005-06-05 11:53:25 +0200963static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
964 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200966 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
967 int nr = sensor_attr->index;
968
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200969 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100970 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200971 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 u8 old;
973
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100974 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100975 return -EINVAL;
976
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100977 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200978 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200980 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100981 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 switch (nr) {
984 case 0:
985 case 1:
986 data->fan_div[nr] = DIV_TO_REG(val);
987 break;
988 case 2:
989 if (val < 8)
990 data->fan_div[nr] = 1;
991 else
992 data->fan_div[nr] = 3;
993 }
994 val = old & 0x80;
995 val |= (data->fan_div[0] & 0x07);
996 val |= (data->fan_div[1] & 0x07) << 3;
997 if (data->fan_div[2] == 3)
998 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200999 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001001 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001002 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1003 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001004
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001005 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return count;
1007}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001008
1009/* Returns 0 if OK, -EINVAL otherwise */
1010static int check_trip_points(struct device *dev, int nr)
1011{
1012 const struct it87_data *data = dev_get_drvdata(dev);
1013 int i, err = 0;
1014
1015 if (has_old_autopwm(data)) {
1016 for (i = 0; i < 3; i++) {
1017 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1018 err = -EINVAL;
1019 }
1020 for (i = 0; i < 2; i++) {
1021 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1022 err = -EINVAL;
1023 }
1024 }
1025
1026 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001027 dev_err(dev,
1028 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001029 dev_err(dev, "Adjust the trip points and try again\n");
1030 }
1031 return err;
1032}
1033
Jean Delvare20ad93d2005-06-05 11:53:25 +02001034static ssize_t set_pwm_enable(struct device *dev,
1035 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001037 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1038 int nr = sensor_attr->index;
1039
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001040 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001041 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001043 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001044 return -EINVAL;
1045
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001046 /* Check trip points before switching to automatic mode */
1047 if (val == 2) {
1048 if (check_trip_points(dev, nr) < 0)
1049 return -EINVAL;
1050 }
1051
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001052 /* IT8603E does not have on/off mode */
1053 if (val == 0 && data->type == it8603)
1054 return -EINVAL;
1055
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001056 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (val == 0) {
1059 int tmp;
1060 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001061 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1062 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /* set on/off mode */
1064 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +01001065 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1066 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +01001067 } else {
1068 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001069 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001070 data->pwm_temp_map[nr] :
1071 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001072 else /* Automatic mode */
1073 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001074 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001075
1076 if (data->type != it8603) {
1077 /* set SmartGuardian mode */
1078 data->fan_main_ctrl |= (1 << nr);
1079 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1080 data->fan_main_ctrl);
1081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001084 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return count;
1086}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001087static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1088 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001090 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1091 int nr = sensor_attr->index;
1092
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001093 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001094 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001096 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return -EINVAL;
1098
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001099 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001100 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001101 /*
1102 * If we are in automatic mode, the PWM duty cycle register
1103 * is read-only so we can't write the value.
1104 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001105 if (data->pwm_ctrl[nr] & 0x80) {
1106 mutex_unlock(&data->update_lock);
1107 return -EBUSY;
1108 }
1109 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001110 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001111 data->pwm_duty[nr]);
1112 } else {
1113 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001114 /*
1115 * If we are in manual mode, write the duty cycle immediately;
1116 * otherwise, just store it for later use.
1117 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001118 if (!(data->pwm_ctrl[nr] & 0x80)) {
1119 data->pwm_ctrl[nr] = data->pwm_duty[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001120 it87_write_value(data, IT87_REG_PWM[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001121 data->pwm_ctrl[nr]);
1122 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001123 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001124 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return count;
1126}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001127static ssize_t set_pwm_freq(struct device *dev,
1128 struct device_attribute *attr, const char *buf, size_t count)
1129{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001130 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001131 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001132 int i;
1133
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001134 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001135 return -EINVAL;
1136
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001137 val = clamp_val(val, 0, 1000000);
1138 val *= has_newer_autopwm(data) ? 256 : 128;
1139
Jean Delvaref8d0c192007-02-14 21:15:02 +01001140 /* Search for the nearest available frequency */
1141 for (i = 0; i < 7; i++) {
1142 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1143 break;
1144 }
1145
1146 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001147 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001148 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001149 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001150 mutex_unlock(&data->update_lock);
1151
1152 return count;
1153}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001154static ssize_t show_pwm_temp_map(struct device *dev,
1155 struct device_attribute *attr, char *buf)
1156{
1157 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1158 int nr = sensor_attr->index;
1159
1160 struct it87_data *data = it87_update_device(dev);
1161 int map;
1162
1163 if (data->pwm_temp_map[nr] < 3)
1164 map = 1 << data->pwm_temp_map[nr];
1165 else
1166 map = 0; /* Should never happen */
1167 return sprintf(buf, "%d\n", map);
1168}
1169static ssize_t set_pwm_temp_map(struct device *dev,
1170 struct device_attribute *attr, const char *buf, size_t count)
1171{
1172 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1173 int nr = sensor_attr->index;
1174
1175 struct it87_data *data = dev_get_drvdata(dev);
1176 long val;
1177 u8 reg;
1178
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001179 /*
1180 * This check can go away if we ever support automatic fan speed
1181 * control on newer chips.
1182 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001183 if (!has_old_autopwm(data)) {
1184 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1185 return -EINVAL;
1186 }
1187
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001188 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001189 return -EINVAL;
1190
1191 switch (val) {
1192 case (1 << 0):
1193 reg = 0x00;
1194 break;
1195 case (1 << 1):
1196 reg = 0x01;
1197 break;
1198 case (1 << 2):
1199 reg = 0x02;
1200 break;
1201 default:
1202 return -EINVAL;
1203 }
1204
1205 mutex_lock(&data->update_lock);
1206 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001207 /*
1208 * If we are in automatic mode, write the temp mapping immediately;
1209 * otherwise, just store it for later use.
1210 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001211 if (data->pwm_ctrl[nr] & 0x80) {
1212 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001213 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001214 }
1215 mutex_unlock(&data->update_lock);
1216 return count;
1217}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001219static ssize_t show_auto_pwm(struct device *dev,
1220 struct device_attribute *attr, char *buf)
1221{
1222 struct it87_data *data = it87_update_device(dev);
1223 struct sensor_device_attribute_2 *sensor_attr =
1224 to_sensor_dev_attr_2(attr);
1225 int nr = sensor_attr->nr;
1226 int point = sensor_attr->index;
1227
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001228 return sprintf(buf, "%d\n",
1229 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001230}
1231
1232static ssize_t set_auto_pwm(struct device *dev,
1233 struct device_attribute *attr, const char *buf, size_t count)
1234{
1235 struct it87_data *data = dev_get_drvdata(dev);
1236 struct sensor_device_attribute_2 *sensor_attr =
1237 to_sensor_dev_attr_2(attr);
1238 int nr = sensor_attr->nr;
1239 int point = sensor_attr->index;
1240 long val;
1241
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001242 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001243 return -EINVAL;
1244
1245 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001246 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001247 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1248 data->auto_pwm[nr][point]);
1249 mutex_unlock(&data->update_lock);
1250 return count;
1251}
1252
1253static ssize_t show_auto_temp(struct device *dev,
1254 struct device_attribute *attr, char *buf)
1255{
1256 struct it87_data *data = it87_update_device(dev);
1257 struct sensor_device_attribute_2 *sensor_attr =
1258 to_sensor_dev_attr_2(attr);
1259 int nr = sensor_attr->nr;
1260 int point = sensor_attr->index;
1261
1262 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1263}
1264
1265static ssize_t set_auto_temp(struct device *dev,
1266 struct device_attribute *attr, const char *buf, size_t count)
1267{
1268 struct it87_data *data = dev_get_drvdata(dev);
1269 struct sensor_device_attribute_2 *sensor_attr =
1270 to_sensor_dev_attr_2(attr);
1271 int nr = sensor_attr->nr;
1272 int point = sensor_attr->index;
1273 long val;
1274
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001275 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001276 return -EINVAL;
1277
1278 mutex_lock(&data->update_lock);
1279 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1280 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1281 data->auto_temp[nr][point]);
1282 mutex_unlock(&data->update_lock);
1283 return count;
1284}
1285
Guenter Roecke1169ba2012-12-19 22:17:01 +01001286static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1287static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1288 0, 1);
1289static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1290 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Guenter Roecke1169ba2012-12-19 22:17:01 +01001292static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1293static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1294 1, 1);
1295static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1296 set_fan_div, 1);
1297
1298static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1299static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1300 2, 1);
1301static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1302 set_fan_div, 2);
1303
1304static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1305static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1306 3, 1);
1307
1308static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1309static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1310 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001312static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1313static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1314 5, 1);
1315
Guenter Roeckc4458db2012-12-19 22:17:02 +01001316static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1317 show_pwm_enable, set_pwm_enable, 0);
1318static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1319static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1320static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1321 show_pwm_temp_map, set_pwm_temp_map, 0);
1322static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1323 show_auto_pwm, set_auto_pwm, 0, 0);
1324static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1325 show_auto_pwm, set_auto_pwm, 0, 1);
1326static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1327 show_auto_pwm, set_auto_pwm, 0, 2);
1328static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1329 show_auto_pwm, NULL, 0, 3);
1330static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1331 show_auto_temp, set_auto_temp, 0, 1);
1332static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1333 show_auto_temp, set_auto_temp, 0, 0);
1334static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1335 show_auto_temp, set_auto_temp, 0, 2);
1336static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1337 show_auto_temp, set_auto_temp, 0, 3);
1338static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1339 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Guenter Roeckc4458db2012-12-19 22:17:02 +01001341static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1342 show_pwm_enable, set_pwm_enable, 1);
1343static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1344static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1345static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1346 show_pwm_temp_map, set_pwm_temp_map, 1);
1347static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1348 show_auto_pwm, set_auto_pwm, 1, 0);
1349static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1350 show_auto_pwm, set_auto_pwm, 1, 1);
1351static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1352 show_auto_pwm, set_auto_pwm, 1, 2);
1353static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1354 show_auto_pwm, NULL, 1, 3);
1355static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1356 show_auto_temp, set_auto_temp, 1, 1);
1357static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1358 show_auto_temp, set_auto_temp, 1, 0);
1359static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1360 show_auto_temp, set_auto_temp, 1, 2);
1361static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1362 show_auto_temp, set_auto_temp, 1, 3);
1363static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1364 show_auto_temp, set_auto_temp, 1, 4);
1365
1366static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1367 show_pwm_enable, set_pwm_enable, 2);
1368static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1369static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1370static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1371 show_pwm_temp_map, set_pwm_temp_map, 2);
1372static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1373 show_auto_pwm, set_auto_pwm, 2, 0);
1374static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1375 show_auto_pwm, set_auto_pwm, 2, 1);
1376static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1377 show_auto_pwm, set_auto_pwm, 2, 2);
1378static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1379 show_auto_pwm, NULL, 2, 3);
1380static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1381 show_auto_temp, set_auto_temp, 2, 1);
1382static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1383 show_auto_temp, set_auto_temp, 2, 0);
1384static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1385 show_auto_temp, set_auto_temp, 2, 2);
1386static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1387 show_auto_temp, set_auto_temp, 2, 3);
1388static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1389 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Guenter Roeck36c4d982015-03-26 18:18:19 -07001391static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1392 show_pwm_enable, set_pwm_enable, 3);
1393static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
1394static DEVICE_ATTR(pwm4_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1395static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO | S_IWUSR,
1396 show_pwm_temp_map, set_pwm_temp_map, 3);
1397
1398static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1399 show_pwm_enable, set_pwm_enable, 4);
1400static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
1401static DEVICE_ATTR(pwm5_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1402static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO | S_IWUSR,
1403 show_pwm_temp_map, set_pwm_temp_map, 4);
1404
1405static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1406 show_pwm_enable, set_pwm_enable, 5);
1407static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
1408static DEVICE_ATTR(pwm6_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1409static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO | S_IWUSR,
1410 show_pwm_temp_map, set_pwm_temp_map, 5);
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001413static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1414 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001417 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
Jean Delvare1d66c642005-04-18 21:16:59 -07001419static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Jean Delvare0124dd72007-11-25 16:16:41 +01001421static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1422 char *buf)
1423{
1424 int bitnr = to_sensor_dev_attr(attr)->index;
1425 struct it87_data *data = it87_update_device(dev);
1426 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1427}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001428
1429static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1430 *attr, const char *buf, size_t count)
1431{
1432 struct it87_data *data = dev_get_drvdata(dev);
1433 long val;
1434 int config;
1435
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001436 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001437 return -EINVAL;
1438
1439 mutex_lock(&data->update_lock);
1440 config = it87_read_value(data, IT87_REG_CONFIG);
1441 if (config < 0) {
1442 count = config;
1443 } else {
1444 config |= 1 << 5;
1445 it87_write_value(data, IT87_REG_CONFIG, config);
1446 /* Invalidate cache to force re-read */
1447 data->valid = 0;
1448 }
1449 mutex_unlock(&data->update_lock);
1450
1451 return count;
1452}
1453
Jean Delvare0124dd72007-11-25 16:16:41 +01001454static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1455static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1456static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1457static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1458static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1459static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1460static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1461static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1462static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1463static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1464static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1465static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1466static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001467static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001468static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1469static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1470static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001471static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1472 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001473
Jean Delvared9b327c2010-03-05 22:17:17 +01001474static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1475 char *buf)
1476{
1477 int bitnr = to_sensor_dev_attr(attr)->index;
1478 struct it87_data *data = it87_update_device(dev);
1479 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1480}
1481static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1482 const char *buf, size_t count)
1483{
1484 int bitnr = to_sensor_dev_attr(attr)->index;
1485 struct it87_data *data = dev_get_drvdata(dev);
1486 long val;
1487
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001488 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001489 || (val != 0 && val != 1))
1490 return -EINVAL;
1491
1492 mutex_lock(&data->update_lock);
1493 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1494 if (val)
1495 data->beeps |= (1 << bitnr);
1496 else
1497 data->beeps &= ~(1 << bitnr);
1498 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1499 mutex_unlock(&data->update_lock);
1500 return count;
1501}
1502
1503static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1504 show_beep, set_beep, 1);
1505static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1506static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1507static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1508static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1509static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1510static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1511static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1512/* fanX_beep writability is set later */
1513static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1514static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1515static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1516static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1517static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001518static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001519static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1520 show_beep, set_beep, 2);
1521static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1522static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1523
Jean Delvare5f2dc792010-03-05 22:17:18 +01001524static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1525 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Jean Delvare90d66192007-10-08 18:24:35 +02001527 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001528 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001530static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1531 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001533 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001534 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001536 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001537 return -EINVAL;
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 data->vrm = val;
1540
1541 return count;
1542}
1543static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Jean Delvare5f2dc792010-03-05 22:17:18 +01001545static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1546 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 struct it87_data *data = it87_update_device(dev);
1549 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1550}
1551static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001552
Jean Delvare738e5e02010-08-14 21:08:50 +02001553static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1554 char *buf)
1555{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001556 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001557 "+5V",
1558 "5VSB",
1559 "Vbat",
1560 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001561 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001562 "+3.3V",
1563 "3VSB",
1564 "Vbat",
1565 };
1566 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001567 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001568 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001569
Justin Maggardead80802015-08-05 12:53:08 -07001570 if (has_12mv_adc(data) || has_10_9mv_adc(data))
1571 label = labels_it8721[nr];
1572 else
1573 label = labels[nr];
1574
1575 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001576}
1577static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1578static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1579static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001580/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001581static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001582
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001583static ssize_t show_name(struct device *dev, struct device_attribute
1584 *devattr, char *buf)
1585{
1586 struct it87_data *data = dev_get_drvdata(dev);
1587 return sprintf(buf, "%s\n", data->name);
1588}
1589static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1590
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001591static struct attribute *it87_attributes_in[10][5] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001592{
Jean Delvare87808be2006-09-24 21:17:13 +02001593 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001594 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001595 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001596 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001597 NULL
1598}, {
1599 &sensor_dev_attr_in1_input.dev_attr.attr,
1600 &sensor_dev_attr_in1_min.dev_attr.attr,
1601 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001602 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001603 NULL
1604}, {
1605 &sensor_dev_attr_in2_input.dev_attr.attr,
1606 &sensor_dev_attr_in2_min.dev_attr.attr,
1607 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001608 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001609 NULL
1610}, {
1611 &sensor_dev_attr_in3_input.dev_attr.attr,
1612 &sensor_dev_attr_in3_min.dev_attr.attr,
1613 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001614 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001615 NULL
1616}, {
1617 &sensor_dev_attr_in4_input.dev_attr.attr,
1618 &sensor_dev_attr_in4_min.dev_attr.attr,
1619 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001620 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001621 NULL
1622}, {
1623 &sensor_dev_attr_in5_input.dev_attr.attr,
1624 &sensor_dev_attr_in5_min.dev_attr.attr,
1625 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001626 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001627 NULL
1628}, {
1629 &sensor_dev_attr_in6_input.dev_attr.attr,
1630 &sensor_dev_attr_in6_min.dev_attr.attr,
1631 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001632 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001633 NULL
1634}, {
1635 &sensor_dev_attr_in7_input.dev_attr.attr,
1636 &sensor_dev_attr_in7_min.dev_attr.attr,
1637 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001638 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001639 NULL
1640}, {
1641 &sensor_dev_attr_in8_input.dev_attr.attr,
1642 NULL
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001643}, {
1644 &sensor_dev_attr_in9_input.dev_attr.attr,
1645 NULL
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001646} };
Jean Delvare87808be2006-09-24 21:17:13 +02001647
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001648static const struct attribute_group it87_group_in[10] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001649 { .attrs = it87_attributes_in[0] },
1650 { .attrs = it87_attributes_in[1] },
1651 { .attrs = it87_attributes_in[2] },
1652 { .attrs = it87_attributes_in[3] },
1653 { .attrs = it87_attributes_in[4] },
1654 { .attrs = it87_attributes_in[5] },
1655 { .attrs = it87_attributes_in[6] },
1656 { .attrs = it87_attributes_in[7] },
1657 { .attrs = it87_attributes_in[8] },
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001658 { .attrs = it87_attributes_in[9] },
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001659};
1660
Guenter Roeck4573acb2012-03-26 16:17:41 -07001661static struct attribute *it87_attributes_temp[3][6] = {
1662{
Jean Delvare87808be2006-09-24 21:17:13 +02001663 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001664 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001665 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001666 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001667 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001668 NULL
1669} , {
1670 &sensor_dev_attr_temp2_input.dev_attr.attr,
1671 &sensor_dev_attr_temp2_max.dev_attr.attr,
1672 &sensor_dev_attr_temp2_min.dev_attr.attr,
1673 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001674 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001675 NULL
1676} , {
1677 &sensor_dev_attr_temp3_input.dev_attr.attr,
1678 &sensor_dev_attr_temp3_max.dev_attr.attr,
1679 &sensor_dev_attr_temp3_min.dev_attr.attr,
1680 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001681 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001682 NULL
1683} };
Jean Delvare87808be2006-09-24 21:17:13 +02001684
Guenter Roeck4573acb2012-03-26 16:17:41 -07001685static const struct attribute_group it87_group_temp[3] = {
1686 { .attrs = it87_attributes_temp[0] },
1687 { .attrs = it87_attributes_temp[1] },
1688 { .attrs = it87_attributes_temp[2] },
1689};
1690
Guenter Roeck161d8982012-12-19 22:17:01 +01001691static struct attribute *it87_attributes_temp_offset[] = {
1692 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1693 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1694 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1695};
1696
Guenter Roeck4573acb2012-03-26 16:17:41 -07001697static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001698 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001699 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001700 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001701 NULL
1702};
1703
1704static const struct attribute_group it87_group = {
1705 .attrs = it87_attributes,
1706};
1707
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001708static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001709 &sensor_dev_attr_in0_beep.dev_attr.attr,
1710 &sensor_dev_attr_in1_beep.dev_attr.attr,
1711 &sensor_dev_attr_in2_beep.dev_attr.attr,
1712 &sensor_dev_attr_in3_beep.dev_attr.attr,
1713 &sensor_dev_attr_in4_beep.dev_attr.attr,
1714 &sensor_dev_attr_in5_beep.dev_attr.attr,
1715 &sensor_dev_attr_in6_beep.dev_attr.attr,
1716 &sensor_dev_attr_in7_beep.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001717 NULL,
1718 NULL,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001719};
Jean Delvared9b327c2010-03-05 22:17:17 +01001720
Guenter Roeck4573acb2012-03-26 16:17:41 -07001721static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001722 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1723 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1724 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001725};
1726
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001727static struct attribute *it87_attributes_fan[6][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001728 &sensor_dev_attr_fan1_input.dev_attr.attr,
1729 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001730 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1731 NULL
1732}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001733 &sensor_dev_attr_fan2_input.dev_attr.attr,
1734 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001735 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1736 NULL
1737}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001738 &sensor_dev_attr_fan3_input.dev_attr.attr,
1739 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001740 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001741 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001742}, {
1743 &sensor_dev_attr_fan4_input.dev_attr.attr,
1744 &sensor_dev_attr_fan4_min.dev_attr.attr,
1745 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1746 NULL
1747}, {
1748 &sensor_dev_attr_fan5_input.dev_attr.attr,
1749 &sensor_dev_attr_fan5_min.dev_attr.attr,
1750 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1751 NULL
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001752}, {
1753 &sensor_dev_attr_fan6_input.dev_attr.attr,
1754 &sensor_dev_attr_fan6_min.dev_attr.attr,
1755 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1756 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001757} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001758
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001759static const struct attribute_group it87_group_fan[6] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001760 { .attrs = it87_attributes_fan[0] },
1761 { .attrs = it87_attributes_fan[1] },
1762 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001763 { .attrs = it87_attributes_fan[3] },
1764 { .attrs = it87_attributes_fan[4] },
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001765 { .attrs = it87_attributes_fan[5] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001766};
1767
Guenter Roecke1169ba2012-12-19 22:17:01 +01001768static const struct attribute *it87_attributes_fan_div[] = {
1769 &sensor_dev_attr_fan1_div.dev_attr.attr,
1770 &sensor_dev_attr_fan2_div.dev_attr.attr,
1771 &sensor_dev_attr_fan3_div.dev_attr.attr,
1772};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001773
Guenter Roeck36c4d982015-03-26 18:18:19 -07001774static struct attribute *it87_attributes_pwm[6][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001775 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001776 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001777 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001778 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001779 NULL
1780}, {
1781 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1782 &sensor_dev_attr_pwm2.dev_attr.attr,
1783 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001784 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001785 NULL
1786}, {
1787 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1788 &sensor_dev_attr_pwm3.dev_attr.attr,
1789 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001790 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001791 NULL
Guenter Roeck36c4d982015-03-26 18:18:19 -07001792}, {
1793 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1794 &sensor_dev_attr_pwm4.dev_attr.attr,
1795 &dev_attr_pwm4_freq.attr,
1796 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1797 NULL
1798}, {
1799 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1800 &sensor_dev_attr_pwm5.dev_attr.attr,
1801 &dev_attr_pwm5_freq.attr,
1802 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
1803 NULL
1804}, {
1805 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
1806 &sensor_dev_attr_pwm6.dev_attr.attr,
1807 &dev_attr_pwm6_freq.attr,
1808 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
1809 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001810} };
Jean Delvare87808be2006-09-24 21:17:13 +02001811
Guenter Roeck36c4d982015-03-26 18:18:19 -07001812static const struct attribute_group it87_group_pwm[6] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001813 { .attrs = it87_attributes_pwm[0] },
1814 { .attrs = it87_attributes_pwm[1] },
1815 { .attrs = it87_attributes_pwm[2] },
Guenter Roeck36c4d982015-03-26 18:18:19 -07001816 { .attrs = it87_attributes_pwm[3] },
1817 { .attrs = it87_attributes_pwm[4] },
1818 { .attrs = it87_attributes_pwm[5] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001819};
1820
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001821static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1822 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1823 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1824 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1825 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1826 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1827 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1828 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1829 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1830 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1831 NULL
1832}, {
1833 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1834 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1835 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1836 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1837 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1838 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1839 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1840 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1841 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1842 NULL
1843}, {
1844 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1845 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1846 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1847 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1848 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1849 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1850 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1851 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1852 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1853 NULL
1854} };
1855
1856static const struct attribute_group it87_group_autopwm[3] = {
1857 { .attrs = it87_attributes_autopwm[0] },
1858 { .attrs = it87_attributes_autopwm[1] },
1859 { .attrs = it87_attributes_autopwm[2] },
1860};
1861
Jean Delvared9b327c2010-03-05 22:17:17 +01001862static struct attribute *it87_attributes_fan_beep[] = {
1863 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1864 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1865 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1866 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1867 &sensor_dev_attr_fan5_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001868 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001869};
1870
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001871static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001872 &dev_attr_vrm.attr,
1873 &dev_attr_cpu0_vid.attr,
1874 NULL
1875};
1876
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001877static const struct attribute_group it87_group_vid = {
1878 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001879};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Jean Delvare738e5e02010-08-14 21:08:50 +02001881static struct attribute *it87_attributes_label[] = {
1882 &sensor_dev_attr_in3_label.dev_attr.attr,
1883 &sensor_dev_attr_in7_label.dev_attr.attr,
1884 &sensor_dev_attr_in8_label.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001885 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare738e5e02010-08-14 21:08:50 +02001886 NULL
1887};
1888
1889static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001890 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001891};
1892
Jean Delvare2d8672c2005-07-19 23:56:35 +02001893/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001894static int __init it87_find(unsigned short *address,
1895 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001897 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001898 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001899 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001900 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001902 err = superio_enter();
1903 if (err)
1904 return err;
1905
1906 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001907 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001908
1909 switch (chip_type) {
1910 case IT8705F_DEVID:
1911 sio_data->type = it87;
1912 break;
1913 case IT8712F_DEVID:
1914 sio_data->type = it8712;
1915 break;
1916 case IT8716F_DEVID:
1917 case IT8726F_DEVID:
1918 sio_data->type = it8716;
1919 break;
1920 case IT8718F_DEVID:
1921 sio_data->type = it8718;
1922 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001923 case IT8720F_DEVID:
1924 sio_data->type = it8720;
1925 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001926 case IT8721F_DEVID:
1927 sio_data->type = it8721;
1928 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001929 case IT8728F_DEVID:
1930 sio_data->type = it8728;
1931 break;
Justin Maggardead80802015-08-05 12:53:08 -07001932 case IT8732F_DEVID:
1933 sio_data->type = it8732;
1934 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06001935 case IT8771E_DEVID:
1936 sio_data->type = it8771;
1937 break;
1938 case IT8772E_DEVID:
1939 sio_data->type = it8772;
1940 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08001941 case IT8781F_DEVID:
1942 sio_data->type = it8781;
1943 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001944 case IT8782F_DEVID:
1945 sio_data->type = it8782;
1946 break;
1947 case IT8783E_DEVID:
1948 sio_data->type = it8783;
1949 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001950 case IT8786E_DEVID:
1951 sio_data->type = it8786;
1952 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07001953 case IT8790E_DEVID:
1954 sio_data->type = it8790;
1955 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02001956 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02001957 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001958 sio_data->type = it8603;
1959 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08001960 case IT8620E_DEVID:
1961 sio_data->type = it8620;
1962 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001963 case 0xffff: /* No device at all */
1964 goto exit;
1965 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001966 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001967 goto exit;
1968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Jean Delvare87673dd2006-08-28 14:37:19 +02001970 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001972 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 goto exit;
1974 }
1975
1976 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1977 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001978 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 goto exit;
1980 }
1981
1982 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001983 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07001984 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
1985 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001986 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001988 config = &it87_devices[sio_data->type];
1989
Guenter Roeck7f5726c2015-03-26 08:49:18 -07001990 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001991 if (has_in7_internal(config))
Guenter Roeck7f5726c2015-03-26 08:49:18 -07001992 sio_data->internal |= (1 << 1);
1993
Jean Delvare738e5e02010-08-14 21:08:50 +02001994 /* in8 (Vbat) is always internal */
Guenter Roeck7f5726c2015-03-26 08:49:18 -07001995 sio_data->internal |= (1 << 2);
1996
Guenter Roeck73055402015-03-26 09:16:32 -07001997 /* in9 (AVCC3), always internal if supported */
1998 if (has_avcc3(config))
1999 sio_data->internal |= (1 << 3); /* in9 is AVCC */
2000 else
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002001 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02002002
Guenter Roeck36c4d982015-03-26 18:18:19 -07002003 if (!has_six_pwm(config))
2004 sio_data->skip_pwm |= (1 << 3) | (1 << 4) | (1 << 5);
2005
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002006 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01002007 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01002008
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002009 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2010 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01002011 /* The IT8705F has a different LD number for GPIO */
2012 superio_select(5);
2013 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002014 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002015 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002016
Guenter Roeck0531d982012-03-02 11:46:44 -08002017 superio_select(GPIO);
2018
2019 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
2020 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002021 reg2a = superio_inb(IT87_SIO_PINX1_REG);
2022 reg2c = superio_inb(IT87_SIO_PINX2_REG);
2023 regef = superio_inb(IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002024
Guenter Roeck0531d982012-03-02 11:46:44 -08002025 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002026 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08002027 sio_data->skip_fan |= (1 << 2);
2028 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002029 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08002030 sio_data->skip_pwm |= (1 << 2);
2031
2032 /* Check if fan2 is there or not */
2033 if (reg27 & (1 << 7))
2034 sio_data->skip_fan |= (1 << 1);
2035 if (reg27 & (1 << 3))
2036 sio_data->skip_pwm |= (1 << 1);
2037
2038 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002039 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002040 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002041
2042 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002043 if (reg27 & (1 << 1))
2044 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002045
2046 /*
2047 * VIN7
2048 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2049 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002050 if (reg27 & (1 << 2)) {
2051 /*
2052 * The data sheet is a bit unclear regarding the
2053 * internal voltage divider for VCCH5V. It says
2054 * "This bit enables and switches VIN7 (pin 91) to the
2055 * internal voltage divider for VCCH5V".
2056 * This is different to other chips, where the internal
2057 * voltage divider would connect VIN7 to an internal
2058 * voltage source. Maybe that is the case here as well.
2059 *
2060 * Since we don't know for sure, re-route it if that is
2061 * not the case, and ask the user to report if the
2062 * resulting voltage is sane.
2063 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002064 if (!(reg2c & (1 << 1))) {
2065 reg2c |= (1 << 1);
2066 superio_outb(IT87_SIO_PINX2_REG, reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002067 pr_notice("Routing internal VCCH5V to in7.\n");
2068 }
2069 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2070 pr_notice("Please report if it displays a reasonable voltage.\n");
2071 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002072
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002073 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08002074 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002075 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08002076 sio_data->internal |= (1 << 1);
2077
2078 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002079 } else if (sio_data->type == it8603) {
2080 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002081
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002082 superio_select(GPIO);
2083
2084 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
2085
2086 /* Check if fan3 is there or not */
2087 if (reg27 & (1 << 6))
2088 sio_data->skip_pwm |= (1 << 2);
2089 if (reg27 & (1 << 7))
2090 sio_data->skip_fan |= (1 << 2);
2091
2092 /* Check if fan2 is there or not */
2093 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
2094 if (reg29 & (1 << 1))
2095 sio_data->skip_pwm |= (1 << 1);
2096 if (reg29 & (1 << 2))
2097 sio_data->skip_fan |= (1 << 1);
2098
2099 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2100 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2101
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002102 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002103 } else if (sio_data->type == it8620) {
2104 int reg;
2105
2106 superio_select(GPIO);
2107
Guenter Roeck36c4d982015-03-26 18:18:19 -07002108 /* Check for pwm5 */
2109 reg = superio_inb(IT87_SIO_GPIO1_REG);
2110 if (reg & (1 << 6))
2111 sio_data->skip_pwm |= (1 << 4);
2112
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002113 /* Check for fan4, fan5 */
2114 reg = superio_inb(IT87_SIO_GPIO2_REG);
2115 if (!(reg & (1 << 5)))
2116 sio_data->skip_fan |= (1 << 3);
2117 if (!(reg & (1 << 4)))
2118 sio_data->skip_fan |= (1 << 4);
2119
2120 /* Check for pwm3, fan3 */
2121 reg = superio_inb(IT87_SIO_GPIO3_REG);
2122 if (reg & (1 << 6))
2123 sio_data->skip_pwm |= (1 << 2);
2124 if (reg & (1 << 7))
2125 sio_data->skip_fan |= (1 << 2);
2126
Guenter Roeck36c4d982015-03-26 18:18:19 -07002127 /* Check for pwm4 */
2128 reg = superio_inb(IT87_SIO_GPIO4_REG);
2129 if (!(reg & (1 << 2)))
2130 sio_data->skip_pwm |= (1 << 3);
2131
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002132 /* Check for pwm2, fan2 */
2133 reg = superio_inb(IT87_SIO_GPIO5_REG);
2134 if (reg & (1 << 1))
2135 sio_data->skip_pwm |= (1 << 1);
2136 if (reg & (1 << 2))
2137 sio_data->skip_fan |= (1 << 1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002138 /* Check for pwm6, fan6 */
2139 if (!(reg & (1 << 7))) {
2140 sio_data->skip_pwm |= (1 << 5);
2141 sio_data->skip_fan |= (1 << 5);
2142 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002143
2144 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002145 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002146 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002147 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002148
2149 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002150
Jean Delvare895ff262009-12-09 20:35:47 +01002151 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002152 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002153 /* We need at least 4 VID pins */
2154 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002155 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002156 sio_data->skip_vid = 1;
2157 }
Jean Delvare895ff262009-12-09 20:35:47 +01002158 }
2159
Jean Delvare591ec652009-12-09 20:35:48 +01002160 /* Check if fan3 is there or not */
2161 if (reg & (1 << 6))
2162 sio_data->skip_pwm |= (1 << 2);
2163 if (reg & (1 << 7))
2164 sio_data->skip_fan |= (1 << 2);
2165
2166 /* Check if fan2 is there or not */
2167 reg = superio_inb(IT87_SIO_GPIO5_REG);
2168 if (reg & (1 << 1))
2169 sio_data->skip_pwm |= (1 << 1);
2170 if (reg & (1 << 2))
2171 sio_data->skip_fan |= (1 << 1);
2172
Jean Delvare895ff262009-12-09 20:35:47 +01002173 if ((sio_data->type == it8718 || sio_data->type == it8720)
2174 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002175 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002176
2177 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002178
2179 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2180
Jean Delvare436cad22010-07-09 16:22:48 +02002181 /*
2182 * The IT8720F has no VIN7 pin, so VCCH should always be
2183 * routed internally to VIN7 with an internal divider.
2184 * Curiously, there still is a configuration bit to control
2185 * this, which means it can be set incorrectly. And even
2186 * more curiously, many boards out there are improperly
2187 * configured, even though the IT8720F datasheet claims
2188 * that the internal routing of VCCH to VIN7 is the default
2189 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002190 *
2191 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002192 * If UART6 is enabled, re-route VIN7 to the internal divider
2193 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002194 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002195 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02002196 reg |= (1 << 1);
2197 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002198 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002199 }
Jean Delvare87673dd2006-08-28 14:37:19 +02002200 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02002201 sio_data->internal |= (1 << 0);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002202 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02002203 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002204
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002205 /*
2206 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2207 * While VIN7 can be routed to the internal voltage divider,
2208 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002209 *
2210 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2211 * is the temperature source. Since we can not read the
2212 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002213 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002214 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002215 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002216 sio_data->skip_temp |= (1 << 2);
2217 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002218
Jean Delvared9b327c2010-03-05 22:17:17 +01002219 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002220 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002221 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002222 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002223
Jean Delvare98dd22c2008-10-09 15:33:58 +02002224 /* Disable specific features based on DMI strings */
2225 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2226 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2227 if (board_vendor && board_name) {
2228 if (strcmp(board_vendor, "nVIDIA") == 0
2229 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002230 /*
2231 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2232 * connected to a fan, but to something else. One user
2233 * has reported instant system power-off when changing
2234 * the PWM2 duty cycle, so we disable it.
2235 * I use the board name string as the trigger in case
2236 * the same board is ever used in other systems.
2237 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002238 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002239 sio_data->skip_pwm = (1 << 1);
2240 }
2241 }
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243exit:
2244 superio_exit();
2245 return err;
2246}
2247
Jean Delvare723a0aa2010-03-05 22:17:16 +01002248static void it87_remove_files(struct device *dev)
2249{
2250 struct it87_data *data = platform_get_drvdata(pdev);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002251 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002252 int i;
2253
2254 sysfs_remove_group(&dev->kobj, &it87_group);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002255 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002256 if (sio_data->skip_in & (1 << i))
2257 continue;
2258 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2259 if (it87_attributes_in_beep[i])
2260 sysfs_remove_file(&dev->kobj,
2261 it87_attributes_in_beep[i]);
2262 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002263 for (i = 0; i < 3; i++) {
2264 if (!(data->has_temp & (1 << i)))
2265 continue;
2266 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01002267 if (has_temp_offset(data))
2268 sysfs_remove_file(&dev->kobj,
2269 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002270 if (sio_data->beep_pin)
2271 sysfs_remove_file(&dev->kobj,
2272 it87_attributes_temp_beep[i]);
2273 }
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002274 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002275 if (!(data->has_fan & (1 << i)))
2276 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002277 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002278 if (sio_data->beep_pin)
2279 sysfs_remove_file(&dev->kobj,
2280 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002281 if (i < 3 && !has_16bit_fans(data))
2282 sysfs_remove_file(&dev->kobj,
2283 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002284 }
Guenter Roeck36c4d982015-03-26 18:18:19 -07002285 for (i = 0; i < 6; i++) {
Guenter Roeck1696d1d2015-03-27 06:03:41 -07002286 if (sio_data->skip_pwm & (1 << i))
Jean Delvare723a0aa2010-03-05 22:17:16 +01002287 continue;
2288 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002289 if (has_old_autopwm(data))
2290 sysfs_remove_group(&dev->kobj,
2291 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002292 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002293 if (!sio_data->skip_vid)
2294 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02002295 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002296}
2297
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002298static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002301 struct resource *res;
2302 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002303 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002304 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01002306 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002308 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002309 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2310 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002311 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2312 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002313 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002314 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Guenter Roeck62a1d052012-03-24 21:54:41 -07002317 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2318 if (!data)
2319 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002321 data->addr = res->start;
2322 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002323 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002324 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002325 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002326 data->name = it87_devices[sio_data->type].name;
2327 /*
2328 * IT8705F Datasheet 0.4.1, 3h == Version G.
2329 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2330 * These are the first revisions with 16-bit tachometer support.
2331 */
2332 switch (data->type) {
2333 case it87:
2334 if (sio_data->revision >= 0x03) {
2335 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002336 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002337 }
2338 break;
2339 case it8712:
2340 if (sio_data->revision >= 0x08) {
2341 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002342 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2343 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002344 }
2345 break;
2346 default:
2347 break;
2348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002351 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002352 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2353 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002355 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002357 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002360 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002362 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002363 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002364 if (sio_data->internal & (1 << 0))
2365 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2366 if (sio_data->internal & (1 << 1))
2367 data->in_scaled |= (1 << 7); /* in7 is VSB */
2368 if (sio_data->internal & (1 << 2))
2369 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002370 if (sio_data->internal & (1 << 3))
2371 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002372 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2373 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002374 if (sio_data->internal & (1 << 0))
2375 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2376 if (sio_data->internal & (1 << 1))
2377 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002378 }
2379
Guenter Roeck4573acb2012-03-26 16:17:41 -07002380 data->has_temp = 0x07;
2381 if (sio_data->skip_temp & (1 << 2)) {
2382 if (sio_data->type == it8782
2383 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2384 data->has_temp &= ~(1 << 2);
2385 }
2386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002388 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002391 err = sysfs_create_group(&dev->kobj, &it87_group);
2392 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002393 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002394
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002395 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002396 if (sio_data->skip_in & (1 << i))
2397 continue;
2398 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2399 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002400 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002401 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2402 err = sysfs_create_file(&dev->kobj,
2403 it87_attributes_in_beep[i]);
2404 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002405 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002406 }
2407 }
2408
Guenter Roeck4573acb2012-03-26 16:17:41 -07002409 for (i = 0; i < 3; i++) {
2410 if (!(data->has_temp & (1 << i)))
2411 continue;
2412 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002413 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002414 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002415 if (has_temp_offset(data)) {
2416 err = sysfs_create_file(&dev->kobj,
2417 it87_attributes_temp_offset[i]);
2418 if (err)
2419 goto error;
2420 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002421 if (sio_data->beep_pin) {
2422 err = sysfs_create_file(&dev->kobj,
2423 it87_attributes_temp_beep[i]);
2424 if (err)
2425 goto error;
2426 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002427 }
2428
Jean Delvare9060f8b2006-08-28 14:24:17 +02002429 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002430 fan_beep_need_rw = 1;
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002431 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002432 if (!(data->has_fan & (1 << i)))
2433 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002434 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002435 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002436 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002437
Guenter Roecke1169ba2012-12-19 22:17:01 +01002438 if (i < 3 && !has_16bit_fans(data)) {
2439 err = sysfs_create_file(&dev->kobj,
2440 it87_attributes_fan_div[i]);
2441 if (err)
2442 goto error;
2443 }
2444
Jean Delvared9b327c2010-03-05 22:17:17 +01002445 if (sio_data->beep_pin) {
2446 err = sysfs_create_file(&dev->kobj,
2447 it87_attributes_fan_beep[i]);
2448 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002449 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002450 if (!fan_beep_need_rw)
2451 continue;
2452
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002453 /*
2454 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002455 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002456 * for it.
2457 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002458 if (sysfs_chmod_file(&dev->kobj,
2459 it87_attributes_fan_beep[i],
2460 S_IRUGO | S_IWUSR))
2461 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2462 i + 1);
2463 fan_beep_need_rw = 0;
2464 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002465 }
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (enable_pwm_interface) {
Guenter Roeck36c4d982015-03-26 18:18:19 -07002468 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002469 if (sio_data->skip_pwm & (1 << i))
2470 continue;
2471 err = sysfs_create_group(&dev->kobj,
2472 &it87_group_pwm[i]);
2473 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002474 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002475
2476 if (!has_old_autopwm(data))
2477 continue;
2478 err = sysfs_create_group(&dev->kobj,
2479 &it87_group_autopwm[i]);
2480 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002481 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484
Jean Delvare895ff262009-12-09 20:35:47 +01002485 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002486 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002487 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002488 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002489 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2490 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002491 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002492 }
2493
Jean Delvare738e5e02010-08-14 21:08:50 +02002494 /* Export labels for internal sensors */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002495 for (i = 0; i < 4; i++) {
Jean Delvare738e5e02010-08-14 21:08:50 +02002496 if (!(sio_data->internal & (1 << i)))
2497 continue;
2498 err = sysfs_create_file(&dev->kobj,
2499 it87_attributes_label[i]);
2500 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002501 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002502 }
2503
Tony Jones1beeffe2007-08-20 13:46:20 -07002504 data->hwmon_dev = hwmon_device_register(dev);
2505 if (IS_ERR(data->hwmon_dev)) {
2506 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002507 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 }
2509
2510 return 0;
2511
Guenter Roeck62a1d052012-03-24 21:54:41 -07002512error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002513 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return err;
2515}
2516
Bill Pemberton281dfd02012-11-19 13:25:51 -05002517static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002519 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Tony Jones1beeffe2007-08-20 13:46:20 -07002521 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002522 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return 0;
2525}
2526
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002527/*
2528 * Must be called with data->update_lock held, except during initialization.
2529 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2530 * would slow down the IT87 access and should not be necessary.
2531 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002532static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002534 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2535 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002538/*
2539 * Must be called with data->update_lock held, except during initialization.
2540 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2541 * would slow down the IT87 access and should not be necessary.
2542 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002543static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002545 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2546 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547}
2548
2549/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002550static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002552 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002553 /*
2554 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002556 * disable pwm control to protect the user.
2557 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002558 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if ((tmp & 0x87) == 0) {
2560 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002561 /*
2562 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002564 * inverting all fan speed values.
2565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 int i;
2567 u8 pwm[3];
2568
2569 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002570 pwm[i] = it87_read_value(data,
Guenter Roeck36c4d982015-03-26 18:18:19 -07002571 IT87_REG_PWM[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002573 /*
2574 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 * might be correct, as suspicious as it seems, so we
2576 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002577 * PWM interface).
2578 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002580 dev_info(dev,
2581 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002582 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 tmp | 0x87);
2584 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002585 it87_write_value(data,
Guenter Roeck36c4d982015-03-26 18:18:19 -07002586 IT87_REG_PWM[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 0x7f & ~pwm[i]);
2588 return 1;
2589 }
2590
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002591 dev_info(dev,
2592 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
2594
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002595 dev_info(dev,
2596 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return 0;
2598 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002599 dev_info(dev,
2600 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
2602
2603 return 1;
2604}
2605
2606/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002607static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002609 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002610 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002612 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002614 /*
2615 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002616 * - If it is in automatic mode, setting to manual mode should set
2617 * the fan to full speed by default.
2618 * - If it is in manual mode, we need a mapping to temperature
2619 * channels to use when later setting to automatic mode later.
2620 * Use a 1:1 mapping by default (we are clueless.)
2621 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002622 * prior to switching to a different mode.
2623 * Note that this is no longer needed for the IT8721F and later, as
2624 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002625 * manual duty cycle.
2626 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002628 data->pwm_temp_map[i] = i;
2629 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002630 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002633 /*
2634 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002635 * registers. For low voltage limits it makes no sense and triggers
2636 * alarms, so change to 0 instead. For high temperature limits, it
2637 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002638 * but is still confusing, so change to 127 degrees C.
2639 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002640 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002641 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002642 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002643 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002644 }
2645 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002646 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002647 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002648 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002649 }
2650
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002651 /*
2652 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002653 * set to two different sensor types and we can't guess which one
2654 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002655 * run-time through the temp{1-3}_type sysfs accessors if needed.
2656 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002659 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 if ((tmp & 0xff) == 0) {
2661 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002662 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 }
2664
2665 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002666 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002667 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002668 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002670 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002671 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2672 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002674 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002676 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2677
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002678 /* Set tachometers to 16-bit mode if needed */
2679 if (has_fan16_config(data)) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002680 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002681 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002682 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002683 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002684 tmp | 0x07);
2685 }
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002686 }
2687
2688 /* Check for additional fans */
2689 if (has_five_fans(data)) {
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002690 if (tmp & (1 << 4))
2691 data->has_fan |= (1 << 3); /* fan4 enabled */
2692 if (tmp & (1 << 5))
2693 data->has_fan |= (1 << 4); /* fan5 enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002694 if (has_six_fans(data) && (tmp & (1 << 2)))
2695 data->has_fan |= (1 << 5); /* fan6 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02002696 }
2697
Jean Delvare591ec652009-12-09 20:35:48 +01002698 /* Fan input pins may be used for alternative functions */
2699 data->has_fan &= ~sio_data->skip_fan;
2700
Guenter Roeck36c4d982015-03-26 18:18:19 -07002701 /* Check if pwm5, pwm6 are enabled */
2702 if (has_six_pwm(data)) {
2703 /* The following code may be IT8620E specific */
2704 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2705 if ((tmp & 0xc0) == 0xc0)
2706 sio_data->skip_pwm |= (1 << 4);
2707 if (!(tmp & (1 << 3)))
2708 sio_data->skip_pwm |= (1 << 5);
2709 }
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002712 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002713 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 | (update_vbat ? 0x41 : 0x01));
2715}
2716
Jean Delvareb99883d2010-03-05 22:17:15 +01002717static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2718{
Guenter Roeck36c4d982015-03-26 18:18:19 -07002719 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
Jean Delvare16b5dda2012-01-16 22:51:48 +01002720 if (has_newer_autopwm(data)) {
Guenter Roeck36c4d982015-03-26 18:18:19 -07002721 data->pwm_temp_map[nr] = (data->pwm_ctrl[nr] & 0x03) +
2722 nr < 3 ? 0 : 3;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002723 data->pwm_duty[nr] = it87_read_value(data,
Guenter Roeck36c4d982015-03-26 18:18:19 -07002724 IT87_REG_PWM_DUTY[nr]);
Jean Delvare6229cdb2010-12-08 16:27:22 +01002725 } else {
2726 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2727 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2728 else /* Manual mode */
2729 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2730 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002731
2732 if (has_old_autopwm(data)) {
2733 int i;
2734
2735 for (i = 0; i < 5 ; i++)
2736 data->auto_temp[nr][i] = it87_read_value(data,
2737 IT87_REG_AUTO_TEMP(nr, i));
2738 for (i = 0; i < 3 ; i++)
2739 data->auto_pwm[nr][i] = it87_read_value(data,
2740 IT87_REG_AUTO_PWM(nr, i));
2741 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002742}
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744static struct it87_data *it87_update_device(struct device *dev)
2745{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002746 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 int i;
2748
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002749 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2752 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002754 /*
2755 * Cleared after each update, so reenable. Value
2756 * returned by this read will be previous value
2757 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002758 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002759 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 }
2761 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002762 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002763 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002764 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002765 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002766 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002767 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
Jean Delvare3543a532006-08-28 14:27:25 +02002769 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002770 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Guenter Roeck73055402015-03-26 09:16:32 -07002771 if (has_avcc3(data))
2772 data->in[9][0] = it87_read_value(data, IT87_REG_AVCC3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002774 for (i = 0; i < 6; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002775 /* Skip disabled fans */
2776 if (!(data->has_fan & (1 << i)))
2777 continue;
2778
Guenter Roecke1169ba2012-12-19 22:17:01 +01002779 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002780 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002781 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002782 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002783 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002784 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002785 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002786 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002787 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002788 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
2791 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002792 if (!(data->has_temp & (1 << i)))
2793 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002794 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002795 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002796 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002797 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002798 data->temp[i][2] =
2799 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002800 if (has_temp_offset(data))
2801 data->temp[i][3] =
2802 it87_read_value(data,
2803 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
2805
Jean Delvare17d648b2006-08-28 14:23:46 +02002806 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002807 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002808 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002809 data->fan_div[0] = i & 0x07;
2810 data->fan_div[1] = (i >> 3) & 0x07;
2811 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002815 it87_read_value(data, IT87_REG_ALARM1) |
2816 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2817 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002818 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002819
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002820 data->fan_main_ctrl = it87_read_value(data,
2821 IT87_REG_FAN_MAIN_CTRL);
2822 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002823 for (i = 0; i < 6; i++)
Jean Delvareb99883d2010-03-05 22:17:15 +01002824 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002826 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002827 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002828 /*
2829 * The IT8705F does not have VID capability.
2830 * The IT8718F and later don't use IT87_REG_VID for the
2831 * same purpose.
2832 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002833 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002834 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002835 /*
2836 * The older IT8712F revisions had only 5 VID pins,
2837 * but we assume it is always safe to read 6 bits.
2838 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002839 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
2841 data->last_updated = jiffies;
2842 data->valid = 1;
2843 }
2844
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002845 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847 return data;
2848}
2849
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002850static int __init it87_device_add(unsigned short address,
2851 const struct it87_sio_data *sio_data)
2852{
2853 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002854 .start = address + IT87_EC_OFFSET,
2855 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002856 .name = DRVNAME,
2857 .flags = IORESOURCE_IO,
2858 };
2859 int err;
2860
Jean Delvareb9acb642009-01-07 16:37:35 +01002861 err = acpi_check_resource_conflict(&res);
2862 if (err)
2863 goto exit;
2864
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002865 pdev = platform_device_alloc(DRVNAME, address);
2866 if (!pdev) {
2867 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002868 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002869 goto exit;
2870 }
2871
2872 err = platform_device_add_resources(pdev, &res, 1);
2873 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002874 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002875 goto exit_device_put;
2876 }
2877
2878 err = platform_device_add_data(pdev, sio_data,
2879 sizeof(struct it87_sio_data));
2880 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002881 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002882 goto exit_device_put;
2883 }
2884
2885 err = platform_device_add(pdev);
2886 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002887 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002888 goto exit_device_put;
2889 }
2890
2891 return 0;
2892
2893exit_device_put:
2894 platform_device_put(pdev);
2895exit:
2896 return err;
2897}
2898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899static int __init sm_it87_init(void)
2900{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002901 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002902 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002903 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002904
Jean Delvare98dd22c2008-10-09 15:33:58 +02002905 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002906 err = it87_find(&isa_address, &sio_data);
2907 if (err)
2908 return err;
2909 err = platform_driver_register(&it87_driver);
2910 if (err)
2911 return err;
2912
2913 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002914 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002915 platform_driver_unregister(&it87_driver);
2916 return err;
2917 }
2918
2919 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
2922static void __exit sm_it87_exit(void)
2923{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002924 platform_device_unregister(pdev);
2925 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
2928
Jean Delvare7c81c602014-01-29 20:40:08 +01002929MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002930MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931module_param(update_vbat, bool, 0);
2932MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2933module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002934MODULE_PARM_DESC(fix_pwm_polarity,
2935 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936MODULE_LICENSE("GPL");
2937
2938module_init(sm_it87_init);
2939module_exit(sm_it87_exit);