blob: 4b38ecb91959ec4a6dc3be3063367c2963da90c8 [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
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700606/*
607 * Must be called with data->update_lock held, except during initialization.
608 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
609 * would slow down the IT87 access and should not be necessary.
610 */
611static int it87_read_value(struct it87_data *data, u8 reg)
612{
613 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
614 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
615}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700617/*
618 * Must be called with data->update_lock held, except during initialization.
619 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
620 * would slow down the IT87 access and should not be necessary.
621 */
622static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
623{
624 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
625 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
626}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700628static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
629{
630 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
631 if (has_newer_autopwm(data)) {
632 data->pwm_temp_map[nr] = (data->pwm_ctrl[nr] & 0x03) +
633 nr < 3 ? 0 : 3;
634 data->pwm_duty[nr] = it87_read_value(data,
635 IT87_REG_PWM_DUTY[nr]);
636 } else {
637 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
638 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
639 else /* Manual mode */
640 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700643 if (has_old_autopwm(data)) {
644 int i;
645
646 for (i = 0; i < 5 ; i++)
647 data->auto_temp[nr][i] = it87_read_value(data,
648 IT87_REG_AUTO_TEMP(nr, i));
649 for (i = 0; i < 3 ; i++)
650 data->auto_pwm[nr][i] = it87_read_value(data,
651 IT87_REG_AUTO_PWM(nr, i));
652 }
653}
654
655static struct it87_data *it87_update_device(struct device *dev)
656{
657 struct it87_data *data = dev_get_drvdata(dev);
658 int i;
659
660 mutex_lock(&data->update_lock);
661
662 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
663 || !data->valid) {
664 if (update_vbat) {
665 /*
666 * Cleared after each update, so reenable. Value
667 * returned by this read will be previous value
668 */
669 it87_write_value(data, IT87_REG_CONFIG,
670 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
671 }
672 for (i = 0; i <= 7; i++) {
673 data->in[i][0] =
674 it87_read_value(data, IT87_REG_VIN(i));
675 data->in[i][1] =
676 it87_read_value(data, IT87_REG_VIN_MIN(i));
677 data->in[i][2] =
678 it87_read_value(data, IT87_REG_VIN_MAX(i));
679 }
680 /* in8 (battery) has no limit registers */
681 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
682 if (has_avcc3(data))
683 data->in[9][0] = it87_read_value(data, IT87_REG_AVCC3);
684
685 for (i = 0; i < 6; i++) {
686 /* Skip disabled fans */
687 if (!(data->has_fan & (1 << i)))
688 continue;
689
690 data->fan[i][1] =
691 it87_read_value(data, IT87_REG_FAN_MIN[i]);
692 data->fan[i][0] = it87_read_value(data,
693 IT87_REG_FAN[i]);
694 /* Add high byte if in 16-bit mode */
695 if (has_16bit_fans(data)) {
696 data->fan[i][0] |= it87_read_value(data,
697 IT87_REG_FANX[i]) << 8;
698 data->fan[i][1] |= it87_read_value(data,
699 IT87_REG_FANX_MIN[i]) << 8;
700 }
701 }
702 for (i = 0; i < 3; i++) {
703 if (!(data->has_temp & (1 << i)))
704 continue;
705 data->temp[i][0] =
706 it87_read_value(data, IT87_REG_TEMP(i));
707 data->temp[i][1] =
708 it87_read_value(data, IT87_REG_TEMP_LOW(i));
709 data->temp[i][2] =
710 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
711 if (has_temp_offset(data))
712 data->temp[i][3] =
713 it87_read_value(data,
714 IT87_REG_TEMP_OFFSET[i]);
715 }
716
717 /* Newer chips don't have clock dividers */
718 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
719 i = it87_read_value(data, IT87_REG_FAN_DIV);
720 data->fan_div[0] = i & 0x07;
721 data->fan_div[1] = (i >> 3) & 0x07;
722 data->fan_div[2] = (i & 0x40) ? 3 : 1;
723 }
724
725 data->alarms =
726 it87_read_value(data, IT87_REG_ALARM1) |
727 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
728 (it87_read_value(data, IT87_REG_ALARM3) << 16);
729 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
730
731 data->fan_main_ctrl = it87_read_value(data,
732 IT87_REG_FAN_MAIN_CTRL);
733 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
734 for (i = 0; i < 6; i++)
735 it87_update_pwm_ctrl(data, i);
736
737 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
738 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
739 /*
740 * The IT8705F does not have VID capability.
741 * The IT8718F and later don't use IT87_REG_VID for the
742 * same purpose.
743 */
744 if (data->type == it8712 || data->type == it8716) {
745 data->vid = it87_read_value(data, IT87_REG_VID);
746 /*
747 * The older IT8712F revisions had only 5 VID pins,
748 * but we assume it is always safe to read 6 bits.
749 */
750 data->vid &= 0x3f;
751 }
752 data->last_updated = jiffies;
753 data->valid = 1;
754 }
755
756 mutex_unlock(&data->update_lock);
757
758 return data;
759}
Jean Delvarefde09502005-07-19 23:51:07 +0200760
Jean Delvare20ad93d2005-06-05 11:53:25 +0200761static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100762 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100764 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
765 int nr = sattr->nr;
766 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100769 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Guenter Roeck929c6a52012-12-19 22:17:00 +0100772static ssize_t set_in(struct device *dev, struct device_attribute *attr,
773 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100775 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
776 int nr = sattr->nr;
777 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200778
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200779 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100780 unsigned long val;
781
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100782 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100783 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100785 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100786 data->in[nr][index] = in_to_reg(data, nr, val);
787 it87_write_value(data,
788 index == 1 ? IT87_REG_VIN_MIN(nr)
789 : IT87_REG_VIN_MAX(nr),
790 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100791 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return count;
793}
794
Guenter Roeck929c6a52012-12-19 22:17:00 +0100795static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
796static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
797 0, 1);
798static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
799 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Guenter Roeck929c6a52012-12-19 22:17:00 +0100801static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
802static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
803 1, 1);
804static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
805 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Guenter Roeck929c6a52012-12-19 22:17:00 +0100807static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
808static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
809 2, 1);
810static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
811 2, 2);
812
813static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
814static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
815 3, 1);
816static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
817 3, 2);
818
819static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
820static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
821 4, 1);
822static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
823 4, 2);
824
825static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
826static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
827 5, 1);
828static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
829 5, 2);
830
831static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
832static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
833 6, 1);
834static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
835 6, 2);
836
837static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
838static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
839 7, 1);
840static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
841 7, 2);
842
843static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100844static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200847static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100848 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100850 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
851 int nr = sattr->nr;
852 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200854
Guenter Roeck60ca3852012-12-19 22:17:00 +0100855 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200857
Guenter Roeck60ca3852012-12-19 22:17:00 +0100858static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
859 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100861 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
862 int nr = sattr->nr;
863 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200864 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100865 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100866 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100867
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100868 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100869 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100871 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100872
873 switch (index) {
874 default:
875 case 1:
876 reg = IT87_REG_TEMP_LOW(nr);
877 break;
878 case 2:
879 reg = IT87_REG_TEMP_HIGH(nr);
880 break;
881 case 3:
882 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
883 if (!(regval & 0x80)) {
884 regval |= 0x80;
885 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
886 }
887 data->valid = 0;
888 reg = IT87_REG_TEMP_OFFSET[nr];
889 break;
890 }
891
Guenter Roeck60ca3852012-12-19 22:17:00 +0100892 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100893 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100894 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return count;
896}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200897
Guenter Roeck60ca3852012-12-19 22:17:00 +0100898static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
899static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
900 0, 1);
901static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
902 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100903static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
904 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100905static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
906static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
907 1, 1);
908static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
909 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100910static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
911 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100912static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
913static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
914 2, 1);
915static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
916 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100917static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
918 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Guenter Roeck2cece012012-12-19 22:17:01 +0100920static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
921 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200923 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
924 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800926 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100927 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100928
Guenter Roeck19529782012-12-19 22:17:02 +0100929 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
930 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100931 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (reg & (1 << nr))
933 return sprintf(buf, "3\n"); /* thermal diode */
934 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200935 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return sprintf(buf, "0\n"); /* disabled */
937}
Guenter Roeck2cece012012-12-19 22:17:01 +0100938
939static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
940 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200942 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
943 int nr = sensor_attr->index;
944
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200945 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100946 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100947 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100948
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100949 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100950 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Jean Delvare8acf07c2010-04-14 16:14:09 +0200952 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
953 reg &= ~(1 << nr);
954 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100955 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
956 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100957 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
958 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
959 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200960 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100961 dev_warn(dev,
962 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200963 val = 4;
964 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100965 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200967 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200968 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200969 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100970 else if (has_temp_peci(data, nr) && val == 6)
971 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100972 else if (has_temp_old_peci(data, nr) && val == 6)
973 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200974 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200976
977 mutex_lock(&data->update_lock);
978 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100979 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200980 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100981 if (has_temp_old_peci(data, nr))
982 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200983 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100984 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return count;
986}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Guenter Roeck2cece012012-12-19 22:17:01 +0100988static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
989 set_temp_type, 0);
990static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
991 set_temp_type, 1);
992static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
993 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100996
997static int pwm_mode(const struct it87_data *data, int nr)
998{
999 int ctrl = data->fan_main_ctrl & (1 << nr);
1000
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001001 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +01001002 return 0;
1003 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
1004 return 2;
1005 else /* Manual mode */
1006 return 1;
1007}
1008
Jean Delvare20ad93d2005-06-05 11:53:25 +02001009static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +01001010 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001012 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1013 int nr = sattr->nr;
1014 int index = sattr->index;
1015 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001017
Guenter Roecke1169ba2012-12-19 22:17:01 +01001018 speed = has_16bit_fans(data) ?
1019 FAN16_FROM_REG(data->fan[nr][index]) :
1020 FAN_FROM_REG(data->fan[nr][index],
1021 DIV_FROM_REG(data->fan_div[nr]));
1022 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001024
Jean Delvare20ad93d2005-06-05 11:53:25 +02001025static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1026 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001028 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1029 int nr = sensor_attr->index;
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct it87_data *data = it87_update_device(dev);
1032 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
1033}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001034static ssize_t show_pwm_enable(struct device *dev,
1035 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001037 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1038 int nr = sensor_attr->index;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +01001041 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001043static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1044 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001046 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1047 int nr = sensor_attr->index;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001050 return sprintf(buf, "%d\n",
1051 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001053static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1054 char *buf)
1055{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001056 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001057 struct it87_data *data = it87_update_device(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001058 int nr = sensor_attr->index;
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001059 unsigned int freq;
Guenter Roeck60878bc2015-03-26 19:57:42 -07001060 int index;
1061
1062 if (has_pwm_freq2(data) && nr == 1)
1063 index = (data->extra >> 4) & 0x07;
1064 else
1065 index = (data->fan_ctl >> 4) & 0x07;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001066
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001067 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1068
1069 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001070}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001071
1072static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1073 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001075 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1076 int nr = sattr->nr;
1077 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +02001078
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001079 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001080 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +01001081 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001083 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001084 return -EINVAL;
1085
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001086 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +01001087
1088 if (has_16bit_fans(data)) {
1089 data->fan[nr][index] = FAN16_TO_REG(val);
1090 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1091 data->fan[nr][index] & 0xff);
1092 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1093 data->fan[nr][index] >> 8);
1094 } else {
1095 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1096 switch (nr) {
1097 case 0:
1098 data->fan_div[nr] = reg & 0x07;
1099 break;
1100 case 1:
1101 data->fan_div[nr] = (reg >> 3) & 0x07;
1102 break;
1103 case 2:
1104 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1105 break;
1106 }
1107 data->fan[nr][index] =
1108 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1109 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1110 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -08001111 }
1112
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001113 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return count;
1115}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001116
Jean Delvare20ad93d2005-06-05 11:53:25 +02001117static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1118 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001120 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1121 int nr = sensor_attr->index;
1122
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001123 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001124 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001125 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 u8 old;
1127
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001128 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001129 return -EINVAL;
1130
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001131 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001132 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001134 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001135 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 switch (nr) {
1138 case 0:
1139 case 1:
1140 data->fan_div[nr] = DIV_TO_REG(val);
1141 break;
1142 case 2:
1143 if (val < 8)
1144 data->fan_div[nr] = 1;
1145 else
1146 data->fan_div[nr] = 3;
1147 }
1148 val = old & 0x80;
1149 val |= (data->fan_div[0] & 0x07);
1150 val |= (data->fan_div[1] & 0x07) << 3;
1151 if (data->fan_div[2] == 3)
1152 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001153 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001155 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001156 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1157 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001158
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001159 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return count;
1161}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001162
1163/* Returns 0 if OK, -EINVAL otherwise */
1164static int check_trip_points(struct device *dev, int nr)
1165{
1166 const struct it87_data *data = dev_get_drvdata(dev);
1167 int i, err = 0;
1168
1169 if (has_old_autopwm(data)) {
1170 for (i = 0; i < 3; i++) {
1171 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1172 err = -EINVAL;
1173 }
1174 for (i = 0; i < 2; i++) {
1175 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1176 err = -EINVAL;
1177 }
1178 }
1179
1180 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001181 dev_err(dev,
1182 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001183 dev_err(dev, "Adjust the trip points and try again\n");
1184 }
1185 return err;
1186}
1187
Jean Delvare20ad93d2005-06-05 11:53:25 +02001188static ssize_t set_pwm_enable(struct device *dev,
1189 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001191 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1192 int nr = sensor_attr->index;
1193
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001194 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001195 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001197 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001198 return -EINVAL;
1199
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001200 /* Check trip points before switching to automatic mode */
1201 if (val == 2) {
1202 if (check_trip_points(dev, nr) < 0)
1203 return -EINVAL;
1204 }
1205
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001206 /* IT8603E does not have on/off mode */
1207 if (val == 0 && data->type == it8603)
1208 return -EINVAL;
1209
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001210 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (val == 0) {
1213 int tmp;
1214 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001215 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1216 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /* set on/off mode */
1218 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +01001219 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1220 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +01001221 } else {
1222 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001223 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001224 data->pwm_temp_map[nr] :
1225 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001226 else /* Automatic mode */
1227 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001228 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001229
1230 if (data->type != it8603) {
1231 /* set SmartGuardian mode */
1232 data->fan_main_ctrl |= (1 << nr);
1233 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1234 data->fan_main_ctrl);
1235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001238 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return count;
1240}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001241static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1242 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001244 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1245 int nr = sensor_attr->index;
1246
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001247 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001248 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001250 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return -EINVAL;
1252
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001253 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001254 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001255 /*
1256 * If we are in automatic mode, the PWM duty cycle register
1257 * is read-only so we can't write the value.
1258 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001259 if (data->pwm_ctrl[nr] & 0x80) {
1260 mutex_unlock(&data->update_lock);
1261 return -EBUSY;
1262 }
1263 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001264 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001265 data->pwm_duty[nr]);
1266 } else {
1267 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001268 /*
1269 * If we are in manual mode, write the duty cycle immediately;
1270 * otherwise, just store it for later use.
1271 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001272 if (!(data->pwm_ctrl[nr] & 0x80)) {
1273 data->pwm_ctrl[nr] = data->pwm_duty[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001274 it87_write_value(data, IT87_REG_PWM[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001275 data->pwm_ctrl[nr]);
1276 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001277 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001278 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return count;
1280}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001281static ssize_t set_pwm_freq(struct device *dev,
1282 struct device_attribute *attr, const char *buf, size_t count)
1283{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001284 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001285 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001286 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001287 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001288 int i;
1289
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001290 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001291 return -EINVAL;
1292
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001293 val = clamp_val(val, 0, 1000000);
1294 val *= has_newer_autopwm(data) ? 256 : 128;
1295
Jean Delvaref8d0c192007-02-14 21:15:02 +01001296 /* Search for the nearest available frequency */
1297 for (i = 0; i < 7; i++) {
1298 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1299 break;
1300 }
1301
1302 mutex_lock(&data->update_lock);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001303 if (nr == 0) {
1304 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1305 data->fan_ctl |= i << 4;
1306 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1307 } else {
1308 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1309 data->extra |= i << 4;
1310 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1311 }
Jean Delvaref8d0c192007-02-14 21:15:02 +01001312 mutex_unlock(&data->update_lock);
1313
1314 return count;
1315}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001316static ssize_t show_pwm_temp_map(struct device *dev,
1317 struct device_attribute *attr, char *buf)
1318{
1319 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1320 int nr = sensor_attr->index;
1321
1322 struct it87_data *data = it87_update_device(dev);
1323 int map;
1324
1325 if (data->pwm_temp_map[nr] < 3)
1326 map = 1 << data->pwm_temp_map[nr];
1327 else
1328 map = 0; /* Should never happen */
1329 return sprintf(buf, "%d\n", map);
1330}
1331static ssize_t set_pwm_temp_map(struct device *dev,
1332 struct device_attribute *attr, const char *buf, size_t count)
1333{
1334 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1335 int nr = sensor_attr->index;
1336
1337 struct it87_data *data = dev_get_drvdata(dev);
1338 long val;
1339 u8 reg;
1340
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001341 /*
1342 * This check can go away if we ever support automatic fan speed
1343 * control on newer chips.
1344 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001345 if (!has_old_autopwm(data)) {
1346 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1347 return -EINVAL;
1348 }
1349
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001350 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001351 return -EINVAL;
1352
1353 switch (val) {
1354 case (1 << 0):
1355 reg = 0x00;
1356 break;
1357 case (1 << 1):
1358 reg = 0x01;
1359 break;
1360 case (1 << 2):
1361 reg = 0x02;
1362 break;
1363 default:
1364 return -EINVAL;
1365 }
1366
1367 mutex_lock(&data->update_lock);
1368 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001369 /*
1370 * If we are in automatic mode, write the temp mapping immediately;
1371 * otherwise, just store it for later use.
1372 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001373 if (data->pwm_ctrl[nr] & 0x80) {
1374 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001375 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001376 }
1377 mutex_unlock(&data->update_lock);
1378 return count;
1379}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001381static ssize_t show_auto_pwm(struct device *dev,
1382 struct device_attribute *attr, char *buf)
1383{
1384 struct it87_data *data = it87_update_device(dev);
1385 struct sensor_device_attribute_2 *sensor_attr =
1386 to_sensor_dev_attr_2(attr);
1387 int nr = sensor_attr->nr;
1388 int point = sensor_attr->index;
1389
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001390 return sprintf(buf, "%d\n",
1391 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001392}
1393
1394static ssize_t set_auto_pwm(struct device *dev,
1395 struct device_attribute *attr, const char *buf, size_t count)
1396{
1397 struct it87_data *data = dev_get_drvdata(dev);
1398 struct sensor_device_attribute_2 *sensor_attr =
1399 to_sensor_dev_attr_2(attr);
1400 int nr = sensor_attr->nr;
1401 int point = sensor_attr->index;
1402 long val;
1403
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001404 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001405 return -EINVAL;
1406
1407 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001408 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001409 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1410 data->auto_pwm[nr][point]);
1411 mutex_unlock(&data->update_lock);
1412 return count;
1413}
1414
1415static ssize_t show_auto_temp(struct device *dev,
1416 struct device_attribute *attr, char *buf)
1417{
1418 struct it87_data *data = it87_update_device(dev);
1419 struct sensor_device_attribute_2 *sensor_attr =
1420 to_sensor_dev_attr_2(attr);
1421 int nr = sensor_attr->nr;
1422 int point = sensor_attr->index;
1423
1424 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1425}
1426
1427static ssize_t set_auto_temp(struct device *dev,
1428 struct device_attribute *attr, const char *buf, size_t count)
1429{
1430 struct it87_data *data = dev_get_drvdata(dev);
1431 struct sensor_device_attribute_2 *sensor_attr =
1432 to_sensor_dev_attr_2(attr);
1433 int nr = sensor_attr->nr;
1434 int point = sensor_attr->index;
1435 long val;
1436
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001437 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001438 return -EINVAL;
1439
1440 mutex_lock(&data->update_lock);
1441 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1442 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1443 data->auto_temp[nr][point]);
1444 mutex_unlock(&data->update_lock);
1445 return count;
1446}
1447
Guenter Roecke1169ba2012-12-19 22:17:01 +01001448static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1449static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1450 0, 1);
1451static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1452 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Guenter Roecke1169ba2012-12-19 22:17:01 +01001454static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1455static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1456 1, 1);
1457static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1458 set_fan_div, 1);
1459
1460static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1461static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1462 2, 1);
1463static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1464 set_fan_div, 2);
1465
1466static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1467static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1468 3, 1);
1469
1470static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1471static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1472 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001474static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1475static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1476 5, 1);
1477
Guenter Roeckc4458db2012-12-19 22:17:02 +01001478static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1479 show_pwm_enable, set_pwm_enable, 0);
1480static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001481static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1482 set_pwm_freq, 0);
Guenter Roeckc4458db2012-12-19 22:17:02 +01001483static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1484 show_pwm_temp_map, set_pwm_temp_map, 0);
1485static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1486 show_auto_pwm, set_auto_pwm, 0, 0);
1487static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1488 show_auto_pwm, set_auto_pwm, 0, 1);
1489static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1490 show_auto_pwm, set_auto_pwm, 0, 2);
1491static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1492 show_auto_pwm, NULL, 0, 3);
1493static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1494 show_auto_temp, set_auto_temp, 0, 1);
1495static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1496 show_auto_temp, set_auto_temp, 0, 0);
1497static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1498 show_auto_temp, set_auto_temp, 0, 2);
1499static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1500 show_auto_temp, set_auto_temp, 0, 3);
1501static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1502 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Guenter Roeckc4458db2012-12-19 22:17:02 +01001504static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1505 show_pwm_enable, set_pwm_enable, 1);
1506static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001507static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
Guenter Roeckc4458db2012-12-19 22:17:02 +01001508static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1509 show_pwm_temp_map, set_pwm_temp_map, 1);
1510static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1511 show_auto_pwm, set_auto_pwm, 1, 0);
1512static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1513 show_auto_pwm, set_auto_pwm, 1, 1);
1514static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1515 show_auto_pwm, set_auto_pwm, 1, 2);
1516static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1517 show_auto_pwm, NULL, 1, 3);
1518static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1519 show_auto_temp, set_auto_temp, 1, 1);
1520static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1521 show_auto_temp, set_auto_temp, 1, 0);
1522static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1523 show_auto_temp, set_auto_temp, 1, 2);
1524static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1525 show_auto_temp, set_auto_temp, 1, 3);
1526static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1527 show_auto_temp, set_auto_temp, 1, 4);
1528
1529static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1530 show_pwm_enable, set_pwm_enable, 2);
1531static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001532static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
Guenter Roeckc4458db2012-12-19 22:17:02 +01001533static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1534 show_pwm_temp_map, set_pwm_temp_map, 2);
1535static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1536 show_auto_pwm, set_auto_pwm, 2, 0);
1537static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1538 show_auto_pwm, set_auto_pwm, 2, 1);
1539static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1540 show_auto_pwm, set_auto_pwm, 2, 2);
1541static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1542 show_auto_pwm, NULL, 2, 3);
1543static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1544 show_auto_temp, set_auto_temp, 2, 1);
1545static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1546 show_auto_temp, set_auto_temp, 2, 0);
1547static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1548 show_auto_temp, set_auto_temp, 2, 2);
1549static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1550 show_auto_temp, set_auto_temp, 2, 3);
1551static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1552 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Guenter Roeck36c4d982015-03-26 18:18:19 -07001554static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1555 show_pwm_enable, set_pwm_enable, 3);
1556static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001557static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001558static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO | S_IWUSR,
1559 show_pwm_temp_map, set_pwm_temp_map, 3);
1560
1561static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1562 show_pwm_enable, set_pwm_enable, 4);
1563static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001564static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001565static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO | S_IWUSR,
1566 show_pwm_temp_map, set_pwm_temp_map, 4);
1567
1568static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1569 show_pwm_enable, set_pwm_enable, 5);
1570static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001571static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001572static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO | S_IWUSR,
1573 show_pwm_temp_map, set_pwm_temp_map, 5);
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001576static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1577 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001580 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
Jean Delvare1d66c642005-04-18 21:16:59 -07001582static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Jean Delvare0124dd72007-11-25 16:16:41 +01001584static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1585 char *buf)
1586{
1587 int bitnr = to_sensor_dev_attr(attr)->index;
1588 struct it87_data *data = it87_update_device(dev);
1589 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1590}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001591
1592static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1593 *attr, const char *buf, size_t count)
1594{
1595 struct it87_data *data = dev_get_drvdata(dev);
1596 long val;
1597 int config;
1598
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001599 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001600 return -EINVAL;
1601
1602 mutex_lock(&data->update_lock);
1603 config = it87_read_value(data, IT87_REG_CONFIG);
1604 if (config < 0) {
1605 count = config;
1606 } else {
1607 config |= 1 << 5;
1608 it87_write_value(data, IT87_REG_CONFIG, config);
1609 /* Invalidate cache to force re-read */
1610 data->valid = 0;
1611 }
1612 mutex_unlock(&data->update_lock);
1613
1614 return count;
1615}
1616
Jean Delvare0124dd72007-11-25 16:16:41 +01001617static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1618static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1619static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1620static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1621static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1622static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1623static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1624static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1625static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1626static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1627static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1628static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1629static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001630static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001631static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1632static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1633static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001634static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1635 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001636
Jean Delvared9b327c2010-03-05 22:17:17 +01001637static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1638 char *buf)
1639{
1640 int bitnr = to_sensor_dev_attr(attr)->index;
1641 struct it87_data *data = it87_update_device(dev);
1642 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1643}
1644static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1645 const char *buf, size_t count)
1646{
1647 int bitnr = to_sensor_dev_attr(attr)->index;
1648 struct it87_data *data = dev_get_drvdata(dev);
1649 long val;
1650
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001651 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001652 || (val != 0 && val != 1))
1653 return -EINVAL;
1654
1655 mutex_lock(&data->update_lock);
1656 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1657 if (val)
1658 data->beeps |= (1 << bitnr);
1659 else
1660 data->beeps &= ~(1 << bitnr);
1661 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1662 mutex_unlock(&data->update_lock);
1663 return count;
1664}
1665
1666static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1667 show_beep, set_beep, 1);
1668static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1669static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1670static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1671static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1672static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1673static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1674static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1675/* fanX_beep writability is set later */
1676static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1677static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1678static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1679static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1680static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001681static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001682static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1683 show_beep, set_beep, 2);
1684static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1685static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1686
Jean Delvare5f2dc792010-03-05 22:17:18 +01001687static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1688 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Jean Delvare90d66192007-10-08 18:24:35 +02001690 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001691 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001693static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1694 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001696 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001697 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001699 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001700 return -EINVAL;
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 data->vrm = val;
1703
1704 return count;
1705}
1706static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Jean Delvare5f2dc792010-03-05 22:17:18 +01001708static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1709 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 struct it87_data *data = it87_update_device(dev);
1712 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1713}
1714static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001715
Jean Delvare738e5e02010-08-14 21:08:50 +02001716static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1717 char *buf)
1718{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001719 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001720 "+5V",
1721 "5VSB",
1722 "Vbat",
1723 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001724 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001725 "+3.3V",
1726 "3VSB",
1727 "Vbat",
1728 };
1729 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001730 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001731 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001732
Justin Maggardead80802015-08-05 12:53:08 -07001733 if (has_12mv_adc(data) || has_10_9mv_adc(data))
1734 label = labels_it8721[nr];
1735 else
1736 label = labels[nr];
1737
1738 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001739}
1740static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1741static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1742static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001743/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001744static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001745
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001746static ssize_t show_name(struct device *dev, struct device_attribute
1747 *devattr, char *buf)
1748{
1749 struct it87_data *data = dev_get_drvdata(dev);
1750 return sprintf(buf, "%s\n", data->name);
1751}
1752static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1753
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001754static struct attribute *it87_attributes_in[10][5] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001755{
Jean Delvare87808be2006-09-24 21:17:13 +02001756 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001757 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001758 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001759 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001760 NULL
1761}, {
1762 &sensor_dev_attr_in1_input.dev_attr.attr,
1763 &sensor_dev_attr_in1_min.dev_attr.attr,
1764 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001765 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001766 NULL
1767}, {
1768 &sensor_dev_attr_in2_input.dev_attr.attr,
1769 &sensor_dev_attr_in2_min.dev_attr.attr,
1770 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001771 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001772 NULL
1773}, {
1774 &sensor_dev_attr_in3_input.dev_attr.attr,
1775 &sensor_dev_attr_in3_min.dev_attr.attr,
1776 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001777 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001778 NULL
1779}, {
1780 &sensor_dev_attr_in4_input.dev_attr.attr,
1781 &sensor_dev_attr_in4_min.dev_attr.attr,
1782 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001783 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001784 NULL
1785}, {
1786 &sensor_dev_attr_in5_input.dev_attr.attr,
1787 &sensor_dev_attr_in5_min.dev_attr.attr,
1788 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001789 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001790 NULL
1791}, {
1792 &sensor_dev_attr_in6_input.dev_attr.attr,
1793 &sensor_dev_attr_in6_min.dev_attr.attr,
1794 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001795 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001796 NULL
1797}, {
1798 &sensor_dev_attr_in7_input.dev_attr.attr,
1799 &sensor_dev_attr_in7_min.dev_attr.attr,
1800 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001801 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001802 NULL
1803}, {
1804 &sensor_dev_attr_in8_input.dev_attr.attr,
1805 NULL
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001806}, {
1807 &sensor_dev_attr_in9_input.dev_attr.attr,
1808 NULL
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001809} };
Jean Delvare87808be2006-09-24 21:17:13 +02001810
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001811static const struct attribute_group it87_group_in[10] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001812 { .attrs = it87_attributes_in[0] },
1813 { .attrs = it87_attributes_in[1] },
1814 { .attrs = it87_attributes_in[2] },
1815 { .attrs = it87_attributes_in[3] },
1816 { .attrs = it87_attributes_in[4] },
1817 { .attrs = it87_attributes_in[5] },
1818 { .attrs = it87_attributes_in[6] },
1819 { .attrs = it87_attributes_in[7] },
1820 { .attrs = it87_attributes_in[8] },
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001821 { .attrs = it87_attributes_in[9] },
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001822};
1823
Guenter Roeck4573acb2012-03-26 16:17:41 -07001824static struct attribute *it87_attributes_temp[3][6] = {
1825{
Jean Delvare87808be2006-09-24 21:17:13 +02001826 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001827 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001828 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001829 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001830 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001831 NULL
1832} , {
1833 &sensor_dev_attr_temp2_input.dev_attr.attr,
1834 &sensor_dev_attr_temp2_max.dev_attr.attr,
1835 &sensor_dev_attr_temp2_min.dev_attr.attr,
1836 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001837 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001838 NULL
1839} , {
1840 &sensor_dev_attr_temp3_input.dev_attr.attr,
1841 &sensor_dev_attr_temp3_max.dev_attr.attr,
1842 &sensor_dev_attr_temp3_min.dev_attr.attr,
1843 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001844 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001845 NULL
1846} };
Jean Delvare87808be2006-09-24 21:17:13 +02001847
Guenter Roeck4573acb2012-03-26 16:17:41 -07001848static const struct attribute_group it87_group_temp[3] = {
1849 { .attrs = it87_attributes_temp[0] },
1850 { .attrs = it87_attributes_temp[1] },
1851 { .attrs = it87_attributes_temp[2] },
1852};
1853
Guenter Roeck161d8982012-12-19 22:17:01 +01001854static struct attribute *it87_attributes_temp_offset[] = {
1855 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1856 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1857 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1858};
1859
Guenter Roeck4573acb2012-03-26 16:17:41 -07001860static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001861 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001862 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001863 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001864 NULL
1865};
1866
1867static const struct attribute_group it87_group = {
1868 .attrs = it87_attributes,
1869};
1870
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001871static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001872 &sensor_dev_attr_in0_beep.dev_attr.attr,
1873 &sensor_dev_attr_in1_beep.dev_attr.attr,
1874 &sensor_dev_attr_in2_beep.dev_attr.attr,
1875 &sensor_dev_attr_in3_beep.dev_attr.attr,
1876 &sensor_dev_attr_in4_beep.dev_attr.attr,
1877 &sensor_dev_attr_in5_beep.dev_attr.attr,
1878 &sensor_dev_attr_in6_beep.dev_attr.attr,
1879 &sensor_dev_attr_in7_beep.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001880 NULL,
1881 NULL,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001882};
Jean Delvared9b327c2010-03-05 22:17:17 +01001883
Guenter Roeck4573acb2012-03-26 16:17:41 -07001884static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001885 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1886 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1887 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001888};
1889
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001890static struct attribute *it87_attributes_fan[6][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001891 &sensor_dev_attr_fan1_input.dev_attr.attr,
1892 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001893 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1894 NULL
1895}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001896 &sensor_dev_attr_fan2_input.dev_attr.attr,
1897 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001898 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1899 NULL
1900}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001901 &sensor_dev_attr_fan3_input.dev_attr.attr,
1902 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001903 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001904 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001905}, {
1906 &sensor_dev_attr_fan4_input.dev_attr.attr,
1907 &sensor_dev_attr_fan4_min.dev_attr.attr,
1908 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1909 NULL
1910}, {
1911 &sensor_dev_attr_fan5_input.dev_attr.attr,
1912 &sensor_dev_attr_fan5_min.dev_attr.attr,
1913 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1914 NULL
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001915}, {
1916 &sensor_dev_attr_fan6_input.dev_attr.attr,
1917 &sensor_dev_attr_fan6_min.dev_attr.attr,
1918 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1919 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001920} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001921
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001922static const struct attribute_group it87_group_fan[6] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001923 { .attrs = it87_attributes_fan[0] },
1924 { .attrs = it87_attributes_fan[1] },
1925 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001926 { .attrs = it87_attributes_fan[3] },
1927 { .attrs = it87_attributes_fan[4] },
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001928 { .attrs = it87_attributes_fan[5] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001929};
1930
Guenter Roecke1169ba2012-12-19 22:17:01 +01001931static const struct attribute *it87_attributes_fan_div[] = {
1932 &sensor_dev_attr_fan1_div.dev_attr.attr,
1933 &sensor_dev_attr_fan2_div.dev_attr.attr,
1934 &sensor_dev_attr_fan3_div.dev_attr.attr,
1935};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001936
Guenter Roeck36c4d982015-03-26 18:18:19 -07001937static struct attribute *it87_attributes_pwm[6][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001938 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001939 &sensor_dev_attr_pwm1.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001940 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001941 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001942 NULL
1943}, {
1944 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1945 &sensor_dev_attr_pwm2.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001946 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001947 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001948 NULL
1949}, {
1950 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1951 &sensor_dev_attr_pwm3.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001952 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001953 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001954 NULL
Guenter Roeck36c4d982015-03-26 18:18:19 -07001955}, {
1956 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1957 &sensor_dev_attr_pwm4.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001958 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001959 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1960 NULL
1961}, {
1962 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1963 &sensor_dev_attr_pwm5.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001964 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001965 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
1966 NULL
1967}, {
1968 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
1969 &sensor_dev_attr_pwm6.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001970 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001971 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
1972 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001973} };
Jean Delvare87808be2006-09-24 21:17:13 +02001974
Guenter Roeck60878bc2015-03-26 19:57:42 -07001975static umode_t pwm_attribute_mode(struct kobject *kobj, struct attribute *attr,
1976 int index)
1977{
1978 struct device *dev = container_of(kobj, struct device, kobj);
1979 struct it87_data *data = dev_get_drvdata(dev);
1980
1981 if (has_pwm_freq2(data) && index == 2)
1982 return attr->mode | S_IWUSR;
1983
1984 return attr->mode;
1985}
1986
Guenter Roeck36c4d982015-03-26 18:18:19 -07001987static const struct attribute_group it87_group_pwm[6] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001988 { .attrs = it87_attributes_pwm[0] },
Guenter Roeck60878bc2015-03-26 19:57:42 -07001989 { .attrs = it87_attributes_pwm[1],
1990 .is_visible = pwm_attribute_mode, },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001991 { .attrs = it87_attributes_pwm[2] },
Guenter Roeck36c4d982015-03-26 18:18:19 -07001992 { .attrs = it87_attributes_pwm[3] },
1993 { .attrs = it87_attributes_pwm[4] },
1994 { .attrs = it87_attributes_pwm[5] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001995};
1996
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001997static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1998 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1999 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2000 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2001 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2002 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2003 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2004 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2005 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2006 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2007 NULL
2008}, {
2009 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
2010 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2011 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2012 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2013 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2014 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2015 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2016 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2017 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2018 NULL
2019}, {
2020 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
2021 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2022 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2023 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2024 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2025 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2026 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2027 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2028 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
2029 NULL
2030} };
2031
2032static const struct attribute_group it87_group_autopwm[3] = {
2033 { .attrs = it87_attributes_autopwm[0] },
2034 { .attrs = it87_attributes_autopwm[1] },
2035 { .attrs = it87_attributes_autopwm[2] },
2036};
2037
Jean Delvared9b327c2010-03-05 22:17:17 +01002038static struct attribute *it87_attributes_fan_beep[] = {
2039 &sensor_dev_attr_fan1_beep.dev_attr.attr,
2040 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2041 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2042 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2043 &sensor_dev_attr_fan5_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002044 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01002045};
2046
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002047static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02002048 &dev_attr_vrm.attr,
2049 &dev_attr_cpu0_vid.attr,
2050 NULL
2051};
2052
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002053static const struct attribute_group it87_group_vid = {
2054 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02002055};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Jean Delvare738e5e02010-08-14 21:08:50 +02002057static struct attribute *it87_attributes_label[] = {
2058 &sensor_dev_attr_in3_label.dev_attr.attr,
2059 &sensor_dev_attr_in7_label.dev_attr.attr,
2060 &sensor_dev_attr_in8_label.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002061 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare738e5e02010-08-14 21:08:50 +02002062 NULL
2063};
2064
2065static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02002066 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02002067};
2068
Jean Delvare2d8672c2005-07-19 23:56:35 +02002069/* SuperIO detection - will change isa_address if a chip is found */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002070static int __init it87_find(int sioaddr, unsigned short *address,
2071 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002073 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002074 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002075 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002076 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002078 err = superio_enter(sioaddr);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002079 if (err)
2080 return err;
2081
2082 err = -ENODEV;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002083 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002084
2085 switch (chip_type) {
2086 case IT8705F_DEVID:
2087 sio_data->type = it87;
2088 break;
2089 case IT8712F_DEVID:
2090 sio_data->type = it8712;
2091 break;
2092 case IT8716F_DEVID:
2093 case IT8726F_DEVID:
2094 sio_data->type = it8716;
2095 break;
2096 case IT8718F_DEVID:
2097 sio_data->type = it8718;
2098 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002099 case IT8720F_DEVID:
2100 sio_data->type = it8720;
2101 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002102 case IT8721F_DEVID:
2103 sio_data->type = it8721;
2104 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01002105 case IT8728F_DEVID:
2106 sio_data->type = it8728;
2107 break;
Justin Maggardead80802015-08-05 12:53:08 -07002108 case IT8732F_DEVID:
2109 sio_data->type = it8732;
2110 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06002111 case IT8771E_DEVID:
2112 sio_data->type = it8771;
2113 break;
2114 case IT8772E_DEVID:
2115 sio_data->type = it8772;
2116 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002117 case IT8781F_DEVID:
2118 sio_data->type = it8781;
2119 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08002120 case IT8782F_DEVID:
2121 sio_data->type = it8782;
2122 break;
2123 case IT8783E_DEVID:
2124 sio_data->type = it8783;
2125 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002126 case IT8786E_DEVID:
2127 sio_data->type = it8786;
2128 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07002129 case IT8790E_DEVID:
2130 sio_data->type = it8790;
2131 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02002132 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02002133 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002134 sio_data->type = it8603;
2135 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002136 case IT8620E_DEVID:
2137 sio_data->type = it8620;
2138 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002139 case 0xffff: /* No device at all */
2140 goto exit;
2141 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01002142 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002143 goto exit;
2144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002146 superio_select(sioaddr, PME);
2147 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002148 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 goto exit;
2150 }
2151
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002152 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002154 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 goto exit;
2156 }
2157
2158 err = 0;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002159 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07002160 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2161 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002162 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002164 config = &it87_devices[sio_data->type];
2165
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002166 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002167 if (has_in7_internal(config))
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002168 sio_data->internal |= (1 << 1);
2169
Jean Delvare738e5e02010-08-14 21:08:50 +02002170 /* in8 (Vbat) is always internal */
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002171 sio_data->internal |= (1 << 2);
2172
Guenter Roeck73055402015-03-26 09:16:32 -07002173 /* in9 (AVCC3), always internal if supported */
2174 if (has_avcc3(config))
2175 sio_data->internal |= (1 << 3); /* in9 is AVCC */
2176 else
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002177 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02002178
Guenter Roeck36c4d982015-03-26 18:18:19 -07002179 if (!has_six_pwm(config))
2180 sio_data->skip_pwm |= (1 << 3) | (1 << 4) | (1 << 5);
2181
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002182 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01002183 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01002184
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002185 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2186 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01002187 /* The IT8705F has a different LD number for GPIO */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002188 superio_select(sioaddr, 5);
2189 sio_data->beep_pin = superio_inb(sioaddr,
2190 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002191 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002192 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002193
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002194 superio_select(sioaddr, GPIO);
Guenter Roeck0531d982012-03-02 11:46:44 -08002195
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002196 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2197 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2198 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2199 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2200 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002201
Guenter Roeck0531d982012-03-02 11:46:44 -08002202 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002203 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08002204 sio_data->skip_fan |= (1 << 2);
2205 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002206 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08002207 sio_data->skip_pwm |= (1 << 2);
2208
2209 /* Check if fan2 is there or not */
2210 if (reg27 & (1 << 7))
2211 sio_data->skip_fan |= (1 << 1);
2212 if (reg27 & (1 << 3))
2213 sio_data->skip_pwm |= (1 << 1);
2214
2215 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002216 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002217 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002218
2219 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002220 if (reg27 & (1 << 1))
2221 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002222
2223 /*
2224 * VIN7
2225 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2226 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002227 if (reg27 & (1 << 2)) {
2228 /*
2229 * The data sheet is a bit unclear regarding the
2230 * internal voltage divider for VCCH5V. It says
2231 * "This bit enables and switches VIN7 (pin 91) to the
2232 * internal voltage divider for VCCH5V".
2233 * This is different to other chips, where the internal
2234 * voltage divider would connect VIN7 to an internal
2235 * voltage source. Maybe that is the case here as well.
2236 *
2237 * Since we don't know for sure, re-route it if that is
2238 * not the case, and ask the user to report if the
2239 * resulting voltage is sane.
2240 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002241 if (!(reg2c & (1 << 1))) {
2242 reg2c |= (1 << 1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002243 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2244 reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002245 pr_notice("Routing internal VCCH5V to in7.\n");
2246 }
2247 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2248 pr_notice("Please report if it displays a reasonable voltage.\n");
2249 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002250
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002251 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08002252 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002253 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08002254 sio_data->internal |= (1 << 1);
2255
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002256 sio_data->beep_pin = superio_inb(sioaddr,
2257 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002258 } else if (sio_data->type == it8603) {
2259 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002260
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002261 superio_select(sioaddr, GPIO);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002262
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002263 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002264
2265 /* Check if fan3 is there or not */
2266 if (reg27 & (1 << 6))
2267 sio_data->skip_pwm |= (1 << 2);
2268 if (reg27 & (1 << 7))
2269 sio_data->skip_fan |= (1 << 2);
2270
2271 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002272 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002273 if (reg29 & (1 << 1))
2274 sio_data->skip_pwm |= (1 << 1);
2275 if (reg29 & (1 << 2))
2276 sio_data->skip_fan |= (1 << 1);
2277
2278 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2279 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2280
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002281 sio_data->beep_pin = superio_inb(sioaddr,
2282 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002283 } else if (sio_data->type == it8620) {
2284 int reg;
2285
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002286 superio_select(sioaddr, GPIO);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002287
Guenter Roeck36c4d982015-03-26 18:18:19 -07002288 /* Check for pwm5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002289 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002290 if (reg & (1 << 6))
2291 sio_data->skip_pwm |= (1 << 4);
2292
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002293 /* Check for fan4, fan5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002294 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002295 if (!(reg & (1 << 5)))
2296 sio_data->skip_fan |= (1 << 3);
2297 if (!(reg & (1 << 4)))
2298 sio_data->skip_fan |= (1 << 4);
2299
2300 /* Check for pwm3, fan3 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002301 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002302 if (reg & (1 << 6))
2303 sio_data->skip_pwm |= (1 << 2);
2304 if (reg & (1 << 7))
2305 sio_data->skip_fan |= (1 << 2);
2306
Guenter Roeck36c4d982015-03-26 18:18:19 -07002307 /* Check for pwm4 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002308 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002309 if (!(reg & (1 << 2)))
2310 sio_data->skip_pwm |= (1 << 3);
2311
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002312 /* Check for pwm2, fan2 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002313 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002314 if (reg & (1 << 1))
2315 sio_data->skip_pwm |= (1 << 1);
2316 if (reg & (1 << 2))
2317 sio_data->skip_fan |= (1 << 1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002318 /* Check for pwm6, fan6 */
2319 if (!(reg & (1 << 7))) {
2320 sio_data->skip_pwm |= (1 << 5);
2321 sio_data->skip_fan |= (1 << 5);
2322 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002323
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002324 sio_data->beep_pin = superio_inb(sioaddr,
2325 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002326 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002327 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002328 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002329
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002330 superio_select(sioaddr, GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002331
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002332 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002333 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002334 /* We need at least 4 VID pins */
2335 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002336 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002337 sio_data->skip_vid = 1;
2338 }
Jean Delvare895ff262009-12-09 20:35:47 +01002339 }
2340
Jean Delvare591ec652009-12-09 20:35:48 +01002341 /* Check if fan3 is there or not */
2342 if (reg & (1 << 6))
2343 sio_data->skip_pwm |= (1 << 2);
2344 if (reg & (1 << 7))
2345 sio_data->skip_fan |= (1 << 2);
2346
2347 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002348 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Jean Delvare591ec652009-12-09 20:35:48 +01002349 if (reg & (1 << 1))
2350 sio_data->skip_pwm |= (1 << 1);
2351 if (reg & (1 << 2))
2352 sio_data->skip_fan |= (1 << 1);
2353
Jean Delvare895ff262009-12-09 20:35:47 +01002354 if ((sio_data->type == it8718 || sio_data->type == it8720)
2355 && !(sio_data->skip_vid))
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002356 sio_data->vid_value = superio_inb(sioaddr,
2357 IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002358
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002359 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002360
2361 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2362
Jean Delvare436cad22010-07-09 16:22:48 +02002363 /*
2364 * The IT8720F has no VIN7 pin, so VCCH should always be
2365 * routed internally to VIN7 with an internal divider.
2366 * Curiously, there still is a configuration bit to control
2367 * this, which means it can be set incorrectly. And even
2368 * more curiously, many boards out there are improperly
2369 * configured, even though the IT8720F datasheet claims
2370 * that the internal routing of VCCH to VIN7 is the default
2371 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002372 *
2373 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002374 * If UART6 is enabled, re-route VIN7 to the internal divider
2375 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002376 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002377 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02002378 reg |= (1 << 1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002379 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002380 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002381 }
Jean Delvare87673dd2006-08-28 14:37:19 +02002382 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02002383 sio_data->internal |= (1 << 0);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002384 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02002385 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002386
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002387 /*
2388 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2389 * While VIN7 can be routed to the internal voltage divider,
2390 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002391 *
2392 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2393 * is the temperature source. Since we can not read the
2394 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002395 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002396 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002397 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002398 sio_data->skip_temp |= (1 << 2);
2399 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002400
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002401 sio_data->beep_pin = superio_inb(sioaddr,
2402 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002403 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002404 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002405 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002406
Jean Delvare98dd22c2008-10-09 15:33:58 +02002407 /* Disable specific features based on DMI strings */
2408 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2409 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2410 if (board_vendor && board_name) {
2411 if (strcmp(board_vendor, "nVIDIA") == 0
2412 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002413 /*
2414 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2415 * connected to a fan, but to something else. One user
2416 * has reported instant system power-off when changing
2417 * the PWM2 duty cycle, so we disable it.
2418 * I use the board name string as the trigger in case
2419 * the same board is ever used in other systems.
2420 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002421 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002422 sio_data->skip_pwm = (1 << 1);
2423 }
2424 }
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426exit:
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002427 superio_exit(sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return err;
2429}
2430
Jean Delvare723a0aa2010-03-05 22:17:16 +01002431static void it87_remove_files(struct device *dev)
2432{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002433 struct it87_data *data = dev_get_drvdata(dev);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002434 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002435 int i;
2436
2437 sysfs_remove_group(&dev->kobj, &it87_group);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002438 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002439 if (sio_data->skip_in & (1 << i))
2440 continue;
2441 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2442 if (it87_attributes_in_beep[i])
2443 sysfs_remove_file(&dev->kobj,
2444 it87_attributes_in_beep[i]);
2445 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002446 for (i = 0; i < 3; i++) {
2447 if (!(data->has_temp & (1 << i)))
2448 continue;
2449 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01002450 if (has_temp_offset(data))
2451 sysfs_remove_file(&dev->kobj,
2452 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002453 if (sio_data->beep_pin)
2454 sysfs_remove_file(&dev->kobj,
2455 it87_attributes_temp_beep[i]);
2456 }
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002457 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002458 if (!(data->has_fan & (1 << i)))
2459 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002460 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002461 if (sio_data->beep_pin)
2462 sysfs_remove_file(&dev->kobj,
2463 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002464 if (i < 3 && !has_16bit_fans(data))
2465 sysfs_remove_file(&dev->kobj,
2466 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002467 }
Guenter Roeck36c4d982015-03-26 18:18:19 -07002468 for (i = 0; i < 6; i++) {
Guenter Roeck1696d1d2015-03-27 06:03:41 -07002469 if (sio_data->skip_pwm & (1 << i))
Jean Delvare723a0aa2010-03-05 22:17:16 +01002470 continue;
2471 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002472 if (has_old_autopwm(data))
2473 sysfs_remove_group(&dev->kobj,
2474 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002475 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002476 if (!sio_data->skip_vid)
2477 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02002478 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002479}
2480
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002481/* Called when we have found a new IT87. */
2482static void it87_init_device(struct platform_device *pdev)
2483{
2484 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2485 struct it87_data *data = platform_get_drvdata(pdev);
2486 int tmp, i;
2487 u8 mask;
2488
2489 /*
2490 * For each PWM channel:
2491 * - If it is in automatic mode, setting to manual mode should set
2492 * the fan to full speed by default.
2493 * - If it is in manual mode, we need a mapping to temperature
2494 * channels to use when later setting to automatic mode later.
2495 * Use a 1:1 mapping by default (we are clueless.)
2496 * In both cases, the value can (and should) be changed by the user
2497 * prior to switching to a different mode.
2498 * Note that this is no longer needed for the IT8721F and later, as
2499 * these have separate registers for the temperature mapping and the
2500 * manual duty cycle.
2501 */
2502 for (i = 0; i < 3; i++) {
2503 data->pwm_temp_map[i] = i;
2504 data->pwm_duty[i] = 0x7f; /* Full speed */
2505 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2506 }
2507
2508 /*
2509 * Some chips seem to have default value 0xff for all limit
2510 * registers. For low voltage limits it makes no sense and triggers
2511 * alarms, so change to 0 instead. For high temperature limits, it
2512 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2513 * but is still confusing, so change to 127 degrees C.
2514 */
2515 for (i = 0; i < 8; i++) {
2516 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2517 if (tmp == 0xff)
2518 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2519 }
2520 for (i = 0; i < 3; i++) {
2521 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2522 if (tmp == 0xff)
2523 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2524 }
2525
2526 /*
2527 * Temperature channels are not forcibly enabled, as they can be
2528 * set to two different sensor types and we can't guess which one
2529 * is correct for a given system. These channels can be enabled at
2530 * run-time through the temp{1-3}_type sysfs accessors if needed.
2531 */
2532
2533 /* Check if voltage monitors are reset manually or by some reason */
2534 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2535 if ((tmp & 0xff) == 0) {
2536 /* Enable all voltage monitors */
2537 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2538 }
2539
2540 /* Check if tachometers are reset manually or by some reason */
2541 mask = 0x70 & ~(sio_data->skip_fan << 4);
2542 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2543 if ((data->fan_main_ctrl & mask) == 0) {
2544 /* Enable all fan tachometers */
2545 data->fan_main_ctrl |= mask;
2546 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2547 data->fan_main_ctrl);
2548 }
2549 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2550
2551 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2552
2553 /* Set tachometers to 16-bit mode if needed */
2554 if (has_fan16_config(data)) {
2555 if (~tmp & 0x07 & data->has_fan) {
2556 dev_dbg(&pdev->dev,
2557 "Setting fan1-3 to 16-bit mode\n");
2558 it87_write_value(data, IT87_REG_FAN_16BIT,
2559 tmp | 0x07);
2560 }
2561 }
2562
2563 /* Check for additional fans */
2564 if (has_five_fans(data)) {
2565 if (tmp & (1 << 4))
2566 data->has_fan |= (1 << 3); /* fan4 enabled */
2567 if (tmp & (1 << 5))
2568 data->has_fan |= (1 << 4); /* fan5 enabled */
2569 if (has_six_fans(data) && (tmp & (1 << 2)))
2570 data->has_fan |= (1 << 5); /* fan6 enabled */
2571 }
2572
2573 /* Fan input pins may be used for alternative functions */
2574 data->has_fan &= ~sio_data->skip_fan;
2575
2576 /* Check if pwm5, pwm6 are enabled */
2577 if (has_six_pwm(data)) {
2578 /* The following code may be IT8620E specific */
2579 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2580 if ((tmp & 0xc0) == 0xc0)
2581 sio_data->skip_pwm |= (1 << 4);
2582 if (!(tmp & (1 << 3)))
2583 sio_data->skip_pwm |= (1 << 5);
2584 }
2585
2586 /* Start monitoring */
2587 it87_write_value(data, IT87_REG_CONFIG,
2588 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2589 | (update_vbat ? 0x41 : 0x01));
2590}
2591
2592/* Return 1 if and only if the PWM interface is safe to use */
2593static int it87_check_pwm(struct device *dev)
2594{
2595 struct it87_data *data = dev_get_drvdata(dev);
2596 /*
2597 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2598 * and polarity set to active low is sign that this is the case so we
2599 * disable pwm control to protect the user.
2600 */
2601 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2602
2603 if ((tmp & 0x87) == 0) {
2604 if (fix_pwm_polarity) {
2605 /*
2606 * The user asks us to attempt a chip reconfiguration.
2607 * This means switching to active high polarity and
2608 * inverting all fan speed values.
2609 */
2610 int i;
2611 u8 pwm[3];
2612
2613 for (i = 0; i < 3; i++)
2614 pwm[i] = it87_read_value(data,
2615 IT87_REG_PWM[i]);
2616
2617 /*
2618 * If any fan is in automatic pwm mode, the polarity
2619 * might be correct, as suspicious as it seems, so we
2620 * better don't change anything (but still disable the
2621 * PWM interface).
2622 */
2623 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2624 dev_info(dev,
2625 "Reconfiguring PWM to active high polarity\n");
2626 it87_write_value(data, IT87_REG_FAN_CTL,
2627 tmp | 0x87);
2628 for (i = 0; i < 3; i++)
2629 it87_write_value(data,
2630 IT87_REG_PWM[i],
2631 0x7f & ~pwm[i]);
2632 return 1;
2633 }
2634
2635 dev_info(dev,
2636 "PWM configuration is too broken to be fixed\n");
2637 }
2638
2639 dev_info(dev,
2640 "Detected broken BIOS defaults, disabling PWM interface\n");
2641 return 0;
2642 } else if (fix_pwm_polarity) {
2643 dev_info(dev,
2644 "PWM configuration looks sane, won't touch\n");
2645 }
2646
2647 return 1;
2648}
2649
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002650static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002653 struct resource *res;
2654 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002655 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002656 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01002658 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002660 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002661 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2662 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002663 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2664 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002665 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002666 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Guenter Roeck62a1d052012-03-24 21:54:41 -07002669 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2670 if (!data)
2671 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002673 data->addr = res->start;
2674 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002675 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002676 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002677 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002678 data->name = it87_devices[sio_data->type].name;
2679 /*
2680 * IT8705F Datasheet 0.4.1, 3h == Version G.
2681 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2682 * These are the first revisions with 16-bit tachometer support.
2683 */
2684 switch (data->type) {
2685 case it87:
2686 if (sio_data->revision >= 0x03) {
2687 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002688 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002689 }
2690 break;
2691 case it8712:
2692 if (sio_data->revision >= 0x08) {
2693 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002694 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2695 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002696 }
2697 break;
2698 default:
2699 break;
2700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002703 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002704 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2705 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002707 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002709 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002712 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002714 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002715 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002716 if (sio_data->internal & (1 << 0))
2717 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2718 if (sio_data->internal & (1 << 1))
2719 data->in_scaled |= (1 << 7); /* in7 is VSB */
2720 if (sio_data->internal & (1 << 2))
2721 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002722 if (sio_data->internal & (1 << 3))
2723 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002724 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2725 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002726 if (sio_data->internal & (1 << 0))
2727 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2728 if (sio_data->internal & (1 << 1))
2729 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002730 }
2731
Guenter Roeck4573acb2012-03-26 16:17:41 -07002732 data->has_temp = 0x07;
2733 if (sio_data->skip_temp & (1 << 2)) {
2734 if (sio_data->type == it8782
2735 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2736 data->has_temp &= ~(1 << 2);
2737 }
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002740 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002743 err = sysfs_create_group(&dev->kobj, &it87_group);
2744 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002745 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002746
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002747 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002748 if (sio_data->skip_in & (1 << i))
2749 continue;
2750 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2751 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002752 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002753 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2754 err = sysfs_create_file(&dev->kobj,
2755 it87_attributes_in_beep[i]);
2756 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002757 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002758 }
2759 }
2760
Guenter Roeck4573acb2012-03-26 16:17:41 -07002761 for (i = 0; i < 3; i++) {
2762 if (!(data->has_temp & (1 << i)))
2763 continue;
2764 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002765 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002766 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002767 if (has_temp_offset(data)) {
2768 err = sysfs_create_file(&dev->kobj,
2769 it87_attributes_temp_offset[i]);
2770 if (err)
2771 goto error;
2772 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002773 if (sio_data->beep_pin) {
2774 err = sysfs_create_file(&dev->kobj,
2775 it87_attributes_temp_beep[i]);
2776 if (err)
2777 goto error;
2778 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002779 }
2780
Jean Delvare9060f8b2006-08-28 14:24:17 +02002781 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002782 fan_beep_need_rw = 1;
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002783 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002784 if (!(data->has_fan & (1 << i)))
2785 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002786 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002787 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002788 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002789
Guenter Roecke1169ba2012-12-19 22:17:01 +01002790 if (i < 3 && !has_16bit_fans(data)) {
2791 err = sysfs_create_file(&dev->kobj,
2792 it87_attributes_fan_div[i]);
2793 if (err)
2794 goto error;
2795 }
2796
Jean Delvared9b327c2010-03-05 22:17:17 +01002797 if (sio_data->beep_pin) {
2798 err = sysfs_create_file(&dev->kobj,
2799 it87_attributes_fan_beep[i]);
2800 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002801 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002802 if (!fan_beep_need_rw)
2803 continue;
2804
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002805 /*
2806 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002807 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002808 * for it.
2809 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002810 if (sysfs_chmod_file(&dev->kobj,
2811 it87_attributes_fan_beep[i],
2812 S_IRUGO | S_IWUSR))
2813 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2814 i + 1);
2815 fan_beep_need_rw = 0;
2816 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002817 }
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 if (enable_pwm_interface) {
Guenter Roeck36c4d982015-03-26 18:18:19 -07002820 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002821 if (sio_data->skip_pwm & (1 << i))
2822 continue;
2823 err = sysfs_create_group(&dev->kobj,
2824 &it87_group_pwm[i]);
2825 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002826 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002827
2828 if (!has_old_autopwm(data))
2829 continue;
2830 err = sysfs_create_group(&dev->kobj,
2831 &it87_group_autopwm[i]);
2832 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002833 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
2836
Jean Delvare895ff262009-12-09 20:35:47 +01002837 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002838 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002839 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002840 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002841 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2842 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002843 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002844 }
2845
Jean Delvare738e5e02010-08-14 21:08:50 +02002846 /* Export labels for internal sensors */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002847 for (i = 0; i < 4; i++) {
Jean Delvare738e5e02010-08-14 21:08:50 +02002848 if (!(sio_data->internal & (1 << i)))
2849 continue;
2850 err = sysfs_create_file(&dev->kobj,
2851 it87_attributes_label[i]);
2852 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002853 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002854 }
2855
Tony Jones1beeffe2007-08-20 13:46:20 -07002856 data->hwmon_dev = hwmon_device_register(dev);
2857 if (IS_ERR(data->hwmon_dev)) {
2858 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002859 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 }
2861
2862 return 0;
2863
Guenter Roeck62a1d052012-03-24 21:54:41 -07002864error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002865 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return err;
2867}
2868
Bill Pemberton281dfd02012-11-19 13:25:51 -05002869static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002871 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Tony Jones1beeffe2007-08-20 13:46:20 -07002873 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002874 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return 0;
2877}
2878
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002879static struct platform_driver it87_driver = {
2880 .driver = {
2881 .name = DRVNAME,
2882 },
2883 .probe = it87_probe,
2884 .remove = it87_remove,
2885};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Guenter Roecke84bd952015-03-28 08:24:29 -07002887static int __init it87_device_add(int index, unsigned short address,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002888 const struct it87_sio_data *sio_data)
2889{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002890 struct platform_device *pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002891 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002892 .start = address + IT87_EC_OFFSET,
2893 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002894 .name = DRVNAME,
2895 .flags = IORESOURCE_IO,
2896 };
2897 int err;
2898
Jean Delvareb9acb642009-01-07 16:37:35 +01002899 err = acpi_check_resource_conflict(&res);
2900 if (err)
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002901 return err;
Jean Delvareb9acb642009-01-07 16:37:35 +01002902
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002903 pdev = platform_device_alloc(DRVNAME, address);
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002904 if (!pdev)
2905 return -ENOMEM;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002906
2907 err = platform_device_add_resources(pdev, &res, 1);
2908 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002909 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002910 goto exit_device_put;
2911 }
2912
2913 err = platform_device_add_data(pdev, sio_data,
2914 sizeof(struct it87_sio_data));
2915 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002916 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002917 goto exit_device_put;
2918 }
2919
2920 err = platform_device_add(pdev);
2921 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002922 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002923 goto exit_device_put;
2924 }
2925
Guenter Roecke84bd952015-03-28 08:24:29 -07002926 it87_pdev[index] = pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002927 return 0;
2928
2929exit_device_put:
2930 platform_device_put(pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002931 return err;
2932}
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934static int __init sm_it87_init(void)
2935{
Guenter Roecke84bd952015-03-28 08:24:29 -07002936 int sioaddr[2] = { REG_2E, REG_4E };
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002937 struct it87_sio_data sio_data;
Guenter Roecke84bd952015-03-28 08:24:29 -07002938 unsigned short isa_address;
2939 bool found = false;
2940 int i, err;
Jean Delvarefde09502005-07-19 23:51:07 +02002941
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002942 err = platform_driver_register(&it87_driver);
2943 if (err)
2944 return err;
2945
Guenter Roecke84bd952015-03-28 08:24:29 -07002946 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
2947 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2948 isa_address = 0;
2949 err = it87_find(sioaddr[i], &isa_address, &sio_data);
2950 if (err || isa_address == 0)
2951 continue;
2952
2953 err = it87_device_add(i, isa_address, &sio_data);
2954 if (err)
2955 goto exit_dev_unregister;
2956 found = true;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002957 }
2958
Guenter Roecke84bd952015-03-28 08:24:29 -07002959 if (!found) {
2960 err = -ENODEV;
2961 goto exit_unregister;
2962 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002963 return 0;
Guenter Roecke84bd952015-03-28 08:24:29 -07002964
2965exit_dev_unregister:
2966 /* NULL check handled by platform_device_unregister */
2967 platform_device_unregister(it87_pdev[0]);
2968exit_unregister:
2969 platform_driver_unregister(&it87_driver);
2970 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972
2973static void __exit sm_it87_exit(void)
2974{
Guenter Roecke84bd952015-03-28 08:24:29 -07002975 /* NULL check handled by platform_device_unregister */
2976 platform_device_unregister(it87_pdev[1]);
2977 platform_device_unregister(it87_pdev[0]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002978 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979}
2980
2981
Jean Delvare7c81c602014-01-29 20:40:08 +01002982MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002983MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984module_param(update_vbat, bool, 0);
2985MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2986module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002987MODULE_PARM_DESC(fix_pwm_polarity,
2988 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989MODULE_LICENSE("GPL");
2990
2991module_init(sm_it87_init);
2992module_exit(sm_it87_exit);