blob: aa3ec50527a34cdddcccdba9e436c4aae30f024e [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
Guenter Roecke84bd952015-03-28 08:24:29 -070081static struct platform_device *it87_pdev[2];
corentin.labbeb74f3fd2007-06-13 20:27:36 +020082
Guenter Roeck3c2e3512015-03-28 08:03:10 -070083#define REG_2E 0x2e /* The register to read/write */
Guenter Roecke84bd952015-03-28 08:24:29 -070084#define REG_4E 0x4e /* Secondary register to read/write */
Guenter Roeck3c2e3512015-03-28 08:03:10 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define DEV 0x07 /* Register: Logical device select */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010088
89/* The device with the IT8718F/IT8720F VID value in it */
90#define GPIO 0x07
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define DEVID 0x20 /* Register: Device ID */
93#define DEVREV 0x22 /* Register: Device Revision */
94
Guenter Roeck3c2e3512015-03-28 08:03:10 -070095static inline int superio_inb(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Guenter Roeck3c2e3512015-03-28 08:03:10 -070097 outb(reg, ioreg);
98 return inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700101static inline void superio_outb(int ioreg, int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +0200102{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700103 outb(reg, ioreg);
104 outb(val, ioreg + 1);
Jean Delvare436cad22010-07-09 16:22:48 +0200105}
106
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700107static int superio_inw(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 int val;
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700110 outb(reg++, ioreg);
111 val = inb(ioreg + 1) << 8;
112 outb(reg, ioreg);
113 val |= inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return val;
115}
116
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700117static inline void superio_select(int ioreg, int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700119 outb(DEV, ioreg);
120 outb(ldn, ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700123static inline int superio_enter(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200125 /*
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700126 * Try to reserve ioreg and ioreg + 1 for exclusive access.
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200127 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700128 if (!request_muxed_region(ioreg, 2, DRVNAME))
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200129 return -EBUSY;
130
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700131 outb(0x87, ioreg);
132 outb(0x01, ioreg);
133 outb(0x55, ioreg);
Guenter Roecke84bd952015-03-28 08:24:29 -0700134 outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700138static inline void superio_exit(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700140 outb(0x02, ioreg);
141 outb(0x02, ioreg + 1);
142 release_region(ioreg, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Jean Delvare87673dd2006-08-28 14:37:19 +0200145/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define IT8712F_DEVID 0x8712
147#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200148#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200149#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100150#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200151#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400152#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100153#define IT8728F_DEVID 0x8728
Justin Maggardead80802015-08-05 12:53:08 -0700154#define IT8732F_DEVID 0x8732
Guenter Roeckb0636702012-09-07 17:34:41 -0600155#define IT8771E_DEVID 0x8771
156#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800157#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800158#define IT8782F_DEVID 0x8782
159#define IT8783E_DEVID 0x8783
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100160#define IT8786E_DEVID 0x8786
Guenter Roeck4ee07152015-03-25 23:26:28 -0700161#define IT8790E_DEVID 0x8790
Rudolf Marek7183ae82014-04-04 18:01:35 +0200162#define IT8603E_DEVID 0x8603
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800163#define IT8620E_DEVID 0x8620
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200164#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define IT87_ACT_REG 0x30
166#define IT87_BASE_REG 0x60
167
Jean Delvare87673dd2006-08-28 14:37:19 +0200168/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800169#define IT87_SIO_GPIO1_REG 0x25
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800170#define IT87_SIO_GPIO2_REG 0x26
Jean Delvare895ff262009-12-09 20:35:47 +0100171#define IT87_SIO_GPIO3_REG 0x27
Guenter Roeck36c4d982015-03-26 18:18:19 -0700172#define IT87_SIO_GPIO4_REG 0x28
Jean Delvare591ec652009-12-09 20:35:48 +0100173#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800174#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200175#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800176#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200177#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100178#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030181static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030184static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/* Many IT87 constants specified below */
187
188/* Length of ISA address segment */
189#define IT87_EXTENT 8
190
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500191/* Length of ISA address segment for Environmental Controller */
192#define IT87_EC_EXTENT 2
193
194/* Offset of EC registers from ISA base address */
195#define IT87_EC_OFFSET 5
196
197/* Where are the ISA address/data registers relative to the EC base address */
198#define IT87_ADDR_REG_OFFSET 0
199#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201/*----- The IT87 registers -----*/
202
203#define IT87_REG_CONFIG 0x00
204
205#define IT87_REG_ALARM1 0x01
206#define IT87_REG_ALARM2 0x02
207#define IT87_REG_ALARM3 0x03
208
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800209/*
210 * The IT8718F and IT8720F have the VID value in a different register, in
211 * Super-I/O configuration space.
212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800214/*
215 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
216 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
217 * mode.
218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200220#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Guenter Roeckcc18da72015-04-01 10:03:01 -0700222/* Monitors: 9 voltage (0 to 7, battery), 6 temp (1 to 6), 3 fan (1 to 3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700224static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
225static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
226static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
227static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
228static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#define IT87_REG_FAN_MAIN_CTRL 0x13
231#define IT87_REG_FAN_CTL 0x14
Guenter Roeck36c4d982015-03-26 18:18:19 -0700232static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
233static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Guenter Roeck559313c2015-04-01 10:15:38 -0700235static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
236 0x27, 0x28, 0x2f };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Guenter Roeck559313c2015-04-01 10:15:38 -0700238#define IT87_REG_TEMP(nr) (0x29 + (nr))
Guenter Roeck73055402015-03-26 09:16:32 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
241#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
242#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
243#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#define IT87_REG_VIN_ENABLE 0x50
246#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700247#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100248#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250#define IT87_REG_CHIPID 0x58
251
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100252#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
253#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
254
Guenter Roeckcc18da72015-04-01 10:03:01 -0700255#define IT87_REG_TEMP456_ENABLE 0x77
256
Guenter Roeck483db432012-12-19 22:17:02 +0100257struct it87_devices {
258 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700259 const char * const suffix;
Guenter Roeckcc18da72015-04-01 10:03:01 -0700260 u32 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100261 u8 peci_mask;
262 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100263};
264
265#define FEAT_12MV_ADC (1 << 0)
266#define FEAT_NEWER_AUTOPWM (1 << 1)
267#define FEAT_OLD_AUTOPWM (1 << 2)
268#define FEAT_16BIT_FANS (1 << 3)
269#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100270#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100271#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800272#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
273#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800274#define FEAT_VID (1 << 9) /* Set if chip supports VID */
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700275#define FEAT_IN7_INTERNAL (1 << 10) /* Set if in7 is internal */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700276#define FEAT_SIX_FANS (1 << 11) /* Supports six fans */
Justin Maggardead80802015-08-05 12:53:08 -0700277#define FEAT_10_9MV_ADC (1 << 12)
Guenter Roeck73055402015-03-26 09:16:32 -0700278#define FEAT_AVCC3 (1 << 13) /* Chip supports in9/AVCC3 */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700279#define FEAT_SIX_PWM (1 << 14) /* Chip supports 6 pwm chn */
Guenter Roeck60878bc2015-03-26 19:57:42 -0700280#define FEAT_PWM_FREQ2 (1 << 15) /* Separate pwm freq 2 */
Guenter Roeckcc18da72015-04-01 10:03:01 -0700281#define FEAT_SIX_TEMP (1 << 16) /* Up to 6 temp sensors */
Guenter Roeck483db432012-12-19 22:17:02 +0100282
283static const struct it87_devices it87_devices[] = {
284 [it87] = {
285 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700286 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100287 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
288 },
289 [it8712] = {
290 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700291 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800292 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
293 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100294 },
295 [it8716] = {
296 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700297 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800298 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700299 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
Guenter Roeck483db432012-12-19 22:17:02 +0100300 },
301 [it8718] = {
302 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700303 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800304 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700305 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
306 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100307 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100308 },
309 [it8720] = {
310 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700311 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800312 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700313 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
314 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100315 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100316 },
317 [it8721] = {
318 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700319 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100320 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800321 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck60878bc2015-03-26 19:57:42 -0700322 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
323 | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100324 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100325 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100326 },
327 [it8728] = {
328 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700329 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100330 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700331 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700332 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100333 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100334 },
Justin Maggardead80802015-08-05 12:53:08 -0700335 [it8732] = {
336 .name = "it8732",
337 .suffix = "F",
338 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
339 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
340 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
341 .peci_mask = 0x07,
342 .old_peci_mask = 0x02, /* Actually reports PCH */
343 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600344 [it8771] = {
345 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700346 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600347 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700348 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
349 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800350 /* PECI: guesswork */
351 /* 12mV ADC (OHM) */
352 /* 16 bit fans (OHM) */
353 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600354 .peci_mask = 0x07,
355 },
356 [it8772] = {
357 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700358 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600359 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700360 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
361 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800362 /* PECI (coreboot) */
363 /* 12mV ADC (HWSensors4, OHM) */
364 /* 16 bit fans (HWSensors4, OHM) */
365 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600366 .peci_mask = 0x07,
367 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800368 [it8781] = {
369 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700370 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800371 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700372 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800373 .old_peci_mask = 0x4,
374 },
Guenter Roeck483db432012-12-19 22:17:02 +0100375 [it8782] = {
376 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700377 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100378 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700379 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100380 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100381 },
382 [it8783] = {
383 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700384 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100385 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700386 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100387 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100388 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100389 [it8786] = {
390 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700391 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100392 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700393 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
394 | FEAT_PWM_FREQ2,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100395 .peci_mask = 0x07,
396 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700397 [it8790] = {
398 .name = "it8790",
399 .suffix = "E",
400 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700401 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
402 | FEAT_PWM_FREQ2,
Guenter Roeck4ee07152015-03-25 23:26:28 -0700403 .peci_mask = 0x07,
404 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100405 [it8603] = {
406 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700407 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100408 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck73055402015-03-26 09:16:32 -0700409 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
Guenter Roeck60878bc2015-03-26 19:57:42 -0700410 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100411 .peci_mask = 0x07,
412 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800413 [it8620] = {
414 .name = "it8620",
415 .suffix = "E",
416 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700417 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeckcc18da72015-04-01 10:03:01 -0700418 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
419 | FEAT_SIX_TEMP,
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800420 .peci_mask = 0x07,
421 },
Guenter Roeck483db432012-12-19 22:17:02 +0100422};
423
424#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
425#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
Justin Maggardead80802015-08-05 12:53:08 -0700426#define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
Guenter Roeck483db432012-12-19 22:17:02 +0100427#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
428#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
429#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100430#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
431 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100432#define has_temp_old_peci(data, nr) \
433 (((data)->features & FEAT_TEMP_OLD_PECI) && \
434 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800435#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700436#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
437 FEAT_SIX_FANS))
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800438#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700439#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700440#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Guenter Roeck73055402015-03-26 09:16:32 -0700441#define has_avcc3(data) ((data)->features & FEAT_AVCC3)
Guenter Roeck36c4d982015-03-26 18:18:19 -0700442#define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
Guenter Roeck60878bc2015-03-26 19:57:42 -0700443#define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
Guenter Roeckcc18da72015-04-01 10:03:01 -0700444#define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200446struct it87_sio_data {
447 enum chips type;
448 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200449 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200450 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100451 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200452 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100453 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700454 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100455 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100456 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200457 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700458 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200459};
460
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800461/*
462 * For each registered chip, we need to keep some data in memory.
463 * The structure is dynamically allocated.
464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465struct it87_data {
Guenter Roeck8638d0a2015-03-30 11:13:49 -0700466 const struct attribute_group *groups[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100468 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100469 u8 peci_mask;
470 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200472 unsigned short addr;
473 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100474 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 char valid; /* !=0 if following fields are valid */
476 unsigned long last_updated; /* In jiffies */
477
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200478 u16 in_scaled; /* Internal voltage sensors are scaled */
Guenter Roeckd3766842013-03-30 15:47:21 -0700479 u16 in_internal; /* Bitfield, internal sensors (for labels) */
Guenter Roeck52929712013-03-30 14:00:08 -0700480 u16 has_in; /* Bitfield, voltage sensors enabled */
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100481 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200482 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700483 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700484 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeckcc18da72015-04-01 10:03:01 -0700485 s8 temp[6][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100486 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
487 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 u8 fan_div[3]; /* Register encoding, shifted right */
Guenter Roeckd3766842013-03-30 15:47:21 -0700489 bool has_vid; /* True if VID supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100491 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 u32 alarms; /* Register encoding, combined */
Guenter Roeck52929712013-03-30 14:00:08 -0700493 bool has_beep; /* true if beep supported */
Jean Delvared9b327c2010-03-05 22:17:17 +0100494 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100496 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100497
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800498 /*
499 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100500 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
501 * 7, and we want to preserve settings on mode changes, so we have
502 * to track all values separately.
503 * Starting with the IT8721F, the manual PWM duty cycles are stored
504 * in separate registers (8-bit values), so the separate tracking
505 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800506 * simple.
507 */
Guenter Roeck5c391262013-03-30 15:02:12 -0700508 u8 has_pwm; /* Bitfield, pwm control enabled */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700509 u8 pwm_ctrl[6]; /* Register value */
510 u8 pwm_duty[6]; /* Manual PWM value set by user */
511 u8 pwm_temp_map[6]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100512
513 /* Automatic fan speed control registers */
514 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
515 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516};
517
Guenter Roeck0531d982012-03-02 11:46:44 -0800518static int adc_lsb(const struct it87_data *data, int nr)
519{
Justin Maggardead80802015-08-05 12:53:08 -0700520 int lsb;
521
522 if (has_12mv_adc(data))
523 lsb = 120;
524 else if (has_10_9mv_adc(data))
525 lsb = 109;
526 else
527 lsb = 160;
Guenter Roeck0531d982012-03-02 11:46:44 -0800528 if (data->in_scaled & (1 << nr))
529 lsb <<= 1;
530 return lsb;
531}
532
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200533static u8 in_to_reg(const struct it87_data *data, int nr, long val)
534{
Justin Maggardead80802015-08-05 12:53:08 -0700535 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800536 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200537}
538
539static int in_from_reg(const struct it87_data *data, int nr, int val)
540{
Justin Maggardead80802015-08-05 12:53:08 -0700541 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200542}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200543
544static inline u8 FAN_TO_REG(long rpm, int div)
545{
546 if (rpm == 0)
547 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800548 rpm = clamp_val(rpm, 1, 1000000);
549 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200550}
551
552static inline u16 FAN16_TO_REG(long rpm)
553{
554 if (rpm == 0)
555 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800556 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200557}
558
559#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
560 1350000 / ((val) * (div)))
561/* The divider is fixed to 2 in 16-bit mode */
562#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
563 1350000 / ((val) * 2))
564
Guenter Roeck2a844c12013-01-09 08:09:34 -0800565#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
566 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200567#define TEMP_FROM_REG(val) ((val) * 1000)
568
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200569static u8 pwm_to_reg(const struct it87_data *data, long val)
570{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100571 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200572 return val;
573 else
574 return val >> 1;
575}
576
577static int pwm_from_reg(const struct it87_data *data, u8 reg)
578{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100579 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200580 return reg;
581 else
582 return (reg & 0x7f) << 1;
583}
584
Jean Delvare0df6454d2010-10-28 20:31:51 +0200585
586static int DIV_TO_REG(int val)
587{
588 int answer = 0;
589 while (answer < 7 && (val >>= 1))
590 answer++;
591 return answer;
592}
593#define DIV_FROM_REG(val) (1 << (val))
594
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700595/*
596 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
597 * depending on the chip type, to calculate the actual PWM frequency.
598 *
599 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
600 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
601 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
602 * sometimes just one. It is unknown if this is a datasheet error or real,
603 * so this is ignored for now.
604 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200605static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700606 48000000,
607 24000000,
608 12000000,
609 8000000,
610 6000000,
611 3000000,
612 1500000,
613 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200614};
615
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700616/*
617 * Must be called with data->update_lock held, except during initialization.
618 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
619 * would slow down the IT87 access and should not be necessary.
620 */
621static int it87_read_value(struct it87_data *data, u8 reg)
622{
623 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
624 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
625}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700627/*
628 * Must be called with data->update_lock held, except during initialization.
629 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
630 * would slow down the IT87 access and should not be necessary.
631 */
632static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
633{
634 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
635 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
636}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700638static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
639{
640 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
641 if (has_newer_autopwm(data)) {
642 data->pwm_temp_map[nr] = (data->pwm_ctrl[nr] & 0x03) +
643 nr < 3 ? 0 : 3;
644 data->pwm_duty[nr] = it87_read_value(data,
645 IT87_REG_PWM_DUTY[nr]);
646 } else {
647 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
648 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
649 else /* Manual mode */
650 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700653 if (has_old_autopwm(data)) {
654 int i;
655
656 for (i = 0; i < 5 ; i++)
657 data->auto_temp[nr][i] = it87_read_value(data,
658 IT87_REG_AUTO_TEMP(nr, i));
659 for (i = 0; i < 3 ; i++)
660 data->auto_pwm[nr][i] = it87_read_value(data,
661 IT87_REG_AUTO_PWM(nr, i));
662 }
663}
664
665static struct it87_data *it87_update_device(struct device *dev)
666{
667 struct it87_data *data = dev_get_drvdata(dev);
668 int i;
669
670 mutex_lock(&data->update_lock);
671
672 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
673 || !data->valid) {
674 if (update_vbat) {
675 /*
676 * Cleared after each update, so reenable. Value
677 * returned by this read will be previous value
678 */
679 it87_write_value(data, IT87_REG_CONFIG,
680 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
681 }
Guenter Roeck559313c2015-04-01 10:15:38 -0700682 for (i = 0; i < ARRAY_SIZE(IT87_REG_VIN); i++) {
683 if (!(data->has_in & (1 << i)))
684 continue;
685
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700686 data->in[i][0] =
Guenter Roeck559313c2015-04-01 10:15:38 -0700687 it87_read_value(data, IT87_REG_VIN[i]);
688
689 /* VBAT and AVCC don't have limit registers */
690 if (i >= 8)
691 continue;
692
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700693 data->in[i][1] =
694 it87_read_value(data, IT87_REG_VIN_MIN(i));
695 data->in[i][2] =
696 it87_read_value(data, IT87_REG_VIN_MAX(i));
697 }
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700698
699 for (i = 0; i < 6; i++) {
700 /* Skip disabled fans */
701 if (!(data->has_fan & (1 << i)))
702 continue;
703
704 data->fan[i][1] =
705 it87_read_value(data, IT87_REG_FAN_MIN[i]);
706 data->fan[i][0] = it87_read_value(data,
707 IT87_REG_FAN[i]);
708 /* Add high byte if in 16-bit mode */
709 if (has_16bit_fans(data)) {
710 data->fan[i][0] |= it87_read_value(data,
711 IT87_REG_FANX[i]) << 8;
712 data->fan[i][1] |= it87_read_value(data,
713 IT87_REG_FANX_MIN[i]) << 8;
714 }
715 }
Guenter Roeckcc18da72015-04-01 10:03:01 -0700716 for (i = 0; i < 6; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700717 if (!(data->has_temp & (1 << i)))
718 continue;
719 data->temp[i][0] =
720 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeckcc18da72015-04-01 10:03:01 -0700721
722 /* No limits/offset for additional sensors */
723 if (i >= 3)
724 continue;
725
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700726 data->temp[i][1] =
727 it87_read_value(data, IT87_REG_TEMP_LOW(i));
728 data->temp[i][2] =
729 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
730 if (has_temp_offset(data))
731 data->temp[i][3] =
732 it87_read_value(data,
733 IT87_REG_TEMP_OFFSET[i]);
734 }
735
736 /* Newer chips don't have clock dividers */
737 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
738 i = it87_read_value(data, IT87_REG_FAN_DIV);
739 data->fan_div[0] = i & 0x07;
740 data->fan_div[1] = (i >> 3) & 0x07;
741 data->fan_div[2] = (i & 0x40) ? 3 : 1;
742 }
743
744 data->alarms =
745 it87_read_value(data, IT87_REG_ALARM1) |
746 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
747 (it87_read_value(data, IT87_REG_ALARM3) << 16);
748 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
749
750 data->fan_main_ctrl = it87_read_value(data,
751 IT87_REG_FAN_MAIN_CTRL);
752 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
753 for (i = 0; i < 6; i++)
754 it87_update_pwm_ctrl(data, i);
755
756 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
757 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
758 /*
759 * The IT8705F does not have VID capability.
760 * The IT8718F and later don't use IT87_REG_VID for the
761 * same purpose.
762 */
763 if (data->type == it8712 || data->type == it8716) {
764 data->vid = it87_read_value(data, IT87_REG_VID);
765 /*
766 * The older IT8712F revisions had only 5 VID pins,
767 * but we assume it is always safe to read 6 bits.
768 */
769 data->vid &= 0x3f;
770 }
771 data->last_updated = jiffies;
772 data->valid = 1;
773 }
774
775 mutex_unlock(&data->update_lock);
776
777 return data;
778}
Jean Delvarefde09502005-07-19 23:51:07 +0200779
Jean Delvare20ad93d2005-06-05 11:53:25 +0200780static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100781 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100783 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
784 int nr = sattr->nr;
785 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100788 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Guenter Roeck929c6a52012-12-19 22:17:00 +0100791static ssize_t set_in(struct device *dev, struct device_attribute *attr,
792 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100794 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
795 int nr = sattr->nr;
796 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200797
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200798 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100799 unsigned long val;
800
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100801 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100802 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100804 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100805 data->in[nr][index] = in_to_reg(data, nr, val);
806 it87_write_value(data,
807 index == 1 ? IT87_REG_VIN_MIN(nr)
808 : IT87_REG_VIN_MAX(nr),
809 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100810 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return count;
812}
813
Guenter Roeck929c6a52012-12-19 22:17:00 +0100814static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
815static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
816 0, 1);
817static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
818 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Guenter Roeck929c6a52012-12-19 22:17:00 +0100820static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
821static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
822 1, 1);
823static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
824 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Guenter Roeck929c6a52012-12-19 22:17:00 +0100826static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
827static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
828 2, 1);
829static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
830 2, 2);
831
832static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
833static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
834 3, 1);
835static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
836 3, 2);
837
838static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
839static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
840 4, 1);
841static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
842 4, 2);
843
844static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
845static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
846 5, 1);
847static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
848 5, 2);
849
850static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
851static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
852 6, 1);
853static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
854 6, 2);
855
856static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
857static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
858 7, 1);
859static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
860 7, 2);
861
862static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100863static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Guenter Roeckcc18da72015-04-01 10:03:01 -0700865/* Up to 6 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200866static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100867 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100869 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
870 int nr = sattr->nr;
871 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200873
Guenter Roeck60ca3852012-12-19 22:17:00 +0100874 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200876
Guenter Roeck60ca3852012-12-19 22:17:00 +0100877static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
878 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100880 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
881 int nr = sattr->nr;
882 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200883 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100884 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100885 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100886
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100887 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100888 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100890 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100891
892 switch (index) {
893 default:
894 case 1:
895 reg = IT87_REG_TEMP_LOW(nr);
896 break;
897 case 2:
898 reg = IT87_REG_TEMP_HIGH(nr);
899 break;
900 case 3:
901 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
902 if (!(regval & 0x80)) {
903 regval |= 0x80;
904 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
905 }
906 data->valid = 0;
907 reg = IT87_REG_TEMP_OFFSET[nr];
908 break;
909 }
910
Guenter Roeck60ca3852012-12-19 22:17:00 +0100911 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100912 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100913 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return count;
915}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200916
Guenter Roeck60ca3852012-12-19 22:17:00 +0100917static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
918static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
919 0, 1);
920static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
921 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100922static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
923 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100924static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
925static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
926 1, 1);
927static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
928 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100929static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
930 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100931static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
932static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
933 2, 1);
934static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
935 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100936static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
937 set_temp, 2, 3);
Guenter Roeckcc18da72015-04-01 10:03:01 -0700938static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
939static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
940static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Guenter Roeck2cece012012-12-19 22:17:01 +0100942static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
943 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200945 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
946 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800948 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100949 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100950
Guenter Roeck19529782012-12-19 22:17:02 +0100951 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
952 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100953 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (reg & (1 << nr))
955 return sprintf(buf, "3\n"); /* thermal diode */
956 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200957 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return sprintf(buf, "0\n"); /* disabled */
959}
Guenter Roeck2cece012012-12-19 22:17:01 +0100960
961static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
962 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200964 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
965 int nr = sensor_attr->index;
966
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200967 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100968 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100969 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100970
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100971 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100972 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Jean Delvare8acf07c2010-04-14 16:14:09 +0200974 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
975 reg &= ~(1 << nr);
976 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100977 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
978 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100979 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
980 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
981 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200982 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100983 dev_warn(dev,
984 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200985 val = 4;
986 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100987 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200989 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200990 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200991 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100992 else if (has_temp_peci(data, nr) && val == 6)
993 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100994 else if (has_temp_old_peci(data, nr) && val == 6)
995 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200996 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200998
999 mutex_lock(&data->update_lock);
1000 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +01001001 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001002 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +01001003 if (has_temp_old_peci(data, nr))
1004 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +02001005 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001006 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return count;
1008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Guenter Roeck2cece012012-12-19 22:17:01 +01001010static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1011 set_temp_type, 0);
1012static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1013 set_temp_type, 1);
1014static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1015 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +01001018
1019static int pwm_mode(const struct it87_data *data, int nr)
1020{
1021 int ctrl = data->fan_main_ctrl & (1 << nr);
1022
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001023 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +01001024 return 0;
1025 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
1026 return 2;
1027 else /* Manual mode */
1028 return 1;
1029}
1030
Jean Delvare20ad93d2005-06-05 11:53:25 +02001031static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +01001032 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001034 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1035 int nr = sattr->nr;
1036 int index = sattr->index;
1037 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001039
Guenter Roecke1169ba2012-12-19 22:17:01 +01001040 speed = has_16bit_fans(data) ?
1041 FAN16_FROM_REG(data->fan[nr][index]) :
1042 FAN_FROM_REG(data->fan[nr][index],
1043 DIV_FROM_REG(data->fan_div[nr]));
1044 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001046
Jean Delvare20ad93d2005-06-05 11:53:25 +02001047static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1048 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001050 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1051 int nr = sensor_attr->index;
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct it87_data *data = it87_update_device(dev);
1054 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
1055}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001056static ssize_t show_pwm_enable(struct device *dev,
1057 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001059 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1060 int nr = sensor_attr->index;
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +01001063 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001065static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1066 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001068 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1069 int nr = sensor_attr->index;
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001072 return sprintf(buf, "%d\n",
1073 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001075static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1076 char *buf)
1077{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001078 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001079 struct it87_data *data = it87_update_device(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001080 int nr = sensor_attr->index;
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001081 unsigned int freq;
Guenter Roeck60878bc2015-03-26 19:57:42 -07001082 int index;
1083
1084 if (has_pwm_freq2(data) && nr == 1)
1085 index = (data->extra >> 4) & 0x07;
1086 else
1087 index = (data->fan_ctl >> 4) & 0x07;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001088
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001089 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1090
1091 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001092}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001093
1094static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1095 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001097 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1098 int nr = sattr->nr;
1099 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +02001100
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001101 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001102 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +01001103 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001105 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001106 return -EINVAL;
1107
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001108 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +01001109
1110 if (has_16bit_fans(data)) {
1111 data->fan[nr][index] = FAN16_TO_REG(val);
1112 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1113 data->fan[nr][index] & 0xff);
1114 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1115 data->fan[nr][index] >> 8);
1116 } else {
1117 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1118 switch (nr) {
1119 case 0:
1120 data->fan_div[nr] = reg & 0x07;
1121 break;
1122 case 1:
1123 data->fan_div[nr] = (reg >> 3) & 0x07;
1124 break;
1125 case 2:
1126 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1127 break;
1128 }
1129 data->fan[nr][index] =
1130 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1131 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1132 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -08001133 }
1134
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001135 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return count;
1137}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001138
Jean Delvare20ad93d2005-06-05 11:53:25 +02001139static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1140 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001142 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1143 int nr = sensor_attr->index;
1144
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001145 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001146 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001147 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 u8 old;
1149
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001150 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001151 return -EINVAL;
1152
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001153 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001154 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001156 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001157 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 switch (nr) {
1160 case 0:
1161 case 1:
1162 data->fan_div[nr] = DIV_TO_REG(val);
1163 break;
1164 case 2:
1165 if (val < 8)
1166 data->fan_div[nr] = 1;
1167 else
1168 data->fan_div[nr] = 3;
1169 }
1170 val = old & 0x80;
1171 val |= (data->fan_div[0] & 0x07);
1172 val |= (data->fan_div[1] & 0x07) << 3;
1173 if (data->fan_div[2] == 3)
1174 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001175 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001177 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001178 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1179 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001180
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001181 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return count;
1183}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001184
1185/* Returns 0 if OK, -EINVAL otherwise */
1186static int check_trip_points(struct device *dev, int nr)
1187{
1188 const struct it87_data *data = dev_get_drvdata(dev);
1189 int i, err = 0;
1190
1191 if (has_old_autopwm(data)) {
1192 for (i = 0; i < 3; i++) {
1193 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1194 err = -EINVAL;
1195 }
1196 for (i = 0; i < 2; i++) {
1197 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1198 err = -EINVAL;
1199 }
1200 }
1201
1202 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001203 dev_err(dev,
1204 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001205 dev_err(dev, "Adjust the trip points and try again\n");
1206 }
1207 return err;
1208}
1209
Jean Delvare20ad93d2005-06-05 11:53:25 +02001210static ssize_t set_pwm_enable(struct device *dev,
1211 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001213 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1214 int nr = sensor_attr->index;
1215
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001216 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001217 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001219 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001220 return -EINVAL;
1221
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001222 /* Check trip points before switching to automatic mode */
1223 if (val == 2) {
1224 if (check_trip_points(dev, nr) < 0)
1225 return -EINVAL;
1226 }
1227
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001228 /* IT8603E does not have on/off mode */
1229 if (val == 0 && data->type == it8603)
1230 return -EINVAL;
1231
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001232 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (val == 0) {
1235 int tmp;
1236 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001237 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1238 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /* set on/off mode */
1240 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +01001241 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1242 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +01001243 } else {
1244 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001245 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001246 data->pwm_temp_map[nr] :
1247 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001248 else /* Automatic mode */
1249 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001250 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001251
1252 if (data->type != it8603) {
1253 /* set SmartGuardian mode */
1254 data->fan_main_ctrl |= (1 << nr);
1255 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1256 data->fan_main_ctrl);
1257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001260 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return count;
1262}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001263static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1264 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001266 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1267 int nr = sensor_attr->index;
1268
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001269 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001270 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001272 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return -EINVAL;
1274
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001275 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001276 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001277 /*
1278 * If we are in automatic mode, the PWM duty cycle register
1279 * is read-only so we can't write the value.
1280 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001281 if (data->pwm_ctrl[nr] & 0x80) {
1282 mutex_unlock(&data->update_lock);
1283 return -EBUSY;
1284 }
1285 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001286 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001287 data->pwm_duty[nr]);
1288 } else {
1289 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001290 /*
1291 * If we are in manual mode, write the duty cycle immediately;
1292 * otherwise, just store it for later use.
1293 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001294 if (!(data->pwm_ctrl[nr] & 0x80)) {
1295 data->pwm_ctrl[nr] = data->pwm_duty[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001296 it87_write_value(data, IT87_REG_PWM[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001297 data->pwm_ctrl[nr]);
1298 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001299 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001300 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return count;
1302}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001303static ssize_t set_pwm_freq(struct device *dev,
1304 struct device_attribute *attr, const char *buf, size_t count)
1305{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001306 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001307 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001308 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001309 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001310 int i;
1311
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001312 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001313 return -EINVAL;
1314
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001315 val = clamp_val(val, 0, 1000000);
1316 val *= has_newer_autopwm(data) ? 256 : 128;
1317
Jean Delvaref8d0c192007-02-14 21:15:02 +01001318 /* Search for the nearest available frequency */
1319 for (i = 0; i < 7; i++) {
1320 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1321 break;
1322 }
1323
1324 mutex_lock(&data->update_lock);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001325 if (nr == 0) {
1326 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1327 data->fan_ctl |= i << 4;
1328 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1329 } else {
1330 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1331 data->extra |= i << 4;
1332 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1333 }
Jean Delvaref8d0c192007-02-14 21:15:02 +01001334 mutex_unlock(&data->update_lock);
1335
1336 return count;
1337}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001338static ssize_t show_pwm_temp_map(struct device *dev,
1339 struct device_attribute *attr, char *buf)
1340{
1341 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1342 int nr = sensor_attr->index;
1343
1344 struct it87_data *data = it87_update_device(dev);
1345 int map;
1346
1347 if (data->pwm_temp_map[nr] < 3)
1348 map = 1 << data->pwm_temp_map[nr];
1349 else
1350 map = 0; /* Should never happen */
1351 return sprintf(buf, "%d\n", map);
1352}
1353static ssize_t set_pwm_temp_map(struct device *dev,
1354 struct device_attribute *attr, const char *buf, size_t count)
1355{
1356 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1357 int nr = sensor_attr->index;
1358
1359 struct it87_data *data = dev_get_drvdata(dev);
1360 long val;
1361 u8 reg;
1362
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001363 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001364 return -EINVAL;
1365
1366 switch (val) {
1367 case (1 << 0):
1368 reg = 0x00;
1369 break;
1370 case (1 << 1):
1371 reg = 0x01;
1372 break;
1373 case (1 << 2):
1374 reg = 0x02;
1375 break;
1376 default:
1377 return -EINVAL;
1378 }
1379
1380 mutex_lock(&data->update_lock);
1381 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001382 /*
1383 * If we are in automatic mode, write the temp mapping immediately;
1384 * otherwise, just store it for later use.
1385 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001386 if (data->pwm_ctrl[nr] & 0x80) {
1387 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001388 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001389 }
1390 mutex_unlock(&data->update_lock);
1391 return count;
1392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001394static ssize_t show_auto_pwm(struct device *dev,
1395 struct device_attribute *attr, char *buf)
1396{
1397 struct it87_data *data = it87_update_device(dev);
1398 struct sensor_device_attribute_2 *sensor_attr =
1399 to_sensor_dev_attr_2(attr);
1400 int nr = sensor_attr->nr;
1401 int point = sensor_attr->index;
1402
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001403 return sprintf(buf, "%d\n",
1404 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001405}
1406
1407static ssize_t set_auto_pwm(struct device *dev,
1408 struct device_attribute *attr, const char *buf, size_t count)
1409{
1410 struct it87_data *data = dev_get_drvdata(dev);
1411 struct sensor_device_attribute_2 *sensor_attr =
1412 to_sensor_dev_attr_2(attr);
1413 int nr = sensor_attr->nr;
1414 int point = sensor_attr->index;
1415 long val;
1416
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001417 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001418 return -EINVAL;
1419
1420 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001421 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001422 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1423 data->auto_pwm[nr][point]);
1424 mutex_unlock(&data->update_lock);
1425 return count;
1426}
1427
1428static ssize_t show_auto_temp(struct device *dev,
1429 struct device_attribute *attr, char *buf)
1430{
1431 struct it87_data *data = it87_update_device(dev);
1432 struct sensor_device_attribute_2 *sensor_attr =
1433 to_sensor_dev_attr_2(attr);
1434 int nr = sensor_attr->nr;
1435 int point = sensor_attr->index;
1436
1437 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1438}
1439
1440static ssize_t set_auto_temp(struct device *dev,
1441 struct device_attribute *attr, const char *buf, size_t count)
1442{
1443 struct it87_data *data = dev_get_drvdata(dev);
1444 struct sensor_device_attribute_2 *sensor_attr =
1445 to_sensor_dev_attr_2(attr);
1446 int nr = sensor_attr->nr;
1447 int point = sensor_attr->index;
1448 long val;
1449
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001450 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001451 return -EINVAL;
1452
1453 mutex_lock(&data->update_lock);
1454 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1455 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1456 data->auto_temp[nr][point]);
1457 mutex_unlock(&data->update_lock);
1458 return count;
1459}
1460
Guenter Roecke1169ba2012-12-19 22:17:01 +01001461static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1462static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1463 0, 1);
1464static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1465 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Guenter Roecke1169ba2012-12-19 22:17:01 +01001467static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1468static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1469 1, 1);
1470static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1471 set_fan_div, 1);
1472
1473static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1474static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1475 2, 1);
1476static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1477 set_fan_div, 2);
1478
1479static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1480static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1481 3, 1);
1482
1483static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1484static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1485 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001487static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1488static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1489 5, 1);
1490
Guenter Roeckc4458db2012-12-19 22:17:02 +01001491static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1492 show_pwm_enable, set_pwm_enable, 0);
1493static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001494static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1495 set_pwm_freq, 0);
Guenter Roeck5c391262013-03-30 15:02:12 -07001496static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001497 show_pwm_temp_map, set_pwm_temp_map, 0);
1498static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1499 show_auto_pwm, set_auto_pwm, 0, 0);
1500static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1501 show_auto_pwm, set_auto_pwm, 0, 1);
1502static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1503 show_auto_pwm, set_auto_pwm, 0, 2);
1504static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1505 show_auto_pwm, NULL, 0, 3);
1506static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1507 show_auto_temp, set_auto_temp, 0, 1);
1508static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1509 show_auto_temp, set_auto_temp, 0, 0);
1510static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1511 show_auto_temp, set_auto_temp, 0, 2);
1512static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1513 show_auto_temp, set_auto_temp, 0, 3);
1514static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1515 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Guenter Roeckc4458db2012-12-19 22:17:02 +01001517static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1518 show_pwm_enable, set_pwm_enable, 1);
1519static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001520static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
Guenter Roeck5c391262013-03-30 15:02:12 -07001521static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001522 show_pwm_temp_map, set_pwm_temp_map, 1);
1523static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1524 show_auto_pwm, set_auto_pwm, 1, 0);
1525static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1526 show_auto_pwm, set_auto_pwm, 1, 1);
1527static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1528 show_auto_pwm, set_auto_pwm, 1, 2);
1529static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1530 show_auto_pwm, NULL, 1, 3);
1531static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1532 show_auto_temp, set_auto_temp, 1, 1);
1533static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1534 show_auto_temp, set_auto_temp, 1, 0);
1535static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1536 show_auto_temp, set_auto_temp, 1, 2);
1537static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1538 show_auto_temp, set_auto_temp, 1, 3);
1539static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1540 show_auto_temp, set_auto_temp, 1, 4);
1541
1542static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1543 show_pwm_enable, set_pwm_enable, 2);
1544static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001545static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
Guenter Roeck5c391262013-03-30 15:02:12 -07001546static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001547 show_pwm_temp_map, set_pwm_temp_map, 2);
1548static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1549 show_auto_pwm, set_auto_pwm, 2, 0);
1550static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1551 show_auto_pwm, set_auto_pwm, 2, 1);
1552static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1553 show_auto_pwm, set_auto_pwm, 2, 2);
1554static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1555 show_auto_pwm, NULL, 2, 3);
1556static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1557 show_auto_temp, set_auto_temp, 2, 1);
1558static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1559 show_auto_temp, set_auto_temp, 2, 0);
1560static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1561 show_auto_temp, set_auto_temp, 2, 2);
1562static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1563 show_auto_temp, set_auto_temp, 2, 3);
1564static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1565 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Guenter Roeck36c4d982015-03-26 18:18:19 -07001567static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1568 show_pwm_enable, set_pwm_enable, 3);
1569static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001570static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
Guenter Roeck5c391262013-03-30 15:02:12 -07001571static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001572 show_pwm_temp_map, set_pwm_temp_map, 3);
1573
1574static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1575 show_pwm_enable, set_pwm_enable, 4);
1576static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001577static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
Guenter Roeck5c391262013-03-30 15:02:12 -07001578static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001579 show_pwm_temp_map, set_pwm_temp_map, 4);
1580
1581static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1582 show_pwm_enable, set_pwm_enable, 5);
1583static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001584static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
Guenter Roeck5c391262013-03-30 15:02:12 -07001585static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001586 show_pwm_temp_map, set_pwm_temp_map, 5);
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001589static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1590 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001593 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
Jean Delvare1d66c642005-04-18 21:16:59 -07001595static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Jean Delvare0124dd72007-11-25 16:16:41 +01001597static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1598 char *buf)
1599{
1600 int bitnr = to_sensor_dev_attr(attr)->index;
1601 struct it87_data *data = it87_update_device(dev);
1602 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1603}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001604
1605static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1606 *attr, const char *buf, size_t count)
1607{
1608 struct it87_data *data = dev_get_drvdata(dev);
1609 long val;
1610 int config;
1611
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001612 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001613 return -EINVAL;
1614
1615 mutex_lock(&data->update_lock);
1616 config = it87_read_value(data, IT87_REG_CONFIG);
1617 if (config < 0) {
1618 count = config;
1619 } else {
1620 config |= 1 << 5;
1621 it87_write_value(data, IT87_REG_CONFIG, config);
1622 /* Invalidate cache to force re-read */
1623 data->valid = 0;
1624 }
1625 mutex_unlock(&data->update_lock);
1626
1627 return count;
1628}
1629
Jean Delvare0124dd72007-11-25 16:16:41 +01001630static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1631static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1632static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1633static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1634static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1635static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1636static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1637static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1638static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1639static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1640static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1641static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1642static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001643static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001644static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1645static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1646static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001647static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1648 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001649
Jean Delvared9b327c2010-03-05 22:17:17 +01001650static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1651 char *buf)
1652{
1653 int bitnr = to_sensor_dev_attr(attr)->index;
1654 struct it87_data *data = it87_update_device(dev);
1655 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1656}
1657static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1658 const char *buf, size_t count)
1659{
1660 int bitnr = to_sensor_dev_attr(attr)->index;
1661 struct it87_data *data = dev_get_drvdata(dev);
1662 long val;
1663
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001664 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001665 || (val != 0 && val != 1))
1666 return -EINVAL;
1667
1668 mutex_lock(&data->update_lock);
1669 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1670 if (val)
1671 data->beeps |= (1 << bitnr);
1672 else
1673 data->beeps &= ~(1 << bitnr);
1674 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1675 mutex_unlock(&data->update_lock);
1676 return count;
1677}
1678
1679static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1680 show_beep, set_beep, 1);
1681static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1682static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1683static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1684static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1685static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1686static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1687static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1688/* fanX_beep writability is set later */
1689static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1690static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1691static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1692static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1693static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001694static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001695static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1696 show_beep, set_beep, 2);
1697static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1698static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1699
Jean Delvare5f2dc792010-03-05 22:17:18 +01001700static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1701 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Jean Delvare90d66192007-10-08 18:24:35 +02001703 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001704 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001706static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1707 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001709 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001710 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001712 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001713 return -EINVAL;
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 data->vrm = val;
1716
1717 return count;
1718}
1719static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Jean Delvare5f2dc792010-03-05 22:17:18 +01001721static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1722 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 struct it87_data *data = it87_update_device(dev);
1725 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1726}
1727static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001728
Jean Delvare738e5e02010-08-14 21:08:50 +02001729static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1730 char *buf)
1731{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001732 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001733 "+5V",
1734 "5VSB",
1735 "Vbat",
1736 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001737 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001738 "+3.3V",
1739 "3VSB",
1740 "Vbat",
1741 };
1742 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001743 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001744 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001745
Justin Maggardead80802015-08-05 12:53:08 -07001746 if (has_12mv_adc(data) || has_10_9mv_adc(data))
1747 label = labels_it8721[nr];
1748 else
1749 label = labels[nr];
1750
1751 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001752}
1753static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1754static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1755static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001756/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001757static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001758
Guenter Roeck52929712013-03-30 14:00:08 -07001759static umode_t it87_in_is_visible(struct kobject *kobj,
1760 struct attribute *attr, int index)
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001761{
Guenter Roeck52929712013-03-30 14:00:08 -07001762 struct device *dev = container_of(kobj, struct device, kobj);
1763 struct it87_data *data = dev_get_drvdata(dev);
1764 int i = index / 5; /* voltage index */
1765 int a = index % 5; /* attribute index */
1766
1767 if (index >= 40) { /* in8, in9 only have input attributes */
1768 i = index - 40 + 8;
1769 a = 0;
1770 }
1771
1772 if (!(data->has_in & (1 << i)))
1773 return 0;
1774
1775 if (a == 4 && !data->has_beep)
1776 return 0;
1777
1778 return attr->mode;
1779}
1780
1781static struct attribute *it87_attributes_in[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001782 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001783 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001784 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001785 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001786 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
1787
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001788 &sensor_dev_attr_in1_input.dev_attr.attr,
1789 &sensor_dev_attr_in1_min.dev_attr.attr,
1790 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001791 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001792 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
1793
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001794 &sensor_dev_attr_in2_input.dev_attr.attr,
1795 &sensor_dev_attr_in2_min.dev_attr.attr,
1796 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001797 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001798 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
1799
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001800 &sensor_dev_attr_in3_input.dev_attr.attr,
1801 &sensor_dev_attr_in3_min.dev_attr.attr,
1802 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001803 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001804 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
1805
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001806 &sensor_dev_attr_in4_input.dev_attr.attr,
1807 &sensor_dev_attr_in4_min.dev_attr.attr,
1808 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001809 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001810 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
1811
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001812 &sensor_dev_attr_in5_input.dev_attr.attr,
1813 &sensor_dev_attr_in5_min.dev_attr.attr,
1814 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001815 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001816 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
1817
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001818 &sensor_dev_attr_in6_input.dev_attr.attr,
1819 &sensor_dev_attr_in6_min.dev_attr.attr,
1820 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001821 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001822 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
1823
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001824 &sensor_dev_attr_in7_input.dev_attr.attr,
1825 &sensor_dev_attr_in7_min.dev_attr.attr,
1826 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001827 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001828 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
Jean Delvare87808be2006-09-24 21:17:13 +02001829
Guenter Roeck52929712013-03-30 14:00:08 -07001830 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
1831
1832 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
1833};
1834
1835static const struct attribute_group it87_group_in = {
1836 .attrs = it87_attributes_in,
1837 .is_visible = it87_in_is_visible,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001838};
1839
Guenter Roeck87533772013-03-30 14:23:20 -07001840static umode_t it87_temp_is_visible(struct kobject *kobj,
1841 struct attribute *attr, int index)
Guenter Roeck4573acb2012-03-26 16:17:41 -07001842{
Guenter Roeck87533772013-03-30 14:23:20 -07001843 struct device *dev = container_of(kobj, struct device, kobj);
1844 struct it87_data *data = dev_get_drvdata(dev);
1845 int i = index / 7; /* temperature index */
1846 int a = index % 7; /* attribute index */
1847
Guenter Roeckcc18da72015-04-01 10:03:01 -07001848 if (index >= 21) {
1849 i = index - 21 + 3;
1850 a = 0;
1851 }
1852
Guenter Roeck87533772013-03-30 14:23:20 -07001853 if (!(data->has_temp & (1 << i)))
1854 return 0;
1855
1856 if (a == 5 && !has_temp_offset(data))
1857 return 0;
1858
1859 if (a == 6 && !data->has_beep)
1860 return 0;
1861
1862 return attr->mode;
1863}
1864
1865static struct attribute *it87_attributes_temp[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001866 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001867 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001868 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001869 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001870 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001871 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
1872 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
1873
Guenter Roeckcc18da72015-04-01 10:03:01 -07001874 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001875 &sensor_dev_attr_temp2_max.dev_attr.attr,
1876 &sensor_dev_attr_temp2_min.dev_attr.attr,
1877 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001878 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001879 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1880 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1881
Guenter Roeckcc18da72015-04-01 10:03:01 -07001882 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001883 &sensor_dev_attr_temp3_max.dev_attr.attr,
1884 &sensor_dev_attr_temp3_min.dev_attr.attr,
1885 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001886 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001887 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1888 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001889
Guenter Roeckcc18da72015-04-01 10:03:01 -07001890 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
1891 &sensor_dev_attr_temp5_input.dev_attr.attr,
1892 &sensor_dev_attr_temp6_input.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001893 NULL
Guenter Roeck4573acb2012-03-26 16:17:41 -07001894};
1895
Guenter Roeck87533772013-03-30 14:23:20 -07001896static const struct attribute_group it87_group_temp = {
1897 .attrs = it87_attributes_temp,
1898 .is_visible = it87_temp_is_visible,
Guenter Roeck161d8982012-12-19 22:17:01 +01001899};
1900
Guenter Roeckd3766842013-03-30 15:47:21 -07001901static umode_t it87_is_visible(struct kobject *kobj,
1902 struct attribute *attr, int index)
1903{
1904 struct device *dev = container_of(kobj, struct device, kobj);
1905 struct it87_data *data = dev_get_drvdata(dev);
1906
Guenter Roeck8638d0a2015-03-30 11:13:49 -07001907 if ((index == 2 || index == 3) && !data->has_vid)
Guenter Roeckd3766842013-03-30 15:47:21 -07001908 return 0;
1909
Guenter Roeck8638d0a2015-03-30 11:13:49 -07001910 if (index > 3 && !(data->in_internal & (1 << (index - 4))))
Guenter Roeckd3766842013-03-30 15:47:21 -07001911 return 0;
1912
1913 return attr->mode;
1914}
1915
Guenter Roeck4573acb2012-03-26 16:17:41 -07001916static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001917 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001918 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
Guenter Roeck8638d0a2015-03-30 11:13:49 -07001919 &dev_attr_vrm.attr, /* 2 */
1920 &dev_attr_cpu0_vid.attr, /* 3 */
1921 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
Guenter Roeckd3766842013-03-30 15:47:21 -07001922 &sensor_dev_attr_in7_label.dev_attr.attr,
1923 &sensor_dev_attr_in8_label.dev_attr.attr,
1924 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001925 NULL
1926};
1927
1928static const struct attribute_group it87_group = {
1929 .attrs = it87_attributes,
Guenter Roeckd3766842013-03-30 15:47:21 -07001930 .is_visible = it87_is_visible,
Jean Delvare87808be2006-09-24 21:17:13 +02001931};
1932
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001933static umode_t it87_fan_is_visible(struct kobject *kobj,
1934 struct attribute *attr, int index)
1935{
1936 struct device *dev = container_of(kobj, struct device, kobj);
1937 struct it87_data *data = dev_get_drvdata(dev);
1938 int i = index / 5; /* fan index */
1939 int a = index % 5; /* attribute index */
1940
1941 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
1942 i = (index - 15) / 4 + 3;
1943 a = (index - 15) % 4;
1944 }
1945
1946 if (!(data->has_fan & (1 << i)))
1947 return 0;
1948
1949 if (a == 3) { /* beep */
1950 if (!data->has_beep)
1951 return 0;
1952 /* first fan beep attribute is writable */
1953 if (i == __ffs(data->has_fan))
1954 return attr->mode | S_IWUSR;
1955 }
1956
1957 if (a == 4 && has_16bit_fans(data)) /* divisor */
1958 return 0;
1959
1960 return attr->mode;
1961}
1962
1963static struct attribute *it87_attributes_fan[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001964 &sensor_dev_attr_fan1_input.dev_attr.attr,
1965 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001966 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001967 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
1968 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
1969
Jean Delvare87808be2006-09-24 21:17:13 +02001970 &sensor_dev_attr_fan2_input.dev_attr.attr,
1971 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001972 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001973 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1974 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
1975
Jean Delvare87808be2006-09-24 21:17:13 +02001976 &sensor_dev_attr_fan3_input.dev_attr.attr,
1977 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001978 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001979 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1980 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
1981
1982 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001983 &sensor_dev_attr_fan4_min.dev_attr.attr,
1984 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001985 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1986
1987 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001988 &sensor_dev_attr_fan5_min.dev_attr.attr,
1989 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001990 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1991
1992 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001993 &sensor_dev_attr_fan6_min.dev_attr.attr,
1994 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001995 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001996 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001997};
1998
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001999static const struct attribute_group it87_group_fan = {
2000 .attrs = it87_attributes_fan,
2001 .is_visible = it87_fan_is_visible,
Guenter Roecke1169ba2012-12-19 22:17:01 +01002002};
Jean Delvare723a0aa2010-03-05 22:17:16 +01002003
Guenter Roeck5c391262013-03-30 15:02:12 -07002004static umode_t it87_pwm_is_visible(struct kobject *kobj,
2005 struct attribute *attr, int index)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002006{
2007 struct device *dev = container_of(kobj, struct device, kobj);
2008 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck5c391262013-03-30 15:02:12 -07002009 int i = index / 4; /* pwm index */
2010 int a = index % 4; /* attribute index */
Guenter Roeck60878bc2015-03-26 19:57:42 -07002011
Guenter Roeck5c391262013-03-30 15:02:12 -07002012 if (!(data->has_pwm & (1 << i)))
2013 return 0;
2014
2015 /* pwmX_auto_channels_temp is only writable for old auto pwm */
2016 if (a == 3 && has_old_autopwm(data))
2017 return attr->mode | S_IWUSR;
2018
2019 /* pwm2_freq is writable if there are two pwm frequency selects */
2020 if (has_pwm_freq2(data) && i == 1 && a == 2)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002021 return attr->mode | S_IWUSR;
2022
2023 return attr->mode;
2024}
2025
Guenter Roeck5c391262013-03-30 15:02:12 -07002026static struct attribute *it87_attributes_pwm[] = {
2027 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2028 &sensor_dev_attr_pwm1.dev_attr.attr,
2029 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2030 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2031
2032 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2033 &sensor_dev_attr_pwm2.dev_attr.attr,
2034 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2035 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2036
2037 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2038 &sensor_dev_attr_pwm3.dev_attr.attr,
2039 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2040 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2041
2042 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2043 &sensor_dev_attr_pwm4.dev_attr.attr,
2044 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2045 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2046
2047 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2048 &sensor_dev_attr_pwm5.dev_attr.attr,
2049 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2050 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2051
2052 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2053 &sensor_dev_attr_pwm6.dev_attr.attr,
2054 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2055 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2056
2057 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002058};
2059
Guenter Roeck5c391262013-03-30 15:02:12 -07002060static const struct attribute_group it87_group_pwm = {
2061 .attrs = it87_attributes_pwm,
2062 .is_visible = it87_pwm_is_visible,
2063};
2064
2065static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2066 struct attribute *attr, int index)
2067{
2068 struct device *dev = container_of(kobj, struct device, kobj);
2069 struct it87_data *data = dev_get_drvdata(dev);
2070 int i = index / 9; /* pwm index */
2071
2072 if (!(data->has_pwm & (1 << i)))
2073 return 0;
2074
2075 return attr->mode;
2076}
2077
2078static struct attribute *it87_attributes_auto_pwm[] = {
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002079 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2080 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2081 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2082 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2083 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2084 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2085 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2086 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2087 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002088
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002089 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
2090 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2091 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2092 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2093 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2094 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2095 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2096 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2097 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002098
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002099 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
2100 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2101 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2102 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2103 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2104 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2105 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2106 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2107 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002108
Guenter Roeck5c391262013-03-30 15:02:12 -07002109 NULL,
2110};
2111
2112static const struct attribute_group it87_group_auto_pwm = {
2113 .attrs = it87_attributes_auto_pwm,
2114 .is_visible = it87_auto_pwm_is_visible,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002115};
2116
Jean Delvare2d8672c2005-07-19 23:56:35 +02002117/* SuperIO detection - will change isa_address if a chip is found */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002118static int __init it87_find(int sioaddr, unsigned short *address,
2119 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002121 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002122 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002123 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002124 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002126 err = superio_enter(sioaddr);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002127 if (err)
2128 return err;
2129
2130 err = -ENODEV;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002131 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002132
2133 switch (chip_type) {
2134 case IT8705F_DEVID:
2135 sio_data->type = it87;
2136 break;
2137 case IT8712F_DEVID:
2138 sio_data->type = it8712;
2139 break;
2140 case IT8716F_DEVID:
2141 case IT8726F_DEVID:
2142 sio_data->type = it8716;
2143 break;
2144 case IT8718F_DEVID:
2145 sio_data->type = it8718;
2146 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002147 case IT8720F_DEVID:
2148 sio_data->type = it8720;
2149 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002150 case IT8721F_DEVID:
2151 sio_data->type = it8721;
2152 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01002153 case IT8728F_DEVID:
2154 sio_data->type = it8728;
2155 break;
Justin Maggardead80802015-08-05 12:53:08 -07002156 case IT8732F_DEVID:
2157 sio_data->type = it8732;
2158 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06002159 case IT8771E_DEVID:
2160 sio_data->type = it8771;
2161 break;
2162 case IT8772E_DEVID:
2163 sio_data->type = it8772;
2164 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002165 case IT8781F_DEVID:
2166 sio_data->type = it8781;
2167 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08002168 case IT8782F_DEVID:
2169 sio_data->type = it8782;
2170 break;
2171 case IT8783E_DEVID:
2172 sio_data->type = it8783;
2173 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002174 case IT8786E_DEVID:
2175 sio_data->type = it8786;
2176 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07002177 case IT8790E_DEVID:
2178 sio_data->type = it8790;
2179 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02002180 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02002181 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002182 sio_data->type = it8603;
2183 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002184 case IT8620E_DEVID:
2185 sio_data->type = it8620;
2186 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002187 case 0xffff: /* No device at all */
2188 goto exit;
2189 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01002190 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002191 goto exit;
2192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002194 superio_select(sioaddr, PME);
2195 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002196 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 goto exit;
2198 }
2199
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002200 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002202 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 goto exit;
2204 }
2205
2206 err = 0;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002207 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07002208 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2209 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002210 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002212 config = &it87_devices[sio_data->type];
2213
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002214 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002215 if (has_in7_internal(config))
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002216 sio_data->internal |= (1 << 1);
2217
Jean Delvare738e5e02010-08-14 21:08:50 +02002218 /* in8 (Vbat) is always internal */
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002219 sio_data->internal |= (1 << 2);
2220
Guenter Roeck73055402015-03-26 09:16:32 -07002221 /* in9 (AVCC3), always internal if supported */
2222 if (has_avcc3(config))
2223 sio_data->internal |= (1 << 3); /* in9 is AVCC */
2224 else
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002225 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02002226
Guenter Roeck36c4d982015-03-26 18:18:19 -07002227 if (!has_six_pwm(config))
2228 sio_data->skip_pwm |= (1 << 3) | (1 << 4) | (1 << 5);
2229
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002230 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01002231 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01002232
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002233 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2234 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01002235 /* The IT8705F has a different LD number for GPIO */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002236 superio_select(sioaddr, 5);
2237 sio_data->beep_pin = superio_inb(sioaddr,
2238 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002239 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002240 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002241
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002242 superio_select(sioaddr, GPIO);
Guenter Roeck0531d982012-03-02 11:46:44 -08002243
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002244 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2245 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2246 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2247 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2248 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002249
Guenter Roeck0531d982012-03-02 11:46:44 -08002250 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002251 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08002252 sio_data->skip_fan |= (1 << 2);
2253 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002254 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08002255 sio_data->skip_pwm |= (1 << 2);
2256
2257 /* Check if fan2 is there or not */
2258 if (reg27 & (1 << 7))
2259 sio_data->skip_fan |= (1 << 1);
2260 if (reg27 & (1 << 3))
2261 sio_data->skip_pwm |= (1 << 1);
2262
2263 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002264 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002265 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002266
2267 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002268 if (reg27 & (1 << 1))
2269 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002270
2271 /*
2272 * VIN7
2273 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2274 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002275 if (reg27 & (1 << 2)) {
2276 /*
2277 * The data sheet is a bit unclear regarding the
2278 * internal voltage divider for VCCH5V. It says
2279 * "This bit enables and switches VIN7 (pin 91) to the
2280 * internal voltage divider for VCCH5V".
2281 * This is different to other chips, where the internal
2282 * voltage divider would connect VIN7 to an internal
2283 * voltage source. Maybe that is the case here as well.
2284 *
2285 * Since we don't know for sure, re-route it if that is
2286 * not the case, and ask the user to report if the
2287 * resulting voltage is sane.
2288 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002289 if (!(reg2c & (1 << 1))) {
2290 reg2c |= (1 << 1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002291 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2292 reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002293 pr_notice("Routing internal VCCH5V to in7.\n");
2294 }
2295 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2296 pr_notice("Please report if it displays a reasonable voltage.\n");
2297 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002298
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002299 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08002300 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002301 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08002302 sio_data->internal |= (1 << 1);
2303
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002304 sio_data->beep_pin = superio_inb(sioaddr,
2305 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002306 } else if (sio_data->type == it8603) {
2307 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002308
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002309 superio_select(sioaddr, GPIO);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002310
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002311 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002312
2313 /* Check if fan3 is there or not */
2314 if (reg27 & (1 << 6))
2315 sio_data->skip_pwm |= (1 << 2);
2316 if (reg27 & (1 << 7))
2317 sio_data->skip_fan |= (1 << 2);
2318
2319 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002320 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002321 if (reg29 & (1 << 1))
2322 sio_data->skip_pwm |= (1 << 1);
2323 if (reg29 & (1 << 2))
2324 sio_data->skip_fan |= (1 << 1);
2325
2326 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2327 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2328
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002329 sio_data->beep_pin = superio_inb(sioaddr,
2330 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002331 } else if (sio_data->type == it8620) {
2332 int reg;
2333
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002334 superio_select(sioaddr, GPIO);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002335
Guenter Roeck36c4d982015-03-26 18:18:19 -07002336 /* Check for pwm5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002337 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002338 if (reg & (1 << 6))
2339 sio_data->skip_pwm |= (1 << 4);
2340
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002341 /* Check for fan4, fan5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002342 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002343 if (!(reg & (1 << 5)))
2344 sio_data->skip_fan |= (1 << 3);
2345 if (!(reg & (1 << 4)))
2346 sio_data->skip_fan |= (1 << 4);
2347
2348 /* Check for pwm3, fan3 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002349 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002350 if (reg & (1 << 6))
2351 sio_data->skip_pwm |= (1 << 2);
2352 if (reg & (1 << 7))
2353 sio_data->skip_fan |= (1 << 2);
2354
Guenter Roeck36c4d982015-03-26 18:18:19 -07002355 /* Check for pwm4 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002356 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002357 if (!(reg & (1 << 2)))
2358 sio_data->skip_pwm |= (1 << 3);
2359
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002360 /* Check for pwm2, fan2 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002361 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002362 if (reg & (1 << 1))
2363 sio_data->skip_pwm |= (1 << 1);
2364 if (reg & (1 << 2))
2365 sio_data->skip_fan |= (1 << 1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002366 /* Check for pwm6, fan6 */
2367 if (!(reg & (1 << 7))) {
2368 sio_data->skip_pwm |= (1 << 5);
2369 sio_data->skip_fan |= (1 << 5);
2370 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002371
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002372 sio_data->beep_pin = superio_inb(sioaddr,
2373 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002374 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002375 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002376 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002377
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002378 superio_select(sioaddr, GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002379
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002380 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002381 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002382 /* We need at least 4 VID pins */
2383 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002384 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002385 sio_data->skip_vid = 1;
2386 }
Jean Delvare895ff262009-12-09 20:35:47 +01002387 }
2388
Jean Delvare591ec652009-12-09 20:35:48 +01002389 /* Check if fan3 is there or not */
2390 if (reg & (1 << 6))
2391 sio_data->skip_pwm |= (1 << 2);
2392 if (reg & (1 << 7))
2393 sio_data->skip_fan |= (1 << 2);
2394
2395 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002396 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Jean Delvare591ec652009-12-09 20:35:48 +01002397 if (reg & (1 << 1))
2398 sio_data->skip_pwm |= (1 << 1);
2399 if (reg & (1 << 2))
2400 sio_data->skip_fan |= (1 << 1);
2401
Jean Delvare895ff262009-12-09 20:35:47 +01002402 if ((sio_data->type == it8718 || sio_data->type == it8720)
2403 && !(sio_data->skip_vid))
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002404 sio_data->vid_value = superio_inb(sioaddr,
2405 IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002406
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002407 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002408
2409 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2410
Jean Delvare436cad22010-07-09 16:22:48 +02002411 /*
2412 * The IT8720F has no VIN7 pin, so VCCH should always be
2413 * routed internally to VIN7 with an internal divider.
2414 * Curiously, there still is a configuration bit to control
2415 * this, which means it can be set incorrectly. And even
2416 * more curiously, many boards out there are improperly
2417 * configured, even though the IT8720F datasheet claims
2418 * that the internal routing of VCCH to VIN7 is the default
2419 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002420 *
2421 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002422 * If UART6 is enabled, re-route VIN7 to the internal divider
2423 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002424 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002425 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02002426 reg |= (1 << 1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002427 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002428 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002429 }
Jean Delvare87673dd2006-08-28 14:37:19 +02002430 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02002431 sio_data->internal |= (1 << 0);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002432 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02002433 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002434
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002435 /*
2436 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2437 * While VIN7 can be routed to the internal voltage divider,
2438 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002439 *
2440 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2441 * is the temperature source. Since we can not read the
2442 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002443 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002444 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002445 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002446 sio_data->skip_temp |= (1 << 2);
2447 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002448
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002449 sio_data->beep_pin = superio_inb(sioaddr,
2450 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002451 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002452 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002453 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002454
Jean Delvare98dd22c2008-10-09 15:33:58 +02002455 /* Disable specific features based on DMI strings */
2456 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2457 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2458 if (board_vendor && board_name) {
2459 if (strcmp(board_vendor, "nVIDIA") == 0
2460 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002461 /*
2462 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2463 * connected to a fan, but to something else. One user
2464 * has reported instant system power-off when changing
2465 * the PWM2 duty cycle, so we disable it.
2466 * I use the board name string as the trigger in case
2467 * the same board is ever used in other systems.
2468 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002469 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002470 sio_data->skip_pwm = (1 << 1);
2471 }
2472 }
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474exit:
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002475 superio_exit(sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return err;
2477}
2478
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002479/* Called when we have found a new IT87. */
2480static void it87_init_device(struct platform_device *pdev)
2481{
2482 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2483 struct it87_data *data = platform_get_drvdata(pdev);
2484 int tmp, i;
2485 u8 mask;
2486
2487 /*
2488 * For each PWM channel:
2489 * - If it is in automatic mode, setting to manual mode should set
2490 * the fan to full speed by default.
2491 * - If it is in manual mode, we need a mapping to temperature
2492 * channels to use when later setting to automatic mode later.
2493 * Use a 1:1 mapping by default (we are clueless.)
2494 * In both cases, the value can (and should) be changed by the user
2495 * prior to switching to a different mode.
2496 * Note that this is no longer needed for the IT8721F and later, as
2497 * these have separate registers for the temperature mapping and the
2498 * manual duty cycle.
2499 */
2500 for (i = 0; i < 3; i++) {
2501 data->pwm_temp_map[i] = i;
2502 data->pwm_duty[i] = 0x7f; /* Full speed */
2503 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2504 }
2505
2506 /*
2507 * Some chips seem to have default value 0xff for all limit
2508 * registers. For low voltage limits it makes no sense and triggers
2509 * alarms, so change to 0 instead. For high temperature limits, it
2510 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2511 * but is still confusing, so change to 127 degrees C.
2512 */
2513 for (i = 0; i < 8; i++) {
2514 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2515 if (tmp == 0xff)
2516 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2517 }
2518 for (i = 0; i < 3; i++) {
2519 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2520 if (tmp == 0xff)
2521 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2522 }
2523
2524 /*
2525 * Temperature channels are not forcibly enabled, as they can be
2526 * set to two different sensor types and we can't guess which one
2527 * is correct for a given system. These channels can be enabled at
2528 * run-time through the temp{1-3}_type sysfs accessors if needed.
2529 */
2530
2531 /* Check if voltage monitors are reset manually or by some reason */
2532 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2533 if ((tmp & 0xff) == 0) {
2534 /* Enable all voltage monitors */
2535 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2536 }
2537
2538 /* Check if tachometers are reset manually or by some reason */
2539 mask = 0x70 & ~(sio_data->skip_fan << 4);
2540 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2541 if ((data->fan_main_ctrl & mask) == 0) {
2542 /* Enable all fan tachometers */
2543 data->fan_main_ctrl |= mask;
2544 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2545 data->fan_main_ctrl);
2546 }
2547 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2548
2549 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2550
2551 /* Set tachometers to 16-bit mode if needed */
2552 if (has_fan16_config(data)) {
2553 if (~tmp & 0x07 & data->has_fan) {
2554 dev_dbg(&pdev->dev,
2555 "Setting fan1-3 to 16-bit mode\n");
2556 it87_write_value(data, IT87_REG_FAN_16BIT,
2557 tmp | 0x07);
2558 }
2559 }
2560
2561 /* Check for additional fans */
2562 if (has_five_fans(data)) {
2563 if (tmp & (1 << 4))
2564 data->has_fan |= (1 << 3); /* fan4 enabled */
2565 if (tmp & (1 << 5))
2566 data->has_fan |= (1 << 4); /* fan5 enabled */
2567 if (has_six_fans(data) && (tmp & (1 << 2)))
2568 data->has_fan |= (1 << 5); /* fan6 enabled */
2569 }
2570
2571 /* Fan input pins may be used for alternative functions */
2572 data->has_fan &= ~sio_data->skip_fan;
2573
2574 /* Check if pwm5, pwm6 are enabled */
2575 if (has_six_pwm(data)) {
2576 /* The following code may be IT8620E specific */
2577 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2578 if ((tmp & 0xc0) == 0xc0)
2579 sio_data->skip_pwm |= (1 << 4);
2580 if (!(tmp & (1 << 3)))
2581 sio_data->skip_pwm |= (1 << 5);
2582 }
2583
2584 /* Start monitoring */
2585 it87_write_value(data, IT87_REG_CONFIG,
2586 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2587 | (update_vbat ? 0x41 : 0x01));
2588}
2589
2590/* Return 1 if and only if the PWM interface is safe to use */
2591static int it87_check_pwm(struct device *dev)
2592{
2593 struct it87_data *data = dev_get_drvdata(dev);
2594 /*
2595 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2596 * and polarity set to active low is sign that this is the case so we
2597 * disable pwm control to protect the user.
2598 */
2599 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2600
2601 if ((tmp & 0x87) == 0) {
2602 if (fix_pwm_polarity) {
2603 /*
2604 * The user asks us to attempt a chip reconfiguration.
2605 * This means switching to active high polarity and
2606 * inverting all fan speed values.
2607 */
2608 int i;
2609 u8 pwm[3];
2610
2611 for (i = 0; i < 3; i++)
2612 pwm[i] = it87_read_value(data,
2613 IT87_REG_PWM[i]);
2614
2615 /*
2616 * If any fan is in automatic pwm mode, the polarity
2617 * might be correct, as suspicious as it seems, so we
2618 * better don't change anything (but still disable the
2619 * PWM interface).
2620 */
2621 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2622 dev_info(dev,
2623 "Reconfiguring PWM to active high polarity\n");
2624 it87_write_value(data, IT87_REG_FAN_CTL,
2625 tmp | 0x87);
2626 for (i = 0; i < 3; i++)
2627 it87_write_value(data,
2628 IT87_REG_PWM[i],
2629 0x7f & ~pwm[i]);
2630 return 1;
2631 }
2632
2633 dev_info(dev,
2634 "PWM configuration is too broken to be fixed\n");
2635 }
2636
2637 dev_info(dev,
2638 "Detected broken BIOS defaults, disabling PWM interface\n");
2639 return 0;
2640 } else if (fix_pwm_polarity) {
2641 dev_info(dev,
2642 "PWM configuration looks sane, won't touch\n");
2643 }
2644
2645 return 1;
2646}
2647
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002648static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002651 struct resource *res;
2652 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002653 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 int enable_pwm_interface;
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002655 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002657 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002658 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2659 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002660 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2661 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002662 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002663 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Guenter Roeck62a1d052012-03-24 21:54:41 -07002666 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2667 if (!data)
2668 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002670 data->addr = res->start;
2671 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002672 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002673 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002674 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002675 /*
2676 * IT8705F Datasheet 0.4.1, 3h == Version G.
2677 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2678 * These are the first revisions with 16-bit tachometer support.
2679 */
2680 switch (data->type) {
2681 case it87:
2682 if (sio_data->revision >= 0x03) {
2683 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002684 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002685 }
2686 break;
2687 case it8712:
2688 if (sio_data->revision >= 0x08) {
2689 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002690 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2691 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002692 }
2693 break;
2694 default:
2695 break;
2696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002699 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002700 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2701 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002703 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002705 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002708 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002710 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002711 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002712 if (sio_data->internal & (1 << 0))
2713 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2714 if (sio_data->internal & (1 << 1))
2715 data->in_scaled |= (1 << 7); /* in7 is VSB */
2716 if (sio_data->internal & (1 << 2))
2717 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002718 if (sio_data->internal & (1 << 3))
2719 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002720 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2721 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002722 if (sio_data->internal & (1 << 0))
2723 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2724 if (sio_data->internal & (1 << 1))
2725 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002726 }
2727
Guenter Roeck4573acb2012-03-26 16:17:41 -07002728 data->has_temp = 0x07;
2729 if (sio_data->skip_temp & (1 << 2)) {
2730 if (sio_data->type == it8782
2731 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2732 data->has_temp &= ~(1 << 2);
2733 }
2734
Guenter Roeckd3766842013-03-30 15:47:21 -07002735 data->in_internal = sio_data->internal;
Guenter Roeck52929712013-03-30 14:00:08 -07002736 data->has_in = 0x3ff & ~sio_data->skip_in;
2737
Guenter Roeckcc18da72015-04-01 10:03:01 -07002738 if (has_six_temp(data)) {
2739 u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
2740
2741 if ((reg & 0x03) >= 0x02)
2742 data->has_temp |= (1 << 3);
2743 if (((reg >> 2) & 0x03) >= 0x02)
2744 data->has_temp |= (1 << 4);
2745 if (((reg >> 4) & 0x03) >= 0x02)
2746 data->has_temp |= (1 << 5);
2747 }
2748
Guenter Roeck52929712013-03-30 14:00:08 -07002749 data->has_beep = !!sio_data->beep_pin;
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002752 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Guenter Roeckd3766842013-03-30 15:47:21 -07002754 if (!sio_data->skip_vid) {
2755 data->has_vid = true;
2756 data->vrm = vid_which_vrm();
2757 /* VID reading from Super-I/O config space if available */
2758 data->vid = sio_data->vid_value;
2759 }
2760
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002761 /* Prepare for sysfs hooks */
2762 data->groups[0] = &it87_group;
2763 data->groups[1] = &it87_group_in;
2764 data->groups[2] = &it87_group_temp;
2765 data->groups[3] = &it87_group_fan;
Jean Delvare17d648b2006-08-28 14:23:46 +02002766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 if (enable_pwm_interface) {
Guenter Roeck5c391262013-03-30 15:02:12 -07002768 data->has_pwm = (1 << ARRAY_SIZE(IT87_REG_PWM)) - 1;
2769 data->has_pwm &= ~sio_data->skip_pwm;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002770
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002771 data->groups[4] = &it87_group_pwm;
2772 if (has_old_autopwm(data))
2773 data->groups[5] = &it87_group_auto_pwm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
2775
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002776 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
2777 it87_devices[sio_data->type].name,
2778 data, data->groups);
2779 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002782static struct platform_driver it87_driver = {
2783 .driver = {
2784 .name = DRVNAME,
2785 },
2786 .probe = it87_probe,
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002787};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Guenter Roecke84bd952015-03-28 08:24:29 -07002789static int __init it87_device_add(int index, unsigned short address,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002790 const struct it87_sio_data *sio_data)
2791{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002792 struct platform_device *pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002793 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002794 .start = address + IT87_EC_OFFSET,
2795 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002796 .name = DRVNAME,
2797 .flags = IORESOURCE_IO,
2798 };
2799 int err;
2800
Jean Delvareb9acb642009-01-07 16:37:35 +01002801 err = acpi_check_resource_conflict(&res);
2802 if (err)
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002803 return err;
Jean Delvareb9acb642009-01-07 16:37:35 +01002804
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002805 pdev = platform_device_alloc(DRVNAME, address);
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002806 if (!pdev)
2807 return -ENOMEM;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002808
2809 err = platform_device_add_resources(pdev, &res, 1);
2810 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002811 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002812 goto exit_device_put;
2813 }
2814
2815 err = platform_device_add_data(pdev, sio_data,
2816 sizeof(struct it87_sio_data));
2817 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002818 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002819 goto exit_device_put;
2820 }
2821
2822 err = platform_device_add(pdev);
2823 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002824 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002825 goto exit_device_put;
2826 }
2827
Guenter Roecke84bd952015-03-28 08:24:29 -07002828 it87_pdev[index] = pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002829 return 0;
2830
2831exit_device_put:
2832 platform_device_put(pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002833 return err;
2834}
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836static int __init sm_it87_init(void)
2837{
Guenter Roecke84bd952015-03-28 08:24:29 -07002838 int sioaddr[2] = { REG_2E, REG_4E };
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002839 struct it87_sio_data sio_data;
Guenter Roecke84bd952015-03-28 08:24:29 -07002840 unsigned short isa_address;
2841 bool found = false;
2842 int i, err;
Jean Delvarefde09502005-07-19 23:51:07 +02002843
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002844 err = platform_driver_register(&it87_driver);
2845 if (err)
2846 return err;
2847
Guenter Roecke84bd952015-03-28 08:24:29 -07002848 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
2849 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2850 isa_address = 0;
2851 err = it87_find(sioaddr[i], &isa_address, &sio_data);
2852 if (err || isa_address == 0)
2853 continue;
2854
2855 err = it87_device_add(i, isa_address, &sio_data);
2856 if (err)
2857 goto exit_dev_unregister;
2858 found = true;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002859 }
2860
Guenter Roecke84bd952015-03-28 08:24:29 -07002861 if (!found) {
2862 err = -ENODEV;
2863 goto exit_unregister;
2864 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002865 return 0;
Guenter Roecke84bd952015-03-28 08:24:29 -07002866
2867exit_dev_unregister:
2868 /* NULL check handled by platform_device_unregister */
2869 platform_device_unregister(it87_pdev[0]);
2870exit_unregister:
2871 platform_driver_unregister(&it87_driver);
2872 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873}
2874
2875static void __exit sm_it87_exit(void)
2876{
Guenter Roecke84bd952015-03-28 08:24:29 -07002877 /* NULL check handled by platform_device_unregister */
2878 platform_device_unregister(it87_pdev[1]);
2879 platform_device_unregister(it87_pdev[0]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002880 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881}
2882
2883
Jean Delvare7c81c602014-01-29 20:40:08 +01002884MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002885MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886module_param(update_vbat, bool, 0);
2887MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2888module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002889MODULE_PARM_DESC(fix_pwm_polarity,
2890 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891MODULE_LICENSE("GPL");
2892
2893module_init(sm_it87_init);
2894module_exit(sm_it87_exit);