blob: 0d6d106d53a2d4c99bf83ff79f51722e849036fa [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
Guenter Roeck48b2ae72015-04-02 08:06:12 -070055#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/module.h>
57#include <linux/init.h>
58#include <linux/slab.h>
59#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020060#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040061#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020062#include <linux/hwmon-sysfs.h>
63#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040064#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010065#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020066#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020067#include <linux/string.h>
68#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010069#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020070#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
corentin.labbeb74f3fd2007-06-13 20:27:36 +020072#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Justin Maggardead80802015-08-05 12:53:08 -070074enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
75 it8771, it8772, it8781, it8782, it8783, it8786, it8790, it8603,
76 it8620 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Jean Delvare67b671b2007-12-06 23:13:42 +010078static unsigned short force_id;
79module_param(force_id, ushort, 0);
80MODULE_PARM_DESC(force_id, "Override the detected device ID");
81
Guenter Roecke84bd952015-03-28 08:24:29 -070082static struct platform_device *it87_pdev[2];
corentin.labbeb74f3fd2007-06-13 20:27:36 +020083
Guenter Roeck3c2e3512015-03-28 08:03:10 -070084#define REG_2E 0x2e /* The register to read/write */
Guenter Roecke84bd952015-03-28 08:24:29 -070085#define REG_4E 0x4e /* Secondary register to read/write */
Guenter Roeck3c2e3512015-03-28 08:03:10 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define DEV 0x07 /* Register: Logical device select */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010089
90/* The device with the IT8718F/IT8720F VID value in it */
91#define GPIO 0x07
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define DEVID 0x20 /* Register: Device ID */
94#define DEVREV 0x22 /* Register: Device Revision */
95
Guenter Roeck3c2e3512015-03-28 08:03:10 -070096static inline int superio_inb(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Guenter Roeck3c2e3512015-03-28 08:03:10 -070098 outb(reg, ioreg);
99 return inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700102static inline void superio_outb(int ioreg, int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +0200103{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700104 outb(reg, ioreg);
105 outb(val, ioreg + 1);
Jean Delvare436cad22010-07-09 16:22:48 +0200106}
107
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700108static int superio_inw(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 int val;
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700111 outb(reg++, ioreg);
112 val = inb(ioreg + 1) << 8;
113 outb(reg, ioreg);
114 val |= inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return val;
116}
117
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700118static inline void superio_select(int ioreg, int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700120 outb(DEV, ioreg);
121 outb(ldn, ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700124static inline int superio_enter(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200126 /*
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700127 * Try to reserve ioreg and ioreg + 1 for exclusive access.
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200128 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700129 if (!request_muxed_region(ioreg, 2, DRVNAME))
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200130 return -EBUSY;
131
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700132 outb(0x87, ioreg);
133 outb(0x01, ioreg);
134 outb(0x55, ioreg);
Guenter Roecke84bd952015-03-28 08:24:29 -0700135 outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700139static inline void superio_exit(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700141 outb(0x02, ioreg);
142 outb(0x02, ioreg + 1);
143 release_region(ioreg, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Jean Delvare87673dd2006-08-28 14:37:19 +0200146/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define IT8712F_DEVID 0x8712
148#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200149#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200150#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100151#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200152#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400153#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100154#define IT8728F_DEVID 0x8728
Justin Maggardead80802015-08-05 12:53:08 -0700155#define IT8732F_DEVID 0x8732
Guenter Roeckb0636702012-09-07 17:34:41 -0600156#define IT8771E_DEVID 0x8771
157#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800158#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800159#define IT8782F_DEVID 0x8782
160#define IT8783E_DEVID 0x8783
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100161#define IT8786E_DEVID 0x8786
Guenter Roeck4ee07152015-03-25 23:26:28 -0700162#define IT8790E_DEVID 0x8790
Rudolf Marek7183ae82014-04-04 18:01:35 +0200163#define IT8603E_DEVID 0x8603
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800164#define IT8620E_DEVID 0x8620
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200165#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#define IT87_ACT_REG 0x30
167#define IT87_BASE_REG 0x60
168
Jean Delvare87673dd2006-08-28 14:37:19 +0200169/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800170#define IT87_SIO_GPIO1_REG 0x25
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800171#define IT87_SIO_GPIO2_REG 0x26
Jean Delvare895ff262009-12-09 20:35:47 +0100172#define IT87_SIO_GPIO3_REG 0x27
Guenter Roeck36c4d982015-03-26 18:18:19 -0700173#define IT87_SIO_GPIO4_REG 0x28
Jean Delvare591ec652009-12-09 20:35:48 +0100174#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800175#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200176#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800177#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200178#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100179#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030182static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030185static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/* Many IT87 constants specified below */
188
189/* Length of ISA address segment */
190#define IT87_EXTENT 8
191
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500192/* Length of ISA address segment for Environmental Controller */
193#define IT87_EC_EXTENT 2
194
195/* Offset of EC registers from ISA base address */
196#define IT87_EC_OFFSET 5
197
198/* Where are the ISA address/data registers relative to the EC base address */
199#define IT87_ADDR_REG_OFFSET 0
200#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/*----- The IT87 registers -----*/
203
204#define IT87_REG_CONFIG 0x00
205
206#define IT87_REG_ALARM1 0x01
207#define IT87_REG_ALARM2 0x02
208#define IT87_REG_ALARM3 0x03
209
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800210/*
211 * The IT8718F and IT8720F have the VID value in a different register, in
212 * Super-I/O configuration space.
213 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800215/*
216 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
217 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
218 * mode.
219 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200221#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Guenter Roeckf838aa22015-04-01 20:09:36 -0700223/*
224 * Monitors:
225 * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
226 * - up to 6 temp (1 to 6)
227 * - up to 6 fan (1 to 6)
228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700230static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
231static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
232static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
233static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
234static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#define IT87_REG_FAN_MAIN_CTRL 0x13
237#define IT87_REG_FAN_CTL 0x14
Guenter Roeck36c4d982015-03-26 18:18:19 -0700238static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
239static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Guenter Roeck559313c2015-04-01 10:15:38 -0700241static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
Guenter Roeckf838aa22015-04-01 20:09:36 -0700242 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Guenter Roeck559313c2015-04-01 10:15:38 -0700244#define IT87_REG_TEMP(nr) (0x29 + (nr))
Guenter Roeck73055402015-03-26 09:16:32 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
247#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
248#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
249#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#define IT87_REG_VIN_ENABLE 0x50
252#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700253#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100254#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256#define IT87_REG_CHIPID 0x58
257
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100258#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
259#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
260
Guenter Roeckcc18da72015-04-01 10:03:01 -0700261#define IT87_REG_TEMP456_ENABLE 0x77
262
Guenter Roeck483db432012-12-19 22:17:02 +0100263struct it87_devices {
264 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700265 const char * const suffix;
Guenter Roeckcc18da72015-04-01 10:03:01 -0700266 u32 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100267 u8 peci_mask;
268 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100269};
270
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700271#define FEAT_12MV_ADC BIT(0)
272#define FEAT_NEWER_AUTOPWM BIT(1)
273#define FEAT_OLD_AUTOPWM BIT(2)
274#define FEAT_16BIT_FANS BIT(3)
275#define FEAT_TEMP_OFFSET BIT(4)
276#define FEAT_TEMP_PECI BIT(5)
277#define FEAT_TEMP_OLD_PECI BIT(6)
278#define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
279#define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
280#define FEAT_VID BIT(9) /* Set if chip supports VID */
281#define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
282#define FEAT_SIX_FANS BIT(11) /* Supports six fans */
283#define FEAT_10_9MV_ADC BIT(12)
284#define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
285#define FEAT_SIX_PWM BIT(14) /* Chip supports 6 pwm chn */
286#define FEAT_PWM_FREQ2 BIT(15) /* Separate pwm freq 2 */
287#define FEAT_SIX_TEMP BIT(16) /* Up to 6 temp sensors */
Guenter Roeck483db432012-12-19 22:17:02 +0100288
289static const struct it87_devices it87_devices[] = {
290 [it87] = {
291 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700292 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100293 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
294 },
295 [it8712] = {
296 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700297 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800298 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
299 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100300 },
301 [it8716] = {
302 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700303 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800304 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700305 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
Guenter Roeck483db432012-12-19 22:17:02 +0100306 },
307 [it8718] = {
308 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700309 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800310 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700311 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
312 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100313 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100314 },
315 [it8720] = {
316 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700317 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800318 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700319 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
320 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100321 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100322 },
323 [it8721] = {
324 .name = "it8721",
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 Roeck9faf28c2015-02-12 07:11:38 -0800327 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck60878bc2015-03-26 19:57:42 -0700328 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
329 | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100330 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100331 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100332 },
333 [it8728] = {
334 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700335 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100336 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700337 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700338 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100339 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100340 },
Justin Maggardead80802015-08-05 12:53:08 -0700341 [it8732] = {
342 .name = "it8732",
343 .suffix = "F",
344 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
345 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
346 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
347 .peci_mask = 0x07,
348 .old_peci_mask = 0x02, /* Actually reports PCH */
349 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600350 [it8771] = {
351 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700352 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600353 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700354 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
355 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800356 /* PECI: guesswork */
357 /* 12mV ADC (OHM) */
358 /* 16 bit fans (OHM) */
359 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600360 .peci_mask = 0x07,
361 },
362 [it8772] = {
363 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700364 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600365 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700366 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
367 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800368 /* PECI (coreboot) */
369 /* 12mV ADC (HWSensors4, OHM) */
370 /* 16 bit fans (HWSensors4, OHM) */
371 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600372 .peci_mask = 0x07,
373 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800374 [it8781] = {
375 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700376 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800377 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700378 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800379 .old_peci_mask = 0x4,
380 },
Guenter Roeck483db432012-12-19 22:17:02 +0100381 [it8782] = {
382 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700383 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100384 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700385 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100386 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100387 },
388 [it8783] = {
389 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700390 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100391 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700392 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100393 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100394 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100395 [it8786] = {
396 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700397 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100398 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700399 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
400 | FEAT_PWM_FREQ2,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100401 .peci_mask = 0x07,
402 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700403 [it8790] = {
404 .name = "it8790",
405 .suffix = "E",
406 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700407 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
408 | FEAT_PWM_FREQ2,
Guenter Roeck4ee07152015-03-25 23:26:28 -0700409 .peci_mask = 0x07,
410 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100411 [it8603] = {
412 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700413 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100414 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck73055402015-03-26 09:16:32 -0700415 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
Guenter Roeck60878bc2015-03-26 19:57:42 -0700416 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100417 .peci_mask = 0x07,
418 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800419 [it8620] = {
420 .name = "it8620",
421 .suffix = "E",
422 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700423 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeckcc18da72015-04-01 10:03:01 -0700424 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
425 | FEAT_SIX_TEMP,
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800426 .peci_mask = 0x07,
427 },
Guenter Roeck483db432012-12-19 22:17:02 +0100428};
429
430#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
431#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
Justin Maggardead80802015-08-05 12:53:08 -0700432#define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
Guenter Roeck483db432012-12-19 22:17:02 +0100433#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
434#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
435#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100436#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700437 ((data)->peci_mask & BIT(nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100438#define has_temp_old_peci(data, nr) \
439 (((data)->features & FEAT_TEMP_OLD_PECI) && \
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700440 ((data)->old_peci_mask & BIT(nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800441#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700442#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
443 FEAT_SIX_FANS))
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800444#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700445#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700446#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Guenter Roeck73055402015-03-26 09:16:32 -0700447#define has_avcc3(data) ((data)->features & FEAT_AVCC3)
Guenter Roeck36c4d982015-03-26 18:18:19 -0700448#define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
Guenter Roeck60878bc2015-03-26 19:57:42 -0700449#define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
Guenter Roeckcc18da72015-04-01 10:03:01 -0700450#define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200452struct it87_sio_data {
453 enum chips type;
454 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200455 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200456 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100457 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200458 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100459 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700460 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100461 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100462 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200463 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700464 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200465};
466
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800467/*
468 * For each registered chip, we need to keep some data in memory.
469 * The structure is dynamically allocated.
470 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471struct it87_data {
Guenter Roeck8638d0a2015-03-30 11:13:49 -0700472 const struct attribute_group *groups[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100474 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100475 u8 peci_mask;
476 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200478 unsigned short addr;
479 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100480 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 char valid; /* !=0 if following fields are valid */
482 unsigned long last_updated; /* In jiffies */
483
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200484 u16 in_scaled; /* Internal voltage sensors are scaled */
Guenter Roeckd3766842013-03-30 15:47:21 -0700485 u16 in_internal; /* Bitfield, internal sensors (for labels) */
Guenter Roeck52929712013-03-30 14:00:08 -0700486 u16 has_in; /* Bitfield, voltage sensors enabled */
Guenter Roeckf838aa22015-04-01 20:09:36 -0700487 u8 in[13][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200488 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700489 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700490 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeckcc18da72015-04-01 10:03:01 -0700491 s8 temp[6][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100492 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
493 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 u8 fan_div[3]; /* Register encoding, shifted right */
Guenter Roeckd3766842013-03-30 15:47:21 -0700495 bool has_vid; /* True if VID supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100497 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 u32 alarms; /* Register encoding, combined */
Guenter Roeck52929712013-03-30 14:00:08 -0700499 bool has_beep; /* true if beep supported */
Jean Delvared9b327c2010-03-05 22:17:17 +0100500 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100502 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100503
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800504 /*
505 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100506 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
507 * 7, and we want to preserve settings on mode changes, so we have
508 * to track all values separately.
509 * Starting with the IT8721F, the manual PWM duty cycles are stored
510 * in separate registers (8-bit values), so the separate tracking
511 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800512 * simple.
513 */
Guenter Roeck5c391262013-03-30 15:02:12 -0700514 u8 has_pwm; /* Bitfield, pwm control enabled */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700515 u8 pwm_ctrl[6]; /* Register value */
516 u8 pwm_duty[6]; /* Manual PWM value set by user */
517 u8 pwm_temp_map[6]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100518
519 /* Automatic fan speed control registers */
520 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
521 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522};
523
Guenter Roeck0531d982012-03-02 11:46:44 -0800524static int adc_lsb(const struct it87_data *data, int nr)
525{
Justin Maggardead80802015-08-05 12:53:08 -0700526 int lsb;
527
528 if (has_12mv_adc(data))
529 lsb = 120;
530 else if (has_10_9mv_adc(data))
531 lsb = 109;
532 else
533 lsb = 160;
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700534 if (data->in_scaled & BIT(nr))
Guenter Roeck0531d982012-03-02 11:46:44 -0800535 lsb <<= 1;
536 return lsb;
537}
538
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200539static u8 in_to_reg(const struct it87_data *data, int nr, long val)
540{
Justin Maggardead80802015-08-05 12:53:08 -0700541 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800542 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200543}
544
545static int in_from_reg(const struct it87_data *data, int nr, int val)
546{
Justin Maggardead80802015-08-05 12:53:08 -0700547 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200548}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200549
550static inline u8 FAN_TO_REG(long rpm, int div)
551{
552 if (rpm == 0)
553 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800554 rpm = clamp_val(rpm, 1, 1000000);
555 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200556}
557
558static inline u16 FAN16_TO_REG(long rpm)
559{
560 if (rpm == 0)
561 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800562 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200563}
564
565#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
566 1350000 / ((val) * (div)))
567/* The divider is fixed to 2 in 16-bit mode */
568#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
569 1350000 / ((val) * 2))
570
Guenter Roeck2a844c12013-01-09 08:09:34 -0800571#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
572 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200573#define TEMP_FROM_REG(val) ((val) * 1000)
574
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200575static u8 pwm_to_reg(const struct it87_data *data, long val)
576{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100577 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200578 return val;
579 else
580 return val >> 1;
581}
582
583static int pwm_from_reg(const struct it87_data *data, u8 reg)
584{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100585 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200586 return reg;
587 else
588 return (reg & 0x7f) << 1;
589}
590
Jean Delvare0df6454d2010-10-28 20:31:51 +0200591
592static int DIV_TO_REG(int val)
593{
594 int answer = 0;
595 while (answer < 7 && (val >>= 1))
596 answer++;
597 return answer;
598}
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700599
600#define DIV_FROM_REG(val) BIT(val)
Jean Delvare0df6454d2010-10-28 20:31:51 +0200601
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700602/*
603 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
604 * depending on the chip type, to calculate the actual PWM frequency.
605 *
606 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
607 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
608 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
609 * sometimes just one. It is unknown if this is a datasheet error or real,
610 * so this is ignored for now.
611 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200612static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700613 48000000,
614 24000000,
615 12000000,
616 8000000,
617 6000000,
618 3000000,
619 1500000,
620 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200621};
622
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700623/*
624 * Must be called with data->update_lock held, except during initialization.
625 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
626 * would slow down the IT87 access and should not be necessary.
627 */
628static int it87_read_value(struct it87_data *data, u8 reg)
629{
630 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
631 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
632}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700634/*
635 * Must be called with data->update_lock held, except during initialization.
636 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
637 * would slow down the IT87 access and should not be necessary.
638 */
639static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
640{
641 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
642 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
643}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700645static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
646{
647 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
648 if (has_newer_autopwm(data)) {
649 data->pwm_temp_map[nr] = (data->pwm_ctrl[nr] & 0x03) +
650 nr < 3 ? 0 : 3;
651 data->pwm_duty[nr] = it87_read_value(data,
652 IT87_REG_PWM_DUTY[nr]);
653 } else {
654 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
655 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
656 else /* Manual mode */
657 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700660 if (has_old_autopwm(data)) {
661 int i;
662
663 for (i = 0; i < 5 ; i++)
664 data->auto_temp[nr][i] = it87_read_value(data,
665 IT87_REG_AUTO_TEMP(nr, i));
666 for (i = 0; i < 3 ; i++)
667 data->auto_pwm[nr][i] = it87_read_value(data,
668 IT87_REG_AUTO_PWM(nr, i));
669 }
670}
671
672static struct it87_data *it87_update_device(struct device *dev)
673{
674 struct it87_data *data = dev_get_drvdata(dev);
675 int i;
676
677 mutex_lock(&data->update_lock);
678
679 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
680 || !data->valid) {
681 if (update_vbat) {
682 /*
683 * Cleared after each update, so reenable. Value
684 * returned by this read will be previous value
685 */
686 it87_write_value(data, IT87_REG_CONFIG,
687 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
688 }
Guenter Roeck559313c2015-04-01 10:15:38 -0700689 for (i = 0; i < ARRAY_SIZE(IT87_REG_VIN); i++) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700690 if (!(data->has_in & BIT(i)))
Guenter Roeck559313c2015-04-01 10:15:38 -0700691 continue;
692
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700693 data->in[i][0] =
Guenter Roeck559313c2015-04-01 10:15:38 -0700694 it87_read_value(data, IT87_REG_VIN[i]);
695
696 /* VBAT and AVCC don't have limit registers */
697 if (i >= 8)
698 continue;
699
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700700 data->in[i][1] =
701 it87_read_value(data, IT87_REG_VIN_MIN(i));
702 data->in[i][2] =
703 it87_read_value(data, IT87_REG_VIN_MAX(i));
704 }
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700705
706 for (i = 0; i < 6; i++) {
707 /* Skip disabled fans */
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700708 if (!(data->has_fan & BIT(i)))
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700709 continue;
710
711 data->fan[i][1] =
712 it87_read_value(data, IT87_REG_FAN_MIN[i]);
713 data->fan[i][0] = it87_read_value(data,
714 IT87_REG_FAN[i]);
715 /* Add high byte if in 16-bit mode */
716 if (has_16bit_fans(data)) {
717 data->fan[i][0] |= it87_read_value(data,
718 IT87_REG_FANX[i]) << 8;
719 data->fan[i][1] |= it87_read_value(data,
720 IT87_REG_FANX_MIN[i]) << 8;
721 }
722 }
Guenter Roeckcc18da72015-04-01 10:03:01 -0700723 for (i = 0; i < 6; i++) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700724 if (!(data->has_temp & BIT(i)))
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700725 continue;
726 data->temp[i][0] =
727 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeckcc18da72015-04-01 10:03:01 -0700728
729 /* No limits/offset for additional sensors */
730 if (i >= 3)
731 continue;
732
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700733 data->temp[i][1] =
734 it87_read_value(data, IT87_REG_TEMP_LOW(i));
735 data->temp[i][2] =
736 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
737 if (has_temp_offset(data))
738 data->temp[i][3] =
739 it87_read_value(data,
740 IT87_REG_TEMP_OFFSET[i]);
741 }
742
743 /* Newer chips don't have clock dividers */
744 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
745 i = it87_read_value(data, IT87_REG_FAN_DIV);
746 data->fan_div[0] = i & 0x07;
747 data->fan_div[1] = (i >> 3) & 0x07;
748 data->fan_div[2] = (i & 0x40) ? 3 : 1;
749 }
750
751 data->alarms =
752 it87_read_value(data, IT87_REG_ALARM1) |
753 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
754 (it87_read_value(data, IT87_REG_ALARM3) << 16);
755 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
756
757 data->fan_main_ctrl = it87_read_value(data,
758 IT87_REG_FAN_MAIN_CTRL);
759 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
760 for (i = 0; i < 6; i++)
761 it87_update_pwm_ctrl(data, i);
762
763 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
764 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
765 /*
766 * The IT8705F does not have VID capability.
767 * The IT8718F and later don't use IT87_REG_VID for the
768 * same purpose.
769 */
770 if (data->type == it8712 || data->type == it8716) {
771 data->vid = it87_read_value(data, IT87_REG_VID);
772 /*
773 * The older IT8712F revisions had only 5 VID pins,
774 * but we assume it is always safe to read 6 bits.
775 */
776 data->vid &= 0x3f;
777 }
778 data->last_updated = jiffies;
779 data->valid = 1;
780 }
781
782 mutex_unlock(&data->update_lock);
783
784 return data;
785}
Jean Delvarefde09502005-07-19 23:51:07 +0200786
Jean Delvare20ad93d2005-06-05 11:53:25 +0200787static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100788 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100790 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
791 int nr = sattr->nr;
792 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100795 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Guenter Roeck929c6a52012-12-19 22:17:00 +0100798static ssize_t set_in(struct device *dev, struct device_attribute *attr,
799 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100801 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
802 int nr = sattr->nr;
803 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200804
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200805 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100806 unsigned long val;
807
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100808 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100809 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100811 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100812 data->in[nr][index] = in_to_reg(data, nr, val);
813 it87_write_value(data,
814 index == 1 ? IT87_REG_VIN_MIN(nr)
815 : IT87_REG_VIN_MAX(nr),
816 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100817 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return count;
819}
820
Guenter Roeck929c6a52012-12-19 22:17:00 +0100821static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
822static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
823 0, 1);
824static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
825 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Guenter Roeck929c6a52012-12-19 22:17:00 +0100827static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
828static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
829 1, 1);
830static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
831 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Guenter Roeck929c6a52012-12-19 22:17:00 +0100833static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
834static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
835 2, 1);
836static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
837 2, 2);
838
839static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
840static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
841 3, 1);
842static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
843 3, 2);
844
845static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
846static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
847 4, 1);
848static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
849 4, 2);
850
851static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
852static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
853 5, 1);
854static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
855 5, 2);
856
857static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
858static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
859 6, 1);
860static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
861 6, 2);
862
863static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
864static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
865 7, 1);
866static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
867 7, 2);
868
869static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100870static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Guenter Roeckf838aa22015-04-01 20:09:36 -0700871static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
872static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
873static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Guenter Roeckcc18da72015-04-01 10:03:01 -0700875/* Up to 6 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200876static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100877 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100879 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
880 int nr = sattr->nr;
881 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200883
Guenter Roeck60ca3852012-12-19 22:17:00 +0100884 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200886
Guenter Roeck60ca3852012-12-19 22:17:00 +0100887static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
888 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100890 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
891 int nr = sattr->nr;
892 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200893 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100894 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100895 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100896
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100897 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100898 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100900 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100901
902 switch (index) {
903 default:
904 case 1:
905 reg = IT87_REG_TEMP_LOW(nr);
906 break;
907 case 2:
908 reg = IT87_REG_TEMP_HIGH(nr);
909 break;
910 case 3:
911 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
912 if (!(regval & 0x80)) {
913 regval |= 0x80;
914 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
915 }
916 data->valid = 0;
917 reg = IT87_REG_TEMP_OFFSET[nr];
918 break;
919 }
920
Guenter Roeck60ca3852012-12-19 22:17:00 +0100921 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100922 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100923 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return count;
925}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200926
Guenter Roeck60ca3852012-12-19 22:17:00 +0100927static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
928static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
929 0, 1);
930static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
931 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100932static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
933 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100934static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
935static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
936 1, 1);
937static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
938 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100939static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
940 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100941static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
942static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
943 2, 1);
944static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
945 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100946static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
947 set_temp, 2, 3);
Guenter Roeckcc18da72015-04-01 10:03:01 -0700948static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
949static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
950static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Guenter Roeck2cece012012-12-19 22:17:01 +0100952static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
953 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200955 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
956 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800958 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100959 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100960
Guenter Roeck19529782012-12-19 22:17:02 +0100961 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
962 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100963 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (reg & (1 << nr))
965 return sprintf(buf, "3\n"); /* thermal diode */
966 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200967 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return sprintf(buf, "0\n"); /* disabled */
969}
Guenter Roeck2cece012012-12-19 22:17:01 +0100970
971static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
972 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200974 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
975 int nr = sensor_attr->index;
976
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200977 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100978 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100979 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100980
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100981 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100982 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Jean Delvare8acf07c2010-04-14 16:14:09 +0200984 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
985 reg &= ~(1 << nr);
986 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100987 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
988 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100989 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
990 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
991 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200992 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100993 dev_warn(dev,
994 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200995 val = 4;
996 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100997 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200999 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +02001000 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +02001001 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001002 else if (has_temp_peci(data, nr) && val == 6)
1003 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +01001004 else if (has_temp_old_peci(data, nr) && val == 6)
1005 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +02001006 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +02001008
1009 mutex_lock(&data->update_lock);
1010 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +01001011 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001012 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +01001013 if (has_temp_old_peci(data, nr))
1014 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +02001015 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001016 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return count;
1018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Guenter Roeck2cece012012-12-19 22:17:01 +01001020static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1021 set_temp_type, 0);
1022static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1023 set_temp_type, 1);
1024static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1025 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +01001028
1029static int pwm_mode(const struct it87_data *data, int nr)
1030{
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001031 int ctrl = data->fan_main_ctrl & BIT(nr);
Jean Delvareb99883d2010-03-05 22:17:15 +01001032
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001033 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +01001034 return 0;
1035 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
1036 return 2;
1037 else /* Manual mode */
1038 return 1;
1039}
1040
Jean Delvare20ad93d2005-06-05 11:53:25 +02001041static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +01001042 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001044 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1045 int nr = sattr->nr;
1046 int index = sattr->index;
1047 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001049
Guenter Roecke1169ba2012-12-19 22:17:01 +01001050 speed = has_16bit_fans(data) ?
1051 FAN16_FROM_REG(data->fan[nr][index]) :
1052 FAN_FROM_REG(data->fan[nr][index],
1053 DIV_FROM_REG(data->fan_div[nr]));
1054 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001056
Jean Delvare20ad93d2005-06-05 11:53:25 +02001057static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1058 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001060 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1061 int nr = sensor_attr->index;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct it87_data *data = it87_update_device(dev);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001064 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001066static ssize_t show_pwm_enable(struct device *dev,
1067 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001069 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1070 int nr = sensor_attr->index;
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +01001073 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001075static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1076 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001078 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1079 int nr = sensor_attr->index;
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001082 return sprintf(buf, "%d\n",
1083 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001085static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1086 char *buf)
1087{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001088 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001089 struct it87_data *data = it87_update_device(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001090 int nr = sensor_attr->index;
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001091 unsigned int freq;
Guenter Roeck60878bc2015-03-26 19:57:42 -07001092 int index;
1093
1094 if (has_pwm_freq2(data) && nr == 1)
1095 index = (data->extra >> 4) & 0x07;
1096 else
1097 index = (data->fan_ctl >> 4) & 0x07;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001098
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001099 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1100
1101 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001102}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001103
1104static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1105 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001107 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1108 int nr = sattr->nr;
1109 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +02001110
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001111 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001112 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +01001113 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001115 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001116 return -EINVAL;
1117
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001118 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +01001119
1120 if (has_16bit_fans(data)) {
1121 data->fan[nr][index] = FAN16_TO_REG(val);
1122 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1123 data->fan[nr][index] & 0xff);
1124 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1125 data->fan[nr][index] >> 8);
1126 } else {
1127 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1128 switch (nr) {
1129 case 0:
1130 data->fan_div[nr] = reg & 0x07;
1131 break;
1132 case 1:
1133 data->fan_div[nr] = (reg >> 3) & 0x07;
1134 break;
1135 case 2:
1136 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1137 break;
1138 }
1139 data->fan[nr][index] =
1140 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1141 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1142 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -08001143 }
1144
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001145 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return count;
1147}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001148
Jean Delvare20ad93d2005-06-05 11:53:25 +02001149static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1150 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001152 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1153 int nr = sensor_attr->index;
1154
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001155 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001156 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001157 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 u8 old;
1159
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001160 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001161 return -EINVAL;
1162
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001163 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001164 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001166 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001167 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 switch (nr) {
1170 case 0:
1171 case 1:
1172 data->fan_div[nr] = DIV_TO_REG(val);
1173 break;
1174 case 2:
1175 if (val < 8)
1176 data->fan_div[nr] = 1;
1177 else
1178 data->fan_div[nr] = 3;
1179 }
1180 val = old & 0x80;
1181 val |= (data->fan_div[0] & 0x07);
1182 val |= (data->fan_div[1] & 0x07) << 3;
1183 if (data->fan_div[2] == 3)
1184 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001185 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001187 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001188 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1189 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001190
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001191 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return count;
1193}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001194
1195/* Returns 0 if OK, -EINVAL otherwise */
1196static int check_trip_points(struct device *dev, int nr)
1197{
1198 const struct it87_data *data = dev_get_drvdata(dev);
1199 int i, err = 0;
1200
1201 if (has_old_autopwm(data)) {
1202 for (i = 0; i < 3; i++) {
1203 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1204 err = -EINVAL;
1205 }
1206 for (i = 0; i < 2; i++) {
1207 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1208 err = -EINVAL;
1209 }
1210 }
1211
1212 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001213 dev_err(dev,
1214 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001215 dev_err(dev, "Adjust the trip points and try again\n");
1216 }
1217 return err;
1218}
1219
Jean Delvare20ad93d2005-06-05 11:53:25 +02001220static ssize_t set_pwm_enable(struct device *dev,
1221 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001223 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1224 int nr = sensor_attr->index;
1225
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001226 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001227 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001229 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001230 return -EINVAL;
1231
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001232 /* Check trip points before switching to automatic mode */
1233 if (val == 2) {
1234 if (check_trip_points(dev, nr) < 0)
1235 return -EINVAL;
1236 }
1237
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001238 /* IT8603E does not have on/off mode */
1239 if (val == 0 && data->type == it8603)
1240 return -EINVAL;
1241
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001242 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 if (val == 0) {
1245 int tmp;
1246 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001247 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001248 it87_write_value(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /* set on/off mode */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001250 data->fan_main_ctrl &= ~BIT(nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +01001251 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1252 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +01001253 } else {
1254 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001255 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001256 data->pwm_temp_map[nr] :
1257 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001258 else /* Automatic mode */
1259 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001260 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001261
1262 if (data->type != it8603) {
1263 /* set SmartGuardian mode */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001264 data->fan_main_ctrl |= BIT(nr);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001265 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1266 data->fan_main_ctrl);
1267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001270 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return count;
1272}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001273static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1274 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001276 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1277 int nr = sensor_attr->index;
1278
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001279 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001280 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001282 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return -EINVAL;
1284
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001285 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001286 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001287 /*
1288 * If we are in automatic mode, the PWM duty cycle register
1289 * is read-only so we can't write the value.
1290 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001291 if (data->pwm_ctrl[nr] & 0x80) {
1292 mutex_unlock(&data->update_lock);
1293 return -EBUSY;
1294 }
1295 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001296 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001297 data->pwm_duty[nr]);
1298 } else {
1299 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001300 /*
1301 * If we are in manual mode, write the duty cycle immediately;
1302 * otherwise, just store it for later use.
1303 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001304 if (!(data->pwm_ctrl[nr] & 0x80)) {
1305 data->pwm_ctrl[nr] = data->pwm_duty[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001306 it87_write_value(data, IT87_REG_PWM[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001307 data->pwm_ctrl[nr]);
1308 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001309 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001310 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return count;
1312}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001313static ssize_t set_pwm_freq(struct device *dev,
1314 struct device_attribute *attr, const char *buf, size_t count)
1315{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001316 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001317 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001318 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001319 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001320 int i;
1321
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001322 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001323 return -EINVAL;
1324
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001325 val = clamp_val(val, 0, 1000000);
1326 val *= has_newer_autopwm(data) ? 256 : 128;
1327
Jean Delvaref8d0c192007-02-14 21:15:02 +01001328 /* Search for the nearest available frequency */
1329 for (i = 0; i < 7; i++) {
1330 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1331 break;
1332 }
1333
1334 mutex_lock(&data->update_lock);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001335 if (nr == 0) {
1336 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1337 data->fan_ctl |= i << 4;
1338 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1339 } else {
1340 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1341 data->extra |= i << 4;
1342 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1343 }
Jean Delvaref8d0c192007-02-14 21:15:02 +01001344 mutex_unlock(&data->update_lock);
1345
1346 return count;
1347}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001348static ssize_t show_pwm_temp_map(struct device *dev,
1349 struct device_attribute *attr, char *buf)
1350{
1351 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1352 int nr = sensor_attr->index;
1353
1354 struct it87_data *data = it87_update_device(dev);
1355 int map;
1356
1357 if (data->pwm_temp_map[nr] < 3)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001358 map = BIT(data->pwm_temp_map[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001359 else
1360 map = 0; /* Should never happen */
1361 return sprintf(buf, "%d\n", map);
1362}
1363static ssize_t set_pwm_temp_map(struct device *dev,
1364 struct device_attribute *attr, const char *buf, size_t count)
1365{
1366 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1367 int nr = sensor_attr->index;
1368
1369 struct it87_data *data = dev_get_drvdata(dev);
1370 long val;
1371 u8 reg;
1372
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001373 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001374 return -EINVAL;
1375
1376 switch (val) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001377 case BIT(0):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001378 reg = 0x00;
1379 break;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001380 case BIT(1):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001381 reg = 0x01;
1382 break;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001383 case BIT(2):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001384 reg = 0x02;
1385 break;
1386 default:
1387 return -EINVAL;
1388 }
1389
1390 mutex_lock(&data->update_lock);
1391 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001392 /*
1393 * If we are in automatic mode, write the temp mapping immediately;
1394 * otherwise, just store it for later use.
1395 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001396 if (data->pwm_ctrl[nr] & 0x80) {
1397 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001398 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001399 }
1400 mutex_unlock(&data->update_lock);
1401 return count;
1402}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001404static ssize_t show_auto_pwm(struct device *dev,
1405 struct device_attribute *attr, char *buf)
1406{
1407 struct it87_data *data = it87_update_device(dev);
1408 struct sensor_device_attribute_2 *sensor_attr =
1409 to_sensor_dev_attr_2(attr);
1410 int nr = sensor_attr->nr;
1411 int point = sensor_attr->index;
1412
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001413 return sprintf(buf, "%d\n",
1414 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001415}
1416
1417static ssize_t set_auto_pwm(struct device *dev,
1418 struct device_attribute *attr, const char *buf, size_t count)
1419{
1420 struct it87_data *data = dev_get_drvdata(dev);
1421 struct sensor_device_attribute_2 *sensor_attr =
1422 to_sensor_dev_attr_2(attr);
1423 int nr = sensor_attr->nr;
1424 int point = sensor_attr->index;
1425 long val;
1426
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001427 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001428 return -EINVAL;
1429
1430 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001431 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001432 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1433 data->auto_pwm[nr][point]);
1434 mutex_unlock(&data->update_lock);
1435 return count;
1436}
1437
1438static ssize_t show_auto_temp(struct device *dev,
1439 struct device_attribute *attr, char *buf)
1440{
1441 struct it87_data *data = it87_update_device(dev);
1442 struct sensor_device_attribute_2 *sensor_attr =
1443 to_sensor_dev_attr_2(attr);
1444 int nr = sensor_attr->nr;
1445 int point = sensor_attr->index;
1446
1447 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1448}
1449
1450static ssize_t set_auto_temp(struct device *dev,
1451 struct device_attribute *attr, const char *buf, size_t count)
1452{
1453 struct it87_data *data = dev_get_drvdata(dev);
1454 struct sensor_device_attribute_2 *sensor_attr =
1455 to_sensor_dev_attr_2(attr);
1456 int nr = sensor_attr->nr;
1457 int point = sensor_attr->index;
1458 long val;
1459
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001460 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001461 return -EINVAL;
1462
1463 mutex_lock(&data->update_lock);
1464 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1465 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1466 data->auto_temp[nr][point]);
1467 mutex_unlock(&data->update_lock);
1468 return count;
1469}
1470
Guenter Roecke1169ba2012-12-19 22:17:01 +01001471static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1472static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1473 0, 1);
1474static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1475 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Guenter Roecke1169ba2012-12-19 22:17:01 +01001477static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1478static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1479 1, 1);
1480static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1481 set_fan_div, 1);
1482
1483static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1484static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1485 2, 1);
1486static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1487 set_fan_div, 2);
1488
1489static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1490static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1491 3, 1);
1492
1493static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1494static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1495 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001497static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1498static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1499 5, 1);
1500
Guenter Roeckc4458db2012-12-19 22:17:02 +01001501static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1502 show_pwm_enable, set_pwm_enable, 0);
1503static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001504static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1505 set_pwm_freq, 0);
Guenter Roeck5c391262013-03-30 15:02:12 -07001506static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001507 show_pwm_temp_map, set_pwm_temp_map, 0);
1508static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1509 show_auto_pwm, set_auto_pwm, 0, 0);
1510static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1511 show_auto_pwm, set_auto_pwm, 0, 1);
1512static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1513 show_auto_pwm, set_auto_pwm, 0, 2);
1514static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1515 show_auto_pwm, NULL, 0, 3);
1516static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1517 show_auto_temp, set_auto_temp, 0, 1);
1518static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1519 show_auto_temp, set_auto_temp, 0, 0);
1520static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1521 show_auto_temp, set_auto_temp, 0, 2);
1522static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1523 show_auto_temp, set_auto_temp, 0, 3);
1524static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1525 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Guenter Roeckc4458db2012-12-19 22:17:02 +01001527static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1528 show_pwm_enable, set_pwm_enable, 1);
1529static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001530static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
Guenter Roeck5c391262013-03-30 15:02:12 -07001531static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001532 show_pwm_temp_map, set_pwm_temp_map, 1);
1533static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1534 show_auto_pwm, set_auto_pwm, 1, 0);
1535static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1536 show_auto_pwm, set_auto_pwm, 1, 1);
1537static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1538 show_auto_pwm, set_auto_pwm, 1, 2);
1539static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1540 show_auto_pwm, NULL, 1, 3);
1541static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1542 show_auto_temp, set_auto_temp, 1, 1);
1543static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1544 show_auto_temp, set_auto_temp, 1, 0);
1545static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1546 show_auto_temp, set_auto_temp, 1, 2);
1547static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1548 show_auto_temp, set_auto_temp, 1, 3);
1549static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1550 show_auto_temp, set_auto_temp, 1, 4);
1551
1552static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1553 show_pwm_enable, set_pwm_enable, 2);
1554static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001555static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
Guenter Roeck5c391262013-03-30 15:02:12 -07001556static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001557 show_pwm_temp_map, set_pwm_temp_map, 2);
1558static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1559 show_auto_pwm, set_auto_pwm, 2, 0);
1560static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1561 show_auto_pwm, set_auto_pwm, 2, 1);
1562static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1563 show_auto_pwm, set_auto_pwm, 2, 2);
1564static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1565 show_auto_pwm, NULL, 2, 3);
1566static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1567 show_auto_temp, set_auto_temp, 2, 1);
1568static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1569 show_auto_temp, set_auto_temp, 2, 0);
1570static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1571 show_auto_temp, set_auto_temp, 2, 2);
1572static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1573 show_auto_temp, set_auto_temp, 2, 3);
1574static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1575 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Guenter Roeck36c4d982015-03-26 18:18:19 -07001577static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1578 show_pwm_enable, set_pwm_enable, 3);
1579static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001580static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
Guenter Roeck5c391262013-03-30 15:02:12 -07001581static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001582 show_pwm_temp_map, set_pwm_temp_map, 3);
1583
1584static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1585 show_pwm_enable, set_pwm_enable, 4);
1586static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001587static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
Guenter Roeck5c391262013-03-30 15:02:12 -07001588static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001589 show_pwm_temp_map, set_pwm_temp_map, 4);
1590
1591static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1592 show_pwm_enable, set_pwm_enable, 5);
1593static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001594static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
Guenter Roeck5c391262013-03-30 15:02:12 -07001595static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001596 show_pwm_temp_map, set_pwm_temp_map, 5);
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001599static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1600 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001603 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
Jean Delvare1d66c642005-04-18 21:16:59 -07001605static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Jean Delvare0124dd72007-11-25 16:16:41 +01001607static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1608 char *buf)
1609{
1610 int bitnr = to_sensor_dev_attr(attr)->index;
1611 struct it87_data *data = it87_update_device(dev);
1612 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1613}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001614
1615static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1616 *attr, const char *buf, size_t count)
1617{
1618 struct it87_data *data = dev_get_drvdata(dev);
1619 long val;
1620 int config;
1621
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001622 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001623 return -EINVAL;
1624
1625 mutex_lock(&data->update_lock);
1626 config = it87_read_value(data, IT87_REG_CONFIG);
1627 if (config < 0) {
1628 count = config;
1629 } else {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001630 config |= BIT(5);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001631 it87_write_value(data, IT87_REG_CONFIG, config);
1632 /* Invalidate cache to force re-read */
1633 data->valid = 0;
1634 }
1635 mutex_unlock(&data->update_lock);
1636
1637 return count;
1638}
1639
Jean Delvare0124dd72007-11-25 16:16:41 +01001640static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1641static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1642static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1643static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1644static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1645static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1646static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1647static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1648static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1649static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1650static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1651static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1652static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001653static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001654static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1655static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1656static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001657static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1658 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001659
Jean Delvared9b327c2010-03-05 22:17:17 +01001660static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1661 char *buf)
1662{
1663 int bitnr = to_sensor_dev_attr(attr)->index;
1664 struct it87_data *data = it87_update_device(dev);
1665 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1666}
1667static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1668 const char *buf, size_t count)
1669{
1670 int bitnr = to_sensor_dev_attr(attr)->index;
1671 struct it87_data *data = dev_get_drvdata(dev);
1672 long val;
1673
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001674 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001675 || (val != 0 && val != 1))
1676 return -EINVAL;
1677
1678 mutex_lock(&data->update_lock);
1679 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1680 if (val)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001681 data->beeps |= BIT(bitnr);
Jean Delvared9b327c2010-03-05 22:17:17 +01001682 else
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001683 data->beeps &= ~BIT(bitnr);
Jean Delvared9b327c2010-03-05 22:17:17 +01001684 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1685 mutex_unlock(&data->update_lock);
1686 return count;
1687}
1688
1689static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1690 show_beep, set_beep, 1);
1691static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1692static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1693static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1694static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1695static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1696static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1697static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1698/* fanX_beep writability is set later */
1699static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1700static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1701static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1702static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1703static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001704static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001705static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1706 show_beep, set_beep, 2);
1707static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1708static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1709
Jean Delvare5f2dc792010-03-05 22:17:18 +01001710static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1711 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Jean Delvare90d66192007-10-08 18:24:35 +02001713 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001714 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001716static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1717 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001719 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001720 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001722 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001723 return -EINVAL;
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 data->vrm = val;
1726
1727 return count;
1728}
1729static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Jean Delvare5f2dc792010-03-05 22:17:18 +01001731static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1732 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 struct it87_data *data = it87_update_device(dev);
1735 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1736}
1737static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001738
Jean Delvare738e5e02010-08-14 21:08:50 +02001739static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1740 char *buf)
1741{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001742 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001743 "+5V",
1744 "5VSB",
1745 "Vbat",
1746 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001747 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001748 "+3.3V",
1749 "3VSB",
1750 "Vbat",
1751 };
1752 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001753 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001754 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001755
Justin Maggardead80802015-08-05 12:53:08 -07001756 if (has_12mv_adc(data) || has_10_9mv_adc(data))
1757 label = labels_it8721[nr];
1758 else
1759 label = labels[nr];
1760
1761 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001762}
1763static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1764static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1765static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001766/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001767static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001768
Guenter Roeck52929712013-03-30 14:00:08 -07001769static umode_t it87_in_is_visible(struct kobject *kobj,
1770 struct attribute *attr, int index)
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001771{
Guenter Roeck52929712013-03-30 14:00:08 -07001772 struct device *dev = container_of(kobj, struct device, kobj);
1773 struct it87_data *data = dev_get_drvdata(dev);
1774 int i = index / 5; /* voltage index */
1775 int a = index % 5; /* attribute index */
1776
Guenter Roeckf838aa22015-04-01 20:09:36 -07001777 if (index >= 40) { /* in8 and higher only have input attributes */
Guenter Roeck52929712013-03-30 14:00:08 -07001778 i = index - 40 + 8;
1779 a = 0;
1780 }
1781
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001782 if (!(data->has_in & BIT(i)))
Guenter Roeck52929712013-03-30 14:00:08 -07001783 return 0;
1784
1785 if (a == 4 && !data->has_beep)
1786 return 0;
1787
1788 return attr->mode;
1789}
1790
1791static struct attribute *it87_attributes_in[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001792 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001793 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001794 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001795 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001796 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
1797
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001798 &sensor_dev_attr_in1_input.dev_attr.attr,
1799 &sensor_dev_attr_in1_min.dev_attr.attr,
1800 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001801 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001802 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
1803
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001804 &sensor_dev_attr_in2_input.dev_attr.attr,
1805 &sensor_dev_attr_in2_min.dev_attr.attr,
1806 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001807 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001808 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
1809
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001810 &sensor_dev_attr_in3_input.dev_attr.attr,
1811 &sensor_dev_attr_in3_min.dev_attr.attr,
1812 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001813 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001814 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
1815
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001816 &sensor_dev_attr_in4_input.dev_attr.attr,
1817 &sensor_dev_attr_in4_min.dev_attr.attr,
1818 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001819 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001820 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
1821
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001822 &sensor_dev_attr_in5_input.dev_attr.attr,
1823 &sensor_dev_attr_in5_min.dev_attr.attr,
1824 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001825 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001826 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
1827
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001828 &sensor_dev_attr_in6_input.dev_attr.attr,
1829 &sensor_dev_attr_in6_min.dev_attr.attr,
1830 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001831 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001832 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
1833
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001834 &sensor_dev_attr_in7_input.dev_attr.attr,
1835 &sensor_dev_attr_in7_min.dev_attr.attr,
1836 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001837 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001838 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
Jean Delvare87808be2006-09-24 21:17:13 +02001839
Guenter Roeck52929712013-03-30 14:00:08 -07001840 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
Guenter Roeck52929712013-03-30 14:00:08 -07001841 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
Guenter Roeckf838aa22015-04-01 20:09:36 -07001842 &sensor_dev_attr_in10_input.dev_attr.attr, /* 41 */
1843 &sensor_dev_attr_in11_input.dev_attr.attr, /* 41 */
1844 &sensor_dev_attr_in12_input.dev_attr.attr, /* 41 */
Guenter Roeck52929712013-03-30 14:00:08 -07001845};
1846
1847static const struct attribute_group it87_group_in = {
1848 .attrs = it87_attributes_in,
1849 .is_visible = it87_in_is_visible,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001850};
1851
Guenter Roeck87533772013-03-30 14:23:20 -07001852static umode_t it87_temp_is_visible(struct kobject *kobj,
1853 struct attribute *attr, int index)
Guenter Roeck4573acb2012-03-26 16:17:41 -07001854{
Guenter Roeck87533772013-03-30 14:23:20 -07001855 struct device *dev = container_of(kobj, struct device, kobj);
1856 struct it87_data *data = dev_get_drvdata(dev);
1857 int i = index / 7; /* temperature index */
1858 int a = index % 7; /* attribute index */
1859
Guenter Roeckcc18da72015-04-01 10:03:01 -07001860 if (index >= 21) {
1861 i = index - 21 + 3;
1862 a = 0;
1863 }
1864
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001865 if (!(data->has_temp & BIT(i)))
Guenter Roeck87533772013-03-30 14:23:20 -07001866 return 0;
1867
1868 if (a == 5 && !has_temp_offset(data))
1869 return 0;
1870
1871 if (a == 6 && !data->has_beep)
1872 return 0;
1873
1874 return attr->mode;
1875}
1876
1877static struct attribute *it87_attributes_temp[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001878 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001879 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001880 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001881 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001882 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001883 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
1884 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
1885
Guenter Roeckcc18da72015-04-01 10:03:01 -07001886 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001887 &sensor_dev_attr_temp2_max.dev_attr.attr,
1888 &sensor_dev_attr_temp2_min.dev_attr.attr,
1889 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001890 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001891 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1892 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1893
Guenter Roeckcc18da72015-04-01 10:03:01 -07001894 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001895 &sensor_dev_attr_temp3_max.dev_attr.attr,
1896 &sensor_dev_attr_temp3_min.dev_attr.attr,
1897 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001898 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001899 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1900 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001901
Guenter Roeckcc18da72015-04-01 10:03:01 -07001902 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
1903 &sensor_dev_attr_temp5_input.dev_attr.attr,
1904 &sensor_dev_attr_temp6_input.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001905 NULL
Guenter Roeck4573acb2012-03-26 16:17:41 -07001906};
1907
Guenter Roeck87533772013-03-30 14:23:20 -07001908static const struct attribute_group it87_group_temp = {
1909 .attrs = it87_attributes_temp,
1910 .is_visible = it87_temp_is_visible,
Guenter Roeck161d8982012-12-19 22:17:01 +01001911};
1912
Guenter Roeckd3766842013-03-30 15:47:21 -07001913static umode_t it87_is_visible(struct kobject *kobj,
1914 struct attribute *attr, int index)
1915{
1916 struct device *dev = container_of(kobj, struct device, kobj);
1917 struct it87_data *data = dev_get_drvdata(dev);
1918
Guenter Roeck8638d0a2015-03-30 11:13:49 -07001919 if ((index == 2 || index == 3) && !data->has_vid)
Guenter Roeckd3766842013-03-30 15:47:21 -07001920 return 0;
1921
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001922 if (index > 3 && !(data->in_internal & BIT(index - 4)))
Guenter Roeckd3766842013-03-30 15:47:21 -07001923 return 0;
1924
1925 return attr->mode;
1926}
1927
Guenter Roeck4573acb2012-03-26 16:17:41 -07001928static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001929 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001930 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
Guenter Roeck8638d0a2015-03-30 11:13:49 -07001931 &dev_attr_vrm.attr, /* 2 */
1932 &dev_attr_cpu0_vid.attr, /* 3 */
1933 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
Guenter Roeckd3766842013-03-30 15:47:21 -07001934 &sensor_dev_attr_in7_label.dev_attr.attr,
1935 &sensor_dev_attr_in8_label.dev_attr.attr,
1936 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001937 NULL
1938};
1939
1940static const struct attribute_group it87_group = {
1941 .attrs = it87_attributes,
Guenter Roeckd3766842013-03-30 15:47:21 -07001942 .is_visible = it87_is_visible,
Jean Delvare87808be2006-09-24 21:17:13 +02001943};
1944
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001945static umode_t it87_fan_is_visible(struct kobject *kobj,
1946 struct attribute *attr, int index)
1947{
1948 struct device *dev = container_of(kobj, struct device, kobj);
1949 struct it87_data *data = dev_get_drvdata(dev);
1950 int i = index / 5; /* fan index */
1951 int a = index % 5; /* attribute index */
1952
1953 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
1954 i = (index - 15) / 4 + 3;
1955 a = (index - 15) % 4;
1956 }
1957
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001958 if (!(data->has_fan & BIT(i)))
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001959 return 0;
1960
1961 if (a == 3) { /* beep */
1962 if (!data->has_beep)
1963 return 0;
1964 /* first fan beep attribute is writable */
1965 if (i == __ffs(data->has_fan))
1966 return attr->mode | S_IWUSR;
1967 }
1968
1969 if (a == 4 && has_16bit_fans(data)) /* divisor */
1970 return 0;
1971
1972 return attr->mode;
1973}
1974
1975static struct attribute *it87_attributes_fan[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001976 &sensor_dev_attr_fan1_input.dev_attr.attr,
1977 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001978 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001979 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
1980 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
1981
Jean Delvare87808be2006-09-24 21:17:13 +02001982 &sensor_dev_attr_fan2_input.dev_attr.attr,
1983 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001984 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001985 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1986 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
1987
Jean Delvare87808be2006-09-24 21:17:13 +02001988 &sensor_dev_attr_fan3_input.dev_attr.attr,
1989 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001990 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001991 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1992 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
1993
1994 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001995 &sensor_dev_attr_fan4_min.dev_attr.attr,
1996 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001997 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1998
1999 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01002000 &sensor_dev_attr_fan5_min.dev_attr.attr,
2001 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002002 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2003
2004 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002005 &sensor_dev_attr_fan6_min.dev_attr.attr,
2006 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002007 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002008 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002009};
2010
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002011static const struct attribute_group it87_group_fan = {
2012 .attrs = it87_attributes_fan,
2013 .is_visible = it87_fan_is_visible,
Guenter Roecke1169ba2012-12-19 22:17:01 +01002014};
Jean Delvare723a0aa2010-03-05 22:17:16 +01002015
Guenter Roeck5c391262013-03-30 15:02:12 -07002016static umode_t it87_pwm_is_visible(struct kobject *kobj,
2017 struct attribute *attr, int index)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002018{
2019 struct device *dev = container_of(kobj, struct device, kobj);
2020 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck5c391262013-03-30 15:02:12 -07002021 int i = index / 4; /* pwm index */
2022 int a = index % 4; /* attribute index */
Guenter Roeck60878bc2015-03-26 19:57:42 -07002023
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002024 if (!(data->has_pwm & BIT(i)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002025 return 0;
2026
2027 /* pwmX_auto_channels_temp is only writable for old auto pwm */
2028 if (a == 3 && has_old_autopwm(data))
2029 return attr->mode | S_IWUSR;
2030
2031 /* pwm2_freq is writable if there are two pwm frequency selects */
2032 if (has_pwm_freq2(data) && i == 1 && a == 2)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002033 return attr->mode | S_IWUSR;
2034
2035 return attr->mode;
2036}
2037
Guenter Roeck5c391262013-03-30 15:02:12 -07002038static struct attribute *it87_attributes_pwm[] = {
2039 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2040 &sensor_dev_attr_pwm1.dev_attr.attr,
2041 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2042 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2043
2044 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2045 &sensor_dev_attr_pwm2.dev_attr.attr,
2046 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2047 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2048
2049 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2050 &sensor_dev_attr_pwm3.dev_attr.attr,
2051 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2052 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2053
2054 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2055 &sensor_dev_attr_pwm4.dev_attr.attr,
2056 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2057 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2058
2059 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2060 &sensor_dev_attr_pwm5.dev_attr.attr,
2061 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2062 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2063
2064 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2065 &sensor_dev_attr_pwm6.dev_attr.attr,
2066 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2067 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2068
2069 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002070};
2071
Guenter Roeck5c391262013-03-30 15:02:12 -07002072static const struct attribute_group it87_group_pwm = {
2073 .attrs = it87_attributes_pwm,
2074 .is_visible = it87_pwm_is_visible,
2075};
2076
2077static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2078 struct attribute *attr, int index)
2079{
2080 struct device *dev = container_of(kobj, struct device, kobj);
2081 struct it87_data *data = dev_get_drvdata(dev);
2082 int i = index / 9; /* pwm index */
2083
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002084 if (!(data->has_pwm & BIT(i)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002085 return 0;
2086
2087 return attr->mode;
2088}
2089
2090static struct attribute *it87_attributes_auto_pwm[] = {
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002091 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2092 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2093 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2094 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2095 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2096 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2097 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2098 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2099 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002100
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002101 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
2102 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2103 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2104 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2105 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2106 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2107 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2108 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2109 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002110
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002111 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
2112 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2113 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2114 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2115 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2116 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2117 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2118 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2119 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002120
Guenter Roeck5c391262013-03-30 15:02:12 -07002121 NULL,
2122};
2123
2124static const struct attribute_group it87_group_auto_pwm = {
2125 .attrs = it87_attributes_auto_pwm,
2126 .is_visible = it87_auto_pwm_is_visible,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002127};
2128
Jean Delvare2d8672c2005-07-19 23:56:35 +02002129/* SuperIO detection - will change isa_address if a chip is found */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002130static int __init it87_find(int sioaddr, unsigned short *address,
2131 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002133 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002134 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002135 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002136 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002138 err = superio_enter(sioaddr);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002139 if (err)
2140 return err;
2141
2142 err = -ENODEV;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002143 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002144
2145 switch (chip_type) {
2146 case IT8705F_DEVID:
2147 sio_data->type = it87;
2148 break;
2149 case IT8712F_DEVID:
2150 sio_data->type = it8712;
2151 break;
2152 case IT8716F_DEVID:
2153 case IT8726F_DEVID:
2154 sio_data->type = it8716;
2155 break;
2156 case IT8718F_DEVID:
2157 sio_data->type = it8718;
2158 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002159 case IT8720F_DEVID:
2160 sio_data->type = it8720;
2161 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002162 case IT8721F_DEVID:
2163 sio_data->type = it8721;
2164 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01002165 case IT8728F_DEVID:
2166 sio_data->type = it8728;
2167 break;
Justin Maggardead80802015-08-05 12:53:08 -07002168 case IT8732F_DEVID:
2169 sio_data->type = it8732;
2170 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06002171 case IT8771E_DEVID:
2172 sio_data->type = it8771;
2173 break;
2174 case IT8772E_DEVID:
2175 sio_data->type = it8772;
2176 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002177 case IT8781F_DEVID:
2178 sio_data->type = it8781;
2179 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08002180 case IT8782F_DEVID:
2181 sio_data->type = it8782;
2182 break;
2183 case IT8783E_DEVID:
2184 sio_data->type = it8783;
2185 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002186 case IT8786E_DEVID:
2187 sio_data->type = it8786;
2188 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07002189 case IT8790E_DEVID:
2190 sio_data->type = it8790;
2191 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02002192 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02002193 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002194 sio_data->type = it8603;
2195 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002196 case IT8620E_DEVID:
2197 sio_data->type = it8620;
2198 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002199 case 0xffff: /* No device at all */
2200 goto exit;
2201 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01002202 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002203 goto exit;
2204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002206 superio_select(sioaddr, PME);
2207 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002208 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 goto exit;
2210 }
2211
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002212 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002214 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 goto exit;
2216 }
2217
2218 err = 0;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002219 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07002220 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2221 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002222 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002224 config = &it87_devices[sio_data->type];
2225
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002226 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002227 if (has_in7_internal(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002228 sio_data->internal |= BIT(1);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002229
Jean Delvare738e5e02010-08-14 21:08:50 +02002230 /* in8 (Vbat) is always internal */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002231 sio_data->internal |= BIT(2);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002232
Guenter Roeck73055402015-03-26 09:16:32 -07002233 /* in9 (AVCC3), always internal if supported */
2234 if (has_avcc3(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002235 sio_data->internal |= BIT(3); /* in9 is AVCC */
Guenter Roeck73055402015-03-26 09:16:32 -07002236 else
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002237 sio_data->skip_in |= BIT(9);
Jean Delvare738e5e02010-08-14 21:08:50 +02002238
Guenter Roeck36c4d982015-03-26 18:18:19 -07002239 if (!has_six_pwm(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002240 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002241
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002242 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01002243 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01002244
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002245 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2246 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01002247 /* The IT8705F has a different LD number for GPIO */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002248 superio_select(sioaddr, 5);
2249 sio_data->beep_pin = superio_inb(sioaddr,
2250 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002251 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002252 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002253
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002254 superio_select(sioaddr, GPIO);
Guenter Roeck0531d982012-03-02 11:46:44 -08002255
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002256 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2257 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2258 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2259 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2260 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002261
Guenter Roeck0531d982012-03-02 11:46:44 -08002262 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002263 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
2264 sio_data->skip_fan |= BIT(2);
2265 if ((reg25 & BIT(4))
2266 || (!(reg2a & BIT(1)) && (regef & BIT(0))))
2267 sio_data->skip_pwm |= BIT(2);
Guenter Roeck0531d982012-03-02 11:46:44 -08002268
2269 /* Check if fan2 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002270 if (reg27 & BIT(7))
2271 sio_data->skip_fan |= BIT(1);
2272 if (reg27 & BIT(3))
2273 sio_data->skip_pwm |= BIT(1);
Guenter Roeck0531d982012-03-02 11:46:44 -08002274
2275 /* VIN5 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002276 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
2277 sio_data->skip_in |= BIT(5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002278
2279 /* VIN6 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002280 if (reg27 & BIT(1))
2281 sio_data->skip_in |= BIT(6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002282
2283 /*
2284 * VIN7
2285 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2286 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002287 if (reg27 & BIT(2)) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002288 /*
2289 * The data sheet is a bit unclear regarding the
2290 * internal voltage divider for VCCH5V. It says
2291 * "This bit enables and switches VIN7 (pin 91) to the
2292 * internal voltage divider for VCCH5V".
2293 * This is different to other chips, where the internal
2294 * voltage divider would connect VIN7 to an internal
2295 * voltage source. Maybe that is the case here as well.
2296 *
2297 * Since we don't know for sure, re-route it if that is
2298 * not the case, and ask the user to report if the
2299 * resulting voltage is sane.
2300 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002301 if (!(reg2c & BIT(1))) {
2302 reg2c |= BIT(1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002303 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2304 reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002305 pr_notice("Routing internal VCCH5V to in7.\n");
2306 }
2307 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2308 pr_notice("Please report if it displays a reasonable voltage.\n");
2309 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002310
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002311 if (reg2c & BIT(0))
2312 sio_data->internal |= BIT(0);
2313 if (reg2c & BIT(1))
2314 sio_data->internal |= BIT(1);
Guenter Roeck0531d982012-03-02 11:46:44 -08002315
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002316 sio_data->beep_pin = superio_inb(sioaddr,
2317 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002318 } else if (sio_data->type == it8603) {
2319 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002320
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002321 superio_select(sioaddr, GPIO);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002322
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002323 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002324
2325 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002326 if (reg27 & BIT(6))
2327 sio_data->skip_pwm |= BIT(2);
2328 if (reg27 & BIT(7))
2329 sio_data->skip_fan |= BIT(2);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002330
2331 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002332 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002333 if (reg29 & BIT(1))
2334 sio_data->skip_pwm |= BIT(1);
2335 if (reg29 & BIT(2))
2336 sio_data->skip_fan |= BIT(1);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002337
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002338 sio_data->skip_in |= BIT(5); /* No VIN5 */
2339 sio_data->skip_in |= BIT(6); /* No VIN6 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002340
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002341 sio_data->beep_pin = superio_inb(sioaddr,
2342 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002343 } else if (sio_data->type == it8620) {
2344 int reg;
2345
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002346 superio_select(sioaddr, GPIO);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002347
Guenter Roeck36c4d982015-03-26 18:18:19 -07002348 /* Check for pwm5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002349 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002350 if (reg & BIT(6))
2351 sio_data->skip_pwm |= BIT(4);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002352
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002353 /* Check for fan4, fan5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002354 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002355 if (!(reg & BIT(5)))
2356 sio_data->skip_fan |= BIT(3);
2357 if (!(reg & BIT(4)))
2358 sio_data->skip_fan |= BIT(4);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002359
2360 /* Check for pwm3, fan3 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002361 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002362 if (reg & BIT(6))
2363 sio_data->skip_pwm |= BIT(2);
2364 if (reg & BIT(7))
2365 sio_data->skip_fan |= BIT(2);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002366
Guenter Roeck36c4d982015-03-26 18:18:19 -07002367 /* Check for pwm4 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002368 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002369 if (!(reg & BIT(2)))
2370 sio_data->skip_pwm |= BIT(3);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002371
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002372 /* Check for pwm2, fan2 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002373 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002374 if (reg & BIT(1))
2375 sio_data->skip_pwm |= BIT(1);
2376 if (reg & BIT(2))
2377 sio_data->skip_fan |= BIT(1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002378 /* Check for pwm6, fan6 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002379 if (!(reg & BIT(7))) {
2380 sio_data->skip_pwm |= BIT(5);
2381 sio_data->skip_fan |= BIT(5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002382 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002383
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002384 sio_data->beep_pin = superio_inb(sioaddr,
2385 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002386 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002387 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002388 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002389
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002390 superio_select(sioaddr, GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002391
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002392 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002393 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002394 /* We need at least 4 VID pins */
2395 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002396 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002397 sio_data->skip_vid = 1;
2398 }
Jean Delvare895ff262009-12-09 20:35:47 +01002399 }
2400
Jean Delvare591ec652009-12-09 20:35:48 +01002401 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002402 if (reg & BIT(6))
2403 sio_data->skip_pwm |= BIT(2);
2404 if (reg & BIT(7))
2405 sio_data->skip_fan |= BIT(2);
Jean Delvare591ec652009-12-09 20:35:48 +01002406
2407 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002408 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002409 if (reg & BIT(1))
2410 sio_data->skip_pwm |= BIT(1);
2411 if (reg & BIT(2))
2412 sio_data->skip_fan |= BIT(1);
Jean Delvare591ec652009-12-09 20:35:48 +01002413
Jean Delvare895ff262009-12-09 20:35:47 +01002414 if ((sio_data->type == it8718 || sio_data->type == it8720)
2415 && !(sio_data->skip_vid))
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002416 sio_data->vid_value = superio_inb(sioaddr,
2417 IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002418
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002419 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002420
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002421 uart6 = sio_data->type == it8782 && (reg & BIT(2));
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002422
Jean Delvare436cad22010-07-09 16:22:48 +02002423 /*
2424 * The IT8720F has no VIN7 pin, so VCCH should always be
2425 * routed internally to VIN7 with an internal divider.
2426 * Curiously, there still is a configuration bit to control
2427 * this, which means it can be set incorrectly. And even
2428 * more curiously, many boards out there are improperly
2429 * configured, even though the IT8720F datasheet claims
2430 * that the internal routing of VCCH to VIN7 is the default
2431 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002432 *
2433 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002434 * If UART6 is enabled, re-route VIN7 to the internal divider
2435 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002436 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002437 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
2438 reg |= BIT(1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002439 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002440 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002441 }
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002442 if (reg & BIT(0))
2443 sio_data->internal |= BIT(0);
2444 if (reg & BIT(1))
2445 sio_data->internal |= BIT(1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002446
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002447 /*
2448 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2449 * While VIN7 can be routed to the internal voltage divider,
2450 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002451 *
2452 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2453 * is the temperature source. Since we can not read the
2454 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002455 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002456 if (uart6) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002457 sio_data->skip_in |= BIT(5) | BIT(6);
2458 sio_data->skip_temp |= BIT(2);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002459 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002460
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002461 sio_data->beep_pin = superio_inb(sioaddr,
2462 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002463 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002464 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002465 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002466
Jean Delvare98dd22c2008-10-09 15:33:58 +02002467 /* Disable specific features based on DMI strings */
2468 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2469 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2470 if (board_vendor && board_name) {
2471 if (strcmp(board_vendor, "nVIDIA") == 0
2472 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002473 /*
2474 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2475 * connected to a fan, but to something else. One user
2476 * has reported instant system power-off when changing
2477 * the PWM2 duty cycle, so we disable it.
2478 * I use the board name string as the trigger in case
2479 * the same board is ever used in other systems.
2480 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002481 pr_info("Disabling pwm2 due to hardware constraints\n");
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002482 sio_data->skip_pwm = BIT(1);
Jean Delvare98dd22c2008-10-09 15:33:58 +02002483 }
2484 }
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486exit:
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002487 superio_exit(sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return err;
2489}
2490
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002491/* Called when we have found a new IT87. */
2492static void it87_init_device(struct platform_device *pdev)
2493{
2494 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2495 struct it87_data *data = platform_get_drvdata(pdev);
2496 int tmp, i;
2497 u8 mask;
2498
2499 /*
2500 * For each PWM channel:
2501 * - If it is in automatic mode, setting to manual mode should set
2502 * the fan to full speed by default.
2503 * - If it is in manual mode, we need a mapping to temperature
2504 * channels to use when later setting to automatic mode later.
2505 * Use a 1:1 mapping by default (we are clueless.)
2506 * In both cases, the value can (and should) be changed by the user
2507 * prior to switching to a different mode.
2508 * Note that this is no longer needed for the IT8721F and later, as
2509 * these have separate registers for the temperature mapping and the
2510 * manual duty cycle.
2511 */
2512 for (i = 0; i < 3; i++) {
2513 data->pwm_temp_map[i] = i;
2514 data->pwm_duty[i] = 0x7f; /* Full speed */
2515 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2516 }
2517
2518 /*
2519 * Some chips seem to have default value 0xff for all limit
2520 * registers. For low voltage limits it makes no sense and triggers
2521 * alarms, so change to 0 instead. For high temperature limits, it
2522 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2523 * but is still confusing, so change to 127 degrees C.
2524 */
2525 for (i = 0; i < 8; i++) {
2526 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2527 if (tmp == 0xff)
2528 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2529 }
2530 for (i = 0; i < 3; i++) {
2531 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2532 if (tmp == 0xff)
2533 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2534 }
2535
2536 /*
2537 * Temperature channels are not forcibly enabled, as they can be
2538 * set to two different sensor types and we can't guess which one
2539 * is correct for a given system. These channels can be enabled at
2540 * run-time through the temp{1-3}_type sysfs accessors if needed.
2541 */
2542
2543 /* Check if voltage monitors are reset manually or by some reason */
2544 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2545 if ((tmp & 0xff) == 0) {
2546 /* Enable all voltage monitors */
2547 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2548 }
2549
2550 /* Check if tachometers are reset manually or by some reason */
2551 mask = 0x70 & ~(sio_data->skip_fan << 4);
2552 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2553 if ((data->fan_main_ctrl & mask) == 0) {
2554 /* Enable all fan tachometers */
2555 data->fan_main_ctrl |= mask;
2556 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2557 data->fan_main_ctrl);
2558 }
2559 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2560
2561 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2562
2563 /* Set tachometers to 16-bit mode if needed */
2564 if (has_fan16_config(data)) {
2565 if (~tmp & 0x07 & data->has_fan) {
2566 dev_dbg(&pdev->dev,
2567 "Setting fan1-3 to 16-bit mode\n");
2568 it87_write_value(data, IT87_REG_FAN_16BIT,
2569 tmp | 0x07);
2570 }
2571 }
2572
2573 /* Check for additional fans */
2574 if (has_five_fans(data)) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002575 if (tmp & BIT(4))
2576 data->has_fan |= BIT(3); /* fan4 enabled */
2577 if (tmp & BIT(5))
2578 data->has_fan |= BIT(4); /* fan5 enabled */
2579 if (has_six_fans(data) && (tmp & BIT(2)))
2580 data->has_fan |= BIT(5); /* fan6 enabled */
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002581 }
2582
2583 /* Fan input pins may be used for alternative functions */
2584 data->has_fan &= ~sio_data->skip_fan;
2585
2586 /* Check if pwm5, pwm6 are enabled */
2587 if (has_six_pwm(data)) {
2588 /* The following code may be IT8620E specific */
2589 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2590 if ((tmp & 0xc0) == 0xc0)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002591 sio_data->skip_pwm |= BIT(4);
2592 if (!(tmp & BIT(3)))
2593 sio_data->skip_pwm |= BIT(5);
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002594 }
2595
2596 /* Start monitoring */
2597 it87_write_value(data, IT87_REG_CONFIG,
2598 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2599 | (update_vbat ? 0x41 : 0x01));
2600}
2601
2602/* Return 1 if and only if the PWM interface is safe to use */
2603static int it87_check_pwm(struct device *dev)
2604{
2605 struct it87_data *data = dev_get_drvdata(dev);
2606 /*
2607 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2608 * and polarity set to active low is sign that this is the case so we
2609 * disable pwm control to protect the user.
2610 */
2611 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2612
2613 if ((tmp & 0x87) == 0) {
2614 if (fix_pwm_polarity) {
2615 /*
2616 * The user asks us to attempt a chip reconfiguration.
2617 * This means switching to active high polarity and
2618 * inverting all fan speed values.
2619 */
2620 int i;
2621 u8 pwm[3];
2622
2623 for (i = 0; i < 3; i++)
2624 pwm[i] = it87_read_value(data,
2625 IT87_REG_PWM[i]);
2626
2627 /*
2628 * If any fan is in automatic pwm mode, the polarity
2629 * might be correct, as suspicious as it seems, so we
2630 * better don't change anything (but still disable the
2631 * PWM interface).
2632 */
2633 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2634 dev_info(dev,
2635 "Reconfiguring PWM to active high polarity\n");
2636 it87_write_value(data, IT87_REG_FAN_CTL,
2637 tmp | 0x87);
2638 for (i = 0; i < 3; i++)
2639 it87_write_value(data,
2640 IT87_REG_PWM[i],
2641 0x7f & ~pwm[i]);
2642 return 1;
2643 }
2644
2645 dev_info(dev,
2646 "PWM configuration is too broken to be fixed\n");
2647 }
2648
2649 dev_info(dev,
2650 "Detected broken BIOS defaults, disabling PWM interface\n");
2651 return 0;
2652 } else if (fix_pwm_polarity) {
2653 dev_info(dev,
2654 "PWM configuration looks sane, won't touch\n");
2655 }
2656
2657 return 1;
2658}
2659
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002660static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002663 struct resource *res;
2664 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002665 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 int enable_pwm_interface;
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002667 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002669 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002670 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2671 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002672 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2673 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002674 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002675 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Guenter Roeck62a1d052012-03-24 21:54:41 -07002678 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2679 if (!data)
2680 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002682 data->addr = res->start;
2683 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002684 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002685 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002686 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002687 /*
2688 * IT8705F Datasheet 0.4.1, 3h == Version G.
2689 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2690 * These are the first revisions with 16-bit tachometer support.
2691 */
2692 switch (data->type) {
2693 case it87:
2694 if (sio_data->revision >= 0x03) {
2695 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002696 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002697 }
2698 break;
2699 case it8712:
2700 if (sio_data->revision >= 0x08) {
2701 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002702 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2703 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002704 }
2705 break;
2706 default:
2707 break;
2708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002711 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002712 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2713 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002715 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002717 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002720 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002722 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002723 if (has_12mv_adc(data)) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002724 if (sio_data->internal & BIT(0))
2725 data->in_scaled |= BIT(3); /* in3 is AVCC */
2726 if (sio_data->internal & BIT(1))
2727 data->in_scaled |= BIT(7); /* in7 is VSB */
2728 if (sio_data->internal & BIT(2))
2729 data->in_scaled |= BIT(8); /* in8 is Vbat */
2730 if (sio_data->internal & BIT(3))
2731 data->in_scaled |= BIT(9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002732 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2733 sio_data->type == it8783) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002734 if (sio_data->internal & BIT(0))
2735 data->in_scaled |= BIT(3); /* in3 is VCC5V */
2736 if (sio_data->internal & BIT(1))
2737 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002738 }
2739
Guenter Roeck4573acb2012-03-26 16:17:41 -07002740 data->has_temp = 0x07;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002741 if (sio_data->skip_temp & BIT(2)) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002742 if (sio_data->type == it8782
2743 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002744 data->has_temp &= ~BIT(2);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002745 }
2746
Guenter Roeckd3766842013-03-30 15:47:21 -07002747 data->in_internal = sio_data->internal;
Guenter Roeck52929712013-03-30 14:00:08 -07002748 data->has_in = 0x3ff & ~sio_data->skip_in;
2749
Guenter Roeckcc18da72015-04-01 10:03:01 -07002750 if (has_six_temp(data)) {
2751 u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
2752
Guenter Roeckf838aa22015-04-01 20:09:36 -07002753 /* Check for additional temperature sensors */
Guenter Roeckcc18da72015-04-01 10:03:01 -07002754 if ((reg & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002755 data->has_temp |= BIT(3);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002756 if (((reg >> 2) & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002757 data->has_temp |= BIT(4);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002758 if (((reg >> 4) & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002759 data->has_temp |= BIT(5);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002760
2761 /* Check for additional voltage sensors */
2762 if ((reg & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002763 data->has_in |= BIT(10);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002764 if (((reg >> 2) & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002765 data->has_in |= BIT(11);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002766 if (((reg >> 4) & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002767 data->has_in |= BIT(12);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002768 }
2769
Guenter Roeck52929712013-03-30 14:00:08 -07002770 data->has_beep = !!sio_data->beep_pin;
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002773 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Guenter Roeckd3766842013-03-30 15:47:21 -07002775 if (!sio_data->skip_vid) {
2776 data->has_vid = true;
2777 data->vrm = vid_which_vrm();
2778 /* VID reading from Super-I/O config space if available */
2779 data->vid = sio_data->vid_value;
2780 }
2781
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002782 /* Prepare for sysfs hooks */
2783 data->groups[0] = &it87_group;
2784 data->groups[1] = &it87_group_in;
2785 data->groups[2] = &it87_group_temp;
2786 data->groups[3] = &it87_group_fan;
Jean Delvare17d648b2006-08-28 14:23:46 +02002787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (enable_pwm_interface) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002789 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
Guenter Roeck5c391262013-03-30 15:02:12 -07002790 data->has_pwm &= ~sio_data->skip_pwm;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002791
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002792 data->groups[4] = &it87_group_pwm;
2793 if (has_old_autopwm(data))
2794 data->groups[5] = &it87_group_auto_pwm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 }
2796
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002797 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
2798 it87_devices[sio_data->type].name,
2799 data, data->groups);
2800 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002803static struct platform_driver it87_driver = {
2804 .driver = {
2805 .name = DRVNAME,
2806 },
2807 .probe = it87_probe,
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002808};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Guenter Roecke84bd952015-03-28 08:24:29 -07002810static int __init it87_device_add(int index, unsigned short address,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002811 const struct it87_sio_data *sio_data)
2812{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002813 struct platform_device *pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002814 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002815 .start = address + IT87_EC_OFFSET,
2816 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002817 .name = DRVNAME,
2818 .flags = IORESOURCE_IO,
2819 };
2820 int err;
2821
Jean Delvareb9acb642009-01-07 16:37:35 +01002822 err = acpi_check_resource_conflict(&res);
2823 if (err)
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002824 return err;
Jean Delvareb9acb642009-01-07 16:37:35 +01002825
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002826 pdev = platform_device_alloc(DRVNAME, address);
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002827 if (!pdev)
2828 return -ENOMEM;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002829
2830 err = platform_device_add_resources(pdev, &res, 1);
2831 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002832 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002833 goto exit_device_put;
2834 }
2835
2836 err = platform_device_add_data(pdev, sio_data,
2837 sizeof(struct it87_sio_data));
2838 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002839 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002840 goto exit_device_put;
2841 }
2842
2843 err = platform_device_add(pdev);
2844 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002845 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002846 goto exit_device_put;
2847 }
2848
Guenter Roecke84bd952015-03-28 08:24:29 -07002849 it87_pdev[index] = pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002850 return 0;
2851
2852exit_device_put:
2853 platform_device_put(pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002854 return err;
2855}
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857static int __init sm_it87_init(void)
2858{
Guenter Roecke84bd952015-03-28 08:24:29 -07002859 int sioaddr[2] = { REG_2E, REG_4E };
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002860 struct it87_sio_data sio_data;
Guenter Roecke84bd952015-03-28 08:24:29 -07002861 unsigned short isa_address;
2862 bool found = false;
2863 int i, err;
Jean Delvarefde09502005-07-19 23:51:07 +02002864
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002865 err = platform_driver_register(&it87_driver);
2866 if (err)
2867 return err;
2868
Guenter Roecke84bd952015-03-28 08:24:29 -07002869 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
2870 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2871 isa_address = 0;
2872 err = it87_find(sioaddr[i], &isa_address, &sio_data);
2873 if (err || isa_address == 0)
2874 continue;
2875
2876 err = it87_device_add(i, isa_address, &sio_data);
2877 if (err)
2878 goto exit_dev_unregister;
2879 found = true;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002880 }
2881
Guenter Roecke84bd952015-03-28 08:24:29 -07002882 if (!found) {
2883 err = -ENODEV;
2884 goto exit_unregister;
2885 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002886 return 0;
Guenter Roecke84bd952015-03-28 08:24:29 -07002887
2888exit_dev_unregister:
2889 /* NULL check handled by platform_device_unregister */
2890 platform_device_unregister(it87_pdev[0]);
2891exit_unregister:
2892 platform_driver_unregister(&it87_driver);
2893 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
2895
2896static void __exit sm_it87_exit(void)
2897{
Guenter Roecke84bd952015-03-28 08:24:29 -07002898 /* NULL check handled by platform_device_unregister */
2899 platform_device_unregister(it87_pdev[1]);
2900 platform_device_unregister(it87_pdev[0]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002901 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
2904
Jean Delvare7c81c602014-01-29 20:40:08 +01002905MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002906MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907module_param(update_vbat, bool, 0);
2908MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2909module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002910MODULE_PARM_DESC(fix_pwm_polarity,
2911 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912MODULE_LICENSE("GPL");
2913
2914module_init(sm_it87_init);
2915module_exit(sm_it87_exit);