blob: 4840f2d8c7b11b81beffd6530d79178ffafb0ac2 [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 Roeck8e50e3c2015-03-28 07:49:14 -070081static struct platform_device *it87_pdev;
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 */
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define DEV 0x07 /* Register: Logical device select */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010087
88/* The device with the IT8718F/IT8720F VID value in it */
89#define GPIO 0x07
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define DEVID 0x20 /* Register: Device ID */
92#define DEVREV 0x22 /* Register: Device Revision */
93
Guenter Roeck3c2e3512015-03-28 08:03:10 -070094static inline int superio_inb(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Guenter Roeck3c2e3512015-03-28 08:03:10 -070096 outb(reg, ioreg);
97 return inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700100static inline void superio_outb(int ioreg, int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +0200101{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700102 outb(reg, ioreg);
103 outb(val, ioreg + 1);
Jean Delvare436cad22010-07-09 16:22:48 +0200104}
105
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700106static int superio_inw(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 int val;
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700109 outb(reg++, ioreg);
110 val = inb(ioreg + 1) << 8;
111 outb(reg, ioreg);
112 val |= inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return val;
114}
115
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700116static inline void superio_select(int ioreg, int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700118 outb(DEV, ioreg);
119 outb(ldn, ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700122static inline int superio_enter(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200124 /*
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700125 * Try to reserve ioreg and ioreg + 1 for exclusive access.
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200126 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700127 if (!request_muxed_region(ioreg, 2, DRVNAME))
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200128 return -EBUSY;
129
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700130 outb(0x87, ioreg);
131 outb(0x01, ioreg);
132 outb(0x55, ioreg);
133 outb(0x55, ioreg);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700137static inline void superio_exit(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700139 outb(0x02, ioreg);
140 outb(0x02, ioreg + 1);
141 release_region(ioreg, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Jean Delvare87673dd2006-08-28 14:37:19 +0200144/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define IT8712F_DEVID 0x8712
146#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200147#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200148#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100149#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200150#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400151#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100152#define IT8728F_DEVID 0x8728
Justin Maggardead80802015-08-05 12:53:08 -0700153#define IT8732F_DEVID 0x8732
Guenter Roeckb0636702012-09-07 17:34:41 -0600154#define IT8771E_DEVID 0x8771
155#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800156#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800157#define IT8782F_DEVID 0x8782
158#define IT8783E_DEVID 0x8783
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100159#define IT8786E_DEVID 0x8786
Guenter Roeck4ee07152015-03-25 23:26:28 -0700160#define IT8790E_DEVID 0x8790
Rudolf Marek7183ae82014-04-04 18:01:35 +0200161#define IT8603E_DEVID 0x8603
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800162#define IT8620E_DEVID 0x8620
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200163#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define IT87_ACT_REG 0x30
165#define IT87_BASE_REG 0x60
166
Jean Delvare87673dd2006-08-28 14:37:19 +0200167/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800168#define IT87_SIO_GPIO1_REG 0x25
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800169#define IT87_SIO_GPIO2_REG 0x26
Jean Delvare895ff262009-12-09 20:35:47 +0100170#define IT87_SIO_GPIO3_REG 0x27
Guenter Roeck36c4d982015-03-26 18:18:19 -0700171#define IT87_SIO_GPIO4_REG 0x28
Jean Delvare591ec652009-12-09 20:35:48 +0100172#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800173#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200174#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800175#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200176#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100177#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030180static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030183static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* Many IT87 constants specified below */
186
187/* Length of ISA address segment */
188#define IT87_EXTENT 8
189
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500190/* Length of ISA address segment for Environmental Controller */
191#define IT87_EC_EXTENT 2
192
193/* Offset of EC registers from ISA base address */
194#define IT87_EC_OFFSET 5
195
196/* Where are the ISA address/data registers relative to the EC base address */
197#define IT87_ADDR_REG_OFFSET 0
198#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/*----- The IT87 registers -----*/
201
202#define IT87_REG_CONFIG 0x00
203
204#define IT87_REG_ALARM1 0x01
205#define IT87_REG_ALARM2 0x02
206#define IT87_REG_ALARM3 0x03
207
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800208/*
209 * The IT8718F and IT8720F have the VID value in a different register, in
210 * Super-I/O configuration space.
211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800213/*
214 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
215 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
216 * mode.
217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200219#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
222
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700223static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
224static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
225static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
226static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
227static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#define IT87_REG_FAN_MAIN_CTRL 0x13
230#define IT87_REG_FAN_CTL 0x14
Guenter Roeck36c4d982015-03-26 18:18:19 -0700231static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
232static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234#define IT87_REG_VIN(nr) (0x20 + (nr))
235#define IT87_REG_TEMP(nr) (0x29 + (nr))
236
Guenter Roeck73055402015-03-26 09:16:32 -0700237#define IT87_REG_AVCC3 0x2f
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
240#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
241#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
242#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244#define IT87_REG_VIN_ENABLE 0x50
245#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700246#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100247#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249#define IT87_REG_CHIPID 0x58
250
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100251#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
252#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
253
Guenter Roeck483db432012-12-19 22:17:02 +0100254struct it87_devices {
255 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700256 const char * const suffix;
Guenter Roeck483db432012-12-19 22:17:02 +0100257 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100258 u8 peci_mask;
259 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100260};
261
262#define FEAT_12MV_ADC (1 << 0)
263#define FEAT_NEWER_AUTOPWM (1 << 1)
264#define FEAT_OLD_AUTOPWM (1 << 2)
265#define FEAT_16BIT_FANS (1 << 3)
266#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100267#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100268#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800269#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
270#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800271#define FEAT_VID (1 << 9) /* Set if chip supports VID */
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700272#define FEAT_IN7_INTERNAL (1 << 10) /* Set if in7 is internal */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700273#define FEAT_SIX_FANS (1 << 11) /* Supports six fans */
Justin Maggardead80802015-08-05 12:53:08 -0700274#define FEAT_10_9MV_ADC (1 << 12)
Guenter Roeck73055402015-03-26 09:16:32 -0700275#define FEAT_AVCC3 (1 << 13) /* Chip supports in9/AVCC3 */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700276#define FEAT_SIX_PWM (1 << 14) /* Chip supports 6 pwm chn */
Guenter Roeck60878bc2015-03-26 19:57:42 -0700277#define FEAT_PWM_FREQ2 (1 << 15) /* Separate pwm freq 2 */
Guenter Roeck483db432012-12-19 22:17:02 +0100278
279static const struct it87_devices it87_devices[] = {
280 [it87] = {
281 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700282 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100283 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
284 },
285 [it8712] = {
286 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700287 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800288 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
289 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100290 },
291 [it8716] = {
292 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700293 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800294 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700295 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
Guenter Roeck483db432012-12-19 22:17:02 +0100296 },
297 [it8718] = {
298 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700299 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800300 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700301 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
302 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100303 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100304 },
305 [it8720] = {
306 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700307 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800308 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700309 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
310 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100311 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100312 },
313 [it8721] = {
314 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700315 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100316 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800317 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck60878bc2015-03-26 19:57:42 -0700318 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
319 | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100320 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100321 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100322 },
323 [it8728] = {
324 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700325 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100326 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700327 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700328 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100329 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100330 },
Justin Maggardead80802015-08-05 12:53:08 -0700331 [it8732] = {
332 .name = "it8732",
333 .suffix = "F",
334 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
335 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
336 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
337 .peci_mask = 0x07,
338 .old_peci_mask = 0x02, /* Actually reports PCH */
339 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600340 [it8771] = {
341 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700342 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600343 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700344 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
345 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800346 /* PECI: guesswork */
347 /* 12mV ADC (OHM) */
348 /* 16 bit fans (OHM) */
349 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600350 .peci_mask = 0x07,
351 },
352 [it8772] = {
353 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700354 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600355 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700356 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
357 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800358 /* PECI (coreboot) */
359 /* 12mV ADC (HWSensors4, OHM) */
360 /* 16 bit fans (HWSensors4, OHM) */
361 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600362 .peci_mask = 0x07,
363 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800364 [it8781] = {
365 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700366 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800367 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700368 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800369 .old_peci_mask = 0x4,
370 },
Guenter Roeck483db432012-12-19 22:17:02 +0100371 [it8782] = {
372 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700373 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100374 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700375 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100376 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100377 },
378 [it8783] = {
379 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700380 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100381 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700382 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100383 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100384 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100385 [it8786] = {
386 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700387 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100388 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700389 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
390 | FEAT_PWM_FREQ2,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100391 .peci_mask = 0x07,
392 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700393 [it8790] = {
394 .name = "it8790",
395 .suffix = "E",
396 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700397 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
398 | FEAT_PWM_FREQ2,
Guenter Roeck4ee07152015-03-25 23:26:28 -0700399 .peci_mask = 0x07,
400 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100401 [it8603] = {
402 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700403 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100404 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck73055402015-03-26 09:16:32 -0700405 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
Guenter Roeck60878bc2015-03-26 19:57:42 -0700406 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100407 .peci_mask = 0x07,
408 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800409 [it8620] = {
410 .name = "it8620",
411 .suffix = "E",
412 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700413 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700414 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2,
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800415 .peci_mask = 0x07,
416 },
Guenter Roeck483db432012-12-19 22:17:02 +0100417};
418
419#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
420#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
Justin Maggardead80802015-08-05 12:53:08 -0700421#define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
Guenter Roeck483db432012-12-19 22:17:02 +0100422#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
423#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
424#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100425#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
426 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100427#define has_temp_old_peci(data, nr) \
428 (((data)->features & FEAT_TEMP_OLD_PECI) && \
429 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800430#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700431#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
432 FEAT_SIX_FANS))
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800433#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700434#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700435#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Guenter Roeck73055402015-03-26 09:16:32 -0700436#define has_avcc3(data) ((data)->features & FEAT_AVCC3)
Guenter Roeck36c4d982015-03-26 18:18:19 -0700437#define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
Guenter Roeck60878bc2015-03-26 19:57:42 -0700438#define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200440struct it87_sio_data {
441 enum chips type;
442 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200443 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200444 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100445 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200446 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100447 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700448 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100449 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100450 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200451 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700452 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200453};
454
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800455/*
456 * For each registered chip, we need to keep some data in memory.
457 * The structure is dynamically allocated.
458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700460 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100462 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100463 u8 peci_mask;
464 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200466 unsigned short addr;
467 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100468 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 char valid; /* !=0 if following fields are valid */
470 unsigned long last_updated; /* In jiffies */
471
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200472 u16 in_scaled; /* Internal voltage sensors are scaled */
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100473 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200474 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700475 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700476 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck161d8982012-12-19 22:17:01 +0100477 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100478 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
479 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 u8 fan_div[3]; /* Register encoding, shifted right */
481 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100482 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100484 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100486 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100487
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800488 /*
489 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100490 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
491 * 7, and we want to preserve settings on mode changes, so we have
492 * to track all values separately.
493 * Starting with the IT8721F, the manual PWM duty cycles are stored
494 * in separate registers (8-bit values), so the separate tracking
495 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800496 * simple.
497 */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700498 u8 pwm_ctrl[6]; /* Register value */
499 u8 pwm_duty[6]; /* Manual PWM value set by user */
500 u8 pwm_temp_map[6]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100501
502 /* Automatic fan speed control registers */
503 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
504 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505};
506
Guenter Roeck0531d982012-03-02 11:46:44 -0800507static int adc_lsb(const struct it87_data *data, int nr)
508{
Justin Maggardead80802015-08-05 12:53:08 -0700509 int lsb;
510
511 if (has_12mv_adc(data))
512 lsb = 120;
513 else if (has_10_9mv_adc(data))
514 lsb = 109;
515 else
516 lsb = 160;
Guenter Roeck0531d982012-03-02 11:46:44 -0800517 if (data->in_scaled & (1 << nr))
518 lsb <<= 1;
519 return lsb;
520}
521
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200522static u8 in_to_reg(const struct it87_data *data, int nr, long val)
523{
Justin Maggardead80802015-08-05 12:53:08 -0700524 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800525 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200526}
527
528static int in_from_reg(const struct it87_data *data, int nr, int val)
529{
Justin Maggardead80802015-08-05 12:53:08 -0700530 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200531}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200532
533static inline u8 FAN_TO_REG(long rpm, int div)
534{
535 if (rpm == 0)
536 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800537 rpm = clamp_val(rpm, 1, 1000000);
538 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200539}
540
541static inline u16 FAN16_TO_REG(long rpm)
542{
543 if (rpm == 0)
544 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800545 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200546}
547
548#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
549 1350000 / ((val) * (div)))
550/* The divider is fixed to 2 in 16-bit mode */
551#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
552 1350000 / ((val) * 2))
553
Guenter Roeck2a844c12013-01-09 08:09:34 -0800554#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
555 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200556#define TEMP_FROM_REG(val) ((val) * 1000)
557
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200558static u8 pwm_to_reg(const struct it87_data *data, long val)
559{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100560 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200561 return val;
562 else
563 return val >> 1;
564}
565
566static int pwm_from_reg(const struct it87_data *data, u8 reg)
567{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100568 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200569 return reg;
570 else
571 return (reg & 0x7f) << 1;
572}
573
Jean Delvare0df6454d2010-10-28 20:31:51 +0200574
575static int DIV_TO_REG(int val)
576{
577 int answer = 0;
578 while (answer < 7 && (val >>= 1))
579 answer++;
580 return answer;
581}
582#define DIV_FROM_REG(val) (1 << (val))
583
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700584/*
585 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
586 * depending on the chip type, to calculate the actual PWM frequency.
587 *
588 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
589 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
590 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
591 * sometimes just one. It is unknown if this is a datasheet error or real,
592 * so this is ignored for now.
593 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200594static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700595 48000000,
596 24000000,
597 12000000,
598 8000000,
599 6000000,
600 3000000,
601 1500000,
602 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200603};
604
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200605static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500606static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200608static int it87_read_value(struct it87_data *data, u8 reg);
609static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200611static int it87_check_pwm(struct device *dev);
612static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200615static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100616 .driver = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200617 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100618 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200619 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500620 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200621};
622
Jean Delvare20ad93d2005-06-05 11:53:25 +0200623static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100624 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100626 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
627 int nr = sattr->nr;
628 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100631 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Guenter Roeck929c6a52012-12-19 22:17:00 +0100634static ssize_t set_in(struct device *dev, struct device_attribute *attr,
635 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100637 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
638 int nr = sattr->nr;
639 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200640
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200641 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100642 unsigned long val;
643
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100644 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100645 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100647 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100648 data->in[nr][index] = in_to_reg(data, nr, val);
649 it87_write_value(data,
650 index == 1 ? IT87_REG_VIN_MIN(nr)
651 : IT87_REG_VIN_MAX(nr),
652 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100653 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return count;
655}
656
Guenter Roeck929c6a52012-12-19 22:17:00 +0100657static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
658static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
659 0, 1);
660static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
661 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Guenter Roeck929c6a52012-12-19 22:17:00 +0100663static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
664static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
665 1, 1);
666static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
667 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Guenter Roeck929c6a52012-12-19 22:17:00 +0100669static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
670static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
671 2, 1);
672static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
673 2, 2);
674
675static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
676static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
677 3, 1);
678static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
679 3, 2);
680
681static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
682static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
683 4, 1);
684static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
685 4, 2);
686
687static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
688static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
689 5, 1);
690static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
691 5, 2);
692
693static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
694static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
695 6, 1);
696static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
697 6, 2);
698
699static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
700static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
701 7, 1);
702static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
703 7, 2);
704
705static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100706static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200709static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100710 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100712 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
713 int nr = sattr->nr;
714 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200716
Guenter Roeck60ca3852012-12-19 22:17:00 +0100717 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200719
Guenter Roeck60ca3852012-12-19 22:17:00 +0100720static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
721 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100723 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
724 int nr = sattr->nr;
725 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200726 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100727 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100728 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100729
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100730 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100731 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100733 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100734
735 switch (index) {
736 default:
737 case 1:
738 reg = IT87_REG_TEMP_LOW(nr);
739 break;
740 case 2:
741 reg = IT87_REG_TEMP_HIGH(nr);
742 break;
743 case 3:
744 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
745 if (!(regval & 0x80)) {
746 regval |= 0x80;
747 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
748 }
749 data->valid = 0;
750 reg = IT87_REG_TEMP_OFFSET[nr];
751 break;
752 }
753
Guenter Roeck60ca3852012-12-19 22:17:00 +0100754 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100755 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100756 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return count;
758}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200759
Guenter Roeck60ca3852012-12-19 22:17:00 +0100760static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
761static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
762 0, 1);
763static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
764 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100765static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
766 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100767static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
768static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
769 1, 1);
770static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
771 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100772static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
773 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100774static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
775static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
776 2, 1);
777static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
778 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100779static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
780 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Guenter Roeck2cece012012-12-19 22:17:01 +0100782static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
783 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200785 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
786 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800788 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100789 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100790
Guenter Roeck19529782012-12-19 22:17:02 +0100791 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
792 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100793 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (reg & (1 << nr))
795 return sprintf(buf, "3\n"); /* thermal diode */
796 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200797 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return sprintf(buf, "0\n"); /* disabled */
799}
Guenter Roeck2cece012012-12-19 22:17:01 +0100800
801static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
802 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200804 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
805 int nr = sensor_attr->index;
806
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200807 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100808 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100809 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100810
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100811 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100812 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Jean Delvare8acf07c2010-04-14 16:14:09 +0200814 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
815 reg &= ~(1 << nr);
816 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100817 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
818 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100819 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
820 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
821 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200822 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100823 dev_warn(dev,
824 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200825 val = 4;
826 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100827 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200829 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200830 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200831 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100832 else if (has_temp_peci(data, nr) && val == 6)
833 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100834 else if (has_temp_old_peci(data, nr) && val == 6)
835 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200836 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200838
839 mutex_lock(&data->update_lock);
840 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100841 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200842 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100843 if (has_temp_old_peci(data, nr))
844 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200845 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100846 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return count;
848}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Guenter Roeck2cece012012-12-19 22:17:01 +0100850static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
851 set_temp_type, 0);
852static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
853 set_temp_type, 1);
854static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
855 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100858
859static int pwm_mode(const struct it87_data *data, int nr)
860{
861 int ctrl = data->fan_main_ctrl & (1 << nr);
862
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100863 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +0100864 return 0;
865 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
866 return 2;
867 else /* Manual mode */
868 return 1;
869}
870
Jean Delvare20ad93d2005-06-05 11:53:25 +0200871static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +0100872 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100874 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
875 int nr = sattr->nr;
876 int index = sattr->index;
877 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200879
Guenter Roecke1169ba2012-12-19 22:17:01 +0100880 speed = has_16bit_fans(data) ?
881 FAN16_FROM_REG(data->fan[nr][index]) :
882 FAN_FROM_REG(data->fan[nr][index],
883 DIV_FROM_REG(data->fan_div[nr]));
884 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100886
Jean Delvare20ad93d2005-06-05 11:53:25 +0200887static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
888 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200890 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
891 int nr = sensor_attr->index;
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct it87_data *data = it87_update_device(dev);
894 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
895}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100896static ssize_t show_pwm_enable(struct device *dev,
897 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200899 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
900 int nr = sensor_attr->index;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100903 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200905static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
906 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200908 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
909 int nr = sensor_attr->index;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200912 return sprintf(buf, "%d\n",
913 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100915static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
916 char *buf)
917{
Guenter Roeck60878bc2015-03-26 19:57:42 -0700918 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100919 struct it87_data *data = it87_update_device(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -0700920 int nr = sensor_attr->index;
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700921 unsigned int freq;
Guenter Roeck60878bc2015-03-26 19:57:42 -0700922 int index;
923
924 if (has_pwm_freq2(data) && nr == 1)
925 index = (data->extra >> 4) & 0x07;
926 else
927 index = (data->fan_ctl >> 4) & 0x07;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100928
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700929 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
930
931 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100932}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100933
934static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
935 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100937 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
938 int nr = sattr->nr;
939 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200940
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200941 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100942 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100943 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100945 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100946 return -EINVAL;
947
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100948 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +0100949
950 if (has_16bit_fans(data)) {
951 data->fan[nr][index] = FAN16_TO_REG(val);
952 it87_write_value(data, IT87_REG_FAN_MIN[nr],
953 data->fan[nr][index] & 0xff);
954 it87_write_value(data, IT87_REG_FANX_MIN[nr],
955 data->fan[nr][index] >> 8);
956 } else {
957 reg = it87_read_value(data, IT87_REG_FAN_DIV);
958 switch (nr) {
959 case 0:
960 data->fan_div[nr] = reg & 0x07;
961 break;
962 case 1:
963 data->fan_div[nr] = (reg >> 3) & 0x07;
964 break;
965 case 2:
966 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
967 break;
968 }
969 data->fan[nr][index] =
970 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
971 it87_write_value(data, IT87_REG_FAN_MIN[nr],
972 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -0800973 }
974
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100975 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return count;
977}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100978
Jean Delvare20ad93d2005-06-05 11:53:25 +0200979static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
980 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200982 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
983 int nr = sensor_attr->index;
984
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200985 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100986 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200987 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 u8 old;
989
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100990 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100991 return -EINVAL;
992
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100993 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200994 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200996 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100997 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 switch (nr) {
1000 case 0:
1001 case 1:
1002 data->fan_div[nr] = DIV_TO_REG(val);
1003 break;
1004 case 2:
1005 if (val < 8)
1006 data->fan_div[nr] = 1;
1007 else
1008 data->fan_div[nr] = 3;
1009 }
1010 val = old & 0x80;
1011 val |= (data->fan_div[0] & 0x07);
1012 val |= (data->fan_div[1] & 0x07) << 3;
1013 if (data->fan_div[2] == 3)
1014 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001015 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001017 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001018 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1019 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001020
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001021 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return count;
1023}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001024
1025/* Returns 0 if OK, -EINVAL otherwise */
1026static int check_trip_points(struct device *dev, int nr)
1027{
1028 const struct it87_data *data = dev_get_drvdata(dev);
1029 int i, err = 0;
1030
1031 if (has_old_autopwm(data)) {
1032 for (i = 0; i < 3; i++) {
1033 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1034 err = -EINVAL;
1035 }
1036 for (i = 0; i < 2; i++) {
1037 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1038 err = -EINVAL;
1039 }
1040 }
1041
1042 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001043 dev_err(dev,
1044 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001045 dev_err(dev, "Adjust the trip points and try again\n");
1046 }
1047 return err;
1048}
1049
Jean Delvare20ad93d2005-06-05 11:53:25 +02001050static ssize_t set_pwm_enable(struct device *dev,
1051 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001053 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1054 int nr = sensor_attr->index;
1055
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001056 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001057 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001059 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001060 return -EINVAL;
1061
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001062 /* Check trip points before switching to automatic mode */
1063 if (val == 2) {
1064 if (check_trip_points(dev, nr) < 0)
1065 return -EINVAL;
1066 }
1067
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001068 /* IT8603E does not have on/off mode */
1069 if (val == 0 && data->type == it8603)
1070 return -EINVAL;
1071
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001072 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 if (val == 0) {
1075 int tmp;
1076 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001077 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1078 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /* set on/off mode */
1080 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +01001081 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1082 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +01001083 } else {
1084 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001085 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001086 data->pwm_temp_map[nr] :
1087 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001088 else /* Automatic mode */
1089 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001090 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001091
1092 if (data->type != it8603) {
1093 /* set SmartGuardian mode */
1094 data->fan_main_ctrl |= (1 << nr);
1095 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1096 data->fan_main_ctrl);
1097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001100 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return count;
1102}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001103static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1104 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001106 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1107 int nr = sensor_attr->index;
1108
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001109 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001110 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001112 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return -EINVAL;
1114
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001115 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001116 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001117 /*
1118 * If we are in automatic mode, the PWM duty cycle register
1119 * is read-only so we can't write the value.
1120 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001121 if (data->pwm_ctrl[nr] & 0x80) {
1122 mutex_unlock(&data->update_lock);
1123 return -EBUSY;
1124 }
1125 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001126 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001127 data->pwm_duty[nr]);
1128 } else {
1129 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001130 /*
1131 * If we are in manual mode, write the duty cycle immediately;
1132 * otherwise, just store it for later use.
1133 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001134 if (!(data->pwm_ctrl[nr] & 0x80)) {
1135 data->pwm_ctrl[nr] = data->pwm_duty[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001136 it87_write_value(data, IT87_REG_PWM[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001137 data->pwm_ctrl[nr]);
1138 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001139 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001140 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return count;
1142}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001143static ssize_t set_pwm_freq(struct device *dev,
1144 struct device_attribute *attr, const char *buf, size_t count)
1145{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001146 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001147 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001148 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001149 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001150 int i;
1151
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001152 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001153 return -EINVAL;
1154
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001155 val = clamp_val(val, 0, 1000000);
1156 val *= has_newer_autopwm(data) ? 256 : 128;
1157
Jean Delvaref8d0c192007-02-14 21:15:02 +01001158 /* Search for the nearest available frequency */
1159 for (i = 0; i < 7; i++) {
1160 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1161 break;
1162 }
1163
1164 mutex_lock(&data->update_lock);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001165 if (nr == 0) {
1166 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1167 data->fan_ctl |= i << 4;
1168 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1169 } else {
1170 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1171 data->extra |= i << 4;
1172 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1173 }
Jean Delvaref8d0c192007-02-14 21:15:02 +01001174 mutex_unlock(&data->update_lock);
1175
1176 return count;
1177}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001178static ssize_t show_pwm_temp_map(struct device *dev,
1179 struct device_attribute *attr, char *buf)
1180{
1181 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1182 int nr = sensor_attr->index;
1183
1184 struct it87_data *data = it87_update_device(dev);
1185 int map;
1186
1187 if (data->pwm_temp_map[nr] < 3)
1188 map = 1 << data->pwm_temp_map[nr];
1189 else
1190 map = 0; /* Should never happen */
1191 return sprintf(buf, "%d\n", map);
1192}
1193static ssize_t set_pwm_temp_map(struct device *dev,
1194 struct device_attribute *attr, const char *buf, size_t count)
1195{
1196 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1197 int nr = sensor_attr->index;
1198
1199 struct it87_data *data = dev_get_drvdata(dev);
1200 long val;
1201 u8 reg;
1202
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001203 /*
1204 * This check can go away if we ever support automatic fan speed
1205 * control on newer chips.
1206 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001207 if (!has_old_autopwm(data)) {
1208 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1209 return -EINVAL;
1210 }
1211
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001212 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001213 return -EINVAL;
1214
1215 switch (val) {
1216 case (1 << 0):
1217 reg = 0x00;
1218 break;
1219 case (1 << 1):
1220 reg = 0x01;
1221 break;
1222 case (1 << 2):
1223 reg = 0x02;
1224 break;
1225 default:
1226 return -EINVAL;
1227 }
1228
1229 mutex_lock(&data->update_lock);
1230 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001231 /*
1232 * If we are in automatic mode, write the temp mapping immediately;
1233 * otherwise, just store it for later use.
1234 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001235 if (data->pwm_ctrl[nr] & 0x80) {
1236 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001237 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001238 }
1239 mutex_unlock(&data->update_lock);
1240 return count;
1241}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001243static ssize_t show_auto_pwm(struct device *dev,
1244 struct device_attribute *attr, char *buf)
1245{
1246 struct it87_data *data = it87_update_device(dev);
1247 struct sensor_device_attribute_2 *sensor_attr =
1248 to_sensor_dev_attr_2(attr);
1249 int nr = sensor_attr->nr;
1250 int point = sensor_attr->index;
1251
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001252 return sprintf(buf, "%d\n",
1253 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001254}
1255
1256static ssize_t set_auto_pwm(struct device *dev,
1257 struct device_attribute *attr, const char *buf, size_t count)
1258{
1259 struct it87_data *data = dev_get_drvdata(dev);
1260 struct sensor_device_attribute_2 *sensor_attr =
1261 to_sensor_dev_attr_2(attr);
1262 int nr = sensor_attr->nr;
1263 int point = sensor_attr->index;
1264 long val;
1265
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001266 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001267 return -EINVAL;
1268
1269 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001270 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001271 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1272 data->auto_pwm[nr][point]);
1273 mutex_unlock(&data->update_lock);
1274 return count;
1275}
1276
1277static ssize_t show_auto_temp(struct device *dev,
1278 struct device_attribute *attr, char *buf)
1279{
1280 struct it87_data *data = it87_update_device(dev);
1281 struct sensor_device_attribute_2 *sensor_attr =
1282 to_sensor_dev_attr_2(attr);
1283 int nr = sensor_attr->nr;
1284 int point = sensor_attr->index;
1285
1286 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1287}
1288
1289static ssize_t set_auto_temp(struct device *dev,
1290 struct device_attribute *attr, const char *buf, size_t count)
1291{
1292 struct it87_data *data = dev_get_drvdata(dev);
1293 struct sensor_device_attribute_2 *sensor_attr =
1294 to_sensor_dev_attr_2(attr);
1295 int nr = sensor_attr->nr;
1296 int point = sensor_attr->index;
1297 long val;
1298
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001299 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001300 return -EINVAL;
1301
1302 mutex_lock(&data->update_lock);
1303 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1304 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1305 data->auto_temp[nr][point]);
1306 mutex_unlock(&data->update_lock);
1307 return count;
1308}
1309
Guenter Roecke1169ba2012-12-19 22:17:01 +01001310static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1311static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1312 0, 1);
1313static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1314 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Guenter Roecke1169ba2012-12-19 22:17:01 +01001316static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1317static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1318 1, 1);
1319static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1320 set_fan_div, 1);
1321
1322static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1323static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1324 2, 1);
1325static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1326 set_fan_div, 2);
1327
1328static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1329static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1330 3, 1);
1331
1332static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1333static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1334 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001336static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1337static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1338 5, 1);
1339
Guenter Roeckc4458db2012-12-19 22:17:02 +01001340static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1341 show_pwm_enable, set_pwm_enable, 0);
1342static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001343static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1344 set_pwm_freq, 0);
Guenter Roeckc4458db2012-12-19 22:17:02 +01001345static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1346 show_pwm_temp_map, set_pwm_temp_map, 0);
1347static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1348 show_auto_pwm, set_auto_pwm, 0, 0);
1349static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1350 show_auto_pwm, set_auto_pwm, 0, 1);
1351static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1352 show_auto_pwm, set_auto_pwm, 0, 2);
1353static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1354 show_auto_pwm, NULL, 0, 3);
1355static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1356 show_auto_temp, set_auto_temp, 0, 1);
1357static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1358 show_auto_temp, set_auto_temp, 0, 0);
1359static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1360 show_auto_temp, set_auto_temp, 0, 2);
1361static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1362 show_auto_temp, set_auto_temp, 0, 3);
1363static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1364 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Guenter Roeckc4458db2012-12-19 22:17:02 +01001366static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1367 show_pwm_enable, set_pwm_enable, 1);
1368static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001369static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
Guenter Roeckc4458db2012-12-19 22:17:02 +01001370static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1371 show_pwm_temp_map, set_pwm_temp_map, 1);
1372static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1373 show_auto_pwm, set_auto_pwm, 1, 0);
1374static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1375 show_auto_pwm, set_auto_pwm, 1, 1);
1376static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1377 show_auto_pwm, set_auto_pwm, 1, 2);
1378static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1379 show_auto_pwm, NULL, 1, 3);
1380static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1381 show_auto_temp, set_auto_temp, 1, 1);
1382static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1383 show_auto_temp, set_auto_temp, 1, 0);
1384static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1385 show_auto_temp, set_auto_temp, 1, 2);
1386static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1387 show_auto_temp, set_auto_temp, 1, 3);
1388static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1389 show_auto_temp, set_auto_temp, 1, 4);
1390
1391static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1392 show_pwm_enable, set_pwm_enable, 2);
1393static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001394static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
Guenter Roeckc4458db2012-12-19 22:17:02 +01001395static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1396 show_pwm_temp_map, set_pwm_temp_map, 2);
1397static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1398 show_auto_pwm, set_auto_pwm, 2, 0);
1399static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1400 show_auto_pwm, set_auto_pwm, 2, 1);
1401static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1402 show_auto_pwm, set_auto_pwm, 2, 2);
1403static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1404 show_auto_pwm, NULL, 2, 3);
1405static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1406 show_auto_temp, set_auto_temp, 2, 1);
1407static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1408 show_auto_temp, set_auto_temp, 2, 0);
1409static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1410 show_auto_temp, set_auto_temp, 2, 2);
1411static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1412 show_auto_temp, set_auto_temp, 2, 3);
1413static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1414 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Guenter Roeck36c4d982015-03-26 18:18:19 -07001416static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1417 show_pwm_enable, set_pwm_enable, 3);
1418static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001419static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001420static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO | S_IWUSR,
1421 show_pwm_temp_map, set_pwm_temp_map, 3);
1422
1423static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1424 show_pwm_enable, set_pwm_enable, 4);
1425static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001426static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001427static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO | S_IWUSR,
1428 show_pwm_temp_map, set_pwm_temp_map, 4);
1429
1430static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1431 show_pwm_enable, set_pwm_enable, 5);
1432static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001433static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001434static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO | S_IWUSR,
1435 show_pwm_temp_map, set_pwm_temp_map, 5);
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001438static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1439 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001442 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
Jean Delvare1d66c642005-04-18 21:16:59 -07001444static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Jean Delvare0124dd72007-11-25 16:16:41 +01001446static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1447 char *buf)
1448{
1449 int bitnr = to_sensor_dev_attr(attr)->index;
1450 struct it87_data *data = it87_update_device(dev);
1451 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1452}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001453
1454static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1455 *attr, const char *buf, size_t count)
1456{
1457 struct it87_data *data = dev_get_drvdata(dev);
1458 long val;
1459 int config;
1460
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001461 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001462 return -EINVAL;
1463
1464 mutex_lock(&data->update_lock);
1465 config = it87_read_value(data, IT87_REG_CONFIG);
1466 if (config < 0) {
1467 count = config;
1468 } else {
1469 config |= 1 << 5;
1470 it87_write_value(data, IT87_REG_CONFIG, config);
1471 /* Invalidate cache to force re-read */
1472 data->valid = 0;
1473 }
1474 mutex_unlock(&data->update_lock);
1475
1476 return count;
1477}
1478
Jean Delvare0124dd72007-11-25 16:16:41 +01001479static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1480static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1481static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1482static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1483static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1484static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1485static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1486static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1487static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1488static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1489static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1490static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1491static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001492static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001493static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1494static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1495static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001496static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1497 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001498
Jean Delvared9b327c2010-03-05 22:17:17 +01001499static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1500 char *buf)
1501{
1502 int bitnr = to_sensor_dev_attr(attr)->index;
1503 struct it87_data *data = it87_update_device(dev);
1504 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1505}
1506static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1507 const char *buf, size_t count)
1508{
1509 int bitnr = to_sensor_dev_attr(attr)->index;
1510 struct it87_data *data = dev_get_drvdata(dev);
1511 long val;
1512
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001513 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001514 || (val != 0 && val != 1))
1515 return -EINVAL;
1516
1517 mutex_lock(&data->update_lock);
1518 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1519 if (val)
1520 data->beeps |= (1 << bitnr);
1521 else
1522 data->beeps &= ~(1 << bitnr);
1523 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1524 mutex_unlock(&data->update_lock);
1525 return count;
1526}
1527
1528static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1529 show_beep, set_beep, 1);
1530static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1531static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1532static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1533static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1534static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1535static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1536static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1537/* fanX_beep writability is set later */
1538static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1539static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1540static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1541static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1542static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001543static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001544static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1545 show_beep, set_beep, 2);
1546static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1547static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1548
Jean Delvare5f2dc792010-03-05 22:17:18 +01001549static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1550 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Jean Delvare90d66192007-10-08 18:24:35 +02001552 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001553 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001555static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1556 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001558 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001559 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001561 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001562 return -EINVAL;
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 data->vrm = val;
1565
1566 return count;
1567}
1568static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Jean Delvare5f2dc792010-03-05 22:17:18 +01001570static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1571 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 struct it87_data *data = it87_update_device(dev);
1574 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1575}
1576static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001577
Jean Delvare738e5e02010-08-14 21:08:50 +02001578static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1579 char *buf)
1580{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001581 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001582 "+5V",
1583 "5VSB",
1584 "Vbat",
1585 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001586 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001587 "+3.3V",
1588 "3VSB",
1589 "Vbat",
1590 };
1591 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001592 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001593 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001594
Justin Maggardead80802015-08-05 12:53:08 -07001595 if (has_12mv_adc(data) || has_10_9mv_adc(data))
1596 label = labels_it8721[nr];
1597 else
1598 label = labels[nr];
1599
1600 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001601}
1602static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1603static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1604static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001605/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001606static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001607
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001608static ssize_t show_name(struct device *dev, struct device_attribute
1609 *devattr, char *buf)
1610{
1611 struct it87_data *data = dev_get_drvdata(dev);
1612 return sprintf(buf, "%s\n", data->name);
1613}
1614static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1615
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001616static struct attribute *it87_attributes_in[10][5] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001617{
Jean Delvare87808be2006-09-24 21:17:13 +02001618 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001619 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001620 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001621 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001622 NULL
1623}, {
1624 &sensor_dev_attr_in1_input.dev_attr.attr,
1625 &sensor_dev_attr_in1_min.dev_attr.attr,
1626 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001627 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001628 NULL
1629}, {
1630 &sensor_dev_attr_in2_input.dev_attr.attr,
1631 &sensor_dev_attr_in2_min.dev_attr.attr,
1632 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001633 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001634 NULL
1635}, {
1636 &sensor_dev_attr_in3_input.dev_attr.attr,
1637 &sensor_dev_attr_in3_min.dev_attr.attr,
1638 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001639 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001640 NULL
1641}, {
1642 &sensor_dev_attr_in4_input.dev_attr.attr,
1643 &sensor_dev_attr_in4_min.dev_attr.attr,
1644 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001645 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001646 NULL
1647}, {
1648 &sensor_dev_attr_in5_input.dev_attr.attr,
1649 &sensor_dev_attr_in5_min.dev_attr.attr,
1650 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001651 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001652 NULL
1653}, {
1654 &sensor_dev_attr_in6_input.dev_attr.attr,
1655 &sensor_dev_attr_in6_min.dev_attr.attr,
1656 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001657 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001658 NULL
1659}, {
1660 &sensor_dev_attr_in7_input.dev_attr.attr,
1661 &sensor_dev_attr_in7_min.dev_attr.attr,
1662 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001663 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001664 NULL
1665}, {
1666 &sensor_dev_attr_in8_input.dev_attr.attr,
1667 NULL
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001668}, {
1669 &sensor_dev_attr_in9_input.dev_attr.attr,
1670 NULL
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001671} };
Jean Delvare87808be2006-09-24 21:17:13 +02001672
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001673static const struct attribute_group it87_group_in[10] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001674 { .attrs = it87_attributes_in[0] },
1675 { .attrs = it87_attributes_in[1] },
1676 { .attrs = it87_attributes_in[2] },
1677 { .attrs = it87_attributes_in[3] },
1678 { .attrs = it87_attributes_in[4] },
1679 { .attrs = it87_attributes_in[5] },
1680 { .attrs = it87_attributes_in[6] },
1681 { .attrs = it87_attributes_in[7] },
1682 { .attrs = it87_attributes_in[8] },
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001683 { .attrs = it87_attributes_in[9] },
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001684};
1685
Guenter Roeck4573acb2012-03-26 16:17:41 -07001686static struct attribute *it87_attributes_temp[3][6] = {
1687{
Jean Delvare87808be2006-09-24 21:17:13 +02001688 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001689 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001690 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001691 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001692 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001693 NULL
1694} , {
1695 &sensor_dev_attr_temp2_input.dev_attr.attr,
1696 &sensor_dev_attr_temp2_max.dev_attr.attr,
1697 &sensor_dev_attr_temp2_min.dev_attr.attr,
1698 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001699 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001700 NULL
1701} , {
1702 &sensor_dev_attr_temp3_input.dev_attr.attr,
1703 &sensor_dev_attr_temp3_max.dev_attr.attr,
1704 &sensor_dev_attr_temp3_min.dev_attr.attr,
1705 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001706 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001707 NULL
1708} };
Jean Delvare87808be2006-09-24 21:17:13 +02001709
Guenter Roeck4573acb2012-03-26 16:17:41 -07001710static const struct attribute_group it87_group_temp[3] = {
1711 { .attrs = it87_attributes_temp[0] },
1712 { .attrs = it87_attributes_temp[1] },
1713 { .attrs = it87_attributes_temp[2] },
1714};
1715
Guenter Roeck161d8982012-12-19 22:17:01 +01001716static struct attribute *it87_attributes_temp_offset[] = {
1717 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1718 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1719 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1720};
1721
Guenter Roeck4573acb2012-03-26 16:17:41 -07001722static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001723 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001724 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001725 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001726 NULL
1727};
1728
1729static const struct attribute_group it87_group = {
1730 .attrs = it87_attributes,
1731};
1732
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001733static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001734 &sensor_dev_attr_in0_beep.dev_attr.attr,
1735 &sensor_dev_attr_in1_beep.dev_attr.attr,
1736 &sensor_dev_attr_in2_beep.dev_attr.attr,
1737 &sensor_dev_attr_in3_beep.dev_attr.attr,
1738 &sensor_dev_attr_in4_beep.dev_attr.attr,
1739 &sensor_dev_attr_in5_beep.dev_attr.attr,
1740 &sensor_dev_attr_in6_beep.dev_attr.attr,
1741 &sensor_dev_attr_in7_beep.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001742 NULL,
1743 NULL,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001744};
Jean Delvared9b327c2010-03-05 22:17:17 +01001745
Guenter Roeck4573acb2012-03-26 16:17:41 -07001746static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001747 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1748 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1749 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001750};
1751
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001752static struct attribute *it87_attributes_fan[6][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001753 &sensor_dev_attr_fan1_input.dev_attr.attr,
1754 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001755 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1756 NULL
1757}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001758 &sensor_dev_attr_fan2_input.dev_attr.attr,
1759 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001760 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1761 NULL
1762}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001763 &sensor_dev_attr_fan3_input.dev_attr.attr,
1764 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001765 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001766 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001767}, {
1768 &sensor_dev_attr_fan4_input.dev_attr.attr,
1769 &sensor_dev_attr_fan4_min.dev_attr.attr,
1770 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1771 NULL
1772}, {
1773 &sensor_dev_attr_fan5_input.dev_attr.attr,
1774 &sensor_dev_attr_fan5_min.dev_attr.attr,
1775 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1776 NULL
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001777}, {
1778 &sensor_dev_attr_fan6_input.dev_attr.attr,
1779 &sensor_dev_attr_fan6_min.dev_attr.attr,
1780 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1781 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001782} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001783
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001784static const struct attribute_group it87_group_fan[6] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001785 { .attrs = it87_attributes_fan[0] },
1786 { .attrs = it87_attributes_fan[1] },
1787 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001788 { .attrs = it87_attributes_fan[3] },
1789 { .attrs = it87_attributes_fan[4] },
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001790 { .attrs = it87_attributes_fan[5] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001791};
1792
Guenter Roecke1169ba2012-12-19 22:17:01 +01001793static const struct attribute *it87_attributes_fan_div[] = {
1794 &sensor_dev_attr_fan1_div.dev_attr.attr,
1795 &sensor_dev_attr_fan2_div.dev_attr.attr,
1796 &sensor_dev_attr_fan3_div.dev_attr.attr,
1797};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001798
Guenter Roeck36c4d982015-03-26 18:18:19 -07001799static struct attribute *it87_attributes_pwm[6][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001800 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001801 &sensor_dev_attr_pwm1.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001802 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001803 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001804 NULL
1805}, {
1806 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1807 &sensor_dev_attr_pwm2.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001808 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001809 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001810 NULL
1811}, {
1812 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1813 &sensor_dev_attr_pwm3.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001814 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001815 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001816 NULL
Guenter Roeck36c4d982015-03-26 18:18:19 -07001817}, {
1818 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1819 &sensor_dev_attr_pwm4.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001820 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001821 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1822 NULL
1823}, {
1824 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1825 &sensor_dev_attr_pwm5.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001826 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001827 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
1828 NULL
1829}, {
1830 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
1831 &sensor_dev_attr_pwm6.dev_attr.attr,
Guenter Roeck60878bc2015-03-26 19:57:42 -07001832 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001833 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
1834 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001835} };
Jean Delvare87808be2006-09-24 21:17:13 +02001836
Guenter Roeck60878bc2015-03-26 19:57:42 -07001837static umode_t pwm_attribute_mode(struct kobject *kobj, struct attribute *attr,
1838 int index)
1839{
1840 struct device *dev = container_of(kobj, struct device, kobj);
1841 struct it87_data *data = dev_get_drvdata(dev);
1842
1843 if (has_pwm_freq2(data) && index == 2)
1844 return attr->mode | S_IWUSR;
1845
1846 return attr->mode;
1847}
1848
Guenter Roeck36c4d982015-03-26 18:18:19 -07001849static const struct attribute_group it87_group_pwm[6] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001850 { .attrs = it87_attributes_pwm[0] },
Guenter Roeck60878bc2015-03-26 19:57:42 -07001851 { .attrs = it87_attributes_pwm[1],
1852 .is_visible = pwm_attribute_mode, },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001853 { .attrs = it87_attributes_pwm[2] },
Guenter Roeck36c4d982015-03-26 18:18:19 -07001854 { .attrs = it87_attributes_pwm[3] },
1855 { .attrs = it87_attributes_pwm[4] },
1856 { .attrs = it87_attributes_pwm[5] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001857};
1858
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001859static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1860 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1861 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1862 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1863 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1864 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1865 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1866 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1867 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1868 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1869 NULL
1870}, {
1871 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1872 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1873 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1874 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1875 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1876 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1877 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1878 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1879 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1880 NULL
1881}, {
1882 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1883 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1884 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1885 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1886 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1887 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1888 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1889 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1890 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1891 NULL
1892} };
1893
1894static const struct attribute_group it87_group_autopwm[3] = {
1895 { .attrs = it87_attributes_autopwm[0] },
1896 { .attrs = it87_attributes_autopwm[1] },
1897 { .attrs = it87_attributes_autopwm[2] },
1898};
1899
Jean Delvared9b327c2010-03-05 22:17:17 +01001900static struct attribute *it87_attributes_fan_beep[] = {
1901 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1902 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1903 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1904 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1905 &sensor_dev_attr_fan5_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001906 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001907};
1908
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001909static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001910 &dev_attr_vrm.attr,
1911 &dev_attr_cpu0_vid.attr,
1912 NULL
1913};
1914
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001915static const struct attribute_group it87_group_vid = {
1916 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001917};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Jean Delvare738e5e02010-08-14 21:08:50 +02001919static struct attribute *it87_attributes_label[] = {
1920 &sensor_dev_attr_in3_label.dev_attr.attr,
1921 &sensor_dev_attr_in7_label.dev_attr.attr,
1922 &sensor_dev_attr_in8_label.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001923 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare738e5e02010-08-14 21:08:50 +02001924 NULL
1925};
1926
1927static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001928 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001929};
1930
Jean Delvare2d8672c2005-07-19 23:56:35 +02001931/* SuperIO detection - will change isa_address if a chip is found */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07001932static int __init it87_find(int sioaddr, unsigned short *address,
1933 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001935 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001936 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001937 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001938 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Guenter Roeck3c2e3512015-03-28 08:03:10 -07001940 err = superio_enter(sioaddr);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001941 if (err)
1942 return err;
1943
1944 err = -ENODEV;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07001945 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001946
1947 switch (chip_type) {
1948 case IT8705F_DEVID:
1949 sio_data->type = it87;
1950 break;
1951 case IT8712F_DEVID:
1952 sio_data->type = it8712;
1953 break;
1954 case IT8716F_DEVID:
1955 case IT8726F_DEVID:
1956 sio_data->type = it8716;
1957 break;
1958 case IT8718F_DEVID:
1959 sio_data->type = it8718;
1960 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001961 case IT8720F_DEVID:
1962 sio_data->type = it8720;
1963 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001964 case IT8721F_DEVID:
1965 sio_data->type = it8721;
1966 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001967 case IT8728F_DEVID:
1968 sio_data->type = it8728;
1969 break;
Justin Maggardead80802015-08-05 12:53:08 -07001970 case IT8732F_DEVID:
1971 sio_data->type = it8732;
1972 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06001973 case IT8771E_DEVID:
1974 sio_data->type = it8771;
1975 break;
1976 case IT8772E_DEVID:
1977 sio_data->type = it8772;
1978 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08001979 case IT8781F_DEVID:
1980 sio_data->type = it8781;
1981 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001982 case IT8782F_DEVID:
1983 sio_data->type = it8782;
1984 break;
1985 case IT8783E_DEVID:
1986 sio_data->type = it8783;
1987 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001988 case IT8786E_DEVID:
1989 sio_data->type = it8786;
1990 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07001991 case IT8790E_DEVID:
1992 sio_data->type = it8790;
1993 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02001994 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02001995 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001996 sio_data->type = it8603;
1997 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08001998 case IT8620E_DEVID:
1999 sio_data->type = it8620;
2000 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002001 case 0xffff: /* No device at all */
2002 goto exit;
2003 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01002004 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002005 goto exit;
2006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002008 superio_select(sioaddr, PME);
2009 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002010 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 goto exit;
2012 }
2013
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002014 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002016 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 goto exit;
2018 }
2019
2020 err = 0;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002021 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07002022 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2023 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002024 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002026 config = &it87_devices[sio_data->type];
2027
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002028 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002029 if (has_in7_internal(config))
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002030 sio_data->internal |= (1 << 1);
2031
Jean Delvare738e5e02010-08-14 21:08:50 +02002032 /* in8 (Vbat) is always internal */
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002033 sio_data->internal |= (1 << 2);
2034
Guenter Roeck73055402015-03-26 09:16:32 -07002035 /* in9 (AVCC3), always internal if supported */
2036 if (has_avcc3(config))
2037 sio_data->internal |= (1 << 3); /* in9 is AVCC */
2038 else
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002039 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02002040
Guenter Roeck36c4d982015-03-26 18:18:19 -07002041 if (!has_six_pwm(config))
2042 sio_data->skip_pwm |= (1 << 3) | (1 << 4) | (1 << 5);
2043
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002044 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01002045 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01002046
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002047 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2048 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01002049 /* The IT8705F has a different LD number for GPIO */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002050 superio_select(sioaddr, 5);
2051 sio_data->beep_pin = superio_inb(sioaddr,
2052 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002053 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002054 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002055
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002056 superio_select(sioaddr, GPIO);
Guenter Roeck0531d982012-03-02 11:46:44 -08002057
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002058 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2059 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2060 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2061 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2062 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002063
Guenter Roeck0531d982012-03-02 11:46:44 -08002064 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002065 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08002066 sio_data->skip_fan |= (1 << 2);
2067 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002068 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08002069 sio_data->skip_pwm |= (1 << 2);
2070
2071 /* Check if fan2 is there or not */
2072 if (reg27 & (1 << 7))
2073 sio_data->skip_fan |= (1 << 1);
2074 if (reg27 & (1 << 3))
2075 sio_data->skip_pwm |= (1 << 1);
2076
2077 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002078 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002079 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002080
2081 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002082 if (reg27 & (1 << 1))
2083 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002084
2085 /*
2086 * VIN7
2087 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2088 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002089 if (reg27 & (1 << 2)) {
2090 /*
2091 * The data sheet is a bit unclear regarding the
2092 * internal voltage divider for VCCH5V. It says
2093 * "This bit enables and switches VIN7 (pin 91) to the
2094 * internal voltage divider for VCCH5V".
2095 * This is different to other chips, where the internal
2096 * voltage divider would connect VIN7 to an internal
2097 * voltage source. Maybe that is the case here as well.
2098 *
2099 * Since we don't know for sure, re-route it if that is
2100 * not the case, and ask the user to report if the
2101 * resulting voltage is sane.
2102 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002103 if (!(reg2c & (1 << 1))) {
2104 reg2c |= (1 << 1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002105 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2106 reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002107 pr_notice("Routing internal VCCH5V to in7.\n");
2108 }
2109 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2110 pr_notice("Please report if it displays a reasonable voltage.\n");
2111 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002112
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002113 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08002114 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002115 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08002116 sio_data->internal |= (1 << 1);
2117
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002118 sio_data->beep_pin = superio_inb(sioaddr,
2119 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002120 } else if (sio_data->type == it8603) {
2121 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002122
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002123 superio_select(sioaddr, GPIO);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002124
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002125 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002126
2127 /* Check if fan3 is there or not */
2128 if (reg27 & (1 << 6))
2129 sio_data->skip_pwm |= (1 << 2);
2130 if (reg27 & (1 << 7))
2131 sio_data->skip_fan |= (1 << 2);
2132
2133 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002134 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002135 if (reg29 & (1 << 1))
2136 sio_data->skip_pwm |= (1 << 1);
2137 if (reg29 & (1 << 2))
2138 sio_data->skip_fan |= (1 << 1);
2139
2140 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2141 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2142
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002143 sio_data->beep_pin = superio_inb(sioaddr,
2144 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002145 } else if (sio_data->type == it8620) {
2146 int reg;
2147
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002148 superio_select(sioaddr, GPIO);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002149
Guenter Roeck36c4d982015-03-26 18:18:19 -07002150 /* Check for pwm5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002151 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002152 if (reg & (1 << 6))
2153 sio_data->skip_pwm |= (1 << 4);
2154
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002155 /* Check for fan4, fan5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002156 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002157 if (!(reg & (1 << 5)))
2158 sio_data->skip_fan |= (1 << 3);
2159 if (!(reg & (1 << 4)))
2160 sio_data->skip_fan |= (1 << 4);
2161
2162 /* Check for pwm3, fan3 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002163 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002164 if (reg & (1 << 6))
2165 sio_data->skip_pwm |= (1 << 2);
2166 if (reg & (1 << 7))
2167 sio_data->skip_fan |= (1 << 2);
2168
Guenter Roeck36c4d982015-03-26 18:18:19 -07002169 /* Check for pwm4 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002170 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002171 if (!(reg & (1 << 2)))
2172 sio_data->skip_pwm |= (1 << 3);
2173
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002174 /* Check for pwm2, fan2 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002175 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002176 if (reg & (1 << 1))
2177 sio_data->skip_pwm |= (1 << 1);
2178 if (reg & (1 << 2))
2179 sio_data->skip_fan |= (1 << 1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002180 /* Check for pwm6, fan6 */
2181 if (!(reg & (1 << 7))) {
2182 sio_data->skip_pwm |= (1 << 5);
2183 sio_data->skip_fan |= (1 << 5);
2184 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002185
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002186 sio_data->beep_pin = superio_inb(sioaddr,
2187 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002188 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002189 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002190 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002191
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002192 superio_select(sioaddr, GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002193
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002194 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002195 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002196 /* We need at least 4 VID pins */
2197 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002198 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002199 sio_data->skip_vid = 1;
2200 }
Jean Delvare895ff262009-12-09 20:35:47 +01002201 }
2202
Jean Delvare591ec652009-12-09 20:35:48 +01002203 /* Check if fan3 is there or not */
2204 if (reg & (1 << 6))
2205 sio_data->skip_pwm |= (1 << 2);
2206 if (reg & (1 << 7))
2207 sio_data->skip_fan |= (1 << 2);
2208
2209 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002210 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Jean Delvare591ec652009-12-09 20:35:48 +01002211 if (reg & (1 << 1))
2212 sio_data->skip_pwm |= (1 << 1);
2213 if (reg & (1 << 2))
2214 sio_data->skip_fan |= (1 << 1);
2215
Jean Delvare895ff262009-12-09 20:35:47 +01002216 if ((sio_data->type == it8718 || sio_data->type == it8720)
2217 && !(sio_data->skip_vid))
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002218 sio_data->vid_value = superio_inb(sioaddr,
2219 IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002220
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002221 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002222
2223 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2224
Jean Delvare436cad22010-07-09 16:22:48 +02002225 /*
2226 * The IT8720F has no VIN7 pin, so VCCH should always be
2227 * routed internally to VIN7 with an internal divider.
2228 * Curiously, there still is a configuration bit to control
2229 * this, which means it can be set incorrectly. And even
2230 * more curiously, many boards out there are improperly
2231 * configured, even though the IT8720F datasheet claims
2232 * that the internal routing of VCCH to VIN7 is the default
2233 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002234 *
2235 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002236 * If UART6 is enabled, re-route VIN7 to the internal divider
2237 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002238 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002239 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02002240 reg |= (1 << 1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002241 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002242 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002243 }
Jean Delvare87673dd2006-08-28 14:37:19 +02002244 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02002245 sio_data->internal |= (1 << 0);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002246 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02002247 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002248
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002249 /*
2250 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2251 * While VIN7 can be routed to the internal voltage divider,
2252 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002253 *
2254 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2255 * is the temperature source. Since we can not read the
2256 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002257 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002258 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002259 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002260 sio_data->skip_temp |= (1 << 2);
2261 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002262
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002263 sio_data->beep_pin = superio_inb(sioaddr,
2264 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002265 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002266 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002267 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002268
Jean Delvare98dd22c2008-10-09 15:33:58 +02002269 /* Disable specific features based on DMI strings */
2270 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2271 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2272 if (board_vendor && board_name) {
2273 if (strcmp(board_vendor, "nVIDIA") == 0
2274 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002275 /*
2276 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2277 * connected to a fan, but to something else. One user
2278 * has reported instant system power-off when changing
2279 * the PWM2 duty cycle, so we disable it.
2280 * I use the board name string as the trigger in case
2281 * the same board is ever used in other systems.
2282 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002283 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002284 sio_data->skip_pwm = (1 << 1);
2285 }
2286 }
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288exit:
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002289 superio_exit(sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return err;
2291}
2292
Jean Delvare723a0aa2010-03-05 22:17:16 +01002293static void it87_remove_files(struct device *dev)
2294{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002295 struct it87_data *data = dev_get_drvdata(dev);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002296 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002297 int i;
2298
2299 sysfs_remove_group(&dev->kobj, &it87_group);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002300 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002301 if (sio_data->skip_in & (1 << i))
2302 continue;
2303 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2304 if (it87_attributes_in_beep[i])
2305 sysfs_remove_file(&dev->kobj,
2306 it87_attributes_in_beep[i]);
2307 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002308 for (i = 0; i < 3; i++) {
2309 if (!(data->has_temp & (1 << i)))
2310 continue;
2311 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01002312 if (has_temp_offset(data))
2313 sysfs_remove_file(&dev->kobj,
2314 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002315 if (sio_data->beep_pin)
2316 sysfs_remove_file(&dev->kobj,
2317 it87_attributes_temp_beep[i]);
2318 }
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002319 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002320 if (!(data->has_fan & (1 << i)))
2321 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002322 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002323 if (sio_data->beep_pin)
2324 sysfs_remove_file(&dev->kobj,
2325 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002326 if (i < 3 && !has_16bit_fans(data))
2327 sysfs_remove_file(&dev->kobj,
2328 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002329 }
Guenter Roeck36c4d982015-03-26 18:18:19 -07002330 for (i = 0; i < 6; i++) {
Guenter Roeck1696d1d2015-03-27 06:03:41 -07002331 if (sio_data->skip_pwm & (1 << i))
Jean Delvare723a0aa2010-03-05 22:17:16 +01002332 continue;
2333 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002334 if (has_old_autopwm(data))
2335 sysfs_remove_group(&dev->kobj,
2336 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002337 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002338 if (!sio_data->skip_vid)
2339 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02002340 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002341}
2342
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002343static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002346 struct resource *res;
2347 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002348 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002349 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01002351 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002353 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002354 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2355 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002356 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2357 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002358 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002359 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Guenter Roeck62a1d052012-03-24 21:54:41 -07002362 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2363 if (!data)
2364 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002366 data->addr = res->start;
2367 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002368 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002369 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002370 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002371 data->name = it87_devices[sio_data->type].name;
2372 /*
2373 * IT8705F Datasheet 0.4.1, 3h == Version G.
2374 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2375 * These are the first revisions with 16-bit tachometer support.
2376 */
2377 switch (data->type) {
2378 case it87:
2379 if (sio_data->revision >= 0x03) {
2380 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002381 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002382 }
2383 break;
2384 case it8712:
2385 if (sio_data->revision >= 0x08) {
2386 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002387 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2388 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002389 }
2390 break;
2391 default:
2392 break;
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002396 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002397 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2398 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002400 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002402 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002405 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002407 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002408 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002409 if (sio_data->internal & (1 << 0))
2410 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2411 if (sio_data->internal & (1 << 1))
2412 data->in_scaled |= (1 << 7); /* in7 is VSB */
2413 if (sio_data->internal & (1 << 2))
2414 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002415 if (sio_data->internal & (1 << 3))
2416 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002417 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2418 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002419 if (sio_data->internal & (1 << 0))
2420 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2421 if (sio_data->internal & (1 << 1))
2422 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002423 }
2424
Guenter Roeck4573acb2012-03-26 16:17:41 -07002425 data->has_temp = 0x07;
2426 if (sio_data->skip_temp & (1 << 2)) {
2427 if (sio_data->type == it8782
2428 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2429 data->has_temp &= ~(1 << 2);
2430 }
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002433 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002436 err = sysfs_create_group(&dev->kobj, &it87_group);
2437 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002438 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002439
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002440 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002441 if (sio_data->skip_in & (1 << i))
2442 continue;
2443 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2444 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002445 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002446 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2447 err = sysfs_create_file(&dev->kobj,
2448 it87_attributes_in_beep[i]);
2449 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002450 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002451 }
2452 }
2453
Guenter Roeck4573acb2012-03-26 16:17:41 -07002454 for (i = 0; i < 3; i++) {
2455 if (!(data->has_temp & (1 << i)))
2456 continue;
2457 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002458 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002459 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002460 if (has_temp_offset(data)) {
2461 err = sysfs_create_file(&dev->kobj,
2462 it87_attributes_temp_offset[i]);
2463 if (err)
2464 goto error;
2465 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002466 if (sio_data->beep_pin) {
2467 err = sysfs_create_file(&dev->kobj,
2468 it87_attributes_temp_beep[i]);
2469 if (err)
2470 goto error;
2471 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002472 }
2473
Jean Delvare9060f8b2006-08-28 14:24:17 +02002474 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002475 fan_beep_need_rw = 1;
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002476 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002477 if (!(data->has_fan & (1 << i)))
2478 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002479 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002480 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002481 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002482
Guenter Roecke1169ba2012-12-19 22:17:01 +01002483 if (i < 3 && !has_16bit_fans(data)) {
2484 err = sysfs_create_file(&dev->kobj,
2485 it87_attributes_fan_div[i]);
2486 if (err)
2487 goto error;
2488 }
2489
Jean Delvared9b327c2010-03-05 22:17:17 +01002490 if (sio_data->beep_pin) {
2491 err = sysfs_create_file(&dev->kobj,
2492 it87_attributes_fan_beep[i]);
2493 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002494 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002495 if (!fan_beep_need_rw)
2496 continue;
2497
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002498 /*
2499 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002500 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002501 * for it.
2502 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002503 if (sysfs_chmod_file(&dev->kobj,
2504 it87_attributes_fan_beep[i],
2505 S_IRUGO | S_IWUSR))
2506 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2507 i + 1);
2508 fan_beep_need_rw = 0;
2509 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002510 }
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 if (enable_pwm_interface) {
Guenter Roeck36c4d982015-03-26 18:18:19 -07002513 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002514 if (sio_data->skip_pwm & (1 << i))
2515 continue;
2516 err = sysfs_create_group(&dev->kobj,
2517 &it87_group_pwm[i]);
2518 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002519 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002520
2521 if (!has_old_autopwm(data))
2522 continue;
2523 err = sysfs_create_group(&dev->kobj,
2524 &it87_group_autopwm[i]);
2525 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002526 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
2529
Jean Delvare895ff262009-12-09 20:35:47 +01002530 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002531 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002532 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002533 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002534 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2535 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002536 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002537 }
2538
Jean Delvare738e5e02010-08-14 21:08:50 +02002539 /* Export labels for internal sensors */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002540 for (i = 0; i < 4; i++) {
Jean Delvare738e5e02010-08-14 21:08:50 +02002541 if (!(sio_data->internal & (1 << i)))
2542 continue;
2543 err = sysfs_create_file(&dev->kobj,
2544 it87_attributes_label[i]);
2545 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002546 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002547 }
2548
Tony Jones1beeffe2007-08-20 13:46:20 -07002549 data->hwmon_dev = hwmon_device_register(dev);
2550 if (IS_ERR(data->hwmon_dev)) {
2551 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002552 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
2554
2555 return 0;
2556
Guenter Roeck62a1d052012-03-24 21:54:41 -07002557error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002558 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return err;
2560}
2561
Bill Pemberton281dfd02012-11-19 13:25:51 -05002562static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002564 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Tony Jones1beeffe2007-08-20 13:46:20 -07002566 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002567 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return 0;
2570}
2571
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002572/*
2573 * Must be called with data->update_lock held, except during initialization.
2574 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2575 * would slow down the IT87 access and should not be necessary.
2576 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002577static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002579 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2580 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002583/*
2584 * Must be called with data->update_lock held, except during initialization.
2585 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2586 * would slow down the IT87 access and should not be necessary.
2587 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002588static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002590 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2591 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
2594/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002595static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002597 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002598 /*
2599 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002601 * disable pwm control to protect the user.
2602 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002603 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if ((tmp & 0x87) == 0) {
2605 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002606 /*
2607 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002609 * inverting all fan speed values.
2610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 int i;
2612 u8 pwm[3];
2613
2614 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002615 pwm[i] = it87_read_value(data,
Guenter Roeck36c4d982015-03-26 18:18:19 -07002616 IT87_REG_PWM[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002618 /*
2619 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 * might be correct, as suspicious as it seems, so we
2621 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002622 * PWM interface).
2623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002625 dev_info(dev,
2626 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002627 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 tmp | 0x87);
2629 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002630 it87_write_value(data,
Guenter Roeck36c4d982015-03-26 18:18:19 -07002631 IT87_REG_PWM[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 0x7f & ~pwm[i]);
2633 return 1;
2634 }
2635
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002636 dev_info(dev,
2637 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
2639
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002640 dev_info(dev,
2641 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return 0;
2643 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002644 dev_info(dev,
2645 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
2647
2648 return 1;
2649}
2650
2651/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002652static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002654 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002655 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002657 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002659 /*
2660 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002661 * - If it is in automatic mode, setting to manual mode should set
2662 * the fan to full speed by default.
2663 * - If it is in manual mode, we need a mapping to temperature
2664 * channels to use when later setting to automatic mode later.
2665 * Use a 1:1 mapping by default (we are clueless.)
2666 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002667 * prior to switching to a different mode.
2668 * Note that this is no longer needed for the IT8721F and later, as
2669 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002670 * manual duty cycle.
2671 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002673 data->pwm_temp_map[i] = i;
2674 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002675 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 }
2677
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002678 /*
2679 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002680 * registers. For low voltage limits it makes no sense and triggers
2681 * alarms, so change to 0 instead. For high temperature limits, it
2682 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002683 * but is still confusing, so change to 127 degrees C.
2684 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002685 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002686 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002687 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002688 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002689 }
2690 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002691 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002692 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002693 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002694 }
2695
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002696 /*
2697 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002698 * set to two different sensor types and we can't guess which one
2699 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002700 * run-time through the temp{1-3}_type sysfs accessors if needed.
2701 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002704 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 if ((tmp & 0xff) == 0) {
2706 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002707 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
2709
2710 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002711 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002712 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002713 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002715 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002716 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2717 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002719 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002721 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2722
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002723 /* Set tachometers to 16-bit mode if needed */
2724 if (has_fan16_config(data)) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002725 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002726 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002727 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002728 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002729 tmp | 0x07);
2730 }
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002731 }
2732
2733 /* Check for additional fans */
2734 if (has_five_fans(data)) {
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002735 if (tmp & (1 << 4))
2736 data->has_fan |= (1 << 3); /* fan4 enabled */
2737 if (tmp & (1 << 5))
2738 data->has_fan |= (1 << 4); /* fan5 enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002739 if (has_six_fans(data) && (tmp & (1 << 2)))
2740 data->has_fan |= (1 << 5); /* fan6 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02002741 }
2742
Jean Delvare591ec652009-12-09 20:35:48 +01002743 /* Fan input pins may be used for alternative functions */
2744 data->has_fan &= ~sio_data->skip_fan;
2745
Guenter Roeck36c4d982015-03-26 18:18:19 -07002746 /* Check if pwm5, pwm6 are enabled */
2747 if (has_six_pwm(data)) {
2748 /* The following code may be IT8620E specific */
2749 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2750 if ((tmp & 0xc0) == 0xc0)
2751 sio_data->skip_pwm |= (1 << 4);
2752 if (!(tmp & (1 << 3)))
2753 sio_data->skip_pwm |= (1 << 5);
2754 }
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002757 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002758 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 | (update_vbat ? 0x41 : 0x01));
2760}
2761
Jean Delvareb99883d2010-03-05 22:17:15 +01002762static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2763{
Guenter Roeck36c4d982015-03-26 18:18:19 -07002764 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
Jean Delvare16b5dda2012-01-16 22:51:48 +01002765 if (has_newer_autopwm(data)) {
Guenter Roeck36c4d982015-03-26 18:18:19 -07002766 data->pwm_temp_map[nr] = (data->pwm_ctrl[nr] & 0x03) +
2767 nr < 3 ? 0 : 3;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002768 data->pwm_duty[nr] = it87_read_value(data,
Guenter Roeck36c4d982015-03-26 18:18:19 -07002769 IT87_REG_PWM_DUTY[nr]);
Jean Delvare6229cdb2010-12-08 16:27:22 +01002770 } else {
2771 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2772 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2773 else /* Manual mode */
2774 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2775 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002776
2777 if (has_old_autopwm(data)) {
2778 int i;
2779
2780 for (i = 0; i < 5 ; i++)
2781 data->auto_temp[nr][i] = it87_read_value(data,
2782 IT87_REG_AUTO_TEMP(nr, i));
2783 for (i = 0; i < 3 ; i++)
2784 data->auto_pwm[nr][i] = it87_read_value(data,
2785 IT87_REG_AUTO_PWM(nr, i));
2786 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002787}
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789static struct it87_data *it87_update_device(struct device *dev)
2790{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002791 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 int i;
2793
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002794 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2797 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002799 /*
2800 * Cleared after each update, so reenable. Value
2801 * returned by this read will be previous value
2802 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002803 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002804 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 }
2806 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002807 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002808 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002809 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002810 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002811 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002812 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
Jean Delvare3543a532006-08-28 14:27:25 +02002814 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002815 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Guenter Roeck73055402015-03-26 09:16:32 -07002816 if (has_avcc3(data))
2817 data->in[9][0] = it87_read_value(data, IT87_REG_AVCC3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002819 for (i = 0; i < 6; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002820 /* Skip disabled fans */
2821 if (!(data->has_fan & (1 << i)))
2822 continue;
2823
Guenter Roecke1169ba2012-12-19 22:17:01 +01002824 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002825 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002826 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002827 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002828 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002829 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002830 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002831 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002832 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002833 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
2836 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002837 if (!(data->has_temp & (1 << i)))
2838 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002839 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002840 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002841 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002842 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002843 data->temp[i][2] =
2844 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002845 if (has_temp_offset(data))
2846 data->temp[i][3] =
2847 it87_read_value(data,
2848 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
2850
Jean Delvare17d648b2006-08-28 14:23:46 +02002851 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002852 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002853 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002854 data->fan_div[0] = i & 0x07;
2855 data->fan_div[1] = (i >> 3) & 0x07;
2856 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002860 it87_read_value(data, IT87_REG_ALARM1) |
2861 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2862 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002863 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002864
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002865 data->fan_main_ctrl = it87_read_value(data,
2866 IT87_REG_FAN_MAIN_CTRL);
2867 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002868 for (i = 0; i < 6; i++)
Jean Delvareb99883d2010-03-05 22:17:15 +01002869 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002871 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002872 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002873 /*
2874 * The IT8705F does not have VID capability.
2875 * The IT8718F and later don't use IT87_REG_VID for the
2876 * same purpose.
2877 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002878 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002879 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002880 /*
2881 * The older IT8712F revisions had only 5 VID pins,
2882 * but we assume it is always safe to read 6 bits.
2883 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002884 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
2886 data->last_updated = jiffies;
2887 data->valid = 1;
2888 }
2889
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002890 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 return data;
2893}
2894
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002895static int __init it87_device_add(unsigned short address,
2896 const struct it87_sio_data *sio_data)
2897{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002898 struct platform_device *pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002899 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002900 .start = address + IT87_EC_OFFSET,
2901 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002902 .name = DRVNAME,
2903 .flags = IORESOURCE_IO,
2904 };
2905 int err;
2906
Jean Delvareb9acb642009-01-07 16:37:35 +01002907 err = acpi_check_resource_conflict(&res);
2908 if (err)
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002909 return err;
Jean Delvareb9acb642009-01-07 16:37:35 +01002910
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002911 pdev = platform_device_alloc(DRVNAME, address);
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002912 if (!pdev)
2913 return -ENOMEM;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002914
2915 err = platform_device_add_resources(pdev, &res, 1);
2916 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002917 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002918 goto exit_device_put;
2919 }
2920
2921 err = platform_device_add_data(pdev, sio_data,
2922 sizeof(struct it87_sio_data));
2923 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002924 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002925 goto exit_device_put;
2926 }
2927
2928 err = platform_device_add(pdev);
2929 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002930 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002931 goto exit_device_put;
2932 }
2933
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002934 it87_pdev = pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002935 return 0;
2936
2937exit_device_put:
2938 platform_device_put(pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002939 return err;
2940}
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942static int __init sm_it87_init(void)
2943{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002944 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002945 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002946 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002947
Jean Delvare98dd22c2008-10-09 15:33:58 +02002948 memset(&sio_data, 0, sizeof(struct it87_sio_data));
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002949 err = it87_find(REG_2E, &isa_address, &sio_data);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002950 if (err)
2951 return err;
2952 err = platform_driver_register(&it87_driver);
2953 if (err)
2954 return err;
2955
2956 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002957 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002958 platform_driver_unregister(&it87_driver);
2959 return err;
2960 }
2961
2962 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964
2965static void __exit sm_it87_exit(void)
2966{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002967 platform_device_unregister(it87_pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002968 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
2971
Jean Delvare7c81c602014-01-29 20:40:08 +01002972MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002973MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974module_param(update_vbat, bool, 0);
2975MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2976module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002977MODULE_PARM_DESC(fix_pwm_polarity,
2978 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979MODULE_LICENSE("GPL");
2980
2981module_init(sm_it87_init);
2982module_exit(sm_it87_exit);