blob: f877cc98b2fd32b210295bfd888409618547dd54 [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
222/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
223
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700224static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
225static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
226static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
227static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
228static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#define IT87_REG_FAN_MAIN_CTRL 0x13
231#define IT87_REG_FAN_CTL 0x14
Guenter Roeck36c4d982015-03-26 18:18:19 -0700232static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
233static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235#define IT87_REG_VIN(nr) (0x20 + (nr))
236#define IT87_REG_TEMP(nr) (0x29 + (nr))
237
Guenter Roeck73055402015-03-26 09:16:32 -0700238#define IT87_REG_AVCC3 0x2f
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
241#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
242#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
243#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#define IT87_REG_VIN_ENABLE 0x50
246#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700247#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100248#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250#define IT87_REG_CHIPID 0x58
251
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100252#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
253#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
254
Guenter Roeck483db432012-12-19 22:17:02 +0100255struct it87_devices {
256 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700257 const char * const suffix;
Guenter Roeck483db432012-12-19 22:17:02 +0100258 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100259 u8 peci_mask;
260 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100261};
262
263#define FEAT_12MV_ADC (1 << 0)
264#define FEAT_NEWER_AUTOPWM (1 << 1)
265#define FEAT_OLD_AUTOPWM (1 << 2)
266#define FEAT_16BIT_FANS (1 << 3)
267#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100268#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100269#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800270#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
271#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800272#define FEAT_VID (1 << 9) /* Set if chip supports VID */
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700273#define FEAT_IN7_INTERNAL (1 << 10) /* Set if in7 is internal */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700274#define FEAT_SIX_FANS (1 << 11) /* Supports six fans */
Justin Maggardead80802015-08-05 12:53:08 -0700275#define FEAT_10_9MV_ADC (1 << 12)
Guenter Roeck73055402015-03-26 09:16:32 -0700276#define FEAT_AVCC3 (1 << 13) /* Chip supports in9/AVCC3 */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700277#define FEAT_SIX_PWM (1 << 14) /* Chip supports 6 pwm chn */
Guenter Roeck60878bc2015-03-26 19:57:42 -0700278#define FEAT_PWM_FREQ2 (1 << 15) /* Separate pwm freq 2 */
Guenter Roeck483db432012-12-19 22:17:02 +0100279
280static const struct it87_devices it87_devices[] = {
281 [it87] = {
282 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700283 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100284 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
285 },
286 [it8712] = {
287 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700288 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800289 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
290 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100291 },
292 [it8716] = {
293 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700294 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800295 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700296 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
Guenter Roeck483db432012-12-19 22:17:02 +0100297 },
298 [it8718] = {
299 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700300 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800301 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700302 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
303 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100304 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100305 },
306 [it8720] = {
307 .name = "it8720",
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 [it8721] = {
315 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700316 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100317 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800318 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck60878bc2015-03-26 19:57:42 -0700319 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
320 | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100321 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100322 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100323 },
324 [it8728] = {
325 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700326 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100327 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700328 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700329 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100330 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100331 },
Justin Maggardead80802015-08-05 12:53:08 -0700332 [it8732] = {
333 .name = "it8732",
334 .suffix = "F",
335 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
336 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
337 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
338 .peci_mask = 0x07,
339 .old_peci_mask = 0x02, /* Actually reports PCH */
340 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600341 [it8771] = {
342 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700343 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600344 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700345 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
346 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800347 /* PECI: guesswork */
348 /* 12mV ADC (OHM) */
349 /* 16 bit fans (OHM) */
350 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600351 .peci_mask = 0x07,
352 },
353 [it8772] = {
354 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700355 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600356 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700357 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
358 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800359 /* PECI (coreboot) */
360 /* 12mV ADC (HWSensors4, OHM) */
361 /* 16 bit fans (HWSensors4, OHM) */
362 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600363 .peci_mask = 0x07,
364 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800365 [it8781] = {
366 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700367 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800368 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700369 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800370 .old_peci_mask = 0x4,
371 },
Guenter Roeck483db432012-12-19 22:17:02 +0100372 [it8782] = {
373 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700374 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100375 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700376 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100377 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100378 },
379 [it8783] = {
380 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700381 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100382 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700383 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100384 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100385 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100386 [it8786] = {
387 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700388 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100389 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700390 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
391 | FEAT_PWM_FREQ2,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100392 .peci_mask = 0x07,
393 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700394 [it8790] = {
395 .name = "it8790",
396 .suffix = "E",
397 .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,
Guenter Roeck4ee07152015-03-25 23:26:28 -0700400 .peci_mask = 0x07,
401 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100402 [it8603] = {
403 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700404 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100405 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck73055402015-03-26 09:16:32 -0700406 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
Guenter Roeck60878bc2015-03-26 19:57:42 -0700407 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100408 .peci_mask = 0x07,
409 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800410 [it8620] = {
411 .name = "it8620",
412 .suffix = "E",
413 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700414 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700415 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2,
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800416 .peci_mask = 0x07,
417 },
Guenter Roeck483db432012-12-19 22:17:02 +0100418};
419
420#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
421#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
Justin Maggardead80802015-08-05 12:53:08 -0700422#define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
Guenter Roeck483db432012-12-19 22:17:02 +0100423#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
424#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
425#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100426#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
427 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100428#define has_temp_old_peci(data, nr) \
429 (((data)->features & FEAT_TEMP_OLD_PECI) && \
430 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800431#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700432#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
433 FEAT_SIX_FANS))
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800434#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700435#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700436#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Guenter Roeck73055402015-03-26 09:16:32 -0700437#define has_avcc3(data) ((data)->features & FEAT_AVCC3)
Guenter Roeck36c4d982015-03-26 18:18:19 -0700438#define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
Guenter Roeck60878bc2015-03-26 19:57:42 -0700439#define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200441struct it87_sio_data {
442 enum chips type;
443 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200444 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200445 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100446 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200447 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100448 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700449 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100450 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100451 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200452 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700453 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200454};
455
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800456/*
457 * For each registered chip, we need to keep some data in memory.
458 * The structure is dynamically allocated.
459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700461 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100463 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100464 u8 peci_mask;
465 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200467 unsigned short addr;
468 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100469 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 char valid; /* !=0 if following fields are valid */
471 unsigned long last_updated; /* In jiffies */
472
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200473 u16 in_scaled; /* Internal voltage sensors are scaled */
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100474 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200475 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700476 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700477 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck161d8982012-12-19 22:17:01 +0100478 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100479 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
480 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 u8 fan_div[3]; /* Register encoding, shifted right */
482 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100483 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100485 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100487 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100488
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800489 /*
490 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100491 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
492 * 7, and we want to preserve settings on mode changes, so we have
493 * to track all values separately.
494 * Starting with the IT8721F, the manual PWM duty cycles are stored
495 * in separate registers (8-bit values), so the separate tracking
496 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800497 * simple.
498 */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700499 u8 pwm_ctrl[6]; /* Register value */
500 u8 pwm_duty[6]; /* Manual PWM value set by user */
501 u8 pwm_temp_map[6]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100502
503 /* Automatic fan speed control registers */
504 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
505 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506};
507
Guenter Roeck0531d982012-03-02 11:46:44 -0800508static int adc_lsb(const struct it87_data *data, int nr)
509{
Justin Maggardead80802015-08-05 12:53:08 -0700510 int lsb;
511
512 if (has_12mv_adc(data))
513 lsb = 120;
514 else if (has_10_9mv_adc(data))
515 lsb = 109;
516 else
517 lsb = 160;
Guenter Roeck0531d982012-03-02 11:46:44 -0800518 if (data->in_scaled & (1 << nr))
519 lsb <<= 1;
520 return lsb;
521}
522
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200523static u8 in_to_reg(const struct it87_data *data, int nr, long val)
524{
Justin Maggardead80802015-08-05 12:53:08 -0700525 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800526 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200527}
528
529static int in_from_reg(const struct it87_data *data, int nr, int val)
530{
Justin Maggardead80802015-08-05 12:53:08 -0700531 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200532}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200533
534static inline u8 FAN_TO_REG(long rpm, int div)
535{
536 if (rpm == 0)
537 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800538 rpm = clamp_val(rpm, 1, 1000000);
539 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200540}
541
542static inline u16 FAN16_TO_REG(long rpm)
543{
544 if (rpm == 0)
545 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800546 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200547}
548
549#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
550 1350000 / ((val) * (div)))
551/* The divider is fixed to 2 in 16-bit mode */
552#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
553 1350000 / ((val) * 2))
554
Guenter Roeck2a844c12013-01-09 08:09:34 -0800555#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
556 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200557#define TEMP_FROM_REG(val) ((val) * 1000)
558
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200559static u8 pwm_to_reg(const struct it87_data *data, long val)
560{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100561 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200562 return val;
563 else
564 return val >> 1;
565}
566
567static int pwm_from_reg(const struct it87_data *data, u8 reg)
568{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100569 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200570 return reg;
571 else
572 return (reg & 0x7f) << 1;
573}
574
Jean Delvare0df6454d2010-10-28 20:31:51 +0200575
576static int DIV_TO_REG(int val)
577{
578 int answer = 0;
579 while (answer < 7 && (val >>= 1))
580 answer++;
581 return answer;
582}
583#define DIV_FROM_REG(val) (1 << (val))
584
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700585/*
586 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
587 * depending on the chip type, to calculate the actual PWM frequency.
588 *
589 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
590 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
591 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
592 * sometimes just one. It is unknown if this is a datasheet error or real,
593 * so this is ignored for now.
594 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200595static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700596 48000000,
597 24000000,
598 12000000,
599 8000000,
600 6000000,
601 3000000,
602 1500000,
603 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200604};
605
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200606static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500607static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200609static int it87_read_value(struct it87_data *data, u8 reg);
610static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200612static int it87_check_pwm(struct device *dev);
613static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200616static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100617 .driver = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200618 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100619 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200620 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500621 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200622};
623
Jean Delvare20ad93d2005-06-05 11:53:25 +0200624static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100625 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100627 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
628 int nr = sattr->nr;
629 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100632 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Guenter Roeck929c6a52012-12-19 22:17:00 +0100635static ssize_t set_in(struct device *dev, struct device_attribute *attr,
636 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100638 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
639 int nr = sattr->nr;
640 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200641
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200642 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100643 unsigned long val;
644
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100645 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100646 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100648 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100649 data->in[nr][index] = in_to_reg(data, nr, val);
650 it87_write_value(data,
651 index == 1 ? IT87_REG_VIN_MIN(nr)
652 : IT87_REG_VIN_MAX(nr),
653 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100654 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return count;
656}
657
Guenter Roeck929c6a52012-12-19 22:17:00 +0100658static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
659static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
660 0, 1);
661static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
662 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Guenter Roeck929c6a52012-12-19 22:17:00 +0100664static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
665static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
666 1, 1);
667static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
668 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Guenter Roeck929c6a52012-12-19 22:17:00 +0100670static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
671static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
672 2, 1);
673static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
674 2, 2);
675
676static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
677static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
678 3, 1);
679static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
680 3, 2);
681
682static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
683static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
684 4, 1);
685static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
686 4, 2);
687
688static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
689static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
690 5, 1);
691static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
692 5, 2);
693
694static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
695static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
696 6, 1);
697static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
698 6, 2);
699
700static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
701static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
702 7, 1);
703static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
704 7, 2);
705
706static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100707static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200710static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100711 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100713 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
714 int nr = sattr->nr;
715 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200717
Guenter Roeck60ca3852012-12-19 22:17:00 +0100718 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200720
Guenter Roeck60ca3852012-12-19 22:17:00 +0100721static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
722 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100724 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
725 int nr = sattr->nr;
726 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200727 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100728 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100729 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100730
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100731 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100732 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100734 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100735
736 switch (index) {
737 default:
738 case 1:
739 reg = IT87_REG_TEMP_LOW(nr);
740 break;
741 case 2:
742 reg = IT87_REG_TEMP_HIGH(nr);
743 break;
744 case 3:
745 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
746 if (!(regval & 0x80)) {
747 regval |= 0x80;
748 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
749 }
750 data->valid = 0;
751 reg = IT87_REG_TEMP_OFFSET[nr];
752 break;
753 }
754
Guenter Roeck60ca3852012-12-19 22:17:00 +0100755 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100756 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100757 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return count;
759}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200760
Guenter Roeck60ca3852012-12-19 22:17:00 +0100761static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
762static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
763 0, 1);
764static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
765 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100766static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
767 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100768static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
769static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
770 1, 1);
771static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
772 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100773static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
774 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100775static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
776static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
777 2, 1);
778static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
779 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100780static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
781 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Guenter Roeck2cece012012-12-19 22:17:01 +0100783static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
784 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200786 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
787 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800789 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100790 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100791
Guenter Roeck19529782012-12-19 22:17:02 +0100792 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
793 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100794 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (reg & (1 << nr))
796 return sprintf(buf, "3\n"); /* thermal diode */
797 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200798 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return sprintf(buf, "0\n"); /* disabled */
800}
Guenter Roeck2cece012012-12-19 22:17:01 +0100801
802static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
803 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200805 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
806 int nr = sensor_attr->index;
807
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200808 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100809 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100810 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100811
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100812 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100813 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Jean Delvare8acf07c2010-04-14 16:14:09 +0200815 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
816 reg &= ~(1 << nr);
817 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100818 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
819 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100820 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
821 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
822 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200823 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100824 dev_warn(dev,
825 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200826 val = 4;
827 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100828 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200830 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200831 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200832 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100833 else if (has_temp_peci(data, nr) && val == 6)
834 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100835 else if (has_temp_old_peci(data, nr) && val == 6)
836 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200837 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200839
840 mutex_lock(&data->update_lock);
841 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100842 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200843 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100844 if (has_temp_old_peci(data, nr))
845 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200846 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100847 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return count;
849}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Guenter Roeck2cece012012-12-19 22:17:01 +0100851static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
852 set_temp_type, 0);
853static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
854 set_temp_type, 1);
855static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
856 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100859
860static int pwm_mode(const struct it87_data *data, int nr)
861{
862 int ctrl = data->fan_main_ctrl & (1 << nr);
863
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100864 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +0100865 return 0;
866 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
867 return 2;
868 else /* Manual mode */
869 return 1;
870}
871
Jean Delvare20ad93d2005-06-05 11:53:25 +0200872static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +0100873 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100875 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
876 int nr = sattr->nr;
877 int index = sattr->index;
878 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200880
Guenter Roecke1169ba2012-12-19 22:17:01 +0100881 speed = has_16bit_fans(data) ?
882 FAN16_FROM_REG(data->fan[nr][index]) :
883 FAN_FROM_REG(data->fan[nr][index],
884 DIV_FROM_REG(data->fan_div[nr]));
885 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100887
Jean Delvare20ad93d2005-06-05 11:53:25 +0200888static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
889 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200891 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
892 int nr = sensor_attr->index;
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 struct it87_data *data = it87_update_device(dev);
895 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
896}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100897static ssize_t show_pwm_enable(struct device *dev,
898 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200900 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
901 int nr = sensor_attr->index;
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100904 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200906static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
907 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200909 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
910 int nr = sensor_attr->index;
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200913 return sprintf(buf, "%d\n",
914 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100916static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
917 char *buf)
918{
Guenter Roeck60878bc2015-03-26 19:57:42 -0700919 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100920 struct it87_data *data = it87_update_device(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -0700921 int nr = sensor_attr->index;
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700922 unsigned int freq;
Guenter Roeck60878bc2015-03-26 19:57:42 -0700923 int index;
924
925 if (has_pwm_freq2(data) && nr == 1)
926 index = (data->extra >> 4) & 0x07;
927 else
928 index = (data->fan_ctl >> 4) & 0x07;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100929
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700930 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
931
932 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100933}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100934
935static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
936 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100938 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
939 int nr = sattr->nr;
940 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200941
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200942 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100943 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100944 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100946 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100947 return -EINVAL;
948
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100949 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +0100950
951 if (has_16bit_fans(data)) {
952 data->fan[nr][index] = FAN16_TO_REG(val);
953 it87_write_value(data, IT87_REG_FAN_MIN[nr],
954 data->fan[nr][index] & 0xff);
955 it87_write_value(data, IT87_REG_FANX_MIN[nr],
956 data->fan[nr][index] >> 8);
957 } else {
958 reg = it87_read_value(data, IT87_REG_FAN_DIV);
959 switch (nr) {
960 case 0:
961 data->fan_div[nr] = reg & 0x07;
962 break;
963 case 1:
964 data->fan_div[nr] = (reg >> 3) & 0x07;
965 break;
966 case 2:
967 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
968 break;
969 }
970 data->fan[nr][index] =
971 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
972 it87_write_value(data, IT87_REG_FAN_MIN[nr],
973 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -0800974 }
975
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100976 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return count;
978}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100979
Jean Delvare20ad93d2005-06-05 11:53:25 +0200980static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
981 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200983 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
984 int nr = sensor_attr->index;
985
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200986 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100987 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200988 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 u8 old;
990
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100991 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100992 return -EINVAL;
993
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100994 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200995 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200997 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100998 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 switch (nr) {
1001 case 0:
1002 case 1:
1003 data->fan_div[nr] = DIV_TO_REG(val);
1004 break;
1005 case 2:
1006 if (val < 8)
1007 data->fan_div[nr] = 1;
1008 else
1009 data->fan_div[nr] = 3;
1010 }
1011 val = old & 0x80;
1012 val |= (data->fan_div[0] & 0x07);
1013 val |= (data->fan_div[1] & 0x07) << 3;
1014 if (data->fan_div[2] == 3)
1015 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001016 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001018 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001019 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1020 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001021
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001022 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return count;
1024}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001025
1026/* Returns 0 if OK, -EINVAL otherwise */
1027static int check_trip_points(struct device *dev, int nr)
1028{
1029 const struct it87_data *data = dev_get_drvdata(dev);
1030 int i, err = 0;
1031
1032 if (has_old_autopwm(data)) {
1033 for (i = 0; i < 3; i++) {
1034 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1035 err = -EINVAL;
1036 }
1037 for (i = 0; i < 2; i++) {
1038 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1039 err = -EINVAL;
1040 }
1041 }
1042
1043 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001044 dev_err(dev,
1045 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001046 dev_err(dev, "Adjust the trip points and try again\n");
1047 }
1048 return err;
1049}
1050
Jean Delvare20ad93d2005-06-05 11:53:25 +02001051static ssize_t set_pwm_enable(struct device *dev,
1052 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001054 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1055 int nr = sensor_attr->index;
1056
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001057 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001058 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001060 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001061 return -EINVAL;
1062
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001063 /* Check trip points before switching to automatic mode */
1064 if (val == 2) {
1065 if (check_trip_points(dev, nr) < 0)
1066 return -EINVAL;
1067 }
1068
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001069 /* IT8603E does not have on/off mode */
1070 if (val == 0 && data->type == it8603)
1071 return -EINVAL;
1072
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001073 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 if (val == 0) {
1076 int tmp;
1077 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001078 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1079 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /* set on/off mode */
1081 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +01001082 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1083 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +01001084 } else {
1085 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001086 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001087 data->pwm_temp_map[nr] :
1088 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001089 else /* Automatic mode */
1090 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001091 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001092
1093 if (data->type != it8603) {
1094 /* set SmartGuardian mode */
1095 data->fan_main_ctrl |= (1 << nr);
1096 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1097 data->fan_main_ctrl);
1098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001101 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return count;
1103}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001104static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1105 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001107 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1108 int nr = sensor_attr->index;
1109
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001110 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001111 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001113 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return -EINVAL;
1115
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001116 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001117 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001118 /*
1119 * If we are in automatic mode, the PWM duty cycle register
1120 * is read-only so we can't write the value.
1121 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001122 if (data->pwm_ctrl[nr] & 0x80) {
1123 mutex_unlock(&data->update_lock);
1124 return -EBUSY;
1125 }
1126 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001127 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001128 data->pwm_duty[nr]);
1129 } else {
1130 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001131 /*
1132 * If we are in manual mode, write the duty cycle immediately;
1133 * otherwise, just store it for later use.
1134 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001135 if (!(data->pwm_ctrl[nr] & 0x80)) {
1136 data->pwm_ctrl[nr] = data->pwm_duty[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001137 it87_write_value(data, IT87_REG_PWM[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001138 data->pwm_ctrl[nr]);
1139 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001140 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001141 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return count;
1143}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001144static ssize_t set_pwm_freq(struct device *dev,
1145 struct device_attribute *attr, const char *buf, size_t count)
1146{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001147 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001148 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001149 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001150 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001151 int i;
1152
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001153 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001154 return -EINVAL;
1155
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001156 val = clamp_val(val, 0, 1000000);
1157 val *= has_newer_autopwm(data) ? 256 : 128;
1158
Jean Delvaref8d0c192007-02-14 21:15:02 +01001159 /* Search for the nearest available frequency */
1160 for (i = 0; i < 7; i++) {
1161 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1162 break;
1163 }
1164
1165 mutex_lock(&data->update_lock);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001166 if (nr == 0) {
1167 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1168 data->fan_ctl |= i << 4;
1169 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1170 } else {
1171 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1172 data->extra |= i << 4;
1173 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1174 }
Jean Delvaref8d0c192007-02-14 21:15:02 +01001175 mutex_unlock(&data->update_lock);
1176
1177 return count;
1178}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001179static ssize_t show_pwm_temp_map(struct device *dev,
1180 struct device_attribute *attr, char *buf)
1181{
1182 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1183 int nr = sensor_attr->index;
1184
1185 struct it87_data *data = it87_update_device(dev);
1186 int map;
1187
1188 if (data->pwm_temp_map[nr] < 3)
1189 map = 1 << data->pwm_temp_map[nr];
1190 else
1191 map = 0; /* Should never happen */
1192 return sprintf(buf, "%d\n", map);
1193}
1194static ssize_t set_pwm_temp_map(struct device *dev,
1195 struct device_attribute *attr, const char *buf, size_t count)
1196{
1197 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1198 int nr = sensor_attr->index;
1199
1200 struct it87_data *data = dev_get_drvdata(dev);
1201 long val;
1202 u8 reg;
1203
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001204 /*
1205 * This check can go away if we ever support automatic fan speed
1206 * control on newer chips.
1207 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001208 if (!has_old_autopwm(data)) {
1209 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1210 return -EINVAL;
1211 }
1212
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001213 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001214 return -EINVAL;
1215
1216 switch (val) {
1217 case (1 << 0):
1218 reg = 0x00;
1219 break;
1220 case (1 << 1):
1221 reg = 0x01;
1222 break;
1223 case (1 << 2):
1224 reg = 0x02;
1225 break;
1226 default:
1227 return -EINVAL;
1228 }
1229
1230 mutex_lock(&data->update_lock);
1231 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001232 /*
1233 * If we are in automatic mode, write the temp mapping immediately;
1234 * otherwise, just store it for later use.
1235 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001236 if (data->pwm_ctrl[nr] & 0x80) {
1237 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001238 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001239 }
1240 mutex_unlock(&data->update_lock);
1241 return count;
1242}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001244static ssize_t show_auto_pwm(struct device *dev,
1245 struct device_attribute *attr, char *buf)
1246{
1247 struct it87_data *data = it87_update_device(dev);
1248 struct sensor_device_attribute_2 *sensor_attr =
1249 to_sensor_dev_attr_2(attr);
1250 int nr = sensor_attr->nr;
1251 int point = sensor_attr->index;
1252
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001253 return sprintf(buf, "%d\n",
1254 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001255}
1256
1257static ssize_t set_auto_pwm(struct device *dev,
1258 struct device_attribute *attr, const char *buf, size_t count)
1259{
1260 struct it87_data *data = dev_get_drvdata(dev);
1261 struct sensor_device_attribute_2 *sensor_attr =
1262 to_sensor_dev_attr_2(attr);
1263 int nr = sensor_attr->nr;
1264 int point = sensor_attr->index;
1265 long val;
1266
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001267 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001268 return -EINVAL;
1269
1270 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001271 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001272 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1273 data->auto_pwm[nr][point]);
1274 mutex_unlock(&data->update_lock);
1275 return count;
1276}
1277
1278static ssize_t show_auto_temp(struct device *dev,
1279 struct device_attribute *attr, char *buf)
1280{
1281 struct it87_data *data = it87_update_device(dev);
1282 struct sensor_device_attribute_2 *sensor_attr =
1283 to_sensor_dev_attr_2(attr);
1284 int nr = sensor_attr->nr;
1285 int point = sensor_attr->index;
1286
1287 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1288}
1289
1290static ssize_t set_auto_temp(struct device *dev,
1291 struct device_attribute *attr, const char *buf, size_t count)
1292{
1293 struct it87_data *data = dev_get_drvdata(dev);
1294 struct sensor_device_attribute_2 *sensor_attr =
1295 to_sensor_dev_attr_2(attr);
1296 int nr = sensor_attr->nr;
1297 int point = sensor_attr->index;
1298 long val;
1299
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001300 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001301 return -EINVAL;
1302
1303 mutex_lock(&data->update_lock);
1304 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1305 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1306 data->auto_temp[nr][point]);
1307 mutex_unlock(&data->update_lock);
1308 return count;
1309}
1310
Guenter Roecke1169ba2012-12-19 22:17:01 +01001311static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1312static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1313 0, 1);
1314static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1315 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Guenter Roecke1169ba2012-12-19 22:17:01 +01001317static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1318static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1319 1, 1);
1320static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1321 set_fan_div, 1);
1322
1323static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1324static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1325 2, 1);
1326static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1327 set_fan_div, 2);
1328
1329static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1330static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1331 3, 1);
1332
1333static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1334static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1335 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001337static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1338static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1339 5, 1);
1340
Guenter Roeckc4458db2012-12-19 22:17:02 +01001341static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1342 show_pwm_enable, set_pwm_enable, 0);
1343static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001344static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1345 set_pwm_freq, 0);
Guenter Roeckc4458db2012-12-19 22:17:02 +01001346static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1347 show_pwm_temp_map, set_pwm_temp_map, 0);
1348static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1349 show_auto_pwm, set_auto_pwm, 0, 0);
1350static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1351 show_auto_pwm, set_auto_pwm, 0, 1);
1352static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1353 show_auto_pwm, set_auto_pwm, 0, 2);
1354static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1355 show_auto_pwm, NULL, 0, 3);
1356static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1357 show_auto_temp, set_auto_temp, 0, 1);
1358static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1359 show_auto_temp, set_auto_temp, 0, 0);
1360static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1361 show_auto_temp, set_auto_temp, 0, 2);
1362static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1363 show_auto_temp, set_auto_temp, 0, 3);
1364static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1365 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Guenter Roeckc4458db2012-12-19 22:17:02 +01001367static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1368 show_pwm_enable, set_pwm_enable, 1);
1369static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001370static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
Guenter Roeckc4458db2012-12-19 22:17:02 +01001371static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1372 show_pwm_temp_map, set_pwm_temp_map, 1);
1373static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1374 show_auto_pwm, set_auto_pwm, 1, 0);
1375static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1376 show_auto_pwm, set_auto_pwm, 1, 1);
1377static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1378 show_auto_pwm, set_auto_pwm, 1, 2);
1379static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1380 show_auto_pwm, NULL, 1, 3);
1381static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1382 show_auto_temp, set_auto_temp, 1, 1);
1383static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1384 show_auto_temp, set_auto_temp, 1, 0);
1385static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1386 show_auto_temp, set_auto_temp, 1, 2);
1387static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1388 show_auto_temp, set_auto_temp, 1, 3);
1389static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1390 show_auto_temp, set_auto_temp, 1, 4);
1391
1392static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1393 show_pwm_enable, set_pwm_enable, 2);
1394static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001395static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
Guenter Roeckc4458db2012-12-19 22:17:02 +01001396static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1397 show_pwm_temp_map, set_pwm_temp_map, 2);
1398static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1399 show_auto_pwm, set_auto_pwm, 2, 0);
1400static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1401 show_auto_pwm, set_auto_pwm, 2, 1);
1402static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1403 show_auto_pwm, set_auto_pwm, 2, 2);
1404static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1405 show_auto_pwm, NULL, 2, 3);
1406static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1407 show_auto_temp, set_auto_temp, 2, 1);
1408static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1409 show_auto_temp, set_auto_temp, 2, 0);
1410static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1411 show_auto_temp, set_auto_temp, 2, 2);
1412static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1413 show_auto_temp, set_auto_temp, 2, 3);
1414static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1415 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Guenter Roeck36c4d982015-03-26 18:18:19 -07001417static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1418 show_pwm_enable, set_pwm_enable, 3);
1419static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001420static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001421static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO | S_IWUSR,
1422 show_pwm_temp_map, set_pwm_temp_map, 3);
1423
1424static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1425 show_pwm_enable, set_pwm_enable, 4);
1426static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001427static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001428static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO | S_IWUSR,
1429 show_pwm_temp_map, set_pwm_temp_map, 4);
1430
1431static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1432 show_pwm_enable, set_pwm_enable, 5);
1433static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001434static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001435static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO | S_IWUSR,
1436 show_pwm_temp_map, set_pwm_temp_map, 5);
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001439static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1440 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001443 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
Jean Delvare1d66c642005-04-18 21:16:59 -07001445static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Jean Delvare0124dd72007-11-25 16:16:41 +01001447static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1448 char *buf)
1449{
1450 int bitnr = to_sensor_dev_attr(attr)->index;
1451 struct it87_data *data = it87_update_device(dev);
1452 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1453}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001454
1455static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1456 *attr, const char *buf, size_t count)
1457{
1458 struct it87_data *data = dev_get_drvdata(dev);
1459 long val;
1460 int config;
1461
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001462 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001463 return -EINVAL;
1464
1465 mutex_lock(&data->update_lock);
1466 config = it87_read_value(data, IT87_REG_CONFIG);
1467 if (config < 0) {
1468 count = config;
1469 } else {
1470 config |= 1 << 5;
1471 it87_write_value(data, IT87_REG_CONFIG, config);
1472 /* Invalidate cache to force re-read */
1473 data->valid = 0;
1474 }
1475 mutex_unlock(&data->update_lock);
1476
1477 return count;
1478}
1479
Jean Delvare0124dd72007-11-25 16:16:41 +01001480static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1481static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1482static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1483static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1484static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1485static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1486static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1487static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1488static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1489static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1490static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1491static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1492static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001493static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001494static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1495static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1496static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001497static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1498 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001499
Jean Delvared9b327c2010-03-05 22:17:17 +01001500static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1501 char *buf)
1502{
1503 int bitnr = to_sensor_dev_attr(attr)->index;
1504 struct it87_data *data = it87_update_device(dev);
1505 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1506}
1507static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1508 const char *buf, size_t count)
1509{
1510 int bitnr = to_sensor_dev_attr(attr)->index;
1511 struct it87_data *data = dev_get_drvdata(dev);
1512 long val;
1513
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001514 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001515 || (val != 0 && val != 1))
1516 return -EINVAL;
1517
1518 mutex_lock(&data->update_lock);
1519 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1520 if (val)
1521 data->beeps |= (1 << bitnr);
1522 else
1523 data->beeps &= ~(1 << bitnr);
1524 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1525 mutex_unlock(&data->update_lock);
1526 return count;
1527}
1528
1529static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1530 show_beep, set_beep, 1);
1531static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1532static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1533static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1534static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1535static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1536static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1537static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1538/* fanX_beep writability is set later */
1539static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1540static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1541static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1542static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1543static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001544static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001545static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1546 show_beep, set_beep, 2);
1547static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1548static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1549
Jean Delvare5f2dc792010-03-05 22:17:18 +01001550static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1551 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Jean Delvare90d66192007-10-08 18:24:35 +02001553 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001554 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001556static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1557 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001559 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001560 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001562 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001563 return -EINVAL;
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 data->vrm = val;
1566
1567 return count;
1568}
1569static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Jean Delvare5f2dc792010-03-05 22:17:18 +01001571static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1572 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 struct it87_data *data = it87_update_device(dev);
1575 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1576}
1577static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001578
Jean Delvare738e5e02010-08-14 21:08:50 +02001579static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1580 char *buf)
1581{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001582 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001583 "+5V",
1584 "5VSB",
1585 "Vbat",
1586 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001587 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001588 "+3.3V",
1589 "3VSB",
1590 "Vbat",
1591 };
1592 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001593 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001594 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001595
Justin Maggardead80802015-08-05 12:53:08 -07001596 if (has_12mv_adc(data) || has_10_9mv_adc(data))
1597 label = labels_it8721[nr];
1598 else
1599 label = labels[nr];
1600
1601 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001602}
1603static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1604static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1605static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001606/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001607static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001608
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001609static ssize_t show_name(struct device *dev, struct device_attribute
1610 *devattr, char *buf)
1611{
1612 struct it87_data *data = dev_get_drvdata(dev);
1613 return sprintf(buf, "%s\n", data->name);
1614}
1615static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1616
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001617static struct attribute *it87_attributes_in[10][5] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001618{
Jean Delvare87808be2006-09-24 21:17:13 +02001619 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001620 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001621 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001622 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001623 NULL
1624}, {
1625 &sensor_dev_attr_in1_input.dev_attr.attr,
1626 &sensor_dev_attr_in1_min.dev_attr.attr,
1627 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001628 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001629 NULL
1630}, {
1631 &sensor_dev_attr_in2_input.dev_attr.attr,
1632 &sensor_dev_attr_in2_min.dev_attr.attr,
1633 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001634 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001635 NULL
1636}, {
1637 &sensor_dev_attr_in3_input.dev_attr.attr,
1638 &sensor_dev_attr_in3_min.dev_attr.attr,
1639 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001640 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001641 NULL
1642}, {
1643 &sensor_dev_attr_in4_input.dev_attr.attr,
1644 &sensor_dev_attr_in4_min.dev_attr.attr,
1645 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001646 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001647 NULL
1648}, {
1649 &sensor_dev_attr_in5_input.dev_attr.attr,
1650 &sensor_dev_attr_in5_min.dev_attr.attr,
1651 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001652 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001653 NULL
1654}, {
1655 &sensor_dev_attr_in6_input.dev_attr.attr,
1656 &sensor_dev_attr_in6_min.dev_attr.attr,
1657 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001658 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001659 NULL
1660}, {
1661 &sensor_dev_attr_in7_input.dev_attr.attr,
1662 &sensor_dev_attr_in7_min.dev_attr.attr,
1663 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001664 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001665 NULL
1666}, {
1667 &sensor_dev_attr_in8_input.dev_attr.attr,
1668 NULL
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001669}, {
1670 &sensor_dev_attr_in9_input.dev_attr.attr,
1671 NULL
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001672} };
Jean Delvare87808be2006-09-24 21:17:13 +02001673
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001674static const struct attribute_group it87_group_in[10] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001675 { .attrs = it87_attributes_in[0] },
1676 { .attrs = it87_attributes_in[1] },
1677 { .attrs = it87_attributes_in[2] },
1678 { .attrs = it87_attributes_in[3] },
1679 { .attrs = it87_attributes_in[4] },
1680 { .attrs = it87_attributes_in[5] },
1681 { .attrs = it87_attributes_in[6] },
1682 { .attrs = it87_attributes_in[7] },
1683 { .attrs = it87_attributes_in[8] },
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001684 { .attrs = it87_attributes_in[9] },
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001685};
1686
Guenter Roeck4573acb2012-03-26 16:17:41 -07001687static struct attribute *it87_attributes_temp[3][6] = {
1688{
Jean Delvare87808be2006-09-24 21:17:13 +02001689 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001690 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001691 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001692 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001693 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001694 NULL
1695} , {
1696 &sensor_dev_attr_temp2_input.dev_attr.attr,
1697 &sensor_dev_attr_temp2_max.dev_attr.attr,
1698 &sensor_dev_attr_temp2_min.dev_attr.attr,
1699 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001700 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001701 NULL
1702} , {
1703 &sensor_dev_attr_temp3_input.dev_attr.attr,
1704 &sensor_dev_attr_temp3_max.dev_attr.attr,
1705 &sensor_dev_attr_temp3_min.dev_attr.attr,
1706 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001707 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001708 NULL
1709} };
Jean Delvare87808be2006-09-24 21:17:13 +02001710
Guenter Roeck4573acb2012-03-26 16:17:41 -07001711static const struct attribute_group it87_group_temp[3] = {
1712 { .attrs = it87_attributes_temp[0] },
1713 { .attrs = it87_attributes_temp[1] },
1714 { .attrs = it87_attributes_temp[2] },
1715};
1716
Guenter Roeck161d8982012-12-19 22:17:01 +01001717static struct attribute *it87_attributes_temp_offset[] = {
1718 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1719 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1720 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1721};
1722
Guenter Roeck4573acb2012-03-26 16:17:41 -07001723static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001724 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001725 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001726 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001727 NULL
1728};
1729
1730static const struct attribute_group it87_group = {
1731 .attrs = it87_attributes,
1732};
1733
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001734static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001735 &sensor_dev_attr_in0_beep.dev_attr.attr,
1736 &sensor_dev_attr_in1_beep.dev_attr.attr,
1737 &sensor_dev_attr_in2_beep.dev_attr.attr,
1738 &sensor_dev_attr_in3_beep.dev_attr.attr,
1739 &sensor_dev_attr_in4_beep.dev_attr.attr,
1740 &sensor_dev_attr_in5_beep.dev_attr.attr,
1741 &sensor_dev_attr_in6_beep.dev_attr.attr,
1742 &sensor_dev_attr_in7_beep.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001743 NULL,
1744 NULL,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001745};
Jean Delvared9b327c2010-03-05 22:17:17 +01001746
Guenter Roeck4573acb2012-03-26 16:17:41 -07001747static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001748 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1749 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1750 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001751};
1752
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001753static struct attribute *it87_attributes_fan[6][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001754 &sensor_dev_attr_fan1_input.dev_attr.attr,
1755 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001756 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1757 NULL
1758}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001759 &sensor_dev_attr_fan2_input.dev_attr.attr,
1760 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001761 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1762 NULL
1763}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001764 &sensor_dev_attr_fan3_input.dev_attr.attr,
1765 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001766 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001767 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001768}, {
1769 &sensor_dev_attr_fan4_input.dev_attr.attr,
1770 &sensor_dev_attr_fan4_min.dev_attr.attr,
1771 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1772 NULL
1773}, {
1774 &sensor_dev_attr_fan5_input.dev_attr.attr,
1775 &sensor_dev_attr_fan5_min.dev_attr.attr,
1776 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1777 NULL
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001778}, {
1779 &sensor_dev_attr_fan6_input.dev_attr.attr,
1780 &sensor_dev_attr_fan6_min.dev_attr.attr,
1781 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1782 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001783} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001784
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001785static const struct attribute_group it87_group_fan[6] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001786 { .attrs = it87_attributes_fan[0] },
1787 { .attrs = it87_attributes_fan[1] },
1788 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001789 { .attrs = it87_attributes_fan[3] },
1790 { .attrs = it87_attributes_fan[4] },
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001791 { .attrs = it87_attributes_fan[5] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001792};
1793
Guenter Roecke1169ba2012-12-19 22:17:01 +01001794static const struct attribute *it87_attributes_fan_div[] = {
1795 &sensor_dev_attr_fan1_div.dev_attr.attr,
1796 &sensor_dev_attr_fan2_div.dev_attr.attr,
1797 &sensor_dev_attr_fan3_div.dev_attr.attr,
1798};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001799
Guenter Roeck36c4d982015-03-26 18:18:19 -07001800static struct attribute *it87_attributes_pwm[6][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001801 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001802 &sensor_dev_attr_pwm1.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001803 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001804 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001805 NULL
1806}, {
1807 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1808 &sensor_dev_attr_pwm2.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001809 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001810 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001811 NULL
1812}, {
1813 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1814 &sensor_dev_attr_pwm3.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001815 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001816 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001817 NULL
Guenter Roeck36c4d982015-03-26 18:18:19 -07001818}, {
1819 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1820 &sensor_dev_attr_pwm4.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001821 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001822 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1823 NULL
1824}, {
1825 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1826 &sensor_dev_attr_pwm5.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001827 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001828 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
1829 NULL
1830}, {
1831 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
1832 &sensor_dev_attr_pwm6.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001833 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001834 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
1835 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001836} };
Jean Delvare87808be2006-09-24 21:17:13 +02001837
Guenter Roeck60878bc2015-03-26 19:57:42 -07001838static umode_t pwm_attribute_mode(struct kobject *kobj, struct attribute *attr,
1839 int index)
1840{
1841 struct device *dev = container_of(kobj, struct device, kobj);
1842 struct it87_data *data = dev_get_drvdata(dev);
1843
1844 if (has_pwm_freq2(data) && index == 2)
1845 return attr->mode | S_IWUSR;
1846
1847 return attr->mode;
1848}
1849
Guenter Roeck36c4d982015-03-26 18:18:19 -07001850static const struct attribute_group it87_group_pwm[6] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001851 { .attrs = it87_attributes_pwm[0] },
Guenter Roeck60878bc2015-03-26 19:57:42 -07001852 { .attrs = it87_attributes_pwm[1],
1853 .is_visible = pwm_attribute_mode, },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001854 { .attrs = it87_attributes_pwm[2] },
Guenter Roeck36c4d982015-03-26 18:18:19 -07001855 { .attrs = it87_attributes_pwm[3] },
1856 { .attrs = it87_attributes_pwm[4] },
1857 { .attrs = it87_attributes_pwm[5] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001858};
1859
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001860static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1861 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1862 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1863 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1864 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1865 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1866 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1867 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1868 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1869 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1870 NULL
1871}, {
1872 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1873 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1874 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1875 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1876 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1877 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1878 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1879 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1880 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1881 NULL
1882}, {
1883 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1884 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1885 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1886 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1887 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1888 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1889 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1890 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1891 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1892 NULL
1893} };
1894
1895static const struct attribute_group it87_group_autopwm[3] = {
1896 { .attrs = it87_attributes_autopwm[0] },
1897 { .attrs = it87_attributes_autopwm[1] },
1898 { .attrs = it87_attributes_autopwm[2] },
1899};
1900
Jean Delvared9b327c2010-03-05 22:17:17 +01001901static struct attribute *it87_attributes_fan_beep[] = {
1902 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1903 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1904 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1905 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1906 &sensor_dev_attr_fan5_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001907 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001908};
1909
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001910static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001911 &dev_attr_vrm.attr,
1912 &dev_attr_cpu0_vid.attr,
1913 NULL
1914};
1915
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001916static const struct attribute_group it87_group_vid = {
1917 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001918};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Jean Delvare738e5e02010-08-14 21:08:50 +02001920static struct attribute *it87_attributes_label[] = {
1921 &sensor_dev_attr_in3_label.dev_attr.attr,
1922 &sensor_dev_attr_in7_label.dev_attr.attr,
1923 &sensor_dev_attr_in8_label.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001924 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare738e5e02010-08-14 21:08:50 +02001925 NULL
1926};
1927
1928static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001929 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001930};
1931
Jean Delvare2d8672c2005-07-19 23:56:35 +02001932/* SuperIO detection - will change isa_address if a chip is found */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07001933static int __init it87_find(int sioaddr, unsigned short *address,
1934 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001936 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001937 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001938 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001939 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Guenter Roeck3c2e3512015-03-28 08:03:10 -07001941 err = superio_enter(sioaddr);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001942 if (err)
1943 return err;
1944
1945 err = -ENODEV;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07001946 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001947
1948 switch (chip_type) {
1949 case IT8705F_DEVID:
1950 sio_data->type = it87;
1951 break;
1952 case IT8712F_DEVID:
1953 sio_data->type = it8712;
1954 break;
1955 case IT8716F_DEVID:
1956 case IT8726F_DEVID:
1957 sio_data->type = it8716;
1958 break;
1959 case IT8718F_DEVID:
1960 sio_data->type = it8718;
1961 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001962 case IT8720F_DEVID:
1963 sio_data->type = it8720;
1964 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001965 case IT8721F_DEVID:
1966 sio_data->type = it8721;
1967 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001968 case IT8728F_DEVID:
1969 sio_data->type = it8728;
1970 break;
Justin Maggardead80802015-08-05 12:53:08 -07001971 case IT8732F_DEVID:
1972 sio_data->type = it8732;
1973 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06001974 case IT8771E_DEVID:
1975 sio_data->type = it8771;
1976 break;
1977 case IT8772E_DEVID:
1978 sio_data->type = it8772;
1979 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08001980 case IT8781F_DEVID:
1981 sio_data->type = it8781;
1982 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001983 case IT8782F_DEVID:
1984 sio_data->type = it8782;
1985 break;
1986 case IT8783E_DEVID:
1987 sio_data->type = it8783;
1988 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001989 case IT8786E_DEVID:
1990 sio_data->type = it8786;
1991 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07001992 case IT8790E_DEVID:
1993 sio_data->type = it8790;
1994 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02001995 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02001996 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001997 sio_data->type = it8603;
1998 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08001999 case IT8620E_DEVID:
2000 sio_data->type = it8620;
2001 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002002 case 0xffff: /* No device at all */
2003 goto exit;
2004 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01002005 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002006 goto exit;
2007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002009 superio_select(sioaddr, PME);
2010 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002011 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 goto exit;
2013 }
2014
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002015 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002017 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 goto exit;
2019 }
2020
2021 err = 0;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002022 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07002023 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2024 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002025 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002027 config = &it87_devices[sio_data->type];
2028
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002029 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002030 if (has_in7_internal(config))
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002031 sio_data->internal |= (1 << 1);
2032
Jean Delvare738e5e02010-08-14 21:08:50 +02002033 /* in8 (Vbat) is always internal */
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002034 sio_data->internal |= (1 << 2);
2035
Guenter Roeck73055402015-03-26 09:16:32 -07002036 /* in9 (AVCC3), always internal if supported */
2037 if (has_avcc3(config))
2038 sio_data->internal |= (1 << 3); /* in9 is AVCC */
2039 else
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002040 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02002041
Guenter Roeck36c4d982015-03-26 18:18:19 -07002042 if (!has_six_pwm(config))
2043 sio_data->skip_pwm |= (1 << 3) | (1 << 4) | (1 << 5);
2044
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002045 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01002046 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01002047
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002048 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2049 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01002050 /* The IT8705F has a different LD number for GPIO */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002051 superio_select(sioaddr, 5);
2052 sio_data->beep_pin = superio_inb(sioaddr,
2053 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002054 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002055 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002056
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002057 superio_select(sioaddr, GPIO);
Guenter Roeck0531d982012-03-02 11:46:44 -08002058
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002059 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2060 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2061 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2062 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2063 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002064
Guenter Roeck0531d982012-03-02 11:46:44 -08002065 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002066 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08002067 sio_data->skip_fan |= (1 << 2);
2068 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002069 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08002070 sio_data->skip_pwm |= (1 << 2);
2071
2072 /* Check if fan2 is there or not */
2073 if (reg27 & (1 << 7))
2074 sio_data->skip_fan |= (1 << 1);
2075 if (reg27 & (1 << 3))
2076 sio_data->skip_pwm |= (1 << 1);
2077
2078 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002079 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002080 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002081
2082 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002083 if (reg27 & (1 << 1))
2084 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002085
2086 /*
2087 * VIN7
2088 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2089 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002090 if (reg27 & (1 << 2)) {
2091 /*
2092 * The data sheet is a bit unclear regarding the
2093 * internal voltage divider for VCCH5V. It says
2094 * "This bit enables and switches VIN7 (pin 91) to the
2095 * internal voltage divider for VCCH5V".
2096 * This is different to other chips, where the internal
2097 * voltage divider would connect VIN7 to an internal
2098 * voltage source. Maybe that is the case here as well.
2099 *
2100 * Since we don't know for sure, re-route it if that is
2101 * not the case, and ask the user to report if the
2102 * resulting voltage is sane.
2103 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002104 if (!(reg2c & (1 << 1))) {
2105 reg2c |= (1 << 1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002106 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2107 reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002108 pr_notice("Routing internal VCCH5V to in7.\n");
2109 }
2110 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2111 pr_notice("Please report if it displays a reasonable voltage.\n");
2112 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002113
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002114 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08002115 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002116 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08002117 sio_data->internal |= (1 << 1);
2118
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002119 sio_data->beep_pin = superio_inb(sioaddr,
2120 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002121 } else if (sio_data->type == it8603) {
2122 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002123
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002124 superio_select(sioaddr, GPIO);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002125
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002126 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002127
2128 /* Check if fan3 is there or not */
2129 if (reg27 & (1 << 6))
2130 sio_data->skip_pwm |= (1 << 2);
2131 if (reg27 & (1 << 7))
2132 sio_data->skip_fan |= (1 << 2);
2133
2134 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002135 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002136 if (reg29 & (1 << 1))
2137 sio_data->skip_pwm |= (1 << 1);
2138 if (reg29 & (1 << 2))
2139 sio_data->skip_fan |= (1 << 1);
2140
2141 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2142 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2143
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002144 sio_data->beep_pin = superio_inb(sioaddr,
2145 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002146 } else if (sio_data->type == it8620) {
2147 int reg;
2148
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002149 superio_select(sioaddr, GPIO);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002150
Guenter Roeck36c4d982015-03-26 18:18:19 -07002151 /* Check for pwm5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002152 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002153 if (reg & (1 << 6))
2154 sio_data->skip_pwm |= (1 << 4);
2155
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002156 /* Check for fan4, fan5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002157 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002158 if (!(reg & (1 << 5)))
2159 sio_data->skip_fan |= (1 << 3);
2160 if (!(reg & (1 << 4)))
2161 sio_data->skip_fan |= (1 << 4);
2162
2163 /* Check for pwm3, fan3 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002164 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002165 if (reg & (1 << 6))
2166 sio_data->skip_pwm |= (1 << 2);
2167 if (reg & (1 << 7))
2168 sio_data->skip_fan |= (1 << 2);
2169
Guenter Roeck36c4d982015-03-26 18:18:19 -07002170 /* Check for pwm4 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002171 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002172 if (!(reg & (1 << 2)))
2173 sio_data->skip_pwm |= (1 << 3);
2174
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002175 /* Check for pwm2, fan2 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002176 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002177 if (reg & (1 << 1))
2178 sio_data->skip_pwm |= (1 << 1);
2179 if (reg & (1 << 2))
2180 sio_data->skip_fan |= (1 << 1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002181 /* Check for pwm6, fan6 */
2182 if (!(reg & (1 << 7))) {
2183 sio_data->skip_pwm |= (1 << 5);
2184 sio_data->skip_fan |= (1 << 5);
2185 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002186
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002187 sio_data->beep_pin = superio_inb(sioaddr,
2188 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002189 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002190 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002191 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002192
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002193 superio_select(sioaddr, GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002194
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002195 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002196 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002197 /* We need at least 4 VID pins */
2198 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002199 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002200 sio_data->skip_vid = 1;
2201 }
Jean Delvare895ff262009-12-09 20:35:47 +01002202 }
2203
Jean Delvare591ec652009-12-09 20:35:48 +01002204 /* Check if fan3 is there or not */
2205 if (reg & (1 << 6))
2206 sio_data->skip_pwm |= (1 << 2);
2207 if (reg & (1 << 7))
2208 sio_data->skip_fan |= (1 << 2);
2209
2210 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002211 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Jean Delvare591ec652009-12-09 20:35:48 +01002212 if (reg & (1 << 1))
2213 sio_data->skip_pwm |= (1 << 1);
2214 if (reg & (1 << 2))
2215 sio_data->skip_fan |= (1 << 1);
2216
Jean Delvare895ff262009-12-09 20:35:47 +01002217 if ((sio_data->type == it8718 || sio_data->type == it8720)
2218 && !(sio_data->skip_vid))
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002219 sio_data->vid_value = superio_inb(sioaddr,
2220 IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002221
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002222 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002223
2224 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2225
Jean Delvare436cad22010-07-09 16:22:48 +02002226 /*
2227 * The IT8720F has no VIN7 pin, so VCCH should always be
2228 * routed internally to VIN7 with an internal divider.
2229 * Curiously, there still is a configuration bit to control
2230 * this, which means it can be set incorrectly. And even
2231 * more curiously, many boards out there are improperly
2232 * configured, even though the IT8720F datasheet claims
2233 * that the internal routing of VCCH to VIN7 is the default
2234 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002235 *
2236 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002237 * If UART6 is enabled, re-route VIN7 to the internal divider
2238 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002239 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002240 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02002241 reg |= (1 << 1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002242 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002243 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002244 }
Jean Delvare87673dd2006-08-28 14:37:19 +02002245 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02002246 sio_data->internal |= (1 << 0);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002247 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02002248 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002249
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002250 /*
2251 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2252 * While VIN7 can be routed to the internal voltage divider,
2253 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002254 *
2255 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2256 * is the temperature source. Since we can not read the
2257 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002258 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002259 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002260 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002261 sio_data->skip_temp |= (1 << 2);
2262 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002263
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002264 sio_data->beep_pin = superio_inb(sioaddr,
2265 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002266 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002267 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002268 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002269
Jean Delvare98dd22c2008-10-09 15:33:58 +02002270 /* Disable specific features based on DMI strings */
2271 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2272 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2273 if (board_vendor && board_name) {
2274 if (strcmp(board_vendor, "nVIDIA") == 0
2275 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002276 /*
2277 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2278 * connected to a fan, but to something else. One user
2279 * has reported instant system power-off when changing
2280 * the PWM2 duty cycle, so we disable it.
2281 * I use the board name string as the trigger in case
2282 * the same board is ever used in other systems.
2283 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002284 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002285 sio_data->skip_pwm = (1 << 1);
2286 }
2287 }
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289exit:
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002290 superio_exit(sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return err;
2292}
2293
Jean Delvare723a0aa2010-03-05 22:17:16 +01002294static void it87_remove_files(struct device *dev)
2295{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002296 struct it87_data *data = dev_get_drvdata(dev);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002297 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002298 int i;
2299
2300 sysfs_remove_group(&dev->kobj, &it87_group);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002301 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002302 if (sio_data->skip_in & (1 << i))
2303 continue;
2304 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2305 if (it87_attributes_in_beep[i])
2306 sysfs_remove_file(&dev->kobj,
2307 it87_attributes_in_beep[i]);
2308 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002309 for (i = 0; i < 3; i++) {
2310 if (!(data->has_temp & (1 << i)))
2311 continue;
2312 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01002313 if (has_temp_offset(data))
2314 sysfs_remove_file(&dev->kobj,
2315 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002316 if (sio_data->beep_pin)
2317 sysfs_remove_file(&dev->kobj,
2318 it87_attributes_temp_beep[i]);
2319 }
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002320 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002321 if (!(data->has_fan & (1 << i)))
2322 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002323 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002324 if (sio_data->beep_pin)
2325 sysfs_remove_file(&dev->kobj,
2326 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002327 if (i < 3 && !has_16bit_fans(data))
2328 sysfs_remove_file(&dev->kobj,
2329 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002330 }
Guenter Roeck36c4d982015-03-26 18:18:19 -07002331 for (i = 0; i < 6; i++) {
Guenter Roeck1696d1d2015-03-27 06:03:41 -07002332 if (sio_data->skip_pwm & (1 << i))
Jean Delvare723a0aa2010-03-05 22:17:16 +01002333 continue;
2334 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002335 if (has_old_autopwm(data))
2336 sysfs_remove_group(&dev->kobj,
2337 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002338 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002339 if (!sio_data->skip_vid)
2340 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02002341 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002342}
2343
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002344static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002347 struct resource *res;
2348 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002349 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002350 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01002352 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002354 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002355 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2356 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002357 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2358 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002359 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002360 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Guenter Roeck62a1d052012-03-24 21:54:41 -07002363 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2364 if (!data)
2365 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002367 data->addr = res->start;
2368 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002369 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002370 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002371 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002372 data->name = it87_devices[sio_data->type].name;
2373 /*
2374 * IT8705F Datasheet 0.4.1, 3h == Version G.
2375 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2376 * These are the first revisions with 16-bit tachometer support.
2377 */
2378 switch (data->type) {
2379 case it87:
2380 if (sio_data->revision >= 0x03) {
2381 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002382 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002383 }
2384 break;
2385 case it8712:
2386 if (sio_data->revision >= 0x08) {
2387 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002388 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2389 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002390 }
2391 break;
2392 default:
2393 break;
2394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002397 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002398 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2399 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002401 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002403 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002406 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002408 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002409 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002410 if (sio_data->internal & (1 << 0))
2411 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2412 if (sio_data->internal & (1 << 1))
2413 data->in_scaled |= (1 << 7); /* in7 is VSB */
2414 if (sio_data->internal & (1 << 2))
2415 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002416 if (sio_data->internal & (1 << 3))
2417 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002418 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2419 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002420 if (sio_data->internal & (1 << 0))
2421 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2422 if (sio_data->internal & (1 << 1))
2423 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002424 }
2425
Guenter Roeck4573acb2012-03-26 16:17:41 -07002426 data->has_temp = 0x07;
2427 if (sio_data->skip_temp & (1 << 2)) {
2428 if (sio_data->type == it8782
2429 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2430 data->has_temp &= ~(1 << 2);
2431 }
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002434 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002437 err = sysfs_create_group(&dev->kobj, &it87_group);
2438 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002439 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002440
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002441 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002442 if (sio_data->skip_in & (1 << i))
2443 continue;
2444 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2445 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002446 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002447 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2448 err = sysfs_create_file(&dev->kobj,
2449 it87_attributes_in_beep[i]);
2450 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002451 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002452 }
2453 }
2454
Guenter Roeck4573acb2012-03-26 16:17:41 -07002455 for (i = 0; i < 3; i++) {
2456 if (!(data->has_temp & (1 << i)))
2457 continue;
2458 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002459 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002460 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002461 if (has_temp_offset(data)) {
2462 err = sysfs_create_file(&dev->kobj,
2463 it87_attributes_temp_offset[i]);
2464 if (err)
2465 goto error;
2466 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002467 if (sio_data->beep_pin) {
2468 err = sysfs_create_file(&dev->kobj,
2469 it87_attributes_temp_beep[i]);
2470 if (err)
2471 goto error;
2472 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002473 }
2474
Jean Delvare9060f8b2006-08-28 14:24:17 +02002475 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002476 fan_beep_need_rw = 1;
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002477 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002478 if (!(data->has_fan & (1 << i)))
2479 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002480 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002481 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002482 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002483
Guenter Roecke1169ba2012-12-19 22:17:01 +01002484 if (i < 3 && !has_16bit_fans(data)) {
2485 err = sysfs_create_file(&dev->kobj,
2486 it87_attributes_fan_div[i]);
2487 if (err)
2488 goto error;
2489 }
2490
Jean Delvared9b327c2010-03-05 22:17:17 +01002491 if (sio_data->beep_pin) {
2492 err = sysfs_create_file(&dev->kobj,
2493 it87_attributes_fan_beep[i]);
2494 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002495 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002496 if (!fan_beep_need_rw)
2497 continue;
2498
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002499 /*
2500 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002501 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002502 * for it.
2503 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002504 if (sysfs_chmod_file(&dev->kobj,
2505 it87_attributes_fan_beep[i],
2506 S_IRUGO | S_IWUSR))
2507 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2508 i + 1);
2509 fan_beep_need_rw = 0;
2510 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002511 }
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (enable_pwm_interface) {
Guenter Roeck36c4d982015-03-26 18:18:19 -07002514 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002515 if (sio_data->skip_pwm & (1 << i))
2516 continue;
2517 err = sysfs_create_group(&dev->kobj,
2518 &it87_group_pwm[i]);
2519 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002520 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002521
2522 if (!has_old_autopwm(data))
2523 continue;
2524 err = sysfs_create_group(&dev->kobj,
2525 &it87_group_autopwm[i]);
2526 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002527 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
2530
Jean Delvare895ff262009-12-09 20:35:47 +01002531 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002532 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002533 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002534 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002535 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2536 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002537 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002538 }
2539
Jean Delvare738e5e02010-08-14 21:08:50 +02002540 /* Export labels for internal sensors */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002541 for (i = 0; i < 4; i++) {
Jean Delvare738e5e02010-08-14 21:08:50 +02002542 if (!(sio_data->internal & (1 << i)))
2543 continue;
2544 err = sysfs_create_file(&dev->kobj,
2545 it87_attributes_label[i]);
2546 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002547 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002548 }
2549
Tony Jones1beeffe2007-08-20 13:46:20 -07002550 data->hwmon_dev = hwmon_device_register(dev);
2551 if (IS_ERR(data->hwmon_dev)) {
2552 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002553 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
2555
2556 return 0;
2557
Guenter Roeck62a1d052012-03-24 21:54:41 -07002558error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002559 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 return err;
2561}
2562
Bill Pemberton281dfd02012-11-19 13:25:51 -05002563static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002565 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Tony Jones1beeffe2007-08-20 13:46:20 -07002567 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002568 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002569
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 return 0;
2571}
2572
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002573/*
2574 * Must be called with data->update_lock held, except during initialization.
2575 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2576 * would slow down the IT87 access and should not be necessary.
2577 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002578static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002580 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2581 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002584/*
2585 * Must be called with data->update_lock held, except during initialization.
2586 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2587 * would slow down the IT87 access and should not be necessary.
2588 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002589static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002591 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2592 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
2595/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002596static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002598 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002599 /*
2600 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002602 * disable pwm control to protect the user.
2603 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002604 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 if ((tmp & 0x87) == 0) {
2606 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002607 /*
2608 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002610 * inverting all fan speed values.
2611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 int i;
2613 u8 pwm[3];
2614
2615 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002616 pwm[i] = it87_read_value(data,
Guenter Roeck36c4d982015-03-26 18:18:19 -07002617 IT87_REG_PWM[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002619 /*
2620 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 * might be correct, as suspicious as it seems, so we
2622 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002623 * PWM interface).
2624 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002626 dev_info(dev,
2627 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002628 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 tmp | 0x87);
2630 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002631 it87_write_value(data,
Guenter Roeck36c4d982015-03-26 18:18:19 -07002632 IT87_REG_PWM[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 0x7f & ~pwm[i]);
2634 return 1;
2635 }
2636
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002637 dev_info(dev,
2638 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
2640
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002641 dev_info(dev,
2642 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return 0;
2644 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002645 dev_info(dev,
2646 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648
2649 return 1;
2650}
2651
2652/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002653static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002655 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002656 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002658 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002660 /*
2661 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002662 * - If it is in automatic mode, setting to manual mode should set
2663 * the fan to full speed by default.
2664 * - If it is in manual mode, we need a mapping to temperature
2665 * channels to use when later setting to automatic mode later.
2666 * Use a 1:1 mapping by default (we are clueless.)
2667 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002668 * prior to switching to a different mode.
2669 * Note that this is no longer needed for the IT8721F and later, as
2670 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002671 * manual duty cycle.
2672 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002674 data->pwm_temp_map[i] = i;
2675 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002676 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
2678
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002679 /*
2680 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002681 * registers. For low voltage limits it makes no sense and triggers
2682 * alarms, so change to 0 instead. For high temperature limits, it
2683 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002684 * but is still confusing, so change to 127 degrees C.
2685 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002686 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002687 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002688 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002689 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002690 }
2691 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002692 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002693 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002694 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002695 }
2696
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002697 /*
2698 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002699 * set to two different sensor types and we can't guess which one
2700 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002701 * run-time through the temp{1-3}_type sysfs accessors if needed.
2702 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
2704 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002705 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if ((tmp & 0xff) == 0) {
2707 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002708 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
2710
2711 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002712 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002713 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002714 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002716 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002717 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2718 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002720 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002722 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2723
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002724 /* Set tachometers to 16-bit mode if needed */
2725 if (has_fan16_config(data)) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002726 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002727 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002728 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002729 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002730 tmp | 0x07);
2731 }
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002732 }
2733
2734 /* Check for additional fans */
2735 if (has_five_fans(data)) {
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002736 if (tmp & (1 << 4))
2737 data->has_fan |= (1 << 3); /* fan4 enabled */
2738 if (tmp & (1 << 5))
2739 data->has_fan |= (1 << 4); /* fan5 enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002740 if (has_six_fans(data) && (tmp & (1 << 2)))
2741 data->has_fan |= (1 << 5); /* fan6 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02002742 }
2743
Jean Delvare591ec652009-12-09 20:35:48 +01002744 /* Fan input pins may be used for alternative functions */
2745 data->has_fan &= ~sio_data->skip_fan;
2746
Guenter Roeck36c4d982015-03-26 18:18:19 -07002747 /* Check if pwm5, pwm6 are enabled */
2748 if (has_six_pwm(data)) {
2749 /* The following code may be IT8620E specific */
2750 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2751 if ((tmp & 0xc0) == 0xc0)
2752 sio_data->skip_pwm |= (1 << 4);
2753 if (!(tmp & (1 << 3)))
2754 sio_data->skip_pwm |= (1 << 5);
2755 }
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002758 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002759 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 | (update_vbat ? 0x41 : 0x01));
2761}
2762
Jean Delvareb99883d2010-03-05 22:17:15 +01002763static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2764{
Guenter Roeck36c4d982015-03-26 18:18:19 -07002765 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
Jean Delvare16b5dda2012-01-16 22:51:48 +01002766 if (has_newer_autopwm(data)) {
Guenter Roeck36c4d982015-03-26 18:18:19 -07002767 data->pwm_temp_map[nr] = (data->pwm_ctrl[nr] & 0x03) +
2768 nr < 3 ? 0 : 3;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002769 data->pwm_duty[nr] = it87_read_value(data,
Guenter Roeck36c4d982015-03-26 18:18:19 -07002770 IT87_REG_PWM_DUTY[nr]);
Jean Delvare6229cdb2010-12-08 16:27:22 +01002771 } else {
2772 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2773 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2774 else /* Manual mode */
2775 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2776 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002777
2778 if (has_old_autopwm(data)) {
2779 int i;
2780
2781 for (i = 0; i < 5 ; i++)
2782 data->auto_temp[nr][i] = it87_read_value(data,
2783 IT87_REG_AUTO_TEMP(nr, i));
2784 for (i = 0; i < 3 ; i++)
2785 data->auto_pwm[nr][i] = it87_read_value(data,
2786 IT87_REG_AUTO_PWM(nr, i));
2787 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002788}
2789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790static struct it87_data *it87_update_device(struct device *dev)
2791{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002792 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 int i;
2794
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002795 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2798 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002800 /*
2801 * Cleared after each update, so reenable. Value
2802 * returned by this read will be previous value
2803 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002804 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002805 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
2807 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002808 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002809 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002810 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002811 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002812 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002813 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 }
Jean Delvare3543a532006-08-28 14:27:25 +02002815 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002816 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Guenter Roeck73055402015-03-26 09:16:32 -07002817 if (has_avcc3(data))
2818 data->in[9][0] = it87_read_value(data, IT87_REG_AVCC3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002820 for (i = 0; i < 6; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002821 /* Skip disabled fans */
2822 if (!(data->has_fan & (1 << i)))
2823 continue;
2824
Guenter Roecke1169ba2012-12-19 22:17:01 +01002825 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002826 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002827 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002828 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002829 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002830 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002831 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002832 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002833 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002834 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
2837 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002838 if (!(data->has_temp & (1 << i)))
2839 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002840 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002841 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002842 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002843 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002844 data->temp[i][2] =
2845 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002846 if (has_temp_offset(data))
2847 data->temp[i][3] =
2848 it87_read_value(data,
2849 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
2851
Jean Delvare17d648b2006-08-28 14:23:46 +02002852 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002853 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002854 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002855 data->fan_div[0] = i & 0x07;
2856 data->fan_div[1] = (i >> 3) & 0x07;
2857 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002861 it87_read_value(data, IT87_REG_ALARM1) |
2862 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2863 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002864 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002865
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002866 data->fan_main_ctrl = it87_read_value(data,
2867 IT87_REG_FAN_MAIN_CTRL);
2868 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002869 for (i = 0; i < 6; i++)
Jean Delvareb99883d2010-03-05 22:17:15 +01002870 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002872 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002873 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002874 /*
2875 * The IT8705F does not have VID capability.
2876 * The IT8718F and later don't use IT87_REG_VID for the
2877 * same purpose.
2878 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002879 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002880 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002881 /*
2882 * The older IT8712F revisions had only 5 VID pins,
2883 * but we assume it is always safe to read 6 bits.
2884 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002885 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
2887 data->last_updated = jiffies;
2888 data->valid = 1;
2889 }
2890
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002891 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
2893 return data;
2894}
2895
Guenter Roecke84bd952015-03-28 08:24:29 -07002896static int __init it87_device_add(int index, unsigned short address,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002897 const struct it87_sio_data *sio_data)
2898{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002899 struct platform_device *pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002900 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002901 .start = address + IT87_EC_OFFSET,
2902 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002903 .name = DRVNAME,
2904 .flags = IORESOURCE_IO,
2905 };
2906 int err;
2907
Jean Delvareb9acb642009-01-07 16:37:35 +01002908 err = acpi_check_resource_conflict(&res);
2909 if (err)
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002910 return err;
Jean Delvareb9acb642009-01-07 16:37:35 +01002911
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002912 pdev = platform_device_alloc(DRVNAME, address);
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002913 if (!pdev)
2914 return -ENOMEM;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002915
2916 err = platform_device_add_resources(pdev, &res, 1);
2917 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002918 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002919 goto exit_device_put;
2920 }
2921
2922 err = platform_device_add_data(pdev, sio_data,
2923 sizeof(struct it87_sio_data));
2924 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002925 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002926 goto exit_device_put;
2927 }
2928
2929 err = platform_device_add(pdev);
2930 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002931 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002932 goto exit_device_put;
2933 }
2934
Guenter Roecke84bd952015-03-28 08:24:29 -07002935 it87_pdev[index] = pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002936 return 0;
2937
2938exit_device_put:
2939 platform_device_put(pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002940 return err;
2941}
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943static int __init sm_it87_init(void)
2944{
Guenter Roecke84bd952015-03-28 08:24:29 -07002945 int sioaddr[2] = { REG_2E, REG_4E };
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002946 struct it87_sio_data sio_data;
Guenter Roecke84bd952015-03-28 08:24:29 -07002947 unsigned short isa_address;
2948 bool found = false;
2949 int i, err;
Jean Delvarefde09502005-07-19 23:51:07 +02002950
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002951 err = platform_driver_register(&it87_driver);
2952 if (err)
2953 return err;
2954
Guenter Roecke84bd952015-03-28 08:24:29 -07002955 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
2956 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2957 isa_address = 0;
2958 err = it87_find(sioaddr[i], &isa_address, &sio_data);
2959 if (err || isa_address == 0)
2960 continue;
2961
2962 err = it87_device_add(i, isa_address, &sio_data);
2963 if (err)
2964 goto exit_dev_unregister;
2965 found = true;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002966 }
2967
Guenter Roecke84bd952015-03-28 08:24:29 -07002968 if (!found) {
2969 err = -ENODEV;
2970 goto exit_unregister;
2971 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002972 return 0;
Guenter Roecke84bd952015-03-28 08:24:29 -07002973
2974exit_dev_unregister:
2975 /* NULL check handled by platform_device_unregister */
2976 platform_device_unregister(it87_pdev[0]);
2977exit_unregister:
2978 platform_driver_unregister(&it87_driver);
2979 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
2982static void __exit sm_it87_exit(void)
2983{
Guenter Roecke84bd952015-03-28 08:24:29 -07002984 /* NULL check handled by platform_device_unregister */
2985 platform_device_unregister(it87_pdev[1]);
2986 platform_device_unregister(it87_pdev[0]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002987 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
2989
2990
Jean Delvare7c81c602014-01-29 20:40:08 +01002991MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002992MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993module_param(update_vbat, bool, 0);
2994MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2995module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002996MODULE_PARM_DESC(fix_pwm_polarity,
2997 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998MODULE_LICENSE("GPL");
2999
3000module_init(sm_it87_init);
3001module_exit(sm_it87_exit);