blob: e4fb466fd9c3ad9ac03dbfe19da7397a482e9bd9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jean Delvare5f2dc792010-03-05 22:17:18 +01002 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring.
4 *
5 * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 * addition to an Environment Controller (Enhanced Hardware Monitor and
8 * Fan Controller)
9 *
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
12 *
Rudolf Marekc145d5c2014-01-29 20:40:08 +010013 * Supports: IT8603E Super I/O chip w/LPC interface
Guenter Roeck3ba9d972015-02-13 20:13:20 -080014 * IT8620E Super I/O chip w/LPC interface
Rudolf Marek574e9bd2014-04-04 18:01:35 +020015 * IT8623E Super I/O chip w/LPC interface
Rudolf Marekc145d5c2014-01-29 20:40:08 +010016 * IT8705F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010017 * IT8712F Super I/O chip w/LPC interface
18 * IT8716F Super I/O chip w/LPC interface
19 * IT8718F Super I/O chip w/LPC interface
20 * IT8720F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020021 * IT8721F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010022 * IT8726F Super I/O chip w/LPC interface
Jean Delvare16b5dda2012-01-16 22:51:48 +010023 * IT8728F Super I/O chip w/LPC interface
Justin Maggardead80802015-08-05 12:53:08 -070024 * IT8732F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020025 * IT8758E Super I/O chip w/LPC interface
Guenter Roeckb0636702012-09-07 17:34:41 -060026 * IT8771E Super I/O chip w/LPC interface
27 * IT8772E Super I/O chip w/LPC interface
Guenter Roeck7bc32d22015-01-17 14:10:24 -080028 * IT8781F Super I/O chip w/LPC interface
Guenter Roeck0531d982012-03-02 11:46:44 -080029 * IT8782F Super I/O chip w/LPC interface
30 * IT8783E/F Super I/O chip w/LPC interface
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +010031 * IT8786E Super I/O chip w/LPC interface
Guenter Roeck4ee07152015-03-25 23:26:28 -070032 * IT8790E Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010033 * Sis950 A clone of the IT8705F
34 *
35 * Copyright (C) 2001 Chris Gauthron
Jean Delvare7c81c602014-01-29 20:40:08 +010036 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
Jean Delvare5f2dc792010-03-05 22:17:18 +010037 *
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version.
42 *
43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
47 *
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
51 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Joe Perchesa8ca1032011-01-12 21:55:10 +010053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/module.h>
56#include <linux/init.h>
57#include <linux/slab.h>
58#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020059#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040060#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020061#include <linux/hwmon-sysfs.h>
62#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040063#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010064#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020065#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020066#include <linux/string.h>
67#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010068#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020069#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
corentin.labbeb74f3fd2007-06-13 20:27:36 +020071#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Justin Maggardead80802015-08-05 12:53:08 -070073enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
74 it8771, it8772, it8781, it8782, it8783, it8786, it8790, it8603,
75 it8620 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jean Delvare67b671b2007-12-06 23:13:42 +010077static unsigned short force_id;
78module_param(force_id, ushort, 0);
79MODULE_PARM_DESC(force_id, "Override the detected device ID");
80
Guenter Roecke84bd952015-03-28 08:24:29 -070081static struct platform_device *it87_pdev[2];
corentin.labbeb74f3fd2007-06-13 20:27:36 +020082
Guenter Roeck3c2e3512015-03-28 08:03:10 -070083#define REG_2E 0x2e /* The register to read/write */
Guenter Roecke84bd952015-03-28 08:24:29 -070084#define REG_4E 0x4e /* Secondary register to read/write */
Guenter Roeck3c2e3512015-03-28 08:03:10 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define DEV 0x07 /* Register: Logical device select */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010088
89/* The device with the IT8718F/IT8720F VID value in it */
90#define GPIO 0x07
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define DEVID 0x20 /* Register: Device ID */
93#define DEVREV 0x22 /* Register: Device Revision */
94
Guenter Roeck3c2e3512015-03-28 08:03:10 -070095static inline int superio_inb(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Guenter Roeck3c2e3512015-03-28 08:03:10 -070097 outb(reg, ioreg);
98 return inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700101static inline void superio_outb(int ioreg, int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +0200102{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700103 outb(reg, ioreg);
104 outb(val, ioreg + 1);
Jean Delvare436cad22010-07-09 16:22:48 +0200105}
106
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700107static int superio_inw(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 int val;
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700110 outb(reg++, ioreg);
111 val = inb(ioreg + 1) << 8;
112 outb(reg, ioreg);
113 val |= inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return val;
115}
116
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700117static inline void superio_select(int ioreg, int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700119 outb(DEV, ioreg);
120 outb(ldn, ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700123static inline int superio_enter(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200125 /*
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700126 * Try to reserve ioreg and ioreg + 1 for exclusive access.
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200127 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700128 if (!request_muxed_region(ioreg, 2, DRVNAME))
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200129 return -EBUSY;
130
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700131 outb(0x87, ioreg);
132 outb(0x01, ioreg);
133 outb(0x55, ioreg);
Guenter Roecke84bd952015-03-28 08:24:29 -0700134 outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700138static inline void superio_exit(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700140 outb(0x02, ioreg);
141 outb(0x02, ioreg + 1);
142 release_region(ioreg, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Jean Delvare87673dd2006-08-28 14:37:19 +0200145/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define IT8712F_DEVID 0x8712
147#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200148#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200149#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100150#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200151#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400152#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100153#define IT8728F_DEVID 0x8728
Justin Maggardead80802015-08-05 12:53:08 -0700154#define IT8732F_DEVID 0x8732
Guenter Roeckb0636702012-09-07 17:34:41 -0600155#define IT8771E_DEVID 0x8771
156#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800157#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800158#define IT8782F_DEVID 0x8782
159#define IT8783E_DEVID 0x8783
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100160#define IT8786E_DEVID 0x8786
Guenter Roeck4ee07152015-03-25 23:26:28 -0700161#define IT8790E_DEVID 0x8790
Rudolf Marek7183ae82014-04-04 18:01:35 +0200162#define IT8603E_DEVID 0x8603
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800163#define IT8620E_DEVID 0x8620
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200164#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define IT87_ACT_REG 0x30
166#define IT87_BASE_REG 0x60
167
Jean Delvare87673dd2006-08-28 14:37:19 +0200168/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800169#define IT87_SIO_GPIO1_REG 0x25
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800170#define IT87_SIO_GPIO2_REG 0x26
Jean Delvare895ff262009-12-09 20:35:47 +0100171#define IT87_SIO_GPIO3_REG 0x27
Guenter Roeck36c4d982015-03-26 18:18:19 -0700172#define IT87_SIO_GPIO4_REG 0x28
Jean Delvare591ec652009-12-09 20:35:48 +0100173#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800174#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200175#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800176#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200177#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100178#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030181static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030184static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/* Many IT87 constants specified below */
187
188/* Length of ISA address segment */
189#define IT87_EXTENT 8
190
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500191/* Length of ISA address segment for Environmental Controller */
192#define IT87_EC_EXTENT 2
193
194/* Offset of EC registers from ISA base address */
195#define IT87_EC_OFFSET 5
196
197/* Where are the ISA address/data registers relative to the EC base address */
198#define IT87_ADDR_REG_OFFSET 0
199#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201/*----- The IT87 registers -----*/
202
203#define IT87_REG_CONFIG 0x00
204
205#define IT87_REG_ALARM1 0x01
206#define IT87_REG_ALARM2 0x02
207#define IT87_REG_ALARM3 0x03
208
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800209/*
210 * The IT8718F and IT8720F have the VID value in a different register, in
211 * Super-I/O configuration space.
212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800214/*
215 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
216 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
217 * mode.
218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200220#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
223
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700224static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
225static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
226static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
227static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
228static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#define IT87_REG_FAN_MAIN_CTRL 0x13
231#define IT87_REG_FAN_CTL 0x14
Guenter Roeck36c4d982015-03-26 18:18:19 -0700232static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
233static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235#define IT87_REG_VIN(nr) (0x20 + (nr))
236#define IT87_REG_TEMP(nr) (0x29 + (nr))
237
Guenter Roeck73055402015-03-26 09:16:32 -0700238#define IT87_REG_AVCC3 0x2f
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
241#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
242#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
243#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#define IT87_REG_VIN_ENABLE 0x50
246#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700247#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100248#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250#define IT87_REG_CHIPID 0x58
251
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100252#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
253#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
254
Guenter Roeck483db432012-12-19 22:17:02 +0100255struct it87_devices {
256 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700257 const char * const suffix;
Guenter Roeck483db432012-12-19 22:17:02 +0100258 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100259 u8 peci_mask;
260 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100261};
262
263#define FEAT_12MV_ADC (1 << 0)
264#define FEAT_NEWER_AUTOPWM (1 << 1)
265#define FEAT_OLD_AUTOPWM (1 << 2)
266#define FEAT_16BIT_FANS (1 << 3)
267#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100268#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100269#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800270#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
271#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800272#define FEAT_VID (1 << 9) /* Set if chip supports VID */
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700273#define FEAT_IN7_INTERNAL (1 << 10) /* Set if in7 is internal */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700274#define FEAT_SIX_FANS (1 << 11) /* Supports six fans */
Justin Maggardead80802015-08-05 12:53:08 -0700275#define FEAT_10_9MV_ADC (1 << 12)
Guenter Roeck73055402015-03-26 09:16:32 -0700276#define FEAT_AVCC3 (1 << 13) /* Chip supports in9/AVCC3 */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700277#define FEAT_SIX_PWM (1 << 14) /* Chip supports 6 pwm chn */
Guenter Roeck60878bc2015-03-26 19:57:42 -0700278#define FEAT_PWM_FREQ2 (1 << 15) /* Separate pwm freq 2 */
Guenter Roeck483db432012-12-19 22:17:02 +0100279
280static const struct it87_devices it87_devices[] = {
281 [it87] = {
282 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700283 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100284 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
285 },
286 [it8712] = {
287 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700288 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800289 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
290 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100291 },
292 [it8716] = {
293 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700294 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800295 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700296 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
Guenter Roeck483db432012-12-19 22:17:02 +0100297 },
298 [it8718] = {
299 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700300 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800301 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700302 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
303 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100304 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100305 },
306 [it8720] = {
307 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700308 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800309 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700310 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
311 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100312 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100313 },
314 [it8721] = {
315 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700316 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100317 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800318 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck60878bc2015-03-26 19:57:42 -0700319 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
320 | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100321 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100322 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100323 },
324 [it8728] = {
325 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700326 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100327 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700328 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700329 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100330 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100331 },
Justin Maggardead80802015-08-05 12:53:08 -0700332 [it8732] = {
333 .name = "it8732",
334 .suffix = "F",
335 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
336 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
337 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
338 .peci_mask = 0x07,
339 .old_peci_mask = 0x02, /* Actually reports PCH */
340 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600341 [it8771] = {
342 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700343 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600344 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700345 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
346 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800347 /* PECI: guesswork */
348 /* 12mV ADC (OHM) */
349 /* 16 bit fans (OHM) */
350 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600351 .peci_mask = 0x07,
352 },
353 [it8772] = {
354 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700355 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600356 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700357 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
358 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800359 /* PECI (coreboot) */
360 /* 12mV ADC (HWSensors4, OHM) */
361 /* 16 bit fans (HWSensors4, OHM) */
362 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600363 .peci_mask = 0x07,
364 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800365 [it8781] = {
366 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700367 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800368 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700369 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800370 .old_peci_mask = 0x4,
371 },
Guenter Roeck483db432012-12-19 22:17:02 +0100372 [it8782] = {
373 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700374 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100375 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700376 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100377 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100378 },
379 [it8783] = {
380 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700381 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100382 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700383 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100384 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100385 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100386 [it8786] = {
387 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700388 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100389 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700390 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
391 | FEAT_PWM_FREQ2,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100392 .peci_mask = 0x07,
393 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700394 [it8790] = {
395 .name = "it8790",
396 .suffix = "E",
397 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700398 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
399 | FEAT_PWM_FREQ2,
Guenter Roeck4ee07152015-03-25 23:26:28 -0700400 .peci_mask = 0x07,
401 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100402 [it8603] = {
403 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700404 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100405 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck73055402015-03-26 09:16:32 -0700406 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
Guenter Roeck60878bc2015-03-26 19:57:42 -0700407 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100408 .peci_mask = 0x07,
409 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800410 [it8620] = {
411 .name = "it8620",
412 .suffix = "E",
413 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700414 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700415 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2,
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800416 .peci_mask = 0x07,
417 },
Guenter Roeck483db432012-12-19 22:17:02 +0100418};
419
420#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
421#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
Justin Maggardead80802015-08-05 12:53:08 -0700422#define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
Guenter Roeck483db432012-12-19 22:17:02 +0100423#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
424#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
425#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100426#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
427 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100428#define has_temp_old_peci(data, nr) \
429 (((data)->features & FEAT_TEMP_OLD_PECI) && \
430 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800431#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700432#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
433 FEAT_SIX_FANS))
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800434#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700435#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700436#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Guenter Roeck73055402015-03-26 09:16:32 -0700437#define has_avcc3(data) ((data)->features & FEAT_AVCC3)
Guenter Roeck36c4d982015-03-26 18:18:19 -0700438#define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
Guenter Roeck60878bc2015-03-26 19:57:42 -0700439#define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200441struct it87_sio_data {
442 enum chips type;
443 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200444 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200445 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100446 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200447 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100448 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700449 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100450 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100451 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200452 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700453 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200454};
455
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800456/*
457 * For each registered chip, we need to keep some data in memory.
458 * The structure is dynamically allocated.
459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700461 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100463 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100464 u8 peci_mask;
465 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200467 unsigned short addr;
468 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100469 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 char valid; /* !=0 if following fields are valid */
471 unsigned long last_updated; /* In jiffies */
472
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200473 u16 in_scaled; /* Internal voltage sensors are scaled */
Guenter Roeckd3766842013-03-30 15:47:21 -0700474 u16 in_internal; /* Bitfield, internal sensors (for labels) */
Guenter Roeck52929712013-03-30 14:00:08 -0700475 u16 has_in; /* Bitfield, voltage sensors enabled */
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100476 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200477 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700478 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700479 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck161d8982012-12-19 22:17:01 +0100480 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100481 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
482 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 u8 fan_div[3]; /* Register encoding, shifted right */
Guenter Roeckd3766842013-03-30 15:47:21 -0700484 bool has_vid; /* True if VID supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100486 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 u32 alarms; /* Register encoding, combined */
Guenter Roeck52929712013-03-30 14:00:08 -0700488 bool has_beep; /* true if beep supported */
Jean Delvared9b327c2010-03-05 22:17:17 +0100489 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100491 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100492
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800493 /*
494 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100495 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
496 * 7, and we want to preserve settings on mode changes, so we have
497 * to track all values separately.
498 * Starting with the IT8721F, the manual PWM duty cycles are stored
499 * in separate registers (8-bit values), so the separate tracking
500 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800501 * simple.
502 */
Guenter Roeck5c391262013-03-30 15:02:12 -0700503 u8 has_pwm; /* Bitfield, pwm control enabled */
Guenter Roeck36c4d982015-03-26 18:18:19 -0700504 u8 pwm_ctrl[6]; /* Register value */
505 u8 pwm_duty[6]; /* Manual PWM value set by user */
506 u8 pwm_temp_map[6]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100507
508 /* Automatic fan speed control registers */
509 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
510 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511};
512
Guenter Roeck0531d982012-03-02 11:46:44 -0800513static int adc_lsb(const struct it87_data *data, int nr)
514{
Justin Maggardead80802015-08-05 12:53:08 -0700515 int lsb;
516
517 if (has_12mv_adc(data))
518 lsb = 120;
519 else if (has_10_9mv_adc(data))
520 lsb = 109;
521 else
522 lsb = 160;
Guenter Roeck0531d982012-03-02 11:46:44 -0800523 if (data->in_scaled & (1 << nr))
524 lsb <<= 1;
525 return lsb;
526}
527
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200528static u8 in_to_reg(const struct it87_data *data, int nr, long val)
529{
Justin Maggardead80802015-08-05 12:53:08 -0700530 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800531 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200532}
533
534static int in_from_reg(const struct it87_data *data, int nr, int val)
535{
Justin Maggardead80802015-08-05 12:53:08 -0700536 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200537}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200538
539static inline u8 FAN_TO_REG(long rpm, int div)
540{
541 if (rpm == 0)
542 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800543 rpm = clamp_val(rpm, 1, 1000000);
544 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200545}
546
547static inline u16 FAN16_TO_REG(long rpm)
548{
549 if (rpm == 0)
550 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800551 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200552}
553
554#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
555 1350000 / ((val) * (div)))
556/* The divider is fixed to 2 in 16-bit mode */
557#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
558 1350000 / ((val) * 2))
559
Guenter Roeck2a844c12013-01-09 08:09:34 -0800560#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
561 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200562#define TEMP_FROM_REG(val) ((val) * 1000)
563
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200564static u8 pwm_to_reg(const struct it87_data *data, long val)
565{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100566 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200567 return val;
568 else
569 return val >> 1;
570}
571
572static int pwm_from_reg(const struct it87_data *data, u8 reg)
573{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100574 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200575 return reg;
576 else
577 return (reg & 0x7f) << 1;
578}
579
Jean Delvare0df6454d2010-10-28 20:31:51 +0200580
581static int DIV_TO_REG(int val)
582{
583 int answer = 0;
584 while (answer < 7 && (val >>= 1))
585 answer++;
586 return answer;
587}
588#define DIV_FROM_REG(val) (1 << (val))
589
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700590/*
591 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
592 * depending on the chip type, to calculate the actual PWM frequency.
593 *
594 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
595 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
596 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
597 * sometimes just one. It is unknown if this is a datasheet error or real,
598 * so this is ignored for now.
599 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200600static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700601 48000000,
602 24000000,
603 12000000,
604 8000000,
605 6000000,
606 3000000,
607 1500000,
608 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200609};
610
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700611/*
612 * Must be called with data->update_lock held, except during initialization.
613 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
614 * would slow down the IT87 access and should not be necessary.
615 */
616static int it87_read_value(struct it87_data *data, u8 reg)
617{
618 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
619 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
620}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700622/*
623 * Must be called with data->update_lock held, except during initialization.
624 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
625 * would slow down the IT87 access and should not be necessary.
626 */
627static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
628{
629 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
630 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
631}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700633static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
634{
635 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
636 if (has_newer_autopwm(data)) {
637 data->pwm_temp_map[nr] = (data->pwm_ctrl[nr] & 0x03) +
638 nr < 3 ? 0 : 3;
639 data->pwm_duty[nr] = it87_read_value(data,
640 IT87_REG_PWM_DUTY[nr]);
641 } else {
642 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
643 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
644 else /* Manual mode */
645 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700648 if (has_old_autopwm(data)) {
649 int i;
650
651 for (i = 0; i < 5 ; i++)
652 data->auto_temp[nr][i] = it87_read_value(data,
653 IT87_REG_AUTO_TEMP(nr, i));
654 for (i = 0; i < 3 ; i++)
655 data->auto_pwm[nr][i] = it87_read_value(data,
656 IT87_REG_AUTO_PWM(nr, i));
657 }
658}
659
660static struct it87_data *it87_update_device(struct device *dev)
661{
662 struct it87_data *data = dev_get_drvdata(dev);
663 int i;
664
665 mutex_lock(&data->update_lock);
666
667 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
668 || !data->valid) {
669 if (update_vbat) {
670 /*
671 * Cleared after each update, so reenable. Value
672 * returned by this read will be previous value
673 */
674 it87_write_value(data, IT87_REG_CONFIG,
675 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
676 }
677 for (i = 0; i <= 7; i++) {
678 data->in[i][0] =
679 it87_read_value(data, IT87_REG_VIN(i));
680 data->in[i][1] =
681 it87_read_value(data, IT87_REG_VIN_MIN(i));
682 data->in[i][2] =
683 it87_read_value(data, IT87_REG_VIN_MAX(i));
684 }
685 /* in8 (battery) has no limit registers */
686 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
687 if (has_avcc3(data))
688 data->in[9][0] = it87_read_value(data, IT87_REG_AVCC3);
689
690 for (i = 0; i < 6; i++) {
691 /* Skip disabled fans */
692 if (!(data->has_fan & (1 << i)))
693 continue;
694
695 data->fan[i][1] =
696 it87_read_value(data, IT87_REG_FAN_MIN[i]);
697 data->fan[i][0] = it87_read_value(data,
698 IT87_REG_FAN[i]);
699 /* Add high byte if in 16-bit mode */
700 if (has_16bit_fans(data)) {
701 data->fan[i][0] |= it87_read_value(data,
702 IT87_REG_FANX[i]) << 8;
703 data->fan[i][1] |= it87_read_value(data,
704 IT87_REG_FANX_MIN[i]) << 8;
705 }
706 }
707 for (i = 0; i < 3; i++) {
708 if (!(data->has_temp & (1 << i)))
709 continue;
710 data->temp[i][0] =
711 it87_read_value(data, IT87_REG_TEMP(i));
712 data->temp[i][1] =
713 it87_read_value(data, IT87_REG_TEMP_LOW(i));
714 data->temp[i][2] =
715 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
716 if (has_temp_offset(data))
717 data->temp[i][3] =
718 it87_read_value(data,
719 IT87_REG_TEMP_OFFSET[i]);
720 }
721
722 /* Newer chips don't have clock dividers */
723 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
724 i = it87_read_value(data, IT87_REG_FAN_DIV);
725 data->fan_div[0] = i & 0x07;
726 data->fan_div[1] = (i >> 3) & 0x07;
727 data->fan_div[2] = (i & 0x40) ? 3 : 1;
728 }
729
730 data->alarms =
731 it87_read_value(data, IT87_REG_ALARM1) |
732 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
733 (it87_read_value(data, IT87_REG_ALARM3) << 16);
734 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
735
736 data->fan_main_ctrl = it87_read_value(data,
737 IT87_REG_FAN_MAIN_CTRL);
738 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
739 for (i = 0; i < 6; i++)
740 it87_update_pwm_ctrl(data, i);
741
742 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
743 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
744 /*
745 * The IT8705F does not have VID capability.
746 * The IT8718F and later don't use IT87_REG_VID for the
747 * same purpose.
748 */
749 if (data->type == it8712 || data->type == it8716) {
750 data->vid = it87_read_value(data, IT87_REG_VID);
751 /*
752 * The older IT8712F revisions had only 5 VID pins,
753 * but we assume it is always safe to read 6 bits.
754 */
755 data->vid &= 0x3f;
756 }
757 data->last_updated = jiffies;
758 data->valid = 1;
759 }
760
761 mutex_unlock(&data->update_lock);
762
763 return data;
764}
Jean Delvarefde09502005-07-19 23:51:07 +0200765
Jean Delvare20ad93d2005-06-05 11:53:25 +0200766static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100767 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100769 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
770 int nr = sattr->nr;
771 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100774 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Guenter Roeck929c6a52012-12-19 22:17:00 +0100777static ssize_t set_in(struct device *dev, struct device_attribute *attr,
778 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100780 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
781 int nr = sattr->nr;
782 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200783
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200784 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100785 unsigned long val;
786
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100787 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100788 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100790 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100791 data->in[nr][index] = in_to_reg(data, nr, val);
792 it87_write_value(data,
793 index == 1 ? IT87_REG_VIN_MIN(nr)
794 : IT87_REG_VIN_MAX(nr),
795 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100796 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return count;
798}
799
Guenter Roeck929c6a52012-12-19 22:17:00 +0100800static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
801static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
802 0, 1);
803static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
804 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Guenter Roeck929c6a52012-12-19 22:17:00 +0100806static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
807static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
808 1, 1);
809static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
810 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Guenter Roeck929c6a52012-12-19 22:17:00 +0100812static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
813static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
814 2, 1);
815static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
816 2, 2);
817
818static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
819static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
820 3, 1);
821static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
822 3, 2);
823
824static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
825static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
826 4, 1);
827static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
828 4, 2);
829
830static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
831static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
832 5, 1);
833static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
834 5, 2);
835
836static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
837static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
838 6, 1);
839static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
840 6, 2);
841
842static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
843static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
844 7, 1);
845static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
846 7, 2);
847
848static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100849static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200852static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100853 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100855 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
856 int nr = sattr->nr;
857 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200859
Guenter Roeck60ca3852012-12-19 22:17:00 +0100860 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200862
Guenter Roeck60ca3852012-12-19 22:17:00 +0100863static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
864 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100866 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
867 int nr = sattr->nr;
868 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200869 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100870 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100871 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100872
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100873 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100874 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100876 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100877
878 switch (index) {
879 default:
880 case 1:
881 reg = IT87_REG_TEMP_LOW(nr);
882 break;
883 case 2:
884 reg = IT87_REG_TEMP_HIGH(nr);
885 break;
886 case 3:
887 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
888 if (!(regval & 0x80)) {
889 regval |= 0x80;
890 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
891 }
892 data->valid = 0;
893 reg = IT87_REG_TEMP_OFFSET[nr];
894 break;
895 }
896
Guenter Roeck60ca3852012-12-19 22:17:00 +0100897 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100898 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100899 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return count;
901}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200902
Guenter Roeck60ca3852012-12-19 22:17:00 +0100903static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
904static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
905 0, 1);
906static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
907 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100908static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
909 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100910static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
911static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
912 1, 1);
913static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
914 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100915static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
916 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100917static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
918static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
919 2, 1);
920static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
921 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100922static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
923 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Guenter Roeck2cece012012-12-19 22:17:01 +0100925static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
926 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200928 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
929 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800931 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100932 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100933
Guenter Roeck19529782012-12-19 22:17:02 +0100934 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
935 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100936 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (reg & (1 << nr))
938 return sprintf(buf, "3\n"); /* thermal diode */
939 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200940 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return sprintf(buf, "0\n"); /* disabled */
942}
Guenter Roeck2cece012012-12-19 22:17:01 +0100943
944static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
945 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200947 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
948 int nr = sensor_attr->index;
949
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200950 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100951 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100952 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100953
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100954 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100955 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Jean Delvare8acf07c2010-04-14 16:14:09 +0200957 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
958 reg &= ~(1 << nr);
959 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100960 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
961 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100962 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
963 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
964 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200965 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100966 dev_warn(dev,
967 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200968 val = 4;
969 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100970 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200972 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200973 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200974 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100975 else if (has_temp_peci(data, nr) && val == 6)
976 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100977 else if (has_temp_old_peci(data, nr) && val == 6)
978 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200979 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200981
982 mutex_lock(&data->update_lock);
983 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100984 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200985 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100986 if (has_temp_old_peci(data, nr))
987 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200988 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100989 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return count;
991}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Guenter Roeck2cece012012-12-19 22:17:01 +0100993static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
994 set_temp_type, 0);
995static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
996 set_temp_type, 1);
997static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
998 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +01001001
1002static int pwm_mode(const struct it87_data *data, int nr)
1003{
1004 int ctrl = data->fan_main_ctrl & (1 << nr);
1005
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001006 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +01001007 return 0;
1008 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
1009 return 2;
1010 else /* Manual mode */
1011 return 1;
1012}
1013
Jean Delvare20ad93d2005-06-05 11:53:25 +02001014static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +01001015 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001017 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1018 int nr = sattr->nr;
1019 int index = sattr->index;
1020 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001022
Guenter Roecke1169ba2012-12-19 22:17:01 +01001023 speed = has_16bit_fans(data) ?
1024 FAN16_FROM_REG(data->fan[nr][index]) :
1025 FAN_FROM_REG(data->fan[nr][index],
1026 DIV_FROM_REG(data->fan_div[nr]));
1027 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001029
Jean Delvare20ad93d2005-06-05 11:53:25 +02001030static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1031 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001033 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1034 int nr = sensor_attr->index;
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct it87_data *data = it87_update_device(dev);
1037 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
1038}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001039static ssize_t show_pwm_enable(struct device *dev,
1040 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001042 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1043 int nr = sensor_attr->index;
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +01001046 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001048static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1049 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001051 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1052 int nr = sensor_attr->index;
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001055 return sprintf(buf, "%d\n",
1056 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001058static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1059 char *buf)
1060{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001061 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001062 struct it87_data *data = it87_update_device(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001063 int nr = sensor_attr->index;
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001064 unsigned int freq;
Guenter Roeck60878bc2015-03-26 19:57:42 -07001065 int index;
1066
1067 if (has_pwm_freq2(data) && nr == 1)
1068 index = (data->extra >> 4) & 0x07;
1069 else
1070 index = (data->fan_ctl >> 4) & 0x07;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001071
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001072 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1073
1074 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001075}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001076
1077static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1078 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001080 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1081 int nr = sattr->nr;
1082 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +02001083
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001084 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001085 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +01001086 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001088 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001089 return -EINVAL;
1090
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001091 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +01001092
1093 if (has_16bit_fans(data)) {
1094 data->fan[nr][index] = FAN16_TO_REG(val);
1095 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1096 data->fan[nr][index] & 0xff);
1097 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1098 data->fan[nr][index] >> 8);
1099 } else {
1100 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1101 switch (nr) {
1102 case 0:
1103 data->fan_div[nr] = reg & 0x07;
1104 break;
1105 case 1:
1106 data->fan_div[nr] = (reg >> 3) & 0x07;
1107 break;
1108 case 2:
1109 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1110 break;
1111 }
1112 data->fan[nr][index] =
1113 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1114 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1115 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -08001116 }
1117
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001118 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return count;
1120}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001121
Jean Delvare20ad93d2005-06-05 11:53:25 +02001122static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1123 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001125 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1126 int nr = sensor_attr->index;
1127
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001128 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001129 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001130 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 u8 old;
1132
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001133 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001134 return -EINVAL;
1135
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001136 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001137 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001139 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001140 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 switch (nr) {
1143 case 0:
1144 case 1:
1145 data->fan_div[nr] = DIV_TO_REG(val);
1146 break;
1147 case 2:
1148 if (val < 8)
1149 data->fan_div[nr] = 1;
1150 else
1151 data->fan_div[nr] = 3;
1152 }
1153 val = old & 0x80;
1154 val |= (data->fan_div[0] & 0x07);
1155 val |= (data->fan_div[1] & 0x07) << 3;
1156 if (data->fan_div[2] == 3)
1157 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001158 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001160 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001161 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1162 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001163
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001164 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return count;
1166}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001167
1168/* Returns 0 if OK, -EINVAL otherwise */
1169static int check_trip_points(struct device *dev, int nr)
1170{
1171 const struct it87_data *data = dev_get_drvdata(dev);
1172 int i, err = 0;
1173
1174 if (has_old_autopwm(data)) {
1175 for (i = 0; i < 3; i++) {
1176 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1177 err = -EINVAL;
1178 }
1179 for (i = 0; i < 2; i++) {
1180 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1181 err = -EINVAL;
1182 }
1183 }
1184
1185 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001186 dev_err(dev,
1187 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001188 dev_err(dev, "Adjust the trip points and try again\n");
1189 }
1190 return err;
1191}
1192
Jean Delvare20ad93d2005-06-05 11:53:25 +02001193static ssize_t set_pwm_enable(struct device *dev,
1194 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001196 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1197 int nr = sensor_attr->index;
1198
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001199 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001200 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001202 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001203 return -EINVAL;
1204
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001205 /* Check trip points before switching to automatic mode */
1206 if (val == 2) {
1207 if (check_trip_points(dev, nr) < 0)
1208 return -EINVAL;
1209 }
1210
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001211 /* IT8603E does not have on/off mode */
1212 if (val == 0 && data->type == it8603)
1213 return -EINVAL;
1214
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001215 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if (val == 0) {
1218 int tmp;
1219 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001220 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1221 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /* set on/off mode */
1223 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +01001224 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1225 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +01001226 } else {
1227 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001228 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001229 data->pwm_temp_map[nr] :
1230 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001231 else /* Automatic mode */
1232 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001233 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001234
1235 if (data->type != it8603) {
1236 /* set SmartGuardian mode */
1237 data->fan_main_ctrl |= (1 << nr);
1238 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1239 data->fan_main_ctrl);
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001243 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return count;
1245}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001246static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1247 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001249 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1250 int nr = sensor_attr->index;
1251
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001252 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001253 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001255 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return -EINVAL;
1257
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001258 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001259 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001260 /*
1261 * If we are in automatic mode, the PWM duty cycle register
1262 * is read-only so we can't write the value.
1263 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001264 if (data->pwm_ctrl[nr] & 0x80) {
1265 mutex_unlock(&data->update_lock);
1266 return -EBUSY;
1267 }
1268 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001269 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001270 data->pwm_duty[nr]);
1271 } else {
1272 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001273 /*
1274 * If we are in manual mode, write the duty cycle immediately;
1275 * otherwise, just store it for later use.
1276 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001277 if (!(data->pwm_ctrl[nr] & 0x80)) {
1278 data->pwm_ctrl[nr] = data->pwm_duty[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001279 it87_write_value(data, IT87_REG_PWM[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001280 data->pwm_ctrl[nr]);
1281 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001282 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001283 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return count;
1285}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001286static ssize_t set_pwm_freq(struct device *dev,
1287 struct device_attribute *attr, const char *buf, size_t count)
1288{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001289 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001290 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001291 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001292 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001293 int i;
1294
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001295 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001296 return -EINVAL;
1297
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001298 val = clamp_val(val, 0, 1000000);
1299 val *= has_newer_autopwm(data) ? 256 : 128;
1300
Jean Delvaref8d0c192007-02-14 21:15:02 +01001301 /* Search for the nearest available frequency */
1302 for (i = 0; i < 7; i++) {
1303 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1304 break;
1305 }
1306
1307 mutex_lock(&data->update_lock);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001308 if (nr == 0) {
1309 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1310 data->fan_ctl |= i << 4;
1311 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1312 } else {
1313 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1314 data->extra |= i << 4;
1315 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1316 }
Jean Delvaref8d0c192007-02-14 21:15:02 +01001317 mutex_unlock(&data->update_lock);
1318
1319 return count;
1320}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001321static ssize_t show_pwm_temp_map(struct device *dev,
1322 struct device_attribute *attr, char *buf)
1323{
1324 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1325 int nr = sensor_attr->index;
1326
1327 struct it87_data *data = it87_update_device(dev);
1328 int map;
1329
1330 if (data->pwm_temp_map[nr] < 3)
1331 map = 1 << data->pwm_temp_map[nr];
1332 else
1333 map = 0; /* Should never happen */
1334 return sprintf(buf, "%d\n", map);
1335}
1336static ssize_t set_pwm_temp_map(struct device *dev,
1337 struct device_attribute *attr, const char *buf, size_t count)
1338{
1339 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1340 int nr = sensor_attr->index;
1341
1342 struct it87_data *data = dev_get_drvdata(dev);
1343 long val;
1344 u8 reg;
1345
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001346 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001347 return -EINVAL;
1348
1349 switch (val) {
1350 case (1 << 0):
1351 reg = 0x00;
1352 break;
1353 case (1 << 1):
1354 reg = 0x01;
1355 break;
1356 case (1 << 2):
1357 reg = 0x02;
1358 break;
1359 default:
1360 return -EINVAL;
1361 }
1362
1363 mutex_lock(&data->update_lock);
1364 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001365 /*
1366 * If we are in automatic mode, write the temp mapping immediately;
1367 * otherwise, just store it for later use.
1368 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001369 if (data->pwm_ctrl[nr] & 0x80) {
1370 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001371 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001372 }
1373 mutex_unlock(&data->update_lock);
1374 return count;
1375}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001377static ssize_t show_auto_pwm(struct device *dev,
1378 struct device_attribute *attr, char *buf)
1379{
1380 struct it87_data *data = it87_update_device(dev);
1381 struct sensor_device_attribute_2 *sensor_attr =
1382 to_sensor_dev_attr_2(attr);
1383 int nr = sensor_attr->nr;
1384 int point = sensor_attr->index;
1385
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001386 return sprintf(buf, "%d\n",
1387 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001388}
1389
1390static ssize_t set_auto_pwm(struct device *dev,
1391 struct device_attribute *attr, const char *buf, size_t count)
1392{
1393 struct it87_data *data = dev_get_drvdata(dev);
1394 struct sensor_device_attribute_2 *sensor_attr =
1395 to_sensor_dev_attr_2(attr);
1396 int nr = sensor_attr->nr;
1397 int point = sensor_attr->index;
1398 long val;
1399
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001400 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001401 return -EINVAL;
1402
1403 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001404 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001405 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1406 data->auto_pwm[nr][point]);
1407 mutex_unlock(&data->update_lock);
1408 return count;
1409}
1410
1411static ssize_t show_auto_temp(struct device *dev,
1412 struct device_attribute *attr, char *buf)
1413{
1414 struct it87_data *data = it87_update_device(dev);
1415 struct sensor_device_attribute_2 *sensor_attr =
1416 to_sensor_dev_attr_2(attr);
1417 int nr = sensor_attr->nr;
1418 int point = sensor_attr->index;
1419
1420 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1421}
1422
1423static ssize_t set_auto_temp(struct device *dev,
1424 struct device_attribute *attr, const char *buf, size_t count)
1425{
1426 struct it87_data *data = dev_get_drvdata(dev);
1427 struct sensor_device_attribute_2 *sensor_attr =
1428 to_sensor_dev_attr_2(attr);
1429 int nr = sensor_attr->nr;
1430 int point = sensor_attr->index;
1431 long val;
1432
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001433 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001434 return -EINVAL;
1435
1436 mutex_lock(&data->update_lock);
1437 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1438 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1439 data->auto_temp[nr][point]);
1440 mutex_unlock(&data->update_lock);
1441 return count;
1442}
1443
Guenter Roecke1169ba2012-12-19 22:17:01 +01001444static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1445static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1446 0, 1);
1447static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1448 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Guenter Roecke1169ba2012-12-19 22:17:01 +01001450static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1451static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1452 1, 1);
1453static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1454 set_fan_div, 1);
1455
1456static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1457static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1458 2, 1);
1459static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1460 set_fan_div, 2);
1461
1462static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1463static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1464 3, 1);
1465
1466static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1467static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1468 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001470static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1471static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1472 5, 1);
1473
Guenter Roeckc4458db2012-12-19 22:17:02 +01001474static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1475 show_pwm_enable, set_pwm_enable, 0);
1476static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001477static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1478 set_pwm_freq, 0);
Guenter Roeck5c391262013-03-30 15:02:12 -07001479static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001480 show_pwm_temp_map, set_pwm_temp_map, 0);
1481static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1482 show_auto_pwm, set_auto_pwm, 0, 0);
1483static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1484 show_auto_pwm, set_auto_pwm, 0, 1);
1485static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1486 show_auto_pwm, set_auto_pwm, 0, 2);
1487static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1488 show_auto_pwm, NULL, 0, 3);
1489static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1490 show_auto_temp, set_auto_temp, 0, 1);
1491static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1492 show_auto_temp, set_auto_temp, 0, 0);
1493static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1494 show_auto_temp, set_auto_temp, 0, 2);
1495static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1496 show_auto_temp, set_auto_temp, 0, 3);
1497static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1498 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Guenter Roeckc4458db2012-12-19 22:17:02 +01001500static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1501 show_pwm_enable, set_pwm_enable, 1);
1502static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001503static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
Guenter Roeck5c391262013-03-30 15:02:12 -07001504static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001505 show_pwm_temp_map, set_pwm_temp_map, 1);
1506static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1507 show_auto_pwm, set_auto_pwm, 1, 0);
1508static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1509 show_auto_pwm, set_auto_pwm, 1, 1);
1510static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1511 show_auto_pwm, set_auto_pwm, 1, 2);
1512static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1513 show_auto_pwm, NULL, 1, 3);
1514static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1515 show_auto_temp, set_auto_temp, 1, 1);
1516static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1517 show_auto_temp, set_auto_temp, 1, 0);
1518static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1519 show_auto_temp, set_auto_temp, 1, 2);
1520static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1521 show_auto_temp, set_auto_temp, 1, 3);
1522static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1523 show_auto_temp, set_auto_temp, 1, 4);
1524
1525static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1526 show_pwm_enable, set_pwm_enable, 2);
1527static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001528static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
Guenter Roeck5c391262013-03-30 15:02:12 -07001529static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001530 show_pwm_temp_map, set_pwm_temp_map, 2);
1531static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1532 show_auto_pwm, set_auto_pwm, 2, 0);
1533static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1534 show_auto_pwm, set_auto_pwm, 2, 1);
1535static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1536 show_auto_pwm, set_auto_pwm, 2, 2);
1537static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1538 show_auto_pwm, NULL, 2, 3);
1539static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1540 show_auto_temp, set_auto_temp, 2, 1);
1541static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1542 show_auto_temp, set_auto_temp, 2, 0);
1543static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1544 show_auto_temp, set_auto_temp, 2, 2);
1545static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1546 show_auto_temp, set_auto_temp, 2, 3);
1547static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1548 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Guenter Roeck36c4d982015-03-26 18:18:19 -07001550static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1551 show_pwm_enable, set_pwm_enable, 3);
1552static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001553static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
Guenter Roeck5c391262013-03-30 15:02:12 -07001554static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001555 show_pwm_temp_map, set_pwm_temp_map, 3);
1556
1557static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1558 show_pwm_enable, set_pwm_enable, 4);
1559static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001560static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
Guenter Roeck5c391262013-03-30 15:02:12 -07001561static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001562 show_pwm_temp_map, set_pwm_temp_map, 4);
1563
1564static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1565 show_pwm_enable, set_pwm_enable, 5);
1566static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001567static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
Guenter Roeck5c391262013-03-30 15:02:12 -07001568static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001569 show_pwm_temp_map, set_pwm_temp_map, 5);
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001572static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1573 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001576 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
Jean Delvare1d66c642005-04-18 21:16:59 -07001578static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Jean Delvare0124dd72007-11-25 16:16:41 +01001580static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1581 char *buf)
1582{
1583 int bitnr = to_sensor_dev_attr(attr)->index;
1584 struct it87_data *data = it87_update_device(dev);
1585 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1586}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001587
1588static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1589 *attr, const char *buf, size_t count)
1590{
1591 struct it87_data *data = dev_get_drvdata(dev);
1592 long val;
1593 int config;
1594
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001595 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001596 return -EINVAL;
1597
1598 mutex_lock(&data->update_lock);
1599 config = it87_read_value(data, IT87_REG_CONFIG);
1600 if (config < 0) {
1601 count = config;
1602 } else {
1603 config |= 1 << 5;
1604 it87_write_value(data, IT87_REG_CONFIG, config);
1605 /* Invalidate cache to force re-read */
1606 data->valid = 0;
1607 }
1608 mutex_unlock(&data->update_lock);
1609
1610 return count;
1611}
1612
Jean Delvare0124dd72007-11-25 16:16:41 +01001613static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1614static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1615static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1616static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1617static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1618static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1619static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1620static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1621static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1622static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1623static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1624static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1625static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001626static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001627static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1628static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1629static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001630static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1631 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001632
Jean Delvared9b327c2010-03-05 22:17:17 +01001633static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1634 char *buf)
1635{
1636 int bitnr = to_sensor_dev_attr(attr)->index;
1637 struct it87_data *data = it87_update_device(dev);
1638 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1639}
1640static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1641 const char *buf, size_t count)
1642{
1643 int bitnr = to_sensor_dev_attr(attr)->index;
1644 struct it87_data *data = dev_get_drvdata(dev);
1645 long val;
1646
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001647 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001648 || (val != 0 && val != 1))
1649 return -EINVAL;
1650
1651 mutex_lock(&data->update_lock);
1652 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1653 if (val)
1654 data->beeps |= (1 << bitnr);
1655 else
1656 data->beeps &= ~(1 << bitnr);
1657 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1658 mutex_unlock(&data->update_lock);
1659 return count;
1660}
1661
1662static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1663 show_beep, set_beep, 1);
1664static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1665static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1666static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1667static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1668static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1669static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1670static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1671/* fanX_beep writability is set later */
1672static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1673static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1674static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1675static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1676static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001677static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001678static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1679 show_beep, set_beep, 2);
1680static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1681static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1682
Jean Delvare5f2dc792010-03-05 22:17:18 +01001683static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1684 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Jean Delvare90d66192007-10-08 18:24:35 +02001686 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001687 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001689static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1690 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001692 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001693 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001695 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001696 return -EINVAL;
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 data->vrm = val;
1699
1700 return count;
1701}
1702static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Jean Delvare5f2dc792010-03-05 22:17:18 +01001704static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1705 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 struct it87_data *data = it87_update_device(dev);
1708 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1709}
1710static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001711
Jean Delvare738e5e02010-08-14 21:08:50 +02001712static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1713 char *buf)
1714{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001715 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001716 "+5V",
1717 "5VSB",
1718 "Vbat",
1719 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001720 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001721 "+3.3V",
1722 "3VSB",
1723 "Vbat",
1724 };
1725 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001726 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001727 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001728
Justin Maggardead80802015-08-05 12:53:08 -07001729 if (has_12mv_adc(data) || has_10_9mv_adc(data))
1730 label = labels_it8721[nr];
1731 else
1732 label = labels[nr];
1733
1734 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001735}
1736static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1737static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1738static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001739/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001740static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001741
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001742static ssize_t show_name(struct device *dev, struct device_attribute
1743 *devattr, char *buf)
1744{
1745 struct it87_data *data = dev_get_drvdata(dev);
1746 return sprintf(buf, "%s\n", data->name);
1747}
1748static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1749
Guenter Roeck52929712013-03-30 14:00:08 -07001750static umode_t it87_in_is_visible(struct kobject *kobj,
1751 struct attribute *attr, int index)
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001752{
Guenter Roeck52929712013-03-30 14:00:08 -07001753 struct device *dev = container_of(kobj, struct device, kobj);
1754 struct it87_data *data = dev_get_drvdata(dev);
1755 int i = index / 5; /* voltage index */
1756 int a = index % 5; /* attribute index */
1757
1758 if (index >= 40) { /* in8, in9 only have input attributes */
1759 i = index - 40 + 8;
1760 a = 0;
1761 }
1762
1763 if (!(data->has_in & (1 << i)))
1764 return 0;
1765
1766 if (a == 4 && !data->has_beep)
1767 return 0;
1768
1769 return attr->mode;
1770}
1771
1772static struct attribute *it87_attributes_in[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001773 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001774 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001775 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001776 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001777 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
1778
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001779 &sensor_dev_attr_in1_input.dev_attr.attr,
1780 &sensor_dev_attr_in1_min.dev_attr.attr,
1781 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001782 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001783 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
1784
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001785 &sensor_dev_attr_in2_input.dev_attr.attr,
1786 &sensor_dev_attr_in2_min.dev_attr.attr,
1787 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001788 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001789 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
1790
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001791 &sensor_dev_attr_in3_input.dev_attr.attr,
1792 &sensor_dev_attr_in3_min.dev_attr.attr,
1793 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001794 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001795 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
1796
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001797 &sensor_dev_attr_in4_input.dev_attr.attr,
1798 &sensor_dev_attr_in4_min.dev_attr.attr,
1799 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001800 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001801 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
1802
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001803 &sensor_dev_attr_in5_input.dev_attr.attr,
1804 &sensor_dev_attr_in5_min.dev_attr.attr,
1805 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001806 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001807 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
1808
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001809 &sensor_dev_attr_in6_input.dev_attr.attr,
1810 &sensor_dev_attr_in6_min.dev_attr.attr,
1811 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001812 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001813 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
1814
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001815 &sensor_dev_attr_in7_input.dev_attr.attr,
1816 &sensor_dev_attr_in7_min.dev_attr.attr,
1817 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001818 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001819 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
Jean Delvare87808be2006-09-24 21:17:13 +02001820
Guenter Roeck52929712013-03-30 14:00:08 -07001821 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
1822
1823 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
1824};
1825
1826static const struct attribute_group it87_group_in = {
1827 .attrs = it87_attributes_in,
1828 .is_visible = it87_in_is_visible,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001829};
1830
Guenter Roeck87533772013-03-30 14:23:20 -07001831static umode_t it87_temp_is_visible(struct kobject *kobj,
1832 struct attribute *attr, int index)
Guenter Roeck4573acb2012-03-26 16:17:41 -07001833{
Guenter Roeck87533772013-03-30 14:23:20 -07001834 struct device *dev = container_of(kobj, struct device, kobj);
1835 struct it87_data *data = dev_get_drvdata(dev);
1836 int i = index / 7; /* temperature index */
1837 int a = index % 7; /* attribute index */
1838
1839 if (!(data->has_temp & (1 << i)))
1840 return 0;
1841
1842 if (a == 5 && !has_temp_offset(data))
1843 return 0;
1844
1845 if (a == 6 && !data->has_beep)
1846 return 0;
1847
1848 return attr->mode;
1849}
1850
1851static struct attribute *it87_attributes_temp[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001852 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001853 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001854 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001855 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001856 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001857 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
1858 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
1859
Guenter Roeck4573acb2012-03-26 16:17:41 -07001860 &sensor_dev_attr_temp2_input.dev_attr.attr,
1861 &sensor_dev_attr_temp2_max.dev_attr.attr,
1862 &sensor_dev_attr_temp2_min.dev_attr.attr,
1863 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001864 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001865 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1866 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1867
Guenter Roeck4573acb2012-03-26 16:17:41 -07001868 &sensor_dev_attr_temp3_input.dev_attr.attr,
1869 &sensor_dev_attr_temp3_max.dev_attr.attr,
1870 &sensor_dev_attr_temp3_min.dev_attr.attr,
1871 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001872 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001873 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1874 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001875
Guenter Roeck87533772013-03-30 14:23:20 -07001876 NULL
Guenter Roeck4573acb2012-03-26 16:17:41 -07001877};
1878
Guenter Roeck87533772013-03-30 14:23:20 -07001879static const struct attribute_group it87_group_temp = {
1880 .attrs = it87_attributes_temp,
1881 .is_visible = it87_temp_is_visible,
Guenter Roeck161d8982012-12-19 22:17:01 +01001882};
1883
Guenter Roeckd3766842013-03-30 15:47:21 -07001884static umode_t it87_is_visible(struct kobject *kobj,
1885 struct attribute *attr, int index)
1886{
1887 struct device *dev = container_of(kobj, struct device, kobj);
1888 struct it87_data *data = dev_get_drvdata(dev);
1889
1890 if ((index == 3 || index == 4) && !data->has_vid)
1891 return 0;
1892
1893 if (index > 4 && !(data->in_internal & (1 << (index - 5))))
1894 return 0;
1895
1896 return attr->mode;
1897}
1898
Guenter Roeck4573acb2012-03-26 16:17:41 -07001899static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001900 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001901 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001902 &dev_attr_name.attr,
Guenter Roeckd3766842013-03-30 15:47:21 -07001903 &dev_attr_vrm.attr, /* 3 */
1904 &dev_attr_cpu0_vid.attr, /* 4 */
1905 &sensor_dev_attr_in3_label.dev_attr.attr, /* 5 .. 8 */
1906 &sensor_dev_attr_in7_label.dev_attr.attr,
1907 &sensor_dev_attr_in8_label.dev_attr.attr,
1908 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001909 NULL
1910};
1911
1912static const struct attribute_group it87_group = {
1913 .attrs = it87_attributes,
Guenter Roeckd3766842013-03-30 15:47:21 -07001914 .is_visible = it87_is_visible,
Jean Delvare87808be2006-09-24 21:17:13 +02001915};
1916
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001917static umode_t it87_fan_is_visible(struct kobject *kobj,
1918 struct attribute *attr, int index)
1919{
1920 struct device *dev = container_of(kobj, struct device, kobj);
1921 struct it87_data *data = dev_get_drvdata(dev);
1922 int i = index / 5; /* fan index */
1923 int a = index % 5; /* attribute index */
1924
1925 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
1926 i = (index - 15) / 4 + 3;
1927 a = (index - 15) % 4;
1928 }
1929
1930 if (!(data->has_fan & (1 << i)))
1931 return 0;
1932
1933 if (a == 3) { /* beep */
1934 if (!data->has_beep)
1935 return 0;
1936 /* first fan beep attribute is writable */
1937 if (i == __ffs(data->has_fan))
1938 return attr->mode | S_IWUSR;
1939 }
1940
1941 if (a == 4 && has_16bit_fans(data)) /* divisor */
1942 return 0;
1943
1944 return attr->mode;
1945}
1946
1947static struct attribute *it87_attributes_fan[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001948 &sensor_dev_attr_fan1_input.dev_attr.attr,
1949 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001950 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001951 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
1952 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
1953
Jean Delvare87808be2006-09-24 21:17:13 +02001954 &sensor_dev_attr_fan2_input.dev_attr.attr,
1955 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001956 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001957 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1958 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
1959
Jean Delvare87808be2006-09-24 21:17:13 +02001960 &sensor_dev_attr_fan3_input.dev_attr.attr,
1961 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001962 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001963 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1964 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
1965
1966 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001967 &sensor_dev_attr_fan4_min.dev_attr.attr,
1968 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001969 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1970
1971 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001972 &sensor_dev_attr_fan5_min.dev_attr.attr,
1973 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001974 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1975
1976 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001977 &sensor_dev_attr_fan6_min.dev_attr.attr,
1978 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001979 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001980 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001981};
1982
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001983static const struct attribute_group it87_group_fan = {
1984 .attrs = it87_attributes_fan,
1985 .is_visible = it87_fan_is_visible,
Guenter Roecke1169ba2012-12-19 22:17:01 +01001986};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001987
Guenter Roeck5c391262013-03-30 15:02:12 -07001988static umode_t it87_pwm_is_visible(struct kobject *kobj,
1989 struct attribute *attr, int index)
Guenter Roeck60878bc2015-03-26 19:57:42 -07001990{
1991 struct device *dev = container_of(kobj, struct device, kobj);
1992 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck5c391262013-03-30 15:02:12 -07001993 int i = index / 4; /* pwm index */
1994 int a = index % 4; /* attribute index */
Guenter Roeck60878bc2015-03-26 19:57:42 -07001995
Guenter Roeck5c391262013-03-30 15:02:12 -07001996 if (!(data->has_pwm & (1 << i)))
1997 return 0;
1998
1999 /* pwmX_auto_channels_temp is only writable for old auto pwm */
2000 if (a == 3 && has_old_autopwm(data))
2001 return attr->mode | S_IWUSR;
2002
2003 /* pwm2_freq is writable if there are two pwm frequency selects */
2004 if (has_pwm_freq2(data) && i == 1 && a == 2)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002005 return attr->mode | S_IWUSR;
2006
2007 return attr->mode;
2008}
2009
Guenter Roeck5c391262013-03-30 15:02:12 -07002010static struct attribute *it87_attributes_pwm[] = {
2011 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2012 &sensor_dev_attr_pwm1.dev_attr.attr,
2013 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2014 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2015
2016 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2017 &sensor_dev_attr_pwm2.dev_attr.attr,
2018 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2019 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2020
2021 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2022 &sensor_dev_attr_pwm3.dev_attr.attr,
2023 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2024 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2025
2026 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2027 &sensor_dev_attr_pwm4.dev_attr.attr,
2028 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2029 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2030
2031 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2032 &sensor_dev_attr_pwm5.dev_attr.attr,
2033 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2034 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2035
2036 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2037 &sensor_dev_attr_pwm6.dev_attr.attr,
2038 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2039 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2040
2041 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002042};
2043
Guenter Roeck5c391262013-03-30 15:02:12 -07002044static const struct attribute_group it87_group_pwm = {
2045 .attrs = it87_attributes_pwm,
2046 .is_visible = it87_pwm_is_visible,
2047};
2048
2049static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2050 struct attribute *attr, int index)
2051{
2052 struct device *dev = container_of(kobj, struct device, kobj);
2053 struct it87_data *data = dev_get_drvdata(dev);
2054 int i = index / 9; /* pwm index */
2055
2056 if (!(data->has_pwm & (1 << i)))
2057 return 0;
2058
2059 return attr->mode;
2060}
2061
2062static struct attribute *it87_attributes_auto_pwm[] = {
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002063 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2064 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2065 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2066 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2067 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2068 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2069 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2070 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2071 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002072
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002073 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
2074 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2075 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2076 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2077 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2078 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2079 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2080 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2081 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002082
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002083 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
2084 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2085 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2086 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2087 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2088 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2089 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2090 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2091 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002092
Guenter Roeck5c391262013-03-30 15:02:12 -07002093 NULL,
2094};
2095
2096static const struct attribute_group it87_group_auto_pwm = {
2097 .attrs = it87_attributes_auto_pwm,
2098 .is_visible = it87_auto_pwm_is_visible,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002099};
2100
Jean Delvare2d8672c2005-07-19 23:56:35 +02002101/* SuperIO detection - will change isa_address if a chip is found */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002102static int __init it87_find(int sioaddr, unsigned short *address,
2103 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002105 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002106 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002107 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002108 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002110 err = superio_enter(sioaddr);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002111 if (err)
2112 return err;
2113
2114 err = -ENODEV;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002115 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002116
2117 switch (chip_type) {
2118 case IT8705F_DEVID:
2119 sio_data->type = it87;
2120 break;
2121 case IT8712F_DEVID:
2122 sio_data->type = it8712;
2123 break;
2124 case IT8716F_DEVID:
2125 case IT8726F_DEVID:
2126 sio_data->type = it8716;
2127 break;
2128 case IT8718F_DEVID:
2129 sio_data->type = it8718;
2130 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002131 case IT8720F_DEVID:
2132 sio_data->type = it8720;
2133 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002134 case IT8721F_DEVID:
2135 sio_data->type = it8721;
2136 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01002137 case IT8728F_DEVID:
2138 sio_data->type = it8728;
2139 break;
Justin Maggardead80802015-08-05 12:53:08 -07002140 case IT8732F_DEVID:
2141 sio_data->type = it8732;
2142 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06002143 case IT8771E_DEVID:
2144 sio_data->type = it8771;
2145 break;
2146 case IT8772E_DEVID:
2147 sio_data->type = it8772;
2148 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002149 case IT8781F_DEVID:
2150 sio_data->type = it8781;
2151 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08002152 case IT8782F_DEVID:
2153 sio_data->type = it8782;
2154 break;
2155 case IT8783E_DEVID:
2156 sio_data->type = it8783;
2157 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002158 case IT8786E_DEVID:
2159 sio_data->type = it8786;
2160 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07002161 case IT8790E_DEVID:
2162 sio_data->type = it8790;
2163 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02002164 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02002165 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002166 sio_data->type = it8603;
2167 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002168 case IT8620E_DEVID:
2169 sio_data->type = it8620;
2170 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002171 case 0xffff: /* No device at all */
2172 goto exit;
2173 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01002174 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002175 goto exit;
2176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002178 superio_select(sioaddr, PME);
2179 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002180 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 goto exit;
2182 }
2183
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002184 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002186 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 goto exit;
2188 }
2189
2190 err = 0;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002191 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07002192 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2193 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002194 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002196 config = &it87_devices[sio_data->type];
2197
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002198 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002199 if (has_in7_internal(config))
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002200 sio_data->internal |= (1 << 1);
2201
Jean Delvare738e5e02010-08-14 21:08:50 +02002202 /* in8 (Vbat) is always internal */
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002203 sio_data->internal |= (1 << 2);
2204
Guenter Roeck73055402015-03-26 09:16:32 -07002205 /* in9 (AVCC3), always internal if supported */
2206 if (has_avcc3(config))
2207 sio_data->internal |= (1 << 3); /* in9 is AVCC */
2208 else
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002209 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02002210
Guenter Roeck36c4d982015-03-26 18:18:19 -07002211 if (!has_six_pwm(config))
2212 sio_data->skip_pwm |= (1 << 3) | (1 << 4) | (1 << 5);
2213
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002214 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01002215 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01002216
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002217 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2218 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01002219 /* The IT8705F has a different LD number for GPIO */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002220 superio_select(sioaddr, 5);
2221 sio_data->beep_pin = superio_inb(sioaddr,
2222 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002223 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002224 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002225
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002226 superio_select(sioaddr, GPIO);
Guenter Roeck0531d982012-03-02 11:46:44 -08002227
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002228 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2229 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2230 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2231 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2232 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002233
Guenter Roeck0531d982012-03-02 11:46:44 -08002234 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002235 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08002236 sio_data->skip_fan |= (1 << 2);
2237 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002238 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08002239 sio_data->skip_pwm |= (1 << 2);
2240
2241 /* Check if fan2 is there or not */
2242 if (reg27 & (1 << 7))
2243 sio_data->skip_fan |= (1 << 1);
2244 if (reg27 & (1 << 3))
2245 sio_data->skip_pwm |= (1 << 1);
2246
2247 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002248 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002249 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002250
2251 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002252 if (reg27 & (1 << 1))
2253 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002254
2255 /*
2256 * VIN7
2257 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2258 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002259 if (reg27 & (1 << 2)) {
2260 /*
2261 * The data sheet is a bit unclear regarding the
2262 * internal voltage divider for VCCH5V. It says
2263 * "This bit enables and switches VIN7 (pin 91) to the
2264 * internal voltage divider for VCCH5V".
2265 * This is different to other chips, where the internal
2266 * voltage divider would connect VIN7 to an internal
2267 * voltage source. Maybe that is the case here as well.
2268 *
2269 * Since we don't know for sure, re-route it if that is
2270 * not the case, and ask the user to report if the
2271 * resulting voltage is sane.
2272 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002273 if (!(reg2c & (1 << 1))) {
2274 reg2c |= (1 << 1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002275 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2276 reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002277 pr_notice("Routing internal VCCH5V to in7.\n");
2278 }
2279 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2280 pr_notice("Please report if it displays a reasonable voltage.\n");
2281 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002282
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002283 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08002284 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002285 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08002286 sio_data->internal |= (1 << 1);
2287
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002288 sio_data->beep_pin = superio_inb(sioaddr,
2289 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002290 } else if (sio_data->type == it8603) {
2291 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002292
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002293 superio_select(sioaddr, GPIO);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002294
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002295 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002296
2297 /* Check if fan3 is there or not */
2298 if (reg27 & (1 << 6))
2299 sio_data->skip_pwm |= (1 << 2);
2300 if (reg27 & (1 << 7))
2301 sio_data->skip_fan |= (1 << 2);
2302
2303 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002304 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002305 if (reg29 & (1 << 1))
2306 sio_data->skip_pwm |= (1 << 1);
2307 if (reg29 & (1 << 2))
2308 sio_data->skip_fan |= (1 << 1);
2309
2310 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2311 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2312
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002313 sio_data->beep_pin = superio_inb(sioaddr,
2314 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002315 } else if (sio_data->type == it8620) {
2316 int reg;
2317
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002318 superio_select(sioaddr, GPIO);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002319
Guenter Roeck36c4d982015-03-26 18:18:19 -07002320 /* Check for pwm5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002321 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002322 if (reg & (1 << 6))
2323 sio_data->skip_pwm |= (1 << 4);
2324
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002325 /* Check for fan4, fan5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002326 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002327 if (!(reg & (1 << 5)))
2328 sio_data->skip_fan |= (1 << 3);
2329 if (!(reg & (1 << 4)))
2330 sio_data->skip_fan |= (1 << 4);
2331
2332 /* Check for pwm3, fan3 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002333 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002334 if (reg & (1 << 6))
2335 sio_data->skip_pwm |= (1 << 2);
2336 if (reg & (1 << 7))
2337 sio_data->skip_fan |= (1 << 2);
2338
Guenter Roeck36c4d982015-03-26 18:18:19 -07002339 /* Check for pwm4 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002340 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002341 if (!(reg & (1 << 2)))
2342 sio_data->skip_pwm |= (1 << 3);
2343
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002344 /* Check for pwm2, fan2 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002345 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002346 if (reg & (1 << 1))
2347 sio_data->skip_pwm |= (1 << 1);
2348 if (reg & (1 << 2))
2349 sio_data->skip_fan |= (1 << 1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002350 /* Check for pwm6, fan6 */
2351 if (!(reg & (1 << 7))) {
2352 sio_data->skip_pwm |= (1 << 5);
2353 sio_data->skip_fan |= (1 << 5);
2354 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002355
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002356 sio_data->beep_pin = superio_inb(sioaddr,
2357 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002358 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002359 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002360 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002361
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002362 superio_select(sioaddr, GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002363
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002364 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002365 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002366 /* We need at least 4 VID pins */
2367 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002368 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002369 sio_data->skip_vid = 1;
2370 }
Jean Delvare895ff262009-12-09 20:35:47 +01002371 }
2372
Jean Delvare591ec652009-12-09 20:35:48 +01002373 /* Check if fan3 is there or not */
2374 if (reg & (1 << 6))
2375 sio_data->skip_pwm |= (1 << 2);
2376 if (reg & (1 << 7))
2377 sio_data->skip_fan |= (1 << 2);
2378
2379 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002380 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Jean Delvare591ec652009-12-09 20:35:48 +01002381 if (reg & (1 << 1))
2382 sio_data->skip_pwm |= (1 << 1);
2383 if (reg & (1 << 2))
2384 sio_data->skip_fan |= (1 << 1);
2385
Jean Delvare895ff262009-12-09 20:35:47 +01002386 if ((sio_data->type == it8718 || sio_data->type == it8720)
2387 && !(sio_data->skip_vid))
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002388 sio_data->vid_value = superio_inb(sioaddr,
2389 IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002390
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002391 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002392
2393 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2394
Jean Delvare436cad22010-07-09 16:22:48 +02002395 /*
2396 * The IT8720F has no VIN7 pin, so VCCH should always be
2397 * routed internally to VIN7 with an internal divider.
2398 * Curiously, there still is a configuration bit to control
2399 * this, which means it can be set incorrectly. And even
2400 * more curiously, many boards out there are improperly
2401 * configured, even though the IT8720F datasheet claims
2402 * that the internal routing of VCCH to VIN7 is the default
2403 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002404 *
2405 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002406 * If UART6 is enabled, re-route VIN7 to the internal divider
2407 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002408 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002409 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02002410 reg |= (1 << 1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002411 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002412 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002413 }
Jean Delvare87673dd2006-08-28 14:37:19 +02002414 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02002415 sio_data->internal |= (1 << 0);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002416 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02002417 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002418
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002419 /*
2420 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2421 * While VIN7 can be routed to the internal voltage divider,
2422 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002423 *
2424 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2425 * is the temperature source. Since we can not read the
2426 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002427 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002428 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002429 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002430 sio_data->skip_temp |= (1 << 2);
2431 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002432
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002433 sio_data->beep_pin = superio_inb(sioaddr,
2434 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002435 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002436 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002437 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002438
Jean Delvare98dd22c2008-10-09 15:33:58 +02002439 /* Disable specific features based on DMI strings */
2440 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2441 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2442 if (board_vendor && board_name) {
2443 if (strcmp(board_vendor, "nVIDIA") == 0
2444 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002445 /*
2446 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2447 * connected to a fan, but to something else. One user
2448 * has reported instant system power-off when changing
2449 * the PWM2 duty cycle, so we disable it.
2450 * I use the board name string as the trigger in case
2451 * the same board is ever used in other systems.
2452 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002453 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002454 sio_data->skip_pwm = (1 << 1);
2455 }
2456 }
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458exit:
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002459 superio_exit(sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return err;
2461}
2462
Jean Delvare723a0aa2010-03-05 22:17:16 +01002463static void it87_remove_files(struct device *dev)
2464{
Jean Delvare723a0aa2010-03-05 22:17:16 +01002465 sysfs_remove_group(&dev->kobj, &it87_group);
Guenter Roeck52929712013-03-30 14:00:08 -07002466 sysfs_remove_group(&dev->kobj, &it87_group_in);
Guenter Roeck87533772013-03-30 14:23:20 -07002467 sysfs_remove_group(&dev->kobj, &it87_group_temp);
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002468 sysfs_remove_group(&dev->kobj, &it87_group_fan);
Guenter Roeck5c391262013-03-30 15:02:12 -07002469 sysfs_remove_group(&dev->kobj, &it87_group_pwm);
2470 sysfs_remove_group(&dev->kobj, &it87_group_auto_pwm);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002471}
2472
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002473/* Called when we have found a new IT87. */
2474static void it87_init_device(struct platform_device *pdev)
2475{
2476 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2477 struct it87_data *data = platform_get_drvdata(pdev);
2478 int tmp, i;
2479 u8 mask;
2480
2481 /*
2482 * For each PWM channel:
2483 * - If it is in automatic mode, setting to manual mode should set
2484 * the fan to full speed by default.
2485 * - If it is in manual mode, we need a mapping to temperature
2486 * channels to use when later setting to automatic mode later.
2487 * Use a 1:1 mapping by default (we are clueless.)
2488 * In both cases, the value can (and should) be changed by the user
2489 * prior to switching to a different mode.
2490 * Note that this is no longer needed for the IT8721F and later, as
2491 * these have separate registers for the temperature mapping and the
2492 * manual duty cycle.
2493 */
2494 for (i = 0; i < 3; i++) {
2495 data->pwm_temp_map[i] = i;
2496 data->pwm_duty[i] = 0x7f; /* Full speed */
2497 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2498 }
2499
2500 /*
2501 * Some chips seem to have default value 0xff for all limit
2502 * registers. For low voltage limits it makes no sense and triggers
2503 * alarms, so change to 0 instead. For high temperature limits, it
2504 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2505 * but is still confusing, so change to 127 degrees C.
2506 */
2507 for (i = 0; i < 8; i++) {
2508 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2509 if (tmp == 0xff)
2510 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2511 }
2512 for (i = 0; i < 3; i++) {
2513 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2514 if (tmp == 0xff)
2515 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2516 }
2517
2518 /*
2519 * Temperature channels are not forcibly enabled, as they can be
2520 * set to two different sensor types and we can't guess which one
2521 * is correct for a given system. These channels can be enabled at
2522 * run-time through the temp{1-3}_type sysfs accessors if needed.
2523 */
2524
2525 /* Check if voltage monitors are reset manually or by some reason */
2526 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2527 if ((tmp & 0xff) == 0) {
2528 /* Enable all voltage monitors */
2529 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2530 }
2531
2532 /* Check if tachometers are reset manually or by some reason */
2533 mask = 0x70 & ~(sio_data->skip_fan << 4);
2534 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2535 if ((data->fan_main_ctrl & mask) == 0) {
2536 /* Enable all fan tachometers */
2537 data->fan_main_ctrl |= mask;
2538 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2539 data->fan_main_ctrl);
2540 }
2541 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2542
2543 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2544
2545 /* Set tachometers to 16-bit mode if needed */
2546 if (has_fan16_config(data)) {
2547 if (~tmp & 0x07 & data->has_fan) {
2548 dev_dbg(&pdev->dev,
2549 "Setting fan1-3 to 16-bit mode\n");
2550 it87_write_value(data, IT87_REG_FAN_16BIT,
2551 tmp | 0x07);
2552 }
2553 }
2554
2555 /* Check for additional fans */
2556 if (has_five_fans(data)) {
2557 if (tmp & (1 << 4))
2558 data->has_fan |= (1 << 3); /* fan4 enabled */
2559 if (tmp & (1 << 5))
2560 data->has_fan |= (1 << 4); /* fan5 enabled */
2561 if (has_six_fans(data) && (tmp & (1 << 2)))
2562 data->has_fan |= (1 << 5); /* fan6 enabled */
2563 }
2564
2565 /* Fan input pins may be used for alternative functions */
2566 data->has_fan &= ~sio_data->skip_fan;
2567
2568 /* Check if pwm5, pwm6 are enabled */
2569 if (has_six_pwm(data)) {
2570 /* The following code may be IT8620E specific */
2571 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2572 if ((tmp & 0xc0) == 0xc0)
2573 sio_data->skip_pwm |= (1 << 4);
2574 if (!(tmp & (1 << 3)))
2575 sio_data->skip_pwm |= (1 << 5);
2576 }
2577
2578 /* Start monitoring */
2579 it87_write_value(data, IT87_REG_CONFIG,
2580 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2581 | (update_vbat ? 0x41 : 0x01));
2582}
2583
2584/* Return 1 if and only if the PWM interface is safe to use */
2585static int it87_check_pwm(struct device *dev)
2586{
2587 struct it87_data *data = dev_get_drvdata(dev);
2588 /*
2589 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2590 * and polarity set to active low is sign that this is the case so we
2591 * disable pwm control to protect the user.
2592 */
2593 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2594
2595 if ((tmp & 0x87) == 0) {
2596 if (fix_pwm_polarity) {
2597 /*
2598 * The user asks us to attempt a chip reconfiguration.
2599 * This means switching to active high polarity and
2600 * inverting all fan speed values.
2601 */
2602 int i;
2603 u8 pwm[3];
2604
2605 for (i = 0; i < 3; i++)
2606 pwm[i] = it87_read_value(data,
2607 IT87_REG_PWM[i]);
2608
2609 /*
2610 * If any fan is in automatic pwm mode, the polarity
2611 * might be correct, as suspicious as it seems, so we
2612 * better don't change anything (but still disable the
2613 * PWM interface).
2614 */
2615 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2616 dev_info(dev,
2617 "Reconfiguring PWM to active high polarity\n");
2618 it87_write_value(data, IT87_REG_FAN_CTL,
2619 tmp | 0x87);
2620 for (i = 0; i < 3; i++)
2621 it87_write_value(data,
2622 IT87_REG_PWM[i],
2623 0x7f & ~pwm[i]);
2624 return 1;
2625 }
2626
2627 dev_info(dev,
2628 "PWM configuration is too broken to be fixed\n");
2629 }
2630
2631 dev_info(dev,
2632 "Detected broken BIOS defaults, disabling PWM interface\n");
2633 return 0;
2634 } else if (fix_pwm_polarity) {
2635 dev_info(dev,
2636 "PWM configuration looks sane, won't touch\n");
2637 }
2638
2639 return 1;
2640}
2641
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002642static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002645 struct resource *res;
2646 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002647 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 int enable_pwm_interface;
Guenter Roeckd3766842013-03-30 15:47:21 -07002649 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002651 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002652 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2653 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002654 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2655 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002656 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002657 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Guenter Roeck62a1d052012-03-24 21:54:41 -07002660 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2661 if (!data)
2662 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002664 data->addr = res->start;
2665 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002666 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002667 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002668 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002669 data->name = it87_devices[sio_data->type].name;
2670 /*
2671 * IT8705F Datasheet 0.4.1, 3h == Version G.
2672 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2673 * These are the first revisions with 16-bit tachometer support.
2674 */
2675 switch (data->type) {
2676 case it87:
2677 if (sio_data->revision >= 0x03) {
2678 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002679 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002680 }
2681 break;
2682 case it8712:
2683 if (sio_data->revision >= 0x08) {
2684 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002685 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2686 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002687 }
2688 break;
2689 default:
2690 break;
2691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
2693 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002694 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002695 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2696 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002698 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002700 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002703 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002705 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002706 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002707 if (sio_data->internal & (1 << 0))
2708 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2709 if (sio_data->internal & (1 << 1))
2710 data->in_scaled |= (1 << 7); /* in7 is VSB */
2711 if (sio_data->internal & (1 << 2))
2712 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002713 if (sio_data->internal & (1 << 3))
2714 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002715 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2716 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002717 if (sio_data->internal & (1 << 0))
2718 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2719 if (sio_data->internal & (1 << 1))
2720 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002721 }
2722
Guenter Roeck4573acb2012-03-26 16:17:41 -07002723 data->has_temp = 0x07;
2724 if (sio_data->skip_temp & (1 << 2)) {
2725 if (sio_data->type == it8782
2726 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2727 data->has_temp &= ~(1 << 2);
2728 }
2729
Guenter Roeckd3766842013-03-30 15:47:21 -07002730 data->in_internal = sio_data->internal;
Guenter Roeck52929712013-03-30 14:00:08 -07002731 data->has_in = 0x3ff & ~sio_data->skip_in;
2732
2733 data->has_beep = !!sio_data->beep_pin;
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002736 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Guenter Roeckd3766842013-03-30 15:47:21 -07002738 if (!sio_data->skip_vid) {
2739 data->has_vid = true;
2740 data->vrm = vid_which_vrm();
2741 /* VID reading from Super-I/O config space if available */
2742 data->vid = sio_data->vid_value;
2743 }
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002746 err = sysfs_create_group(&dev->kobj, &it87_group);
2747 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002748 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002749
Guenter Roeck52929712013-03-30 14:00:08 -07002750 err = sysfs_create_group(&dev->kobj, &it87_group_in);
2751 if (err)
2752 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002753
Guenter Roeck87533772013-03-30 14:23:20 -07002754 err = sysfs_create_group(&dev->kobj, &it87_group_temp);
2755 if (err)
2756 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002757
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002758 err = sysfs_create_group(&dev->kobj, &it87_group_fan);
2759 if (err)
2760 goto error;
Jean Delvare17d648b2006-08-28 14:23:46 +02002761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (enable_pwm_interface) {
Guenter Roeck5c391262013-03-30 15:02:12 -07002763 data->has_pwm = (1 << ARRAY_SIZE(IT87_REG_PWM)) - 1;
2764 data->has_pwm &= ~sio_data->skip_pwm;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002765
Guenter Roeck5c391262013-03-30 15:02:12 -07002766 err = sysfs_create_group(&dev->kobj, &it87_group_pwm);
2767 if (err)
2768 goto error;
2769 if (has_old_autopwm(data)) {
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002770 err = sysfs_create_group(&dev->kobj,
Guenter Roeck5c391262013-03-30 15:02:12 -07002771 &it87_group_auto_pwm);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002772 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002773 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
2776
Tony Jones1beeffe2007-08-20 13:46:20 -07002777 data->hwmon_dev = hwmon_device_register(dev);
2778 if (IS_ERR(data->hwmon_dev)) {
2779 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002780 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
2782
2783 return 0;
2784
Guenter Roeck62a1d052012-03-24 21:54:41 -07002785error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002786 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return err;
2788}
2789
Bill Pemberton281dfd02012-11-19 13:25:51 -05002790static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002792 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Tony Jones1beeffe2007-08-20 13:46:20 -07002794 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002795 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return 0;
2798}
2799
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002800static struct platform_driver it87_driver = {
2801 .driver = {
2802 .name = DRVNAME,
2803 },
2804 .probe = it87_probe,
2805 .remove = it87_remove,
2806};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Guenter Roecke84bd952015-03-28 08:24:29 -07002808static int __init it87_device_add(int index, unsigned short address,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002809 const struct it87_sio_data *sio_data)
2810{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002811 struct platform_device *pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002812 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002813 .start = address + IT87_EC_OFFSET,
2814 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002815 .name = DRVNAME,
2816 .flags = IORESOURCE_IO,
2817 };
2818 int err;
2819
Jean Delvareb9acb642009-01-07 16:37:35 +01002820 err = acpi_check_resource_conflict(&res);
2821 if (err)
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002822 return err;
Jean Delvareb9acb642009-01-07 16:37:35 +01002823
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002824 pdev = platform_device_alloc(DRVNAME, address);
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002825 if (!pdev)
2826 return -ENOMEM;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002827
2828 err = platform_device_add_resources(pdev, &res, 1);
2829 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002830 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002831 goto exit_device_put;
2832 }
2833
2834 err = platform_device_add_data(pdev, sio_data,
2835 sizeof(struct it87_sio_data));
2836 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002837 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002838 goto exit_device_put;
2839 }
2840
2841 err = platform_device_add(pdev);
2842 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002843 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002844 goto exit_device_put;
2845 }
2846
Guenter Roecke84bd952015-03-28 08:24:29 -07002847 it87_pdev[index] = pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002848 return 0;
2849
2850exit_device_put:
2851 platform_device_put(pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002852 return err;
2853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855static int __init sm_it87_init(void)
2856{
Guenter Roecke84bd952015-03-28 08:24:29 -07002857 int sioaddr[2] = { REG_2E, REG_4E };
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002858 struct it87_sio_data sio_data;
Guenter Roecke84bd952015-03-28 08:24:29 -07002859 unsigned short isa_address;
2860 bool found = false;
2861 int i, err;
Jean Delvarefde09502005-07-19 23:51:07 +02002862
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002863 err = platform_driver_register(&it87_driver);
2864 if (err)
2865 return err;
2866
Guenter Roecke84bd952015-03-28 08:24:29 -07002867 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
2868 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2869 isa_address = 0;
2870 err = it87_find(sioaddr[i], &isa_address, &sio_data);
2871 if (err || isa_address == 0)
2872 continue;
2873
2874 err = it87_device_add(i, isa_address, &sio_data);
2875 if (err)
2876 goto exit_dev_unregister;
2877 found = true;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002878 }
2879
Guenter Roecke84bd952015-03-28 08:24:29 -07002880 if (!found) {
2881 err = -ENODEV;
2882 goto exit_unregister;
2883 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002884 return 0;
Guenter Roecke84bd952015-03-28 08:24:29 -07002885
2886exit_dev_unregister:
2887 /* NULL check handled by platform_device_unregister */
2888 platform_device_unregister(it87_pdev[0]);
2889exit_unregister:
2890 platform_driver_unregister(&it87_driver);
2891 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
2893
2894static void __exit sm_it87_exit(void)
2895{
Guenter Roecke84bd952015-03-28 08:24:29 -07002896 /* NULL check handled by platform_device_unregister */
2897 platform_device_unregister(it87_pdev[1]);
2898 platform_device_unregister(it87_pdev[0]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002899 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
2902
Jean Delvare7c81c602014-01-29 20:40:08 +01002903MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002904MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905module_param(update_vbat, bool, 0);
2906MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2907module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002908MODULE_PARM_DESC(fix_pwm_polarity,
2909 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910MODULE_LICENSE("GPL");
2911
2912module_init(sm_it87_init);
2913module_exit(sm_it87_exit);