blob: 9b36987d7949ec4298ad41bc4710c04364623d99 [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
corentin.labbeb74f3fd2007-06-13 20:27:36 +020081static struct platform_device *pdev;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define REG 0x2e /* The register to read/write */
84#define DEV 0x07 /* Register: Logical device select */
85#define VAL 0x2f /* The value to read/write */
86#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
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020094static inline int superio_inb(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 outb(reg, REG);
97 return inb(VAL);
98}
99
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200100static inline void superio_outb(int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +0200101{
102 outb(reg, REG);
103 outb(val, VAL);
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static int superio_inw(int reg)
107{
108 int val;
109 outb(reg++, REG);
110 val = inb(VAL) << 8;
111 outb(reg, REG);
112 val |= inb(VAL);
113 return val;
114}
115
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200116static inline void superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200119 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200122static inline int superio_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200124 /*
125 * Try to reserve REG and REG + 1 for exclusive access.
126 */
127 if (!request_muxed_region(REG, 2, DRVNAME))
128 return -EBUSY;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 outb(0x87, REG);
131 outb(0x01, REG);
132 outb(0x55, REG);
133 outb(0x55, REG);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200137static inline void superio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 outb(0x02, REG);
140 outb(0x02, VAL);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200141 release_region(REG, 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 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001932static int __init it87_find(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
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001940 err = superio_enter();
1941 if (err)
1942 return err;
1943
1944 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001945 chip_type = force_id ? force_id : superio_inw(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
Jean Delvare87673dd2006-08-28 14:37:19 +02002008 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (!(superio_inb(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
2014 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
2015 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;
Andrew Paprocki04751692008-08-06 22:41:06 +02002021 sio_data->revision = superio_inb(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 */
2050 superio_select(5);
2051 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002052 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002053 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002054
Guenter Roeck0531d982012-03-02 11:46:44 -08002055 superio_select(GPIO);
2056
2057 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
2058 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002059 reg2a = superio_inb(IT87_SIO_PINX1_REG);
2060 reg2c = superio_inb(IT87_SIO_PINX2_REG);
2061 regef = superio_inb(IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002062
Guenter Roeck0531d982012-03-02 11:46:44 -08002063 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002064 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08002065 sio_data->skip_fan |= (1 << 2);
2066 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002067 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08002068 sio_data->skip_pwm |= (1 << 2);
2069
2070 /* Check if fan2 is there or not */
2071 if (reg27 & (1 << 7))
2072 sio_data->skip_fan |= (1 << 1);
2073 if (reg27 & (1 << 3))
2074 sio_data->skip_pwm |= (1 << 1);
2075
2076 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002077 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002078 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002079
2080 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002081 if (reg27 & (1 << 1))
2082 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002083
2084 /*
2085 * VIN7
2086 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2087 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002088 if (reg27 & (1 << 2)) {
2089 /*
2090 * The data sheet is a bit unclear regarding the
2091 * internal voltage divider for VCCH5V. It says
2092 * "This bit enables and switches VIN7 (pin 91) to the
2093 * internal voltage divider for VCCH5V".
2094 * This is different to other chips, where the internal
2095 * voltage divider would connect VIN7 to an internal
2096 * voltage source. Maybe that is the case here as well.
2097 *
2098 * Since we don't know for sure, re-route it if that is
2099 * not the case, and ask the user to report if the
2100 * resulting voltage is sane.
2101 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002102 if (!(reg2c & (1 << 1))) {
2103 reg2c |= (1 << 1);
2104 superio_outb(IT87_SIO_PINX2_REG, reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002105 pr_notice("Routing internal VCCH5V to in7.\n");
2106 }
2107 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2108 pr_notice("Please report if it displays a reasonable voltage.\n");
2109 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002110
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002111 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08002112 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002113 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08002114 sio_data->internal |= (1 << 1);
2115
2116 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002117 } else if (sio_data->type == it8603) {
2118 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002119
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002120 superio_select(GPIO);
2121
2122 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
2123
2124 /* Check if fan3 is there or not */
2125 if (reg27 & (1 << 6))
2126 sio_data->skip_pwm |= (1 << 2);
2127 if (reg27 & (1 << 7))
2128 sio_data->skip_fan |= (1 << 2);
2129
2130 /* Check if fan2 is there or not */
2131 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
2132 if (reg29 & (1 << 1))
2133 sio_data->skip_pwm |= (1 << 1);
2134 if (reg29 & (1 << 2))
2135 sio_data->skip_fan |= (1 << 1);
2136
2137 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2138 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2139
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002140 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002141 } else if (sio_data->type == it8620) {
2142 int reg;
2143
2144 superio_select(GPIO);
2145
Guenter Roeck36c4d982015-03-26 18:18:19 -07002146 /* Check for pwm5 */
2147 reg = superio_inb(IT87_SIO_GPIO1_REG);
2148 if (reg & (1 << 6))
2149 sio_data->skip_pwm |= (1 << 4);
2150
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002151 /* Check for fan4, fan5 */
2152 reg = superio_inb(IT87_SIO_GPIO2_REG);
2153 if (!(reg & (1 << 5)))
2154 sio_data->skip_fan |= (1 << 3);
2155 if (!(reg & (1 << 4)))
2156 sio_data->skip_fan |= (1 << 4);
2157
2158 /* Check for pwm3, fan3 */
2159 reg = superio_inb(IT87_SIO_GPIO3_REG);
2160 if (reg & (1 << 6))
2161 sio_data->skip_pwm |= (1 << 2);
2162 if (reg & (1 << 7))
2163 sio_data->skip_fan |= (1 << 2);
2164
Guenter Roeck36c4d982015-03-26 18:18:19 -07002165 /* Check for pwm4 */
2166 reg = superio_inb(IT87_SIO_GPIO4_REG);
2167 if (!(reg & (1 << 2)))
2168 sio_data->skip_pwm |= (1 << 3);
2169
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002170 /* Check for pwm2, fan2 */
2171 reg = superio_inb(IT87_SIO_GPIO5_REG);
2172 if (reg & (1 << 1))
2173 sio_data->skip_pwm |= (1 << 1);
2174 if (reg & (1 << 2))
2175 sio_data->skip_fan |= (1 << 1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002176 /* Check for pwm6, fan6 */
2177 if (!(reg & (1 << 7))) {
2178 sio_data->skip_pwm |= (1 << 5);
2179 sio_data->skip_fan |= (1 << 5);
2180 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002181
2182 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002183 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002184 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002185 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002186
2187 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002188
Jean Delvare895ff262009-12-09 20:35:47 +01002189 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002190 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002191 /* We need at least 4 VID pins */
2192 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002193 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002194 sio_data->skip_vid = 1;
2195 }
Jean Delvare895ff262009-12-09 20:35:47 +01002196 }
2197
Jean Delvare591ec652009-12-09 20:35:48 +01002198 /* Check if fan3 is there or not */
2199 if (reg & (1 << 6))
2200 sio_data->skip_pwm |= (1 << 2);
2201 if (reg & (1 << 7))
2202 sio_data->skip_fan |= (1 << 2);
2203
2204 /* Check if fan2 is there or not */
2205 reg = superio_inb(IT87_SIO_GPIO5_REG);
2206 if (reg & (1 << 1))
2207 sio_data->skip_pwm |= (1 << 1);
2208 if (reg & (1 << 2))
2209 sio_data->skip_fan |= (1 << 1);
2210
Jean Delvare895ff262009-12-09 20:35:47 +01002211 if ((sio_data->type == it8718 || sio_data->type == it8720)
2212 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002213 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002214
2215 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002216
2217 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2218
Jean Delvare436cad22010-07-09 16:22:48 +02002219 /*
2220 * The IT8720F has no VIN7 pin, so VCCH should always be
2221 * routed internally to VIN7 with an internal divider.
2222 * Curiously, there still is a configuration bit to control
2223 * this, which means it can be set incorrectly. And even
2224 * more curiously, many boards out there are improperly
2225 * configured, even though the IT8720F datasheet claims
2226 * that the internal routing of VCCH to VIN7 is the default
2227 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002228 *
2229 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002230 * If UART6 is enabled, re-route VIN7 to the internal divider
2231 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002232 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002233 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02002234 reg |= (1 << 1);
2235 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002236 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002237 }
Jean Delvare87673dd2006-08-28 14:37:19 +02002238 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02002239 sio_data->internal |= (1 << 0);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002240 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02002241 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002242
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002243 /*
2244 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2245 * While VIN7 can be routed to the internal voltage divider,
2246 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002247 *
2248 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2249 * is the temperature source. Since we can not read the
2250 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002251 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002252 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002253 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002254 sio_data->skip_temp |= (1 << 2);
2255 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002256
Jean Delvared9b327c2010-03-05 22:17:17 +01002257 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002258 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002259 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002260 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002261
Jean Delvare98dd22c2008-10-09 15:33:58 +02002262 /* Disable specific features based on DMI strings */
2263 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2264 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2265 if (board_vendor && board_name) {
2266 if (strcmp(board_vendor, "nVIDIA") == 0
2267 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002268 /*
2269 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2270 * connected to a fan, but to something else. One user
2271 * has reported instant system power-off when changing
2272 * the PWM2 duty cycle, so we disable it.
2273 * I use the board name string as the trigger in case
2274 * the same board is ever used in other systems.
2275 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002276 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002277 sio_data->skip_pwm = (1 << 1);
2278 }
2279 }
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281exit:
2282 superio_exit();
2283 return err;
2284}
2285
Jean Delvare723a0aa2010-03-05 22:17:16 +01002286static void it87_remove_files(struct device *dev)
2287{
2288 struct it87_data *data = platform_get_drvdata(pdev);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002289 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002290 int i;
2291
2292 sysfs_remove_group(&dev->kobj, &it87_group);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002293 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002294 if (sio_data->skip_in & (1 << i))
2295 continue;
2296 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2297 if (it87_attributes_in_beep[i])
2298 sysfs_remove_file(&dev->kobj,
2299 it87_attributes_in_beep[i]);
2300 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002301 for (i = 0; i < 3; i++) {
2302 if (!(data->has_temp & (1 << i)))
2303 continue;
2304 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01002305 if (has_temp_offset(data))
2306 sysfs_remove_file(&dev->kobj,
2307 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002308 if (sio_data->beep_pin)
2309 sysfs_remove_file(&dev->kobj,
2310 it87_attributes_temp_beep[i]);
2311 }
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002312 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002313 if (!(data->has_fan & (1 << i)))
2314 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002315 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002316 if (sio_data->beep_pin)
2317 sysfs_remove_file(&dev->kobj,
2318 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002319 if (i < 3 && !has_16bit_fans(data))
2320 sysfs_remove_file(&dev->kobj,
2321 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002322 }
Guenter Roeck36c4d982015-03-26 18:18:19 -07002323 for (i = 0; i < 6; i++) {
Guenter Roeck1696d1d2015-03-27 06:03:41 -07002324 if (sio_data->skip_pwm & (1 << i))
Jean Delvare723a0aa2010-03-05 22:17:16 +01002325 continue;
2326 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002327 if (has_old_autopwm(data))
2328 sysfs_remove_group(&dev->kobj,
2329 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002330 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002331 if (!sio_data->skip_vid)
2332 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02002333 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002334}
2335
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002336static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002339 struct resource *res;
2340 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002341 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002342 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01002344 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002346 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002347 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2348 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002349 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2350 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002351 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002352 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Guenter Roeck62a1d052012-03-24 21:54:41 -07002355 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2356 if (!data)
2357 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002359 data->addr = res->start;
2360 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002361 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002362 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002363 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002364 data->name = it87_devices[sio_data->type].name;
2365 /*
2366 * IT8705F Datasheet 0.4.1, 3h == Version G.
2367 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2368 * These are the first revisions with 16-bit tachometer support.
2369 */
2370 switch (data->type) {
2371 case it87:
2372 if (sio_data->revision >= 0x03) {
2373 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002374 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002375 }
2376 break;
2377 case it8712:
2378 if (sio_data->revision >= 0x08) {
2379 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002380 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2381 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002382 }
2383 break;
2384 default:
2385 break;
2386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002389 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002390 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2391 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002393 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002395 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002398 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002400 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002401 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002402 if (sio_data->internal & (1 << 0))
2403 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2404 if (sio_data->internal & (1 << 1))
2405 data->in_scaled |= (1 << 7); /* in7 is VSB */
2406 if (sio_data->internal & (1 << 2))
2407 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002408 if (sio_data->internal & (1 << 3))
2409 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002410 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2411 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002412 if (sio_data->internal & (1 << 0))
2413 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2414 if (sio_data->internal & (1 << 1))
2415 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002416 }
2417
Guenter Roeck4573acb2012-03-26 16:17:41 -07002418 data->has_temp = 0x07;
2419 if (sio_data->skip_temp & (1 << 2)) {
2420 if (sio_data->type == it8782
2421 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2422 data->has_temp &= ~(1 << 2);
2423 }
2424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002426 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002429 err = sysfs_create_group(&dev->kobj, &it87_group);
2430 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002431 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002432
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002433 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002434 if (sio_data->skip_in & (1 << i))
2435 continue;
2436 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2437 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002438 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002439 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2440 err = sysfs_create_file(&dev->kobj,
2441 it87_attributes_in_beep[i]);
2442 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002443 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002444 }
2445 }
2446
Guenter Roeck4573acb2012-03-26 16:17:41 -07002447 for (i = 0; i < 3; i++) {
2448 if (!(data->has_temp & (1 << i)))
2449 continue;
2450 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002451 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002452 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002453 if (has_temp_offset(data)) {
2454 err = sysfs_create_file(&dev->kobj,
2455 it87_attributes_temp_offset[i]);
2456 if (err)
2457 goto error;
2458 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002459 if (sio_data->beep_pin) {
2460 err = sysfs_create_file(&dev->kobj,
2461 it87_attributes_temp_beep[i]);
2462 if (err)
2463 goto error;
2464 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002465 }
2466
Jean Delvare9060f8b2006-08-28 14:24:17 +02002467 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002468 fan_beep_need_rw = 1;
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002469 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002470 if (!(data->has_fan & (1 << i)))
2471 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002472 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002473 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002474 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002475
Guenter Roecke1169ba2012-12-19 22:17:01 +01002476 if (i < 3 && !has_16bit_fans(data)) {
2477 err = sysfs_create_file(&dev->kobj,
2478 it87_attributes_fan_div[i]);
2479 if (err)
2480 goto error;
2481 }
2482
Jean Delvared9b327c2010-03-05 22:17:17 +01002483 if (sio_data->beep_pin) {
2484 err = sysfs_create_file(&dev->kobj,
2485 it87_attributes_fan_beep[i]);
2486 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002487 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002488 if (!fan_beep_need_rw)
2489 continue;
2490
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002491 /*
2492 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002493 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002494 * for it.
2495 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002496 if (sysfs_chmod_file(&dev->kobj,
2497 it87_attributes_fan_beep[i],
2498 S_IRUGO | S_IWUSR))
2499 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2500 i + 1);
2501 fan_beep_need_rw = 0;
2502 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002503 }
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (enable_pwm_interface) {
Guenter Roeck36c4d982015-03-26 18:18:19 -07002506 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002507 if (sio_data->skip_pwm & (1 << i))
2508 continue;
2509 err = sysfs_create_group(&dev->kobj,
2510 &it87_group_pwm[i]);
2511 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002512 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002513
2514 if (!has_old_autopwm(data))
2515 continue;
2516 err = sysfs_create_group(&dev->kobj,
2517 &it87_group_autopwm[i]);
2518 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002519 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
2522
Jean Delvare895ff262009-12-09 20:35:47 +01002523 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002524 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002525 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002526 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002527 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2528 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002529 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002530 }
2531
Jean Delvare738e5e02010-08-14 21:08:50 +02002532 /* Export labels for internal sensors */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002533 for (i = 0; i < 4; i++) {
Jean Delvare738e5e02010-08-14 21:08:50 +02002534 if (!(sio_data->internal & (1 << i)))
2535 continue;
2536 err = sysfs_create_file(&dev->kobj,
2537 it87_attributes_label[i]);
2538 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002539 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002540 }
2541
Tony Jones1beeffe2007-08-20 13:46:20 -07002542 data->hwmon_dev = hwmon_device_register(dev);
2543 if (IS_ERR(data->hwmon_dev)) {
2544 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002545 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
2547
2548 return 0;
2549
Guenter Roeck62a1d052012-03-24 21:54:41 -07002550error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002551 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return err;
2553}
2554
Bill Pemberton281dfd02012-11-19 13:25:51 -05002555static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002557 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Tony Jones1beeffe2007-08-20 13:46:20 -07002559 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002560 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return 0;
2563}
2564
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002565/*
2566 * Must be called with data->update_lock held, except during initialization.
2567 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2568 * would slow down the IT87 access and should not be necessary.
2569 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002570static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002572 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2573 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002576/*
2577 * Must be called with data->update_lock held, except during initialization.
2578 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2579 * would slow down the IT87 access and should not be necessary.
2580 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002581static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002583 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2584 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586
2587/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002588static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002590 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002591 /*
2592 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002594 * disable pwm control to protect the user.
2595 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002596 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 if ((tmp & 0x87) == 0) {
2598 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002599 /*
2600 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002602 * inverting all fan speed values.
2603 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 int i;
2605 u8 pwm[3];
2606
2607 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002608 pwm[i] = it87_read_value(data,
Guenter Roeck36c4d982015-03-26 18:18:19 -07002609 IT87_REG_PWM[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002611 /*
2612 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 * might be correct, as suspicious as it seems, so we
2614 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002615 * PWM interface).
2616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002618 dev_info(dev,
2619 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002620 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 tmp | 0x87);
2622 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002623 it87_write_value(data,
Guenter Roeck36c4d982015-03-26 18:18:19 -07002624 IT87_REG_PWM[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 0x7f & ~pwm[i]);
2626 return 1;
2627 }
2628
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002629 dev_info(dev,
2630 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002633 dev_info(dev,
2634 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return 0;
2636 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002637 dev_info(dev,
2638 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
2640
2641 return 1;
2642}
2643
2644/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002645static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002647 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002648 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002650 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002652 /*
2653 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002654 * - If it is in automatic mode, setting to manual mode should set
2655 * the fan to full speed by default.
2656 * - If it is in manual mode, we need a mapping to temperature
2657 * channels to use when later setting to automatic mode later.
2658 * Use a 1:1 mapping by default (we are clueless.)
2659 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002660 * prior to switching to a different mode.
2661 * Note that this is no longer needed for the IT8721F and later, as
2662 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002663 * manual duty cycle.
2664 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002666 data->pwm_temp_map[i] = i;
2667 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002668 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002671 /*
2672 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002673 * registers. For low voltage limits it makes no sense and triggers
2674 * alarms, so change to 0 instead. For high temperature limits, it
2675 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002676 * but is still confusing, so change to 127 degrees C.
2677 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002678 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002679 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002680 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002681 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002682 }
2683 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002684 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002685 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002686 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002687 }
2688
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002689 /*
2690 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002691 * set to two different sensor types and we can't guess which one
2692 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002693 * run-time through the temp{1-3}_type sysfs accessors if needed.
2694 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002697 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if ((tmp & 0xff) == 0) {
2699 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002700 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
2702
2703 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002704 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002705 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002706 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002708 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002709 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2710 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002712 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002714 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2715
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002716 /* Set tachometers to 16-bit mode if needed */
2717 if (has_fan16_config(data)) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002718 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002719 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002720 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002721 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002722 tmp | 0x07);
2723 }
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002724 }
2725
2726 /* Check for additional fans */
2727 if (has_five_fans(data)) {
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002728 if (tmp & (1 << 4))
2729 data->has_fan |= (1 << 3); /* fan4 enabled */
2730 if (tmp & (1 << 5))
2731 data->has_fan |= (1 << 4); /* fan5 enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002732 if (has_six_fans(data) && (tmp & (1 << 2)))
2733 data->has_fan |= (1 << 5); /* fan6 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02002734 }
2735
Jean Delvare591ec652009-12-09 20:35:48 +01002736 /* Fan input pins may be used for alternative functions */
2737 data->has_fan &= ~sio_data->skip_fan;
2738
Guenter Roeck36c4d982015-03-26 18:18:19 -07002739 /* Check if pwm5, pwm6 are enabled */
2740 if (has_six_pwm(data)) {
2741 /* The following code may be IT8620E specific */
2742 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2743 if ((tmp & 0xc0) == 0xc0)
2744 sio_data->skip_pwm |= (1 << 4);
2745 if (!(tmp & (1 << 3)))
2746 sio_data->skip_pwm |= (1 << 5);
2747 }
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002750 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002751 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 | (update_vbat ? 0x41 : 0x01));
2753}
2754
Jean Delvareb99883d2010-03-05 22:17:15 +01002755static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2756{
Guenter Roeck36c4d982015-03-26 18:18:19 -07002757 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
Jean Delvare16b5dda2012-01-16 22:51:48 +01002758 if (has_newer_autopwm(data)) {
Guenter Roeck36c4d982015-03-26 18:18:19 -07002759 data->pwm_temp_map[nr] = (data->pwm_ctrl[nr] & 0x03) +
2760 nr < 3 ? 0 : 3;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002761 data->pwm_duty[nr] = it87_read_value(data,
Guenter Roeck36c4d982015-03-26 18:18:19 -07002762 IT87_REG_PWM_DUTY[nr]);
Jean Delvare6229cdb2010-12-08 16:27:22 +01002763 } else {
2764 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2765 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2766 else /* Manual mode */
2767 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2768 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002769
2770 if (has_old_autopwm(data)) {
2771 int i;
2772
2773 for (i = 0; i < 5 ; i++)
2774 data->auto_temp[nr][i] = it87_read_value(data,
2775 IT87_REG_AUTO_TEMP(nr, i));
2776 for (i = 0; i < 3 ; i++)
2777 data->auto_pwm[nr][i] = it87_read_value(data,
2778 IT87_REG_AUTO_PWM(nr, i));
2779 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002780}
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782static struct it87_data *it87_update_device(struct device *dev)
2783{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002784 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 int i;
2786
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002787 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2790 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002792 /*
2793 * Cleared after each update, so reenable. Value
2794 * returned by this read will be previous value
2795 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002796 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002797 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 }
2799 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002800 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002801 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002802 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002803 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002804 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002805 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
Jean Delvare3543a532006-08-28 14:27:25 +02002807 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002808 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Guenter Roeck73055402015-03-26 09:16:32 -07002809 if (has_avcc3(data))
2810 data->in[9][0] = it87_read_value(data, IT87_REG_AVCC3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002812 for (i = 0; i < 6; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002813 /* Skip disabled fans */
2814 if (!(data->has_fan & (1 << i)))
2815 continue;
2816
Guenter Roecke1169ba2012-12-19 22:17:01 +01002817 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002818 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002819 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002820 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002821 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002822 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002823 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002824 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002825 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002826 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
2829 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002830 if (!(data->has_temp & (1 << i)))
2831 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002832 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002833 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002834 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002835 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002836 data->temp[i][2] =
2837 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002838 if (has_temp_offset(data))
2839 data->temp[i][3] =
2840 it87_read_value(data,
2841 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
2843
Jean Delvare17d648b2006-08-28 14:23:46 +02002844 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002845 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002846 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002847 data->fan_div[0] = i & 0x07;
2848 data->fan_div[1] = (i >> 3) & 0x07;
2849 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002853 it87_read_value(data, IT87_REG_ALARM1) |
2854 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2855 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002856 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002857
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002858 data->fan_main_ctrl = it87_read_value(data,
2859 IT87_REG_FAN_MAIN_CTRL);
2860 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002861 for (i = 0; i < 6; i++)
Jean Delvareb99883d2010-03-05 22:17:15 +01002862 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002864 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002865 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002866 /*
2867 * The IT8705F does not have VID capability.
2868 * The IT8718F and later don't use IT87_REG_VID for the
2869 * same purpose.
2870 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002871 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002872 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002873 /*
2874 * The older IT8712F revisions had only 5 VID pins,
2875 * but we assume it is always safe to read 6 bits.
2876 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002877 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
2879 data->last_updated = jiffies;
2880 data->valid = 1;
2881 }
2882
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002883 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 return data;
2886}
2887
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002888static int __init it87_device_add(unsigned short address,
2889 const struct it87_sio_data *sio_data)
2890{
2891 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002892 .start = address + IT87_EC_OFFSET,
2893 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002894 .name = DRVNAME,
2895 .flags = IORESOURCE_IO,
2896 };
2897 int err;
2898
Jean Delvareb9acb642009-01-07 16:37:35 +01002899 err = acpi_check_resource_conflict(&res);
2900 if (err)
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002901 return err;
Jean Delvareb9acb642009-01-07 16:37:35 +01002902
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002903 pdev = platform_device_alloc(DRVNAME, address);
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002904 if (!pdev)
2905 return -ENOMEM;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002906
2907 err = platform_device_add_resources(pdev, &res, 1);
2908 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002909 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002910 goto exit_device_put;
2911 }
2912
2913 err = platform_device_add_data(pdev, sio_data,
2914 sizeof(struct it87_sio_data));
2915 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002916 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002917 goto exit_device_put;
2918 }
2919
2920 err = platform_device_add(pdev);
2921 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002922 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002923 goto exit_device_put;
2924 }
2925
2926 return 0;
2927
2928exit_device_put:
2929 platform_device_put(pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002930 return err;
2931}
2932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933static int __init sm_it87_init(void)
2934{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002935 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002936 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002937 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002938
Jean Delvare98dd22c2008-10-09 15:33:58 +02002939 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002940 err = it87_find(&isa_address, &sio_data);
2941 if (err)
2942 return err;
2943 err = platform_driver_register(&it87_driver);
2944 if (err)
2945 return err;
2946
2947 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002948 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002949 platform_driver_unregister(&it87_driver);
2950 return err;
2951 }
2952
2953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954}
2955
2956static void __exit sm_it87_exit(void)
2957{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002958 platform_device_unregister(pdev);
2959 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960}
2961
2962
Jean Delvare7c81c602014-01-29 20:40:08 +01002963MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002964MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965module_param(update_vbat, bool, 0);
2966MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2967module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002968MODULE_PARM_DESC(fix_pwm_polarity,
2969 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970MODULE_LICENSE("GPL");
2971
2972module_init(sm_it87_init);
2973module_exit(sm_it87_exit);