blob: 96876755376c2e304fb8acc0de65ead3b8bc89c9 [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 Roeckf838aa22015-04-01 20:09:36 -0700222/*
223 * Monitors:
224 * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
225 * - up to 6 temp (1 to 6)
226 * - up to 6 fan (1 to 6)
227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700229static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
230static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
231static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
232static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
233static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#define IT87_REG_FAN_MAIN_CTRL 0x13
236#define IT87_REG_FAN_CTL 0x14
Guenter Roeck36c4d982015-03-26 18:18:19 -0700237static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
238static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Guenter Roeck559313c2015-04-01 10:15:38 -0700240static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
Guenter Roeckf838aa22015-04-01 20:09:36 -0700241 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Guenter Roeck559313c2015-04-01 10:15:38 -0700243#define IT87_REG_TEMP(nr) (0x29 + (nr))
Guenter Roeck73055402015-03-26 09:16:32 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
246#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
247#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
248#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#define IT87_REG_VIN_ENABLE 0x50
251#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700252#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100253#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255#define IT87_REG_CHIPID 0x58
256
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100257#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
258#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
259
Guenter Roeckcc18da72015-04-01 10:03:01 -0700260#define IT87_REG_TEMP456_ENABLE 0x77
261
Guenter Roeck483db432012-12-19 22:17:02 +0100262struct it87_devices {
263 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700264 const char * const suffix;
Guenter Roeckcc18da72015-04-01 10:03:01 -0700265 u32 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100266 u8 peci_mask;
267 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100268};
269
270#define FEAT_12MV_ADC (1 << 0)
271#define FEAT_NEWER_AUTOPWM (1 << 1)
272#define FEAT_OLD_AUTOPWM (1 << 2)
273#define FEAT_16BIT_FANS (1 << 3)
274#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100275#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100276#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800277#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
278#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800279#define FEAT_VID (1 << 9) /* Set if chip supports VID */
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700280#define FEAT_IN7_INTERNAL (1 << 10) /* Set if in7 is internal */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700281#define FEAT_SIX_FANS (1 << 11) /* Supports six fans */
Justin Maggardead80802015-08-05 12:53:08 -0700282#define FEAT_10_9MV_ADC (1 << 12)
Guenter Roeck73055402015-03-26 09:16:32 -0700283#define FEAT_AVCC3 (1 << 13) /* Chip supports in9/AVCC3 */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700284#define FEAT_SIX_PWM (1 << 14) /* Chip supports 6 pwm chn */
Guenter Roeck60878bc2015-03-26 19:57:42 -0700285#define FEAT_PWM_FREQ2 (1 << 15) /* Separate pwm freq 2 */
Guenter Roeckcc18da72015-04-01 10:03:01 -0700286#define FEAT_SIX_TEMP (1 << 16) /* Up to 6 temp sensors */
Guenter Roeck483db432012-12-19 22:17:02 +0100287
288static const struct it87_devices it87_devices[] = {
289 [it87] = {
290 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700291 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100292 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
293 },
294 [it8712] = {
295 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700296 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800297 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
298 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100299 },
300 [it8716] = {
301 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700302 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800303 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700304 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
Guenter Roeck483db432012-12-19 22:17:02 +0100305 },
306 [it8718] = {
307 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700308 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800309 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700310 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
311 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100312 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100313 },
314 [it8720] = {
315 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700316 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800317 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700318 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
319 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100320 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100321 },
322 [it8721] = {
323 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700324 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100325 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800326 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck60878bc2015-03-26 19:57:42 -0700327 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
328 | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100329 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100330 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100331 },
332 [it8728] = {
333 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700334 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100335 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700336 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700337 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100338 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100339 },
Justin Maggardead80802015-08-05 12:53:08 -0700340 [it8732] = {
341 .name = "it8732",
342 .suffix = "F",
343 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
344 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
345 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
346 .peci_mask = 0x07,
347 .old_peci_mask = 0x02, /* Actually reports PCH */
348 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600349 [it8771] = {
350 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700351 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600352 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700353 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
354 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800355 /* PECI: guesswork */
356 /* 12mV ADC (OHM) */
357 /* 16 bit fans (OHM) */
358 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600359 .peci_mask = 0x07,
360 },
361 [it8772] = {
362 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700363 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600364 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700365 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
366 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800367 /* PECI (coreboot) */
368 /* 12mV ADC (HWSensors4, OHM) */
369 /* 16 bit fans (HWSensors4, OHM) */
370 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600371 .peci_mask = 0x07,
372 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800373 [it8781] = {
374 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700375 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800376 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700377 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800378 .old_peci_mask = 0x4,
379 },
Guenter Roeck483db432012-12-19 22:17:02 +0100380 [it8782] = {
381 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700382 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100383 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700384 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100385 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100386 },
387 [it8783] = {
388 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700389 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100390 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700391 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100392 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100393 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100394 [it8786] = {
395 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700396 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100397 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700398 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
399 | FEAT_PWM_FREQ2,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100400 .peci_mask = 0x07,
401 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700402 [it8790] = {
403 .name = "it8790",
404 .suffix = "E",
405 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700406 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
407 | FEAT_PWM_FREQ2,
Guenter Roeck4ee07152015-03-25 23:26:28 -0700408 .peci_mask = 0x07,
409 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100410 [it8603] = {
411 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700412 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100413 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck73055402015-03-26 09:16:32 -0700414 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
Guenter Roeck60878bc2015-03-26 19:57:42 -0700415 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100416 .peci_mask = 0x07,
417 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800418 [it8620] = {
419 .name = "it8620",
420 .suffix = "E",
421 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700422 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeckcc18da72015-04-01 10:03:01 -0700423 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
424 | FEAT_SIX_TEMP,
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800425 .peci_mask = 0x07,
426 },
Guenter Roeck483db432012-12-19 22:17:02 +0100427};
428
429#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
430#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
Justin Maggardead80802015-08-05 12:53:08 -0700431#define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
Guenter Roeck483db432012-12-19 22:17:02 +0100432#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
433#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
434#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100435#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
436 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100437#define has_temp_old_peci(data, nr) \
438 (((data)->features & FEAT_TEMP_OLD_PECI) && \
439 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800440#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700441#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
442 FEAT_SIX_FANS))
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800443#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700444#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700445#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Guenter Roeck73055402015-03-26 09:16:32 -0700446#define has_avcc3(data) ((data)->features & FEAT_AVCC3)
Guenter Roeck36c4d982015-03-26 18:18:19 -0700447#define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
Guenter Roeck60878bc2015-03-26 19:57:42 -0700448#define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
Guenter Roeckcc18da72015-04-01 10:03:01 -0700449#define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200451struct it87_sio_data {
452 enum chips type;
453 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200454 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200455 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100456 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200457 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100458 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700459 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100460 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100461 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200462 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700463 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200464};
465
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800466/*
467 * For each registered chip, we need to keep some data in memory.
468 * The structure is dynamically allocated.
469 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470struct it87_data {
Guenter Roeck8638d0a2015-03-30 11:13:49 -0700471 const struct attribute_group *groups[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100473 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100474 u8 peci_mask;
475 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200477 unsigned short addr;
478 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100479 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 char valid; /* !=0 if following fields are valid */
481 unsigned long last_updated; /* In jiffies */
482
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200483 u16 in_scaled; /* Internal voltage sensors are scaled */
Guenter Roeckd3766842013-03-30 15:47:21 -0700484 u16 in_internal; /* Bitfield, internal sensors (for labels) */
Guenter Roeck52929712013-03-30 14:00:08 -0700485 u16 has_in; /* Bitfield, voltage sensors enabled */
Guenter Roeckf838aa22015-04-01 20:09:36 -0700486 u8 in[13][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200487 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700488 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700489 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeckcc18da72015-04-01 10:03:01 -0700490 s8 temp[6][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100491 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
492 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 u8 fan_div[3]; /* Register encoding, shifted right */
Guenter Roeckd3766842013-03-30 15:47:21 -0700494 bool has_vid; /* True if VID supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100496 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 u32 alarms; /* Register encoding, combined */
Guenter Roeck52929712013-03-30 14:00:08 -0700498 bool has_beep; /* true if beep supported */
Jean Delvared9b327c2010-03-05 22:17:17 +0100499 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100501 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100502
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800503 /*
504 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100505 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
506 * 7, and we want to preserve settings on mode changes, so we have
507 * to track all values separately.
508 * Starting with the IT8721F, the manual PWM duty cycles are stored
509 * in separate registers (8-bit values), so the separate tracking
510 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800511 * simple.
512 */
Guenter Roeck5c391262013-03-30 15:02:12 -0700513 u8 has_pwm; /* Bitfield, pwm control enabled */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700514 u8 pwm_ctrl[6]; /* Register value */
515 u8 pwm_duty[6]; /* Manual PWM value set by user */
516 u8 pwm_temp_map[6]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100517
518 /* Automatic fan speed control registers */
519 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
520 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521};
522
Guenter Roeck0531d982012-03-02 11:46:44 -0800523static int adc_lsb(const struct it87_data *data, int nr)
524{
Justin Maggardead80802015-08-05 12:53:08 -0700525 int lsb;
526
527 if (has_12mv_adc(data))
528 lsb = 120;
529 else if (has_10_9mv_adc(data))
530 lsb = 109;
531 else
532 lsb = 160;
Guenter Roeck0531d982012-03-02 11:46:44 -0800533 if (data->in_scaled & (1 << nr))
534 lsb <<= 1;
535 return lsb;
536}
537
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200538static u8 in_to_reg(const struct it87_data *data, int nr, long val)
539{
Justin Maggardead80802015-08-05 12:53:08 -0700540 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800541 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200542}
543
544static int in_from_reg(const struct it87_data *data, int nr, int val)
545{
Justin Maggardead80802015-08-05 12:53:08 -0700546 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200547}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200548
549static inline u8 FAN_TO_REG(long rpm, int div)
550{
551 if (rpm == 0)
552 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800553 rpm = clamp_val(rpm, 1, 1000000);
554 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200555}
556
557static inline u16 FAN16_TO_REG(long rpm)
558{
559 if (rpm == 0)
560 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800561 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200562}
563
564#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
565 1350000 / ((val) * (div)))
566/* The divider is fixed to 2 in 16-bit mode */
567#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
568 1350000 / ((val) * 2))
569
Guenter Roeck2a844c12013-01-09 08:09:34 -0800570#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
571 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200572#define TEMP_FROM_REG(val) ((val) * 1000)
573
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200574static u8 pwm_to_reg(const struct it87_data *data, long val)
575{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100576 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200577 return val;
578 else
579 return val >> 1;
580}
581
582static int pwm_from_reg(const struct it87_data *data, u8 reg)
583{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100584 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200585 return reg;
586 else
587 return (reg & 0x7f) << 1;
588}
589
Jean Delvare0df6454d2010-10-28 20:31:51 +0200590
591static int DIV_TO_REG(int val)
592{
593 int answer = 0;
594 while (answer < 7 && (val >>= 1))
595 answer++;
596 return answer;
597}
598#define DIV_FROM_REG(val) (1 << (val))
599
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700600/*
601 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
602 * depending on the chip type, to calculate the actual PWM frequency.
603 *
604 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
605 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
606 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
607 * sometimes just one. It is unknown if this is a datasheet error or real,
608 * so this is ignored for now.
609 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200610static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700611 48000000,
612 24000000,
613 12000000,
614 8000000,
615 6000000,
616 3000000,
617 1500000,
618 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200619};
620
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700621/*
622 * Must be called with data->update_lock held, except during initialization.
623 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
624 * would slow down the IT87 access and should not be necessary.
625 */
626static int it87_read_value(struct it87_data *data, u8 reg)
627{
628 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
629 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
630}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700632/*
633 * Must be called with data->update_lock held, except during initialization.
634 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
635 * would slow down the IT87 access and should not be necessary.
636 */
637static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
638{
639 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
640 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
641}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700643static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
644{
645 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
646 if (has_newer_autopwm(data)) {
647 data->pwm_temp_map[nr] = (data->pwm_ctrl[nr] & 0x03) +
648 nr < 3 ? 0 : 3;
649 data->pwm_duty[nr] = it87_read_value(data,
650 IT87_REG_PWM_DUTY[nr]);
651 } else {
652 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
653 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
654 else /* Manual mode */
655 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700658 if (has_old_autopwm(data)) {
659 int i;
660
661 for (i = 0; i < 5 ; i++)
662 data->auto_temp[nr][i] = it87_read_value(data,
663 IT87_REG_AUTO_TEMP(nr, i));
664 for (i = 0; i < 3 ; i++)
665 data->auto_pwm[nr][i] = it87_read_value(data,
666 IT87_REG_AUTO_PWM(nr, i));
667 }
668}
669
670static struct it87_data *it87_update_device(struct device *dev)
671{
672 struct it87_data *data = dev_get_drvdata(dev);
673 int i;
674
675 mutex_lock(&data->update_lock);
676
677 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
678 || !data->valid) {
679 if (update_vbat) {
680 /*
681 * Cleared after each update, so reenable. Value
682 * returned by this read will be previous value
683 */
684 it87_write_value(data, IT87_REG_CONFIG,
685 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
686 }
Guenter Roeck559313c2015-04-01 10:15:38 -0700687 for (i = 0; i < ARRAY_SIZE(IT87_REG_VIN); i++) {
688 if (!(data->has_in & (1 << i)))
689 continue;
690
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700691 data->in[i][0] =
Guenter Roeck559313c2015-04-01 10:15:38 -0700692 it87_read_value(data, IT87_REG_VIN[i]);
693
694 /* VBAT and AVCC don't have limit registers */
695 if (i >= 8)
696 continue;
697
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700698 data->in[i][1] =
699 it87_read_value(data, IT87_REG_VIN_MIN(i));
700 data->in[i][2] =
701 it87_read_value(data, IT87_REG_VIN_MAX(i));
702 }
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700703
704 for (i = 0; i < 6; i++) {
705 /* Skip disabled fans */
706 if (!(data->has_fan & (1 << i)))
707 continue;
708
709 data->fan[i][1] =
710 it87_read_value(data, IT87_REG_FAN_MIN[i]);
711 data->fan[i][0] = it87_read_value(data,
712 IT87_REG_FAN[i]);
713 /* Add high byte if in 16-bit mode */
714 if (has_16bit_fans(data)) {
715 data->fan[i][0] |= it87_read_value(data,
716 IT87_REG_FANX[i]) << 8;
717 data->fan[i][1] |= it87_read_value(data,
718 IT87_REG_FANX_MIN[i]) << 8;
719 }
720 }
Guenter Roeckcc18da72015-04-01 10:03:01 -0700721 for (i = 0; i < 6; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700722 if (!(data->has_temp & (1 << i)))
723 continue;
724 data->temp[i][0] =
725 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeckcc18da72015-04-01 10:03:01 -0700726
727 /* No limits/offset for additional sensors */
728 if (i >= 3)
729 continue;
730
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700731 data->temp[i][1] =
732 it87_read_value(data, IT87_REG_TEMP_LOW(i));
733 data->temp[i][2] =
734 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
735 if (has_temp_offset(data))
736 data->temp[i][3] =
737 it87_read_value(data,
738 IT87_REG_TEMP_OFFSET[i]);
739 }
740
741 /* Newer chips don't have clock dividers */
742 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
743 i = it87_read_value(data, IT87_REG_FAN_DIV);
744 data->fan_div[0] = i & 0x07;
745 data->fan_div[1] = (i >> 3) & 0x07;
746 data->fan_div[2] = (i & 0x40) ? 3 : 1;
747 }
748
749 data->alarms =
750 it87_read_value(data, IT87_REG_ALARM1) |
751 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
752 (it87_read_value(data, IT87_REG_ALARM3) << 16);
753 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
754
755 data->fan_main_ctrl = it87_read_value(data,
756 IT87_REG_FAN_MAIN_CTRL);
757 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
758 for (i = 0; i < 6; i++)
759 it87_update_pwm_ctrl(data, i);
760
761 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
762 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
763 /*
764 * The IT8705F does not have VID capability.
765 * The IT8718F and later don't use IT87_REG_VID for the
766 * same purpose.
767 */
768 if (data->type == it8712 || data->type == it8716) {
769 data->vid = it87_read_value(data, IT87_REG_VID);
770 /*
771 * The older IT8712F revisions had only 5 VID pins,
772 * but we assume it is always safe to read 6 bits.
773 */
774 data->vid &= 0x3f;
775 }
776 data->last_updated = jiffies;
777 data->valid = 1;
778 }
779
780 mutex_unlock(&data->update_lock);
781
782 return data;
783}
Jean Delvarefde09502005-07-19 23:51:07 +0200784
Jean Delvare20ad93d2005-06-05 11:53:25 +0200785static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100786 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100788 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
789 int nr = sattr->nr;
790 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100793 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Guenter Roeck929c6a52012-12-19 22:17:00 +0100796static ssize_t set_in(struct device *dev, struct device_attribute *attr,
797 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100799 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
800 int nr = sattr->nr;
801 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200802
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200803 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100804 unsigned long val;
805
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100806 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100807 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100809 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100810 data->in[nr][index] = in_to_reg(data, nr, val);
811 it87_write_value(data,
812 index == 1 ? IT87_REG_VIN_MIN(nr)
813 : IT87_REG_VIN_MAX(nr),
814 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100815 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return count;
817}
818
Guenter Roeck929c6a52012-12-19 22:17:00 +0100819static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
820static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
821 0, 1);
822static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
823 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Guenter Roeck929c6a52012-12-19 22:17:00 +0100825static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
826static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
827 1, 1);
828static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
829 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Guenter Roeck929c6a52012-12-19 22:17:00 +0100831static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
832static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
833 2, 1);
834static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
835 2, 2);
836
837static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
838static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
839 3, 1);
840static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
841 3, 2);
842
843static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
844static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
845 4, 1);
846static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
847 4, 2);
848
849static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
850static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
851 5, 1);
852static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
853 5, 2);
854
855static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
856static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
857 6, 1);
858static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
859 6, 2);
860
861static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
862static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
863 7, 1);
864static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
865 7, 2);
866
867static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100868static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Guenter Roeckf838aa22015-04-01 20:09:36 -0700869static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
870static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
871static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Guenter Roeckcc18da72015-04-01 10:03:01 -0700873/* Up to 6 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200874static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100875 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100877 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
878 int nr = sattr->nr;
879 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200881
Guenter Roeck60ca3852012-12-19 22:17:00 +0100882 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200884
Guenter Roeck60ca3852012-12-19 22:17:00 +0100885static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
886 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100888 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
889 int nr = sattr->nr;
890 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200891 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100892 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100893 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100894
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100895 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100896 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100898 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100899
900 switch (index) {
901 default:
902 case 1:
903 reg = IT87_REG_TEMP_LOW(nr);
904 break;
905 case 2:
906 reg = IT87_REG_TEMP_HIGH(nr);
907 break;
908 case 3:
909 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
910 if (!(regval & 0x80)) {
911 regval |= 0x80;
912 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
913 }
914 data->valid = 0;
915 reg = IT87_REG_TEMP_OFFSET[nr];
916 break;
917 }
918
Guenter Roeck60ca3852012-12-19 22:17:00 +0100919 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100920 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100921 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return count;
923}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200924
Guenter Roeck60ca3852012-12-19 22:17:00 +0100925static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
926static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
927 0, 1);
928static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
929 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100930static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
931 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100932static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
933static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
934 1, 1);
935static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
936 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100937static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
938 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100939static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
940static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
941 2, 1);
942static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
943 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100944static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
945 set_temp, 2, 3);
Guenter Roeckcc18da72015-04-01 10:03:01 -0700946static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
947static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
948static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Guenter Roeck2cece012012-12-19 22:17:01 +0100950static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
951 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200953 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
954 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800956 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100957 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100958
Guenter Roeck19529782012-12-19 22:17:02 +0100959 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
960 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100961 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (reg & (1 << nr))
963 return sprintf(buf, "3\n"); /* thermal diode */
964 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200965 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return sprintf(buf, "0\n"); /* disabled */
967}
Guenter Roeck2cece012012-12-19 22:17:01 +0100968
969static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
970 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200972 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
973 int nr = sensor_attr->index;
974
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200975 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100976 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100977 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100978
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100979 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100980 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Jean Delvare8acf07c2010-04-14 16:14:09 +0200982 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
983 reg &= ~(1 << nr);
984 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100985 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
986 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100987 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
988 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
989 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200990 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100991 dev_warn(dev,
992 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200993 val = 4;
994 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100995 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200997 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200998 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200999 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001000 else if (has_temp_peci(data, nr) && val == 6)
1001 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +01001002 else if (has_temp_old_peci(data, nr) && val == 6)
1003 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +02001004 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +02001006
1007 mutex_lock(&data->update_lock);
1008 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +01001009 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001010 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +01001011 if (has_temp_old_peci(data, nr))
1012 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +02001013 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001014 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return count;
1016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Guenter Roeck2cece012012-12-19 22:17:01 +01001018static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1019 set_temp_type, 0);
1020static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1021 set_temp_type, 1);
1022static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1023 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +01001026
1027static int pwm_mode(const struct it87_data *data, int nr)
1028{
1029 int ctrl = data->fan_main_ctrl & (1 << nr);
1030
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001031 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +01001032 return 0;
1033 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
1034 return 2;
1035 else /* Manual mode */
1036 return 1;
1037}
1038
Jean Delvare20ad93d2005-06-05 11:53:25 +02001039static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +01001040 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001042 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1043 int nr = sattr->nr;
1044 int index = sattr->index;
1045 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001047
Guenter Roecke1169ba2012-12-19 22:17:01 +01001048 speed = has_16bit_fans(data) ?
1049 FAN16_FROM_REG(data->fan[nr][index]) :
1050 FAN_FROM_REG(data->fan[nr][index],
1051 DIV_FROM_REG(data->fan_div[nr]));
1052 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001054
Jean Delvare20ad93d2005-06-05 11:53:25 +02001055static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1056 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001058 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1059 int nr = sensor_attr->index;
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct it87_data *data = it87_update_device(dev);
1062 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
1063}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001064static ssize_t show_pwm_enable(struct device *dev,
1065 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001067 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1068 int nr = sensor_attr->index;
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +01001071 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001073static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1074 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001076 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1077 int nr = sensor_attr->index;
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001080 return sprintf(buf, "%d\n",
1081 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001083static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1084 char *buf)
1085{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001086 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001087 struct it87_data *data = it87_update_device(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001088 int nr = sensor_attr->index;
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001089 unsigned int freq;
Guenter Roeck60878bc2015-03-26 19:57:42 -07001090 int index;
1091
1092 if (has_pwm_freq2(data) && nr == 1)
1093 index = (data->extra >> 4) & 0x07;
1094 else
1095 index = (data->fan_ctl >> 4) & 0x07;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001096
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001097 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1098
1099 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001100}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001101
1102static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1103 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001105 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1106 int nr = sattr->nr;
1107 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +02001108
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001109 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001110 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +01001111 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001113 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001114 return -EINVAL;
1115
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001116 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +01001117
1118 if (has_16bit_fans(data)) {
1119 data->fan[nr][index] = FAN16_TO_REG(val);
1120 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1121 data->fan[nr][index] & 0xff);
1122 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1123 data->fan[nr][index] >> 8);
1124 } else {
1125 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1126 switch (nr) {
1127 case 0:
1128 data->fan_div[nr] = reg & 0x07;
1129 break;
1130 case 1:
1131 data->fan_div[nr] = (reg >> 3) & 0x07;
1132 break;
1133 case 2:
1134 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1135 break;
1136 }
1137 data->fan[nr][index] =
1138 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1139 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1140 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -08001141 }
1142
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001143 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return count;
1145}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001146
Jean Delvare20ad93d2005-06-05 11:53:25 +02001147static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1148 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001150 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1151 int nr = sensor_attr->index;
1152
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001153 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001154 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001155 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 u8 old;
1157
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001158 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001159 return -EINVAL;
1160
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001161 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001162 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001164 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001165 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 switch (nr) {
1168 case 0:
1169 case 1:
1170 data->fan_div[nr] = DIV_TO_REG(val);
1171 break;
1172 case 2:
1173 if (val < 8)
1174 data->fan_div[nr] = 1;
1175 else
1176 data->fan_div[nr] = 3;
1177 }
1178 val = old & 0x80;
1179 val |= (data->fan_div[0] & 0x07);
1180 val |= (data->fan_div[1] & 0x07) << 3;
1181 if (data->fan_div[2] == 3)
1182 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001183 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001185 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001186 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1187 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001188
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001189 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return count;
1191}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001192
1193/* Returns 0 if OK, -EINVAL otherwise */
1194static int check_trip_points(struct device *dev, int nr)
1195{
1196 const struct it87_data *data = dev_get_drvdata(dev);
1197 int i, err = 0;
1198
1199 if (has_old_autopwm(data)) {
1200 for (i = 0; i < 3; i++) {
1201 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1202 err = -EINVAL;
1203 }
1204 for (i = 0; i < 2; i++) {
1205 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1206 err = -EINVAL;
1207 }
1208 }
1209
1210 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001211 dev_err(dev,
1212 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001213 dev_err(dev, "Adjust the trip points and try again\n");
1214 }
1215 return err;
1216}
1217
Jean Delvare20ad93d2005-06-05 11:53:25 +02001218static ssize_t set_pwm_enable(struct device *dev,
1219 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001221 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1222 int nr = sensor_attr->index;
1223
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001224 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001225 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001227 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001228 return -EINVAL;
1229
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001230 /* Check trip points before switching to automatic mode */
1231 if (val == 2) {
1232 if (check_trip_points(dev, nr) < 0)
1233 return -EINVAL;
1234 }
1235
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001236 /* IT8603E does not have on/off mode */
1237 if (val == 0 && data->type == it8603)
1238 return -EINVAL;
1239
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001240 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (val == 0) {
1243 int tmp;
1244 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001245 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1246 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /* set on/off mode */
1248 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +01001249 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1250 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +01001251 } else {
1252 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001253 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001254 data->pwm_temp_map[nr] :
1255 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001256 else /* Automatic mode */
1257 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001258 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001259
1260 if (data->type != it8603) {
1261 /* set SmartGuardian mode */
1262 data->fan_main_ctrl |= (1 << nr);
1263 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1264 data->fan_main_ctrl);
1265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001268 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return count;
1270}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001271static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1272 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001274 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1275 int nr = sensor_attr->index;
1276
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001277 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001278 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001280 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return -EINVAL;
1282
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001283 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001284 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001285 /*
1286 * If we are in automatic mode, the PWM duty cycle register
1287 * is read-only so we can't write the value.
1288 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001289 if (data->pwm_ctrl[nr] & 0x80) {
1290 mutex_unlock(&data->update_lock);
1291 return -EBUSY;
1292 }
1293 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001294 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001295 data->pwm_duty[nr]);
1296 } else {
1297 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001298 /*
1299 * If we are in manual mode, write the duty cycle immediately;
1300 * otherwise, just store it for later use.
1301 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001302 if (!(data->pwm_ctrl[nr] & 0x80)) {
1303 data->pwm_ctrl[nr] = data->pwm_duty[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001304 it87_write_value(data, IT87_REG_PWM[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001305 data->pwm_ctrl[nr]);
1306 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001307 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001308 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return count;
1310}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001311static ssize_t set_pwm_freq(struct device *dev,
1312 struct device_attribute *attr, const char *buf, size_t count)
1313{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001314 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001315 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001316 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001317 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001318 int i;
1319
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001320 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001321 return -EINVAL;
1322
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001323 val = clamp_val(val, 0, 1000000);
1324 val *= has_newer_autopwm(data) ? 256 : 128;
1325
Jean Delvaref8d0c192007-02-14 21:15:02 +01001326 /* Search for the nearest available frequency */
1327 for (i = 0; i < 7; i++) {
1328 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1329 break;
1330 }
1331
1332 mutex_lock(&data->update_lock);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001333 if (nr == 0) {
1334 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1335 data->fan_ctl |= i << 4;
1336 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1337 } else {
1338 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1339 data->extra |= i << 4;
1340 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1341 }
Jean Delvaref8d0c192007-02-14 21:15:02 +01001342 mutex_unlock(&data->update_lock);
1343
1344 return count;
1345}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001346static ssize_t show_pwm_temp_map(struct device *dev,
1347 struct device_attribute *attr, char *buf)
1348{
1349 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1350 int nr = sensor_attr->index;
1351
1352 struct it87_data *data = it87_update_device(dev);
1353 int map;
1354
1355 if (data->pwm_temp_map[nr] < 3)
1356 map = 1 << data->pwm_temp_map[nr];
1357 else
1358 map = 0; /* Should never happen */
1359 return sprintf(buf, "%d\n", map);
1360}
1361static ssize_t set_pwm_temp_map(struct device *dev,
1362 struct device_attribute *attr, const char *buf, size_t count)
1363{
1364 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1365 int nr = sensor_attr->index;
1366
1367 struct it87_data *data = dev_get_drvdata(dev);
1368 long val;
1369 u8 reg;
1370
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001371 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001372 return -EINVAL;
1373
1374 switch (val) {
1375 case (1 << 0):
1376 reg = 0x00;
1377 break;
1378 case (1 << 1):
1379 reg = 0x01;
1380 break;
1381 case (1 << 2):
1382 reg = 0x02;
1383 break;
1384 default:
1385 return -EINVAL;
1386 }
1387
1388 mutex_lock(&data->update_lock);
1389 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001390 /*
1391 * If we are in automatic mode, write the temp mapping immediately;
1392 * otherwise, just store it for later use.
1393 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001394 if (data->pwm_ctrl[nr] & 0x80) {
1395 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001396 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001397 }
1398 mutex_unlock(&data->update_lock);
1399 return count;
1400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001402static ssize_t show_auto_pwm(struct device *dev,
1403 struct device_attribute *attr, char *buf)
1404{
1405 struct it87_data *data = it87_update_device(dev);
1406 struct sensor_device_attribute_2 *sensor_attr =
1407 to_sensor_dev_attr_2(attr);
1408 int nr = sensor_attr->nr;
1409 int point = sensor_attr->index;
1410
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001411 return sprintf(buf, "%d\n",
1412 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001413}
1414
1415static ssize_t set_auto_pwm(struct device *dev,
1416 struct device_attribute *attr, const char *buf, size_t count)
1417{
1418 struct it87_data *data = dev_get_drvdata(dev);
1419 struct sensor_device_attribute_2 *sensor_attr =
1420 to_sensor_dev_attr_2(attr);
1421 int nr = sensor_attr->nr;
1422 int point = sensor_attr->index;
1423 long val;
1424
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001425 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001426 return -EINVAL;
1427
1428 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001429 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001430 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1431 data->auto_pwm[nr][point]);
1432 mutex_unlock(&data->update_lock);
1433 return count;
1434}
1435
1436static ssize_t show_auto_temp(struct device *dev,
1437 struct device_attribute *attr, char *buf)
1438{
1439 struct it87_data *data = it87_update_device(dev);
1440 struct sensor_device_attribute_2 *sensor_attr =
1441 to_sensor_dev_attr_2(attr);
1442 int nr = sensor_attr->nr;
1443 int point = sensor_attr->index;
1444
1445 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1446}
1447
1448static ssize_t set_auto_temp(struct device *dev,
1449 struct device_attribute *attr, const char *buf, size_t count)
1450{
1451 struct it87_data *data = dev_get_drvdata(dev);
1452 struct sensor_device_attribute_2 *sensor_attr =
1453 to_sensor_dev_attr_2(attr);
1454 int nr = sensor_attr->nr;
1455 int point = sensor_attr->index;
1456 long val;
1457
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001458 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001459 return -EINVAL;
1460
1461 mutex_lock(&data->update_lock);
1462 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1463 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1464 data->auto_temp[nr][point]);
1465 mutex_unlock(&data->update_lock);
1466 return count;
1467}
1468
Guenter Roecke1169ba2012-12-19 22:17:01 +01001469static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1470static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1471 0, 1);
1472static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1473 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Guenter Roecke1169ba2012-12-19 22:17:01 +01001475static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1476static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1477 1, 1);
1478static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1479 set_fan_div, 1);
1480
1481static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1482static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1483 2, 1);
1484static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1485 set_fan_div, 2);
1486
1487static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1488static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1489 3, 1);
1490
1491static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1492static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1493 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001495static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1496static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1497 5, 1);
1498
Guenter Roeckc4458db2012-12-19 22:17:02 +01001499static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1500 show_pwm_enable, set_pwm_enable, 0);
1501static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001502static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1503 set_pwm_freq, 0);
Guenter Roeck5c391262013-03-30 15:02:12 -07001504static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001505 show_pwm_temp_map, set_pwm_temp_map, 0);
1506static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1507 show_auto_pwm, set_auto_pwm, 0, 0);
1508static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1509 show_auto_pwm, set_auto_pwm, 0, 1);
1510static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1511 show_auto_pwm, set_auto_pwm, 0, 2);
1512static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1513 show_auto_pwm, NULL, 0, 3);
1514static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1515 show_auto_temp, set_auto_temp, 0, 1);
1516static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1517 show_auto_temp, set_auto_temp, 0, 0);
1518static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1519 show_auto_temp, set_auto_temp, 0, 2);
1520static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1521 show_auto_temp, set_auto_temp, 0, 3);
1522static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1523 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Guenter Roeckc4458db2012-12-19 22:17:02 +01001525static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1526 show_pwm_enable, set_pwm_enable, 1);
1527static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001528static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
Guenter Roeck5c391262013-03-30 15:02:12 -07001529static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001530 show_pwm_temp_map, set_pwm_temp_map, 1);
1531static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1532 show_auto_pwm, set_auto_pwm, 1, 0);
1533static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1534 show_auto_pwm, set_auto_pwm, 1, 1);
1535static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1536 show_auto_pwm, set_auto_pwm, 1, 2);
1537static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1538 show_auto_pwm, NULL, 1, 3);
1539static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1540 show_auto_temp, set_auto_temp, 1, 1);
1541static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1542 show_auto_temp, set_auto_temp, 1, 0);
1543static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1544 show_auto_temp, set_auto_temp, 1, 2);
1545static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1546 show_auto_temp, set_auto_temp, 1, 3);
1547static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1548 show_auto_temp, set_auto_temp, 1, 4);
1549
1550static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1551 show_pwm_enable, set_pwm_enable, 2);
1552static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001553static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
Guenter Roeck5c391262013-03-30 15:02:12 -07001554static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001555 show_pwm_temp_map, set_pwm_temp_map, 2);
1556static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1557 show_auto_pwm, set_auto_pwm, 2, 0);
1558static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1559 show_auto_pwm, set_auto_pwm, 2, 1);
1560static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1561 show_auto_pwm, set_auto_pwm, 2, 2);
1562static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1563 show_auto_pwm, NULL, 2, 3);
1564static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1565 show_auto_temp, set_auto_temp, 2, 1);
1566static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1567 show_auto_temp, set_auto_temp, 2, 0);
1568static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1569 show_auto_temp, set_auto_temp, 2, 2);
1570static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1571 show_auto_temp, set_auto_temp, 2, 3);
1572static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1573 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Guenter Roeck36c4d982015-03-26 18:18:19 -07001575static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1576 show_pwm_enable, set_pwm_enable, 3);
1577static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001578static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
Guenter Roeck5c391262013-03-30 15:02:12 -07001579static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001580 show_pwm_temp_map, set_pwm_temp_map, 3);
1581
1582static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1583 show_pwm_enable, set_pwm_enable, 4);
1584static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001585static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
Guenter Roeck5c391262013-03-30 15:02:12 -07001586static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001587 show_pwm_temp_map, set_pwm_temp_map, 4);
1588
1589static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1590 show_pwm_enable, set_pwm_enable, 5);
1591static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001592static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
Guenter Roeck5c391262013-03-30 15:02:12 -07001593static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001594 show_pwm_temp_map, set_pwm_temp_map, 5);
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001597static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1598 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001601 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
Jean Delvare1d66c642005-04-18 21:16:59 -07001603static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Jean Delvare0124dd72007-11-25 16:16:41 +01001605static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1606 char *buf)
1607{
1608 int bitnr = to_sensor_dev_attr(attr)->index;
1609 struct it87_data *data = it87_update_device(dev);
1610 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1611}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001612
1613static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1614 *attr, const char *buf, size_t count)
1615{
1616 struct it87_data *data = dev_get_drvdata(dev);
1617 long val;
1618 int config;
1619
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001620 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001621 return -EINVAL;
1622
1623 mutex_lock(&data->update_lock);
1624 config = it87_read_value(data, IT87_REG_CONFIG);
1625 if (config < 0) {
1626 count = config;
1627 } else {
1628 config |= 1 << 5;
1629 it87_write_value(data, IT87_REG_CONFIG, config);
1630 /* Invalidate cache to force re-read */
1631 data->valid = 0;
1632 }
1633 mutex_unlock(&data->update_lock);
1634
1635 return count;
1636}
1637
Jean Delvare0124dd72007-11-25 16:16:41 +01001638static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1639static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1640static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1641static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1642static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1643static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1644static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1645static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1646static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1647static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1648static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1649static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1650static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001651static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001652static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1653static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1654static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001655static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1656 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001657
Jean Delvared9b327c2010-03-05 22:17:17 +01001658static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1659 char *buf)
1660{
1661 int bitnr = to_sensor_dev_attr(attr)->index;
1662 struct it87_data *data = it87_update_device(dev);
1663 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1664}
1665static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1666 const char *buf, size_t count)
1667{
1668 int bitnr = to_sensor_dev_attr(attr)->index;
1669 struct it87_data *data = dev_get_drvdata(dev);
1670 long val;
1671
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001672 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001673 || (val != 0 && val != 1))
1674 return -EINVAL;
1675
1676 mutex_lock(&data->update_lock);
1677 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1678 if (val)
1679 data->beeps |= (1 << bitnr);
1680 else
1681 data->beeps &= ~(1 << bitnr);
1682 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1683 mutex_unlock(&data->update_lock);
1684 return count;
1685}
1686
1687static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1688 show_beep, set_beep, 1);
1689static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1690static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1691static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1692static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1693static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1694static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1695static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1696/* fanX_beep writability is set later */
1697static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1698static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1699static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1700static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1701static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001702static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001703static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1704 show_beep, set_beep, 2);
1705static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1706static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1707
Jean Delvare5f2dc792010-03-05 22:17:18 +01001708static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1709 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Jean Delvare90d66192007-10-08 18:24:35 +02001711 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001712 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001714static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1715 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001717 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001718 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001720 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001721 return -EINVAL;
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 data->vrm = val;
1724
1725 return count;
1726}
1727static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Jean Delvare5f2dc792010-03-05 22:17:18 +01001729static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1730 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
1732 struct it87_data *data = it87_update_device(dev);
1733 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1734}
1735static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001736
Jean Delvare738e5e02010-08-14 21:08:50 +02001737static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1738 char *buf)
1739{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001740 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001741 "+5V",
1742 "5VSB",
1743 "Vbat",
1744 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001745 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001746 "+3.3V",
1747 "3VSB",
1748 "Vbat",
1749 };
1750 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001751 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001752 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001753
Justin Maggardead80802015-08-05 12:53:08 -07001754 if (has_12mv_adc(data) || has_10_9mv_adc(data))
1755 label = labels_it8721[nr];
1756 else
1757 label = labels[nr];
1758
1759 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001760}
1761static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1762static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1763static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001764/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001765static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001766
Guenter Roeck52929712013-03-30 14:00:08 -07001767static umode_t it87_in_is_visible(struct kobject *kobj,
1768 struct attribute *attr, int index)
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001769{
Guenter Roeck52929712013-03-30 14:00:08 -07001770 struct device *dev = container_of(kobj, struct device, kobj);
1771 struct it87_data *data = dev_get_drvdata(dev);
1772 int i = index / 5; /* voltage index */
1773 int a = index % 5; /* attribute index */
1774
Guenter Roeckf838aa22015-04-01 20:09:36 -07001775 if (index >= 40) { /* in8 and higher only have input attributes */
Guenter Roeck52929712013-03-30 14:00:08 -07001776 i = index - 40 + 8;
1777 a = 0;
1778 }
1779
1780 if (!(data->has_in & (1 << i)))
1781 return 0;
1782
1783 if (a == 4 && !data->has_beep)
1784 return 0;
1785
1786 return attr->mode;
1787}
1788
1789static struct attribute *it87_attributes_in[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001790 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001791 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001792 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001793 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001794 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
1795
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001796 &sensor_dev_attr_in1_input.dev_attr.attr,
1797 &sensor_dev_attr_in1_min.dev_attr.attr,
1798 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001799 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001800 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
1801
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001802 &sensor_dev_attr_in2_input.dev_attr.attr,
1803 &sensor_dev_attr_in2_min.dev_attr.attr,
1804 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001805 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001806 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
1807
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001808 &sensor_dev_attr_in3_input.dev_attr.attr,
1809 &sensor_dev_attr_in3_min.dev_attr.attr,
1810 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001811 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001812 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
1813
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001814 &sensor_dev_attr_in4_input.dev_attr.attr,
1815 &sensor_dev_attr_in4_min.dev_attr.attr,
1816 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001817 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001818 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
1819
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001820 &sensor_dev_attr_in5_input.dev_attr.attr,
1821 &sensor_dev_attr_in5_min.dev_attr.attr,
1822 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001823 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001824 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
1825
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001826 &sensor_dev_attr_in6_input.dev_attr.attr,
1827 &sensor_dev_attr_in6_min.dev_attr.attr,
1828 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001829 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001830 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
1831
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001832 &sensor_dev_attr_in7_input.dev_attr.attr,
1833 &sensor_dev_attr_in7_min.dev_attr.attr,
1834 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001835 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001836 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
Jean Delvare87808be2006-09-24 21:17:13 +02001837
Guenter Roeck52929712013-03-30 14:00:08 -07001838 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
Guenter Roeck52929712013-03-30 14:00:08 -07001839 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
Guenter Roeckf838aa22015-04-01 20:09:36 -07001840 &sensor_dev_attr_in10_input.dev_attr.attr, /* 41 */
1841 &sensor_dev_attr_in11_input.dev_attr.attr, /* 41 */
1842 &sensor_dev_attr_in12_input.dev_attr.attr, /* 41 */
Guenter Roeck52929712013-03-30 14:00:08 -07001843};
1844
1845static const struct attribute_group it87_group_in = {
1846 .attrs = it87_attributes_in,
1847 .is_visible = it87_in_is_visible,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001848};
1849
Guenter Roeck87533772013-03-30 14:23:20 -07001850static umode_t it87_temp_is_visible(struct kobject *kobj,
1851 struct attribute *attr, int index)
Guenter Roeck4573acb2012-03-26 16:17:41 -07001852{
Guenter Roeck87533772013-03-30 14:23:20 -07001853 struct device *dev = container_of(kobj, struct device, kobj);
1854 struct it87_data *data = dev_get_drvdata(dev);
1855 int i = index / 7; /* temperature index */
1856 int a = index % 7; /* attribute index */
1857
Guenter Roeckcc18da72015-04-01 10:03:01 -07001858 if (index >= 21) {
1859 i = index - 21 + 3;
1860 a = 0;
1861 }
1862
Guenter Roeck87533772013-03-30 14:23:20 -07001863 if (!(data->has_temp & (1 << i)))
1864 return 0;
1865
1866 if (a == 5 && !has_temp_offset(data))
1867 return 0;
1868
1869 if (a == 6 && !data->has_beep)
1870 return 0;
1871
1872 return attr->mode;
1873}
1874
1875static struct attribute *it87_attributes_temp[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001876 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001877 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001878 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001879 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001880 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001881 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
1882 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
1883
Guenter Roeckcc18da72015-04-01 10:03:01 -07001884 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001885 &sensor_dev_attr_temp2_max.dev_attr.attr,
1886 &sensor_dev_attr_temp2_min.dev_attr.attr,
1887 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001888 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001889 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1890 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1891
Guenter Roeckcc18da72015-04-01 10:03:01 -07001892 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001893 &sensor_dev_attr_temp3_max.dev_attr.attr,
1894 &sensor_dev_attr_temp3_min.dev_attr.attr,
1895 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001896 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001897 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1898 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001899
Guenter Roeckcc18da72015-04-01 10:03:01 -07001900 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
1901 &sensor_dev_attr_temp5_input.dev_attr.attr,
1902 &sensor_dev_attr_temp6_input.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001903 NULL
Guenter Roeck4573acb2012-03-26 16:17:41 -07001904};
1905
Guenter Roeck87533772013-03-30 14:23:20 -07001906static const struct attribute_group it87_group_temp = {
1907 .attrs = it87_attributes_temp,
1908 .is_visible = it87_temp_is_visible,
Guenter Roeck161d8982012-12-19 22:17:01 +01001909};
1910
Guenter Roeckd3766842013-03-30 15:47:21 -07001911static umode_t it87_is_visible(struct kobject *kobj,
1912 struct attribute *attr, int index)
1913{
1914 struct device *dev = container_of(kobj, struct device, kobj);
1915 struct it87_data *data = dev_get_drvdata(dev);
1916
Guenter Roeck8638d0a2015-03-30 11:13:49 -07001917 if ((index == 2 || index == 3) && !data->has_vid)
Guenter Roeckd3766842013-03-30 15:47:21 -07001918 return 0;
1919
Guenter Roeck8638d0a2015-03-30 11:13:49 -07001920 if (index > 3 && !(data->in_internal & (1 << (index - 4))))
Guenter Roeckd3766842013-03-30 15:47:21 -07001921 return 0;
1922
1923 return attr->mode;
1924}
1925
Guenter Roeck4573acb2012-03-26 16:17:41 -07001926static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001927 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001928 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
Guenter Roeck8638d0a2015-03-30 11:13:49 -07001929 &dev_attr_vrm.attr, /* 2 */
1930 &dev_attr_cpu0_vid.attr, /* 3 */
1931 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
Guenter Roeckd3766842013-03-30 15:47:21 -07001932 &sensor_dev_attr_in7_label.dev_attr.attr,
1933 &sensor_dev_attr_in8_label.dev_attr.attr,
1934 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001935 NULL
1936};
1937
1938static const struct attribute_group it87_group = {
1939 .attrs = it87_attributes,
Guenter Roeckd3766842013-03-30 15:47:21 -07001940 .is_visible = it87_is_visible,
Jean Delvare87808be2006-09-24 21:17:13 +02001941};
1942
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001943static umode_t it87_fan_is_visible(struct kobject *kobj,
1944 struct attribute *attr, int index)
1945{
1946 struct device *dev = container_of(kobj, struct device, kobj);
1947 struct it87_data *data = dev_get_drvdata(dev);
1948 int i = index / 5; /* fan index */
1949 int a = index % 5; /* attribute index */
1950
1951 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
1952 i = (index - 15) / 4 + 3;
1953 a = (index - 15) % 4;
1954 }
1955
1956 if (!(data->has_fan & (1 << i)))
1957 return 0;
1958
1959 if (a == 3) { /* beep */
1960 if (!data->has_beep)
1961 return 0;
1962 /* first fan beep attribute is writable */
1963 if (i == __ffs(data->has_fan))
1964 return attr->mode | S_IWUSR;
1965 }
1966
1967 if (a == 4 && has_16bit_fans(data)) /* divisor */
1968 return 0;
1969
1970 return attr->mode;
1971}
1972
1973static struct attribute *it87_attributes_fan[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001974 &sensor_dev_attr_fan1_input.dev_attr.attr,
1975 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001976 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001977 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
1978 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
1979
Jean Delvare87808be2006-09-24 21:17:13 +02001980 &sensor_dev_attr_fan2_input.dev_attr.attr,
1981 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001982 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001983 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1984 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
1985
Jean Delvare87808be2006-09-24 21:17:13 +02001986 &sensor_dev_attr_fan3_input.dev_attr.attr,
1987 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001988 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001989 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1990 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
1991
1992 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001993 &sensor_dev_attr_fan4_min.dev_attr.attr,
1994 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001995 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1996
1997 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001998 &sensor_dev_attr_fan5_min.dev_attr.attr,
1999 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002000 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2001
2002 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002003 &sensor_dev_attr_fan6_min.dev_attr.attr,
2004 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002005 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002006 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002007};
2008
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002009static const struct attribute_group it87_group_fan = {
2010 .attrs = it87_attributes_fan,
2011 .is_visible = it87_fan_is_visible,
Guenter Roecke1169ba2012-12-19 22:17:01 +01002012};
Jean Delvare723a0aa2010-03-05 22:17:16 +01002013
Guenter Roeck5c391262013-03-30 15:02:12 -07002014static umode_t it87_pwm_is_visible(struct kobject *kobj,
2015 struct attribute *attr, int index)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002016{
2017 struct device *dev = container_of(kobj, struct device, kobj);
2018 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck5c391262013-03-30 15:02:12 -07002019 int i = index / 4; /* pwm index */
2020 int a = index % 4; /* attribute index */
Guenter Roeck60878bc2015-03-26 19:57:42 -07002021
Guenter Roeck5c391262013-03-30 15:02:12 -07002022 if (!(data->has_pwm & (1 << i)))
2023 return 0;
2024
2025 /* pwmX_auto_channels_temp is only writable for old auto pwm */
2026 if (a == 3 && has_old_autopwm(data))
2027 return attr->mode | S_IWUSR;
2028
2029 /* pwm2_freq is writable if there are two pwm frequency selects */
2030 if (has_pwm_freq2(data) && i == 1 && a == 2)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002031 return attr->mode | S_IWUSR;
2032
2033 return attr->mode;
2034}
2035
Guenter Roeck5c391262013-03-30 15:02:12 -07002036static struct attribute *it87_attributes_pwm[] = {
2037 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2038 &sensor_dev_attr_pwm1.dev_attr.attr,
2039 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2040 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2041
2042 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2043 &sensor_dev_attr_pwm2.dev_attr.attr,
2044 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2045 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2046
2047 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2048 &sensor_dev_attr_pwm3.dev_attr.attr,
2049 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2050 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2051
2052 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2053 &sensor_dev_attr_pwm4.dev_attr.attr,
2054 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2055 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2056
2057 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2058 &sensor_dev_attr_pwm5.dev_attr.attr,
2059 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2060 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2061
2062 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2063 &sensor_dev_attr_pwm6.dev_attr.attr,
2064 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2065 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2066
2067 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002068};
2069
Guenter Roeck5c391262013-03-30 15:02:12 -07002070static const struct attribute_group it87_group_pwm = {
2071 .attrs = it87_attributes_pwm,
2072 .is_visible = it87_pwm_is_visible,
2073};
2074
2075static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2076 struct attribute *attr, int index)
2077{
2078 struct device *dev = container_of(kobj, struct device, kobj);
2079 struct it87_data *data = dev_get_drvdata(dev);
2080 int i = index / 9; /* pwm index */
2081
2082 if (!(data->has_pwm & (1 << i)))
2083 return 0;
2084
2085 return attr->mode;
2086}
2087
2088static struct attribute *it87_attributes_auto_pwm[] = {
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002089 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2090 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2091 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2092 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2093 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2094 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2095 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2096 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2097 &sensor_dev_attr_pwm1_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_pwm2_auto_point1_pwm.dev_attr.attr,
2100 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2101 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2102 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2103 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2104 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2105 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2106 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2107 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002108
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002109 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
2110 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2111 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2112 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2113 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2114 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2115 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2116 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2117 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002118
Guenter Roeck5c391262013-03-30 15:02:12 -07002119 NULL,
2120};
2121
2122static const struct attribute_group it87_group_auto_pwm = {
2123 .attrs = it87_attributes_auto_pwm,
2124 .is_visible = it87_auto_pwm_is_visible,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002125};
2126
Jean Delvare2d8672c2005-07-19 23:56:35 +02002127/* SuperIO detection - will change isa_address if a chip is found */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002128static int __init it87_find(int sioaddr, unsigned short *address,
2129 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002131 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002132 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002133 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002134 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002136 err = superio_enter(sioaddr);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002137 if (err)
2138 return err;
2139
2140 err = -ENODEV;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002141 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002142
2143 switch (chip_type) {
2144 case IT8705F_DEVID:
2145 sio_data->type = it87;
2146 break;
2147 case IT8712F_DEVID:
2148 sio_data->type = it8712;
2149 break;
2150 case IT8716F_DEVID:
2151 case IT8726F_DEVID:
2152 sio_data->type = it8716;
2153 break;
2154 case IT8718F_DEVID:
2155 sio_data->type = it8718;
2156 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002157 case IT8720F_DEVID:
2158 sio_data->type = it8720;
2159 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002160 case IT8721F_DEVID:
2161 sio_data->type = it8721;
2162 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01002163 case IT8728F_DEVID:
2164 sio_data->type = it8728;
2165 break;
Justin Maggardead80802015-08-05 12:53:08 -07002166 case IT8732F_DEVID:
2167 sio_data->type = it8732;
2168 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06002169 case IT8771E_DEVID:
2170 sio_data->type = it8771;
2171 break;
2172 case IT8772E_DEVID:
2173 sio_data->type = it8772;
2174 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002175 case IT8781F_DEVID:
2176 sio_data->type = it8781;
2177 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08002178 case IT8782F_DEVID:
2179 sio_data->type = it8782;
2180 break;
2181 case IT8783E_DEVID:
2182 sio_data->type = it8783;
2183 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002184 case IT8786E_DEVID:
2185 sio_data->type = it8786;
2186 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07002187 case IT8790E_DEVID:
2188 sio_data->type = it8790;
2189 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02002190 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02002191 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002192 sio_data->type = it8603;
2193 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002194 case IT8620E_DEVID:
2195 sio_data->type = it8620;
2196 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002197 case 0xffff: /* No device at all */
2198 goto exit;
2199 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01002200 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002201 goto exit;
2202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002204 superio_select(sioaddr, PME);
2205 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002206 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 goto exit;
2208 }
2209
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002210 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002212 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 goto exit;
2214 }
2215
2216 err = 0;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002217 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07002218 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2219 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002220 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002222 config = &it87_devices[sio_data->type];
2223
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002224 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002225 if (has_in7_internal(config))
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002226 sio_data->internal |= (1 << 1);
2227
Jean Delvare738e5e02010-08-14 21:08:50 +02002228 /* in8 (Vbat) is always internal */
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002229 sio_data->internal |= (1 << 2);
2230
Guenter Roeck73055402015-03-26 09:16:32 -07002231 /* in9 (AVCC3), always internal if supported */
2232 if (has_avcc3(config))
2233 sio_data->internal |= (1 << 3); /* in9 is AVCC */
2234 else
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002235 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02002236
Guenter Roeck36c4d982015-03-26 18:18:19 -07002237 if (!has_six_pwm(config))
2238 sio_data->skip_pwm |= (1 << 3) | (1 << 4) | (1 << 5);
2239
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002240 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01002241 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01002242
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002243 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2244 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01002245 /* The IT8705F has a different LD number for GPIO */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002246 superio_select(sioaddr, 5);
2247 sio_data->beep_pin = superio_inb(sioaddr,
2248 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002249 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002250 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002251
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002252 superio_select(sioaddr, GPIO);
Guenter Roeck0531d982012-03-02 11:46:44 -08002253
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002254 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2255 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2256 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2257 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2258 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002259
Guenter Roeck0531d982012-03-02 11:46:44 -08002260 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002261 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08002262 sio_data->skip_fan |= (1 << 2);
2263 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002264 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08002265 sio_data->skip_pwm |= (1 << 2);
2266
2267 /* Check if fan2 is there or not */
2268 if (reg27 & (1 << 7))
2269 sio_data->skip_fan |= (1 << 1);
2270 if (reg27 & (1 << 3))
2271 sio_data->skip_pwm |= (1 << 1);
2272
2273 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002274 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002275 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002276
2277 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002278 if (reg27 & (1 << 1))
2279 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002280
2281 /*
2282 * VIN7
2283 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2284 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002285 if (reg27 & (1 << 2)) {
2286 /*
2287 * The data sheet is a bit unclear regarding the
2288 * internal voltage divider for VCCH5V. It says
2289 * "This bit enables and switches VIN7 (pin 91) to the
2290 * internal voltage divider for VCCH5V".
2291 * This is different to other chips, where the internal
2292 * voltage divider would connect VIN7 to an internal
2293 * voltage source. Maybe that is the case here as well.
2294 *
2295 * Since we don't know for sure, re-route it if that is
2296 * not the case, and ask the user to report if the
2297 * resulting voltage is sane.
2298 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002299 if (!(reg2c & (1 << 1))) {
2300 reg2c |= (1 << 1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002301 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2302 reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002303 pr_notice("Routing internal VCCH5V to in7.\n");
2304 }
2305 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2306 pr_notice("Please report if it displays a reasonable voltage.\n");
2307 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002308
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002309 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08002310 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002311 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08002312 sio_data->internal |= (1 << 1);
2313
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002314 sio_data->beep_pin = superio_inb(sioaddr,
2315 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002316 } else if (sio_data->type == it8603) {
2317 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002318
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002319 superio_select(sioaddr, GPIO);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002320
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002321 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002322
2323 /* Check if fan3 is there or not */
2324 if (reg27 & (1 << 6))
2325 sio_data->skip_pwm |= (1 << 2);
2326 if (reg27 & (1 << 7))
2327 sio_data->skip_fan |= (1 << 2);
2328
2329 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002330 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002331 if (reg29 & (1 << 1))
2332 sio_data->skip_pwm |= (1 << 1);
2333 if (reg29 & (1 << 2))
2334 sio_data->skip_fan |= (1 << 1);
2335
2336 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2337 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2338
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002339 sio_data->beep_pin = superio_inb(sioaddr,
2340 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002341 } else if (sio_data->type == it8620) {
2342 int reg;
2343
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002344 superio_select(sioaddr, GPIO);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002345
Guenter Roeck36c4d982015-03-26 18:18:19 -07002346 /* Check for pwm5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002347 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002348 if (reg & (1 << 6))
2349 sio_data->skip_pwm |= (1 << 4);
2350
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002351 /* Check for fan4, fan5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002352 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002353 if (!(reg & (1 << 5)))
2354 sio_data->skip_fan |= (1 << 3);
2355 if (!(reg & (1 << 4)))
2356 sio_data->skip_fan |= (1 << 4);
2357
2358 /* Check for pwm3, fan3 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002359 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002360 if (reg & (1 << 6))
2361 sio_data->skip_pwm |= (1 << 2);
2362 if (reg & (1 << 7))
2363 sio_data->skip_fan |= (1 << 2);
2364
Guenter Roeck36c4d982015-03-26 18:18:19 -07002365 /* Check for pwm4 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002366 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002367 if (!(reg & (1 << 2)))
2368 sio_data->skip_pwm |= (1 << 3);
2369
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002370 /* Check for pwm2, fan2 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002371 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002372 if (reg & (1 << 1))
2373 sio_data->skip_pwm |= (1 << 1);
2374 if (reg & (1 << 2))
2375 sio_data->skip_fan |= (1 << 1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002376 /* Check for pwm6, fan6 */
2377 if (!(reg & (1 << 7))) {
2378 sio_data->skip_pwm |= (1 << 5);
2379 sio_data->skip_fan |= (1 << 5);
2380 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002381
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002382 sio_data->beep_pin = superio_inb(sioaddr,
2383 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002384 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002385 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002386 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002387
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002388 superio_select(sioaddr, GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002389
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002390 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002391 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002392 /* We need at least 4 VID pins */
2393 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002394 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002395 sio_data->skip_vid = 1;
2396 }
Jean Delvare895ff262009-12-09 20:35:47 +01002397 }
2398
Jean Delvare591ec652009-12-09 20:35:48 +01002399 /* Check if fan3 is there or not */
2400 if (reg & (1 << 6))
2401 sio_data->skip_pwm |= (1 << 2);
2402 if (reg & (1 << 7))
2403 sio_data->skip_fan |= (1 << 2);
2404
2405 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002406 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Jean Delvare591ec652009-12-09 20:35:48 +01002407 if (reg & (1 << 1))
2408 sio_data->skip_pwm |= (1 << 1);
2409 if (reg & (1 << 2))
2410 sio_data->skip_fan |= (1 << 1);
2411
Jean Delvare895ff262009-12-09 20:35:47 +01002412 if ((sio_data->type == it8718 || sio_data->type == it8720)
2413 && !(sio_data->skip_vid))
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002414 sio_data->vid_value = superio_inb(sioaddr,
2415 IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002416
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002417 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002418
2419 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2420
Jean Delvare436cad22010-07-09 16:22:48 +02002421 /*
2422 * The IT8720F has no VIN7 pin, so VCCH should always be
2423 * routed internally to VIN7 with an internal divider.
2424 * Curiously, there still is a configuration bit to control
2425 * this, which means it can be set incorrectly. And even
2426 * more curiously, many boards out there are improperly
2427 * configured, even though the IT8720F datasheet claims
2428 * that the internal routing of VCCH to VIN7 is the default
2429 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002430 *
2431 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002432 * If UART6 is enabled, re-route VIN7 to the internal divider
2433 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002434 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002435 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02002436 reg |= (1 << 1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002437 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002438 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002439 }
Jean Delvare87673dd2006-08-28 14:37:19 +02002440 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02002441 sio_data->internal |= (1 << 0);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002442 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02002443 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002444
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002445 /*
2446 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2447 * While VIN7 can be routed to the internal voltage divider,
2448 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002449 *
2450 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2451 * is the temperature source. Since we can not read the
2452 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002453 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002454 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002455 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002456 sio_data->skip_temp |= (1 << 2);
2457 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002458
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002459 sio_data->beep_pin = superio_inb(sioaddr,
2460 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002461 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002462 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002463 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002464
Jean Delvare98dd22c2008-10-09 15:33:58 +02002465 /* Disable specific features based on DMI strings */
2466 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2467 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2468 if (board_vendor && board_name) {
2469 if (strcmp(board_vendor, "nVIDIA") == 0
2470 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002471 /*
2472 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2473 * connected to a fan, but to something else. One user
2474 * has reported instant system power-off when changing
2475 * the PWM2 duty cycle, so we disable it.
2476 * I use the board name string as the trigger in case
2477 * the same board is ever used in other systems.
2478 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002479 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002480 sio_data->skip_pwm = (1 << 1);
2481 }
2482 }
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484exit:
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002485 superio_exit(sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 return err;
2487}
2488
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002489/* Called when we have found a new IT87. */
2490static void it87_init_device(struct platform_device *pdev)
2491{
2492 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2493 struct it87_data *data = platform_get_drvdata(pdev);
2494 int tmp, i;
2495 u8 mask;
2496
2497 /*
2498 * For each PWM channel:
2499 * - If it is in automatic mode, setting to manual mode should set
2500 * the fan to full speed by default.
2501 * - If it is in manual mode, we need a mapping to temperature
2502 * channels to use when later setting to automatic mode later.
2503 * Use a 1:1 mapping by default (we are clueless.)
2504 * In both cases, the value can (and should) be changed by the user
2505 * prior to switching to a different mode.
2506 * Note that this is no longer needed for the IT8721F and later, as
2507 * these have separate registers for the temperature mapping and the
2508 * manual duty cycle.
2509 */
2510 for (i = 0; i < 3; i++) {
2511 data->pwm_temp_map[i] = i;
2512 data->pwm_duty[i] = 0x7f; /* Full speed */
2513 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2514 }
2515
2516 /*
2517 * Some chips seem to have default value 0xff for all limit
2518 * registers. For low voltage limits it makes no sense and triggers
2519 * alarms, so change to 0 instead. For high temperature limits, it
2520 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2521 * but is still confusing, so change to 127 degrees C.
2522 */
2523 for (i = 0; i < 8; i++) {
2524 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2525 if (tmp == 0xff)
2526 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2527 }
2528 for (i = 0; i < 3; i++) {
2529 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2530 if (tmp == 0xff)
2531 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2532 }
2533
2534 /*
2535 * Temperature channels are not forcibly enabled, as they can be
2536 * set to two different sensor types and we can't guess which one
2537 * is correct for a given system. These channels can be enabled at
2538 * run-time through the temp{1-3}_type sysfs accessors if needed.
2539 */
2540
2541 /* Check if voltage monitors are reset manually or by some reason */
2542 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2543 if ((tmp & 0xff) == 0) {
2544 /* Enable all voltage monitors */
2545 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2546 }
2547
2548 /* Check if tachometers are reset manually or by some reason */
2549 mask = 0x70 & ~(sio_data->skip_fan << 4);
2550 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2551 if ((data->fan_main_ctrl & mask) == 0) {
2552 /* Enable all fan tachometers */
2553 data->fan_main_ctrl |= mask;
2554 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2555 data->fan_main_ctrl);
2556 }
2557 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2558
2559 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2560
2561 /* Set tachometers to 16-bit mode if needed */
2562 if (has_fan16_config(data)) {
2563 if (~tmp & 0x07 & data->has_fan) {
2564 dev_dbg(&pdev->dev,
2565 "Setting fan1-3 to 16-bit mode\n");
2566 it87_write_value(data, IT87_REG_FAN_16BIT,
2567 tmp | 0x07);
2568 }
2569 }
2570
2571 /* Check for additional fans */
2572 if (has_five_fans(data)) {
2573 if (tmp & (1 << 4))
2574 data->has_fan |= (1 << 3); /* fan4 enabled */
2575 if (tmp & (1 << 5))
2576 data->has_fan |= (1 << 4); /* fan5 enabled */
2577 if (has_six_fans(data) && (tmp & (1 << 2)))
2578 data->has_fan |= (1 << 5); /* fan6 enabled */
2579 }
2580
2581 /* Fan input pins may be used for alternative functions */
2582 data->has_fan &= ~sio_data->skip_fan;
2583
2584 /* Check if pwm5, pwm6 are enabled */
2585 if (has_six_pwm(data)) {
2586 /* The following code may be IT8620E specific */
2587 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2588 if ((tmp & 0xc0) == 0xc0)
2589 sio_data->skip_pwm |= (1 << 4);
2590 if (!(tmp & (1 << 3)))
2591 sio_data->skip_pwm |= (1 << 5);
2592 }
2593
2594 /* Start monitoring */
2595 it87_write_value(data, IT87_REG_CONFIG,
2596 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2597 | (update_vbat ? 0x41 : 0x01));
2598}
2599
2600/* Return 1 if and only if the PWM interface is safe to use */
2601static int it87_check_pwm(struct device *dev)
2602{
2603 struct it87_data *data = dev_get_drvdata(dev);
2604 /*
2605 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2606 * and polarity set to active low is sign that this is the case so we
2607 * disable pwm control to protect the user.
2608 */
2609 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2610
2611 if ((tmp & 0x87) == 0) {
2612 if (fix_pwm_polarity) {
2613 /*
2614 * The user asks us to attempt a chip reconfiguration.
2615 * This means switching to active high polarity and
2616 * inverting all fan speed values.
2617 */
2618 int i;
2619 u8 pwm[3];
2620
2621 for (i = 0; i < 3; i++)
2622 pwm[i] = it87_read_value(data,
2623 IT87_REG_PWM[i]);
2624
2625 /*
2626 * If any fan is in automatic pwm mode, the polarity
2627 * might be correct, as suspicious as it seems, so we
2628 * better don't change anything (but still disable the
2629 * PWM interface).
2630 */
2631 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2632 dev_info(dev,
2633 "Reconfiguring PWM to active high polarity\n");
2634 it87_write_value(data, IT87_REG_FAN_CTL,
2635 tmp | 0x87);
2636 for (i = 0; i < 3; i++)
2637 it87_write_value(data,
2638 IT87_REG_PWM[i],
2639 0x7f & ~pwm[i]);
2640 return 1;
2641 }
2642
2643 dev_info(dev,
2644 "PWM configuration is too broken to be fixed\n");
2645 }
2646
2647 dev_info(dev,
2648 "Detected broken BIOS defaults, disabling PWM interface\n");
2649 return 0;
2650 } else if (fix_pwm_polarity) {
2651 dev_info(dev,
2652 "PWM configuration looks sane, won't touch\n");
2653 }
2654
2655 return 1;
2656}
2657
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002658static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002661 struct resource *res;
2662 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002663 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 int enable_pwm_interface;
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002665 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002667 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002668 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2669 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002670 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2671 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002672 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002673 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Guenter Roeck62a1d052012-03-24 21:54:41 -07002676 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2677 if (!data)
2678 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002680 data->addr = res->start;
2681 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002682 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002683 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002684 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002685 /*
2686 * IT8705F Datasheet 0.4.1, 3h == Version G.
2687 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2688 * These are the first revisions with 16-bit tachometer support.
2689 */
2690 switch (data->type) {
2691 case it87:
2692 if (sio_data->revision >= 0x03) {
2693 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002694 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002695 }
2696 break;
2697 case it8712:
2698 if (sio_data->revision >= 0x08) {
2699 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002700 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2701 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002702 }
2703 break;
2704 default:
2705 break;
2706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002709 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002710 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2711 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002713 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002715 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002718 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002720 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002721 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002722 if (sio_data->internal & (1 << 0))
2723 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2724 if (sio_data->internal & (1 << 1))
2725 data->in_scaled |= (1 << 7); /* in7 is VSB */
2726 if (sio_data->internal & (1 << 2))
2727 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002728 if (sio_data->internal & (1 << 3))
2729 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002730 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2731 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002732 if (sio_data->internal & (1 << 0))
2733 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2734 if (sio_data->internal & (1 << 1))
2735 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002736 }
2737
Guenter Roeck4573acb2012-03-26 16:17:41 -07002738 data->has_temp = 0x07;
2739 if (sio_data->skip_temp & (1 << 2)) {
2740 if (sio_data->type == it8782
2741 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2742 data->has_temp &= ~(1 << 2);
2743 }
2744
Guenter Roeckd3766842013-03-30 15:47:21 -07002745 data->in_internal = sio_data->internal;
Guenter Roeck52929712013-03-30 14:00:08 -07002746 data->has_in = 0x3ff & ~sio_data->skip_in;
2747
Guenter Roeckcc18da72015-04-01 10:03:01 -07002748 if (has_six_temp(data)) {
2749 u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
2750
Guenter Roeckf838aa22015-04-01 20:09:36 -07002751 /* Check for additional temperature sensors */
Guenter Roeckcc18da72015-04-01 10:03:01 -07002752 if ((reg & 0x03) >= 0x02)
2753 data->has_temp |= (1 << 3);
2754 if (((reg >> 2) & 0x03) >= 0x02)
2755 data->has_temp |= (1 << 4);
2756 if (((reg >> 4) & 0x03) >= 0x02)
2757 data->has_temp |= (1 << 5);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002758
2759 /* Check for additional voltage sensors */
2760 if ((reg & 0x03) == 0x01)
2761 data->has_in |= (1 << 10);
2762 if (((reg >> 2) & 0x03) == 0x01)
2763 data->has_in |= (1 << 11);
2764 if (((reg >> 4) & 0x03) == 0x01)
2765 data->has_in |= (1 << 12);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002766 }
2767
Guenter Roeck52929712013-03-30 14:00:08 -07002768 data->has_beep = !!sio_data->beep_pin;
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002771 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Guenter Roeckd3766842013-03-30 15:47:21 -07002773 if (!sio_data->skip_vid) {
2774 data->has_vid = true;
2775 data->vrm = vid_which_vrm();
2776 /* VID reading from Super-I/O config space if available */
2777 data->vid = sio_data->vid_value;
2778 }
2779
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002780 /* Prepare for sysfs hooks */
2781 data->groups[0] = &it87_group;
2782 data->groups[1] = &it87_group_in;
2783 data->groups[2] = &it87_group_temp;
2784 data->groups[3] = &it87_group_fan;
Jean Delvare17d648b2006-08-28 14:23:46 +02002785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (enable_pwm_interface) {
Guenter Roeck5c391262013-03-30 15:02:12 -07002787 data->has_pwm = (1 << ARRAY_SIZE(IT87_REG_PWM)) - 1;
2788 data->has_pwm &= ~sio_data->skip_pwm;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002789
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002790 data->groups[4] = &it87_group_pwm;
2791 if (has_old_autopwm(data))
2792 data->groups[5] = &it87_group_auto_pwm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
2794
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002795 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
2796 it87_devices[sio_data->type].name,
2797 data, data->groups);
2798 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799}
2800
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002801static struct platform_driver it87_driver = {
2802 .driver = {
2803 .name = DRVNAME,
2804 },
2805 .probe = it87_probe,
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002806};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Guenter Roecke84bd952015-03-28 08:24:29 -07002808static int __init it87_device_add(int index, unsigned short address,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002809 const struct it87_sio_data *sio_data)
2810{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002811 struct platform_device *pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002812 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002813 .start = address + IT87_EC_OFFSET,
2814 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002815 .name = DRVNAME,
2816 .flags = IORESOURCE_IO,
2817 };
2818 int err;
2819
Jean Delvareb9acb642009-01-07 16:37:35 +01002820 err = acpi_check_resource_conflict(&res);
2821 if (err)
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002822 return err;
Jean Delvareb9acb642009-01-07 16:37:35 +01002823
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002824 pdev = platform_device_alloc(DRVNAME, address);
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002825 if (!pdev)
2826 return -ENOMEM;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002827
2828 err = platform_device_add_resources(pdev, &res, 1);
2829 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002830 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002831 goto exit_device_put;
2832 }
2833
2834 err = platform_device_add_data(pdev, sio_data,
2835 sizeof(struct it87_sio_data));
2836 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002837 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002838 goto exit_device_put;
2839 }
2840
2841 err = platform_device_add(pdev);
2842 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002843 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002844 goto exit_device_put;
2845 }
2846
Guenter Roecke84bd952015-03-28 08:24:29 -07002847 it87_pdev[index] = pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002848 return 0;
2849
2850exit_device_put:
2851 platform_device_put(pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002852 return err;
2853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855static int __init sm_it87_init(void)
2856{
Guenter Roecke84bd952015-03-28 08:24:29 -07002857 int sioaddr[2] = { REG_2E, REG_4E };
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002858 struct it87_sio_data sio_data;
Guenter Roecke84bd952015-03-28 08:24:29 -07002859 unsigned short isa_address;
2860 bool found = false;
2861 int i, err;
Jean Delvarefde09502005-07-19 23:51:07 +02002862
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002863 err = platform_driver_register(&it87_driver);
2864 if (err)
2865 return err;
2866
Guenter Roecke84bd952015-03-28 08:24:29 -07002867 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
2868 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2869 isa_address = 0;
2870 err = it87_find(sioaddr[i], &isa_address, &sio_data);
2871 if (err || isa_address == 0)
2872 continue;
2873
2874 err = it87_device_add(i, isa_address, &sio_data);
2875 if (err)
2876 goto exit_dev_unregister;
2877 found = true;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002878 }
2879
Guenter Roecke84bd952015-03-28 08:24:29 -07002880 if (!found) {
2881 err = -ENODEV;
2882 goto exit_unregister;
2883 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002884 return 0;
Guenter Roecke84bd952015-03-28 08:24:29 -07002885
2886exit_dev_unregister:
2887 /* NULL check handled by platform_device_unregister */
2888 platform_device_unregister(it87_pdev[0]);
2889exit_unregister:
2890 platform_driver_unregister(&it87_driver);
2891 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
2893
2894static void __exit sm_it87_exit(void)
2895{
Guenter Roecke84bd952015-03-28 08:24:29 -07002896 /* NULL check handled by platform_device_unregister */
2897 platform_device_unregister(it87_pdev[1]);
2898 platform_device_unregister(it87_pdev[0]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002899 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
2902
Jean Delvare7c81c602014-01-29 20:40:08 +01002903MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002904MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905module_param(update_vbat, bool, 0);
2906MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2907module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002908MODULE_PARM_DESC(fix_pwm_polarity,
2909 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910MODULE_LICENSE("GPL");
2911
2912module_init(sm_it87_init);
2913module_exit(sm_it87_exit);