blob: cde53c1109ea47cfefa39662348f551b919930bb [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.
Jean Delvare5f2dc792010-03-05 22:17:18 +010047 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Joe Perchesa8ca1032011-01-12 21:55:10 +010049#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
50
Guenter Roeck48b2ae72015-04-02 08:06:12 -070051#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/module.h>
53#include <linux/init.h>
54#include <linux/slab.h>
55#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020056#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040057#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020058#include <linux/hwmon-sysfs.h>
59#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040060#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010061#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020062#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020063#include <linux/string.h>
64#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010065#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020066#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
corentin.labbeb74f3fd2007-06-13 20:27:36 +020068#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Justin Maggardead80802015-08-05 12:53:08 -070070enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
71 it8771, it8772, it8781, it8782, it8783, it8786, it8790, it8603,
72 it8620 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Jean Delvare67b671b2007-12-06 23:13:42 +010074static unsigned short force_id;
75module_param(force_id, ushort, 0);
76MODULE_PARM_DESC(force_id, "Override the detected device ID");
77
Guenter Roecke84bd952015-03-28 08:24:29 -070078static struct platform_device *it87_pdev[2];
corentin.labbeb74f3fd2007-06-13 20:27:36 +020079
Guenter Roeck3c2e3512015-03-28 08:03:10 -070080#define REG_2E 0x2e /* The register to read/write */
Guenter Roecke84bd952015-03-28 08:24:29 -070081#define REG_4E 0x4e /* Secondary register to read/write */
Guenter Roeck3c2e3512015-03-28 08:03:10 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define DEV 0x07 /* Register: Logical device select */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010085
86/* The device with the IT8718F/IT8720F VID value in it */
87#define GPIO 0x07
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define DEVID 0x20 /* Register: Device ID */
90#define DEVREV 0x22 /* Register: Device Revision */
91
Guenter Roeck3c2e3512015-03-28 08:03:10 -070092static inline int superio_inb(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Guenter Roeck3c2e3512015-03-28 08:03:10 -070094 outb(reg, ioreg);
95 return inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Guenter Roeck3c2e3512015-03-28 08:03:10 -070098static inline void superio_outb(int ioreg, int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +020099{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700100 outb(reg, ioreg);
101 outb(val, ioreg + 1);
Jean Delvare436cad22010-07-09 16:22:48 +0200102}
103
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700104static int superio_inw(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 int val;
Guenter Roeckc9620242015-04-04 09:05:57 -0700107
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700108 outb(reg++, ioreg);
109 val = inb(ioreg + 1) << 8;
110 outb(reg, ioreg);
111 val |= inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return val;
113}
114
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700115static inline void superio_select(int ioreg, int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700117 outb(DEV, ioreg);
118 outb(ldn, ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700121static inline int superio_enter(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200123 /*
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700124 * Try to reserve ioreg and ioreg + 1 for exclusive access.
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200125 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700126 if (!request_muxed_region(ioreg, 2, DRVNAME))
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200127 return -EBUSY;
128
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700129 outb(0x87, ioreg);
130 outb(0x01, ioreg);
131 outb(0x55, ioreg);
Guenter Roecke84bd952015-03-28 08:24:29 -0700132 outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700136static inline void superio_exit(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700138 outb(0x02, ioreg);
139 outb(0x02, ioreg + 1);
140 release_region(ioreg, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Jean Delvare87673dd2006-08-28 14:37:19 +0200143/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define IT8712F_DEVID 0x8712
145#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200146#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200147#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100148#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200149#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400150#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100151#define IT8728F_DEVID 0x8728
Justin Maggardead80802015-08-05 12:53:08 -0700152#define IT8732F_DEVID 0x8732
Guenter Roeckb0636702012-09-07 17:34:41 -0600153#define IT8771E_DEVID 0x8771
154#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800155#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800156#define IT8782F_DEVID 0x8782
157#define IT8783E_DEVID 0x8783
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100158#define IT8786E_DEVID 0x8786
Guenter Roeck4ee07152015-03-25 23:26:28 -0700159#define IT8790E_DEVID 0x8790
Rudolf Marek7183ae82014-04-04 18:01:35 +0200160#define IT8603E_DEVID 0x8603
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800161#define IT8620E_DEVID 0x8620
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200162#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define IT87_ACT_REG 0x30
164#define IT87_BASE_REG 0x60
165
Jean Delvare87673dd2006-08-28 14:37:19 +0200166/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800167#define IT87_SIO_GPIO1_REG 0x25
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800168#define IT87_SIO_GPIO2_REG 0x26
Jean Delvare895ff262009-12-09 20:35:47 +0100169#define IT87_SIO_GPIO3_REG 0x27
Guenter Roeck36c4d982015-03-26 18:18:19 -0700170#define IT87_SIO_GPIO4_REG 0x28
Jean Delvare591ec652009-12-09 20:35:48 +0100171#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800172#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200173#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800174#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200175#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100176#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030179static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030182static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/* Many IT87 constants specified below */
185
186/* Length of ISA address segment */
187#define IT87_EXTENT 8
188
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500189/* Length of ISA address segment for Environmental Controller */
190#define IT87_EC_EXTENT 2
191
192/* Offset of EC registers from ISA base address */
193#define IT87_EC_OFFSET 5
194
195/* Where are the ISA address/data registers relative to the EC base address */
196#define IT87_ADDR_REG_OFFSET 0
197#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199/*----- The IT87 registers -----*/
200
201#define IT87_REG_CONFIG 0x00
202
203#define IT87_REG_ALARM1 0x01
204#define IT87_REG_ALARM2 0x02
205#define IT87_REG_ALARM3 0x03
206
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800207/*
208 * The IT8718F and IT8720F have the VID value in a different register, in
209 * Super-I/O configuration space.
210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800212/*
213 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
214 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
215 * mode.
216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200218#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Guenter Roeckf838aa22015-04-01 20:09:36 -0700220/*
221 * Monitors:
222 * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
223 * - up to 6 temp (1 to 6)
224 * - up to 6 fan (1 to 6)
225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700227static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
228static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
229static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
230static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
231static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#define IT87_REG_FAN_MAIN_CTRL 0x13
234#define IT87_REG_FAN_CTL 0x14
Guenter Roeck36c4d982015-03-26 18:18:19 -0700235static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
236static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Guenter Roeck559313c2015-04-01 10:15:38 -0700238static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
Guenter Roeckf838aa22015-04-01 20:09:36 -0700239 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Guenter Roeck559313c2015-04-01 10:15:38 -0700241#define IT87_REG_TEMP(nr) (0x29 + (nr))
Guenter Roeck73055402015-03-26 09:16:32 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
244#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
245#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
246#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#define IT87_REG_VIN_ENABLE 0x50
249#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700250#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100251#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253#define IT87_REG_CHIPID 0x58
254
Guenter Roeck2cbb9c32015-04-05 11:53:29 -0700255static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
256
257#define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
258#define IT87_REG_AUTO_PWM(nr, i) (IT87_REG_AUTO_BASE[nr] + 5 + (i))
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100259
Guenter Roeckcc18da72015-04-01 10:03:01 -0700260#define IT87_REG_TEMP456_ENABLE 0x77
261
Guenter Roeck23100482015-04-02 08:23:45 -0700262#define NUM_VIN ARRAY_SIZE(IT87_REG_VIN)
263#define NUM_VIN_LIMIT 8
264#define NUM_TEMP 6
265#define NUM_TEMP_OFFSET ARRAY_SIZE(IT87_REG_TEMP_OFFSET)
266#define NUM_TEMP_LIMIT 3
267#define NUM_FAN ARRAY_SIZE(IT87_REG_FAN)
268#define NUM_FAN_DIV 3
269#define NUM_PWM ARRAY_SIZE(IT87_REG_PWM)
270#define NUM_AUTO_PWM ARRAY_SIZE(IT87_REG_PWM)
271
Guenter Roeck483db432012-12-19 22:17:02 +0100272struct it87_devices {
273 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700274 const char * const suffix;
Guenter Roeckcc18da72015-04-01 10:03:01 -0700275 u32 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100276 u8 peci_mask;
277 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100278};
279
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700280#define FEAT_12MV_ADC BIT(0)
281#define FEAT_NEWER_AUTOPWM BIT(1)
282#define FEAT_OLD_AUTOPWM BIT(2)
283#define FEAT_16BIT_FANS BIT(3)
284#define FEAT_TEMP_OFFSET BIT(4)
285#define FEAT_TEMP_PECI BIT(5)
286#define FEAT_TEMP_OLD_PECI BIT(6)
287#define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
288#define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
289#define FEAT_VID BIT(9) /* Set if chip supports VID */
290#define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
291#define FEAT_SIX_FANS BIT(11) /* Supports six fans */
292#define FEAT_10_9MV_ADC BIT(12)
293#define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
294#define FEAT_SIX_PWM BIT(14) /* Chip supports 6 pwm chn */
295#define FEAT_PWM_FREQ2 BIT(15) /* Separate pwm freq 2 */
296#define FEAT_SIX_TEMP BIT(16) /* Up to 6 temp sensors */
Guenter Roeck483db432012-12-19 22:17:02 +0100297
298static const struct it87_devices it87_devices[] = {
299 [it87] = {
300 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700301 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100302 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
303 },
304 [it8712] = {
305 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700306 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800307 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
308 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100309 },
310 [it8716] = {
311 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700312 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800313 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700314 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
Guenter Roeck483db432012-12-19 22:17:02 +0100315 },
316 [it8718] = {
317 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700318 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800319 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700320 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
321 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100322 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100323 },
324 [it8720] = {
325 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700326 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800327 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700328 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
329 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100330 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100331 },
332 [it8721] = {
333 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700334 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100335 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800336 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck60878bc2015-03-26 19:57:42 -0700337 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
338 | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100339 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100340 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100341 },
342 [it8728] = {
343 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700344 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100345 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700346 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700347 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100348 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100349 },
Justin Maggardead80802015-08-05 12:53:08 -0700350 [it8732] = {
351 .name = "it8732",
352 .suffix = "F",
353 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
354 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
355 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
356 .peci_mask = 0x07,
357 .old_peci_mask = 0x02, /* Actually reports PCH */
358 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600359 [it8771] = {
360 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700361 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600362 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700363 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
364 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800365 /* PECI: guesswork */
366 /* 12mV ADC (OHM) */
367 /* 16 bit fans (OHM) */
368 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600369 .peci_mask = 0x07,
370 },
371 [it8772] = {
372 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700373 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600374 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700375 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
376 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800377 /* PECI (coreboot) */
378 /* 12mV ADC (HWSensors4, OHM) */
379 /* 16 bit fans (HWSensors4, OHM) */
380 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600381 .peci_mask = 0x07,
382 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800383 [it8781] = {
384 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700385 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800386 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700387 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800388 .old_peci_mask = 0x4,
389 },
Guenter Roeck483db432012-12-19 22:17:02 +0100390 [it8782] = {
391 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700392 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100393 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700394 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100395 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100396 },
397 [it8783] = {
398 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700399 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100400 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700401 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100402 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100403 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100404 [it8786] = {
405 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700406 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100407 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700408 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
409 | FEAT_PWM_FREQ2,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100410 .peci_mask = 0x07,
411 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700412 [it8790] = {
413 .name = "it8790",
414 .suffix = "E",
415 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700416 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
417 | FEAT_PWM_FREQ2,
Guenter Roeck4ee07152015-03-25 23:26:28 -0700418 .peci_mask = 0x07,
419 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100420 [it8603] = {
421 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700422 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100423 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck73055402015-03-26 09:16:32 -0700424 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
Guenter Roeck60878bc2015-03-26 19:57:42 -0700425 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100426 .peci_mask = 0x07,
427 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800428 [it8620] = {
429 .name = "it8620",
430 .suffix = "E",
431 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700432 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeckcc18da72015-04-01 10:03:01 -0700433 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
434 | FEAT_SIX_TEMP,
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800435 .peci_mask = 0x07,
436 },
Guenter Roeck483db432012-12-19 22:17:02 +0100437};
438
439#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
440#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
Justin Maggardead80802015-08-05 12:53:08 -0700441#define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
Guenter Roeck483db432012-12-19 22:17:02 +0100442#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
443#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
444#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100445#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700446 ((data)->peci_mask & BIT(nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100447#define has_temp_old_peci(data, nr) \
448 (((data)->features & FEAT_TEMP_OLD_PECI) && \
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700449 ((data)->old_peci_mask & BIT(nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800450#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700451#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
452 FEAT_SIX_FANS))
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800453#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700454#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700455#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Guenter Roeck73055402015-03-26 09:16:32 -0700456#define has_avcc3(data) ((data)->features & FEAT_AVCC3)
Guenter Roeck36c4d982015-03-26 18:18:19 -0700457#define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
Guenter Roeck60878bc2015-03-26 19:57:42 -0700458#define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
Guenter Roeckcc18da72015-04-01 10:03:01 -0700459#define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200461struct it87_sio_data {
462 enum chips type;
463 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200464 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200465 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100466 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200467 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100468 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700469 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100470 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100471 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200472 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700473 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200474};
475
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800476/*
477 * For each registered chip, we need to keep some data in memory.
478 * The structure is dynamically allocated.
479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480struct it87_data {
Guenter Roeck8638d0a2015-03-30 11:13:49 -0700481 const struct attribute_group *groups[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100483 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100484 u8 peci_mask;
485 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200487 unsigned short addr;
488 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100489 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 char valid; /* !=0 if following fields are valid */
491 unsigned long last_updated; /* In jiffies */
492
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200493 u16 in_scaled; /* Internal voltage sensors are scaled */
Guenter Roeckd3766842013-03-30 15:47:21 -0700494 u16 in_internal; /* Bitfield, internal sensors (for labels) */
Guenter Roeck52929712013-03-30 14:00:08 -0700495 u16 has_in; /* Bitfield, voltage sensors enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700496 u8 in[NUM_VIN][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200497 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700498 u16 fan[NUM_FAN][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700499 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700500 s8 temp[NUM_TEMP][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100501 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
502 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Guenter Roeck23100482015-04-02 08:23:45 -0700503 u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
Guenter Roeckd3766842013-03-30 15:47:21 -0700504 bool has_vid; /* True if VID supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100506 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 u32 alarms; /* Register encoding, combined */
Guenter Roeck52929712013-03-30 14:00:08 -0700508 bool has_beep; /* true if beep supported */
Jean Delvared9b327c2010-03-05 22:17:17 +0100509 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100511 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100512
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800513 /*
514 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100515 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
516 * 7, and we want to preserve settings on mode changes, so we have
517 * to track all values separately.
518 * Starting with the IT8721F, the manual PWM duty cycles are stored
519 * in separate registers (8-bit values), so the separate tracking
520 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800521 * simple.
522 */
Guenter Roeck5c391262013-03-30 15:02:12 -0700523 u8 has_pwm; /* Bitfield, pwm control enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700524 u8 pwm_ctrl[NUM_PWM]; /* Register value */
525 u8 pwm_duty[NUM_PWM]; /* Manual PWM value set by user */
526 u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100527
528 /* Automatic fan speed control registers */
Guenter Roeck23100482015-04-02 08:23:45 -0700529 u8 auto_pwm[NUM_AUTO_PWM][4]; /* [nr][3] is hard-coded */
530 s8 auto_temp[NUM_AUTO_PWM][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531};
532
Guenter Roeck0531d982012-03-02 11:46:44 -0800533static int adc_lsb(const struct it87_data *data, int nr)
534{
Justin Maggardead80802015-08-05 12:53:08 -0700535 int lsb;
536
537 if (has_12mv_adc(data))
538 lsb = 120;
539 else if (has_10_9mv_adc(data))
540 lsb = 109;
541 else
542 lsb = 160;
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700543 if (data->in_scaled & BIT(nr))
Guenter Roeck0531d982012-03-02 11:46:44 -0800544 lsb <<= 1;
545 return lsb;
546}
547
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200548static u8 in_to_reg(const struct it87_data *data, int nr, long val)
549{
Justin Maggardead80802015-08-05 12:53:08 -0700550 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800551 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200552}
553
554static int in_from_reg(const struct it87_data *data, int nr, int val)
555{
Justin Maggardead80802015-08-05 12:53:08 -0700556 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200557}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200558
559static inline u8 FAN_TO_REG(long rpm, int div)
560{
561 if (rpm == 0)
562 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800563 rpm = clamp_val(rpm, 1, 1000000);
564 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200565}
566
567static inline u16 FAN16_TO_REG(long rpm)
568{
569 if (rpm == 0)
570 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800571 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200572}
573
574#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
575 1350000 / ((val) * (div)))
576/* The divider is fixed to 2 in 16-bit mode */
577#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
578 1350000 / ((val) * 2))
579
Guenter Roeck2a844c12013-01-09 08:09:34 -0800580#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
581 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200582#define TEMP_FROM_REG(val) ((val) * 1000)
583
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200584static u8 pwm_to_reg(const struct it87_data *data, long val)
585{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100586 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200587 return val;
588 else
589 return val >> 1;
590}
591
592static int pwm_from_reg(const struct it87_data *data, u8 reg)
593{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100594 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200595 return reg;
596 else
597 return (reg & 0x7f) << 1;
598}
599
Jean Delvare0df6454d2010-10-28 20:31:51 +0200600static int DIV_TO_REG(int val)
601{
602 int answer = 0;
Guenter Roeckc9620242015-04-04 09:05:57 -0700603
Jean Delvare0df6454d2010-10-28 20:31:51 +0200604 while (answer < 7 && (val >>= 1))
605 answer++;
606 return answer;
607}
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700608
609#define DIV_FROM_REG(val) BIT(val)
Jean Delvare0df6454d2010-10-28 20:31:51 +0200610
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700611/*
612 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
613 * depending on the chip type, to calculate the actual PWM frequency.
614 *
615 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
616 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
617 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
618 * sometimes just one. It is unknown if this is a datasheet error or real,
619 * so this is ignored for now.
620 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200621static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700622 48000000,
623 24000000,
624 12000000,
625 8000000,
626 6000000,
627 3000000,
628 1500000,
629 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200630};
631
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700632/*
633 * Must be called with data->update_lock held, except during initialization.
634 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
635 * would slow down the IT87 access and should not be necessary.
636 */
637static int it87_read_value(struct it87_data *data, u8 reg)
638{
639 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
640 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
641}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700643/*
644 * Must be called with data->update_lock held, except during initialization.
645 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
646 * would slow down the IT87 access and should not be necessary.
647 */
648static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
649{
650 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
651 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700654static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
655{
656 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
657 if (has_newer_autopwm(data)) {
Guenter Roeck0624d862015-04-06 21:04:18 -0700658 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700659 data->pwm_duty[nr] = it87_read_value(data,
660 IT87_REG_PWM_DUTY[nr]);
661 } else {
662 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
663 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
664 else /* Manual mode */
665 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700668 if (has_old_autopwm(data)) {
669 int i;
670
671 for (i = 0; i < 5 ; i++)
672 data->auto_temp[nr][i] = it87_read_value(data,
673 IT87_REG_AUTO_TEMP(nr, i));
674 for (i = 0; i < 3 ; i++)
675 data->auto_pwm[nr][i] = it87_read_value(data,
676 IT87_REG_AUTO_PWM(nr, i));
Guenter Roeck2cbb9c32015-04-05 11:53:29 -0700677 } else if (has_newer_autopwm(data)) {
678 int i;
679
680 /*
681 * 0: temperature hysteresis (base + 5)
682 * 1: fan off temperature (base + 0)
683 * 2: fan start temperature (base + 1)
684 * 3: fan max temperature (base + 2)
685 */
686 data->auto_temp[nr][0] =
687 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 5));
688
689 for (i = 0; i < 3 ; i++)
690 data->auto_temp[nr][i + 1] =
691 it87_read_value(data,
692 IT87_REG_AUTO_TEMP(nr, i));
693 /*
694 * 0: start pwm value (base + 3)
695 * 1: pwm slope (base + 4, 1/8th pwm)
696 */
697 data->auto_pwm[nr][0] =
698 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 3));
699 data->auto_pwm[nr][1] =
700 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 4));
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700701 }
702}
703
704static struct it87_data *it87_update_device(struct device *dev)
705{
706 struct it87_data *data = dev_get_drvdata(dev);
707 int i;
708
709 mutex_lock(&data->update_lock);
710
Guenter Roeckc9620242015-04-04 09:05:57 -0700711 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
712 !data->valid) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700713 if (update_vbat) {
714 /*
715 * Cleared after each update, so reenable. Value
716 * returned by this read will be previous value
717 */
718 it87_write_value(data, IT87_REG_CONFIG,
719 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
720 }
Guenter Roeck23100482015-04-02 08:23:45 -0700721 for (i = 0; i < NUM_VIN; i++) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700722 if (!(data->has_in & BIT(i)))
Guenter Roeck559313c2015-04-01 10:15:38 -0700723 continue;
724
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700725 data->in[i][0] =
Guenter Roeck559313c2015-04-01 10:15:38 -0700726 it87_read_value(data, IT87_REG_VIN[i]);
727
728 /* VBAT and AVCC don't have limit registers */
Guenter Roeck23100482015-04-02 08:23:45 -0700729 if (i >= NUM_VIN_LIMIT)
Guenter Roeck559313c2015-04-01 10:15:38 -0700730 continue;
731
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700732 data->in[i][1] =
733 it87_read_value(data, IT87_REG_VIN_MIN(i));
734 data->in[i][2] =
735 it87_read_value(data, IT87_REG_VIN_MAX(i));
736 }
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700737
Guenter Roeck23100482015-04-02 08:23:45 -0700738 for (i = 0; i < NUM_FAN; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700739 /* Skip disabled fans */
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700740 if (!(data->has_fan & BIT(i)))
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700741 continue;
742
743 data->fan[i][1] =
744 it87_read_value(data, IT87_REG_FAN_MIN[i]);
745 data->fan[i][0] = it87_read_value(data,
746 IT87_REG_FAN[i]);
747 /* Add high byte if in 16-bit mode */
748 if (has_16bit_fans(data)) {
749 data->fan[i][0] |= it87_read_value(data,
750 IT87_REG_FANX[i]) << 8;
751 data->fan[i][1] |= it87_read_value(data,
752 IT87_REG_FANX_MIN[i]) << 8;
753 }
754 }
Guenter Roeck23100482015-04-02 08:23:45 -0700755 for (i = 0; i < NUM_TEMP; i++) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700756 if (!(data->has_temp & BIT(i)))
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700757 continue;
758 data->temp[i][0] =
759 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeckcc18da72015-04-01 10:03:01 -0700760
Guenter Roeck23100482015-04-02 08:23:45 -0700761 if (has_temp_offset(data) && i < NUM_TEMP_OFFSET)
762 data->temp[i][3] =
763 it87_read_value(data,
764 IT87_REG_TEMP_OFFSET[i]);
765
766 if (i >= NUM_TEMP_LIMIT)
Guenter Roeckcc18da72015-04-01 10:03:01 -0700767 continue;
768
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700769 data->temp[i][1] =
770 it87_read_value(data, IT87_REG_TEMP_LOW(i));
771 data->temp[i][2] =
772 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700773 }
774
775 /* Newer chips don't have clock dividers */
776 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
777 i = it87_read_value(data, IT87_REG_FAN_DIV);
778 data->fan_div[0] = i & 0x07;
779 data->fan_div[1] = (i >> 3) & 0x07;
780 data->fan_div[2] = (i & 0x40) ? 3 : 1;
781 }
782
783 data->alarms =
784 it87_read_value(data, IT87_REG_ALARM1) |
785 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
786 (it87_read_value(data, IT87_REG_ALARM3) << 16);
787 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
788
789 data->fan_main_ctrl = it87_read_value(data,
790 IT87_REG_FAN_MAIN_CTRL);
791 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Guenter Roeck0624d862015-04-06 21:04:18 -0700792 for (i = 0; i < NUM_PWM; i++) {
793 if (!(data->has_pwm & BIT(i)))
794 continue;
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700795 it87_update_pwm_ctrl(data, i);
Guenter Roeck0624d862015-04-06 21:04:18 -0700796 }
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700797
798 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
799 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
800 /*
801 * The IT8705F does not have VID capability.
802 * The IT8718F and later don't use IT87_REG_VID for the
803 * same purpose.
804 */
805 if (data->type == it8712 || data->type == it8716) {
806 data->vid = it87_read_value(data, IT87_REG_VID);
807 /*
808 * The older IT8712F revisions had only 5 VID pins,
809 * but we assume it is always safe to read 6 bits.
810 */
811 data->vid &= 0x3f;
812 }
813 data->last_updated = jiffies;
814 data->valid = 1;
815 }
816
817 mutex_unlock(&data->update_lock);
818
819 return data;
820}
Jean Delvarefde09502005-07-19 23:51:07 +0200821
Jean Delvare20ad93d2005-06-05 11:53:25 +0200822static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100823 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100825 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -0700827 int index = sattr->index;
828 int nr = sattr->nr;
829
Guenter Roeck929c6a52012-12-19 22:17:00 +0100830 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Guenter Roeck929c6a52012-12-19 22:17:00 +0100833static ssize_t set_in(struct device *dev, struct device_attribute *attr,
834 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100836 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200837 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -0700838 int index = sattr->index;
839 int nr = sattr->nr;
Jean Delvaref5f64502010-03-05 22:17:19 +0100840 unsigned long val;
841
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100842 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100843 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100845 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100846 data->in[nr][index] = in_to_reg(data, nr, val);
847 it87_write_value(data,
848 index == 1 ? IT87_REG_VIN_MIN(nr)
849 : IT87_REG_VIN_MAX(nr),
850 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100851 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return count;
853}
854
Guenter Roeck929c6a52012-12-19 22:17:00 +0100855static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
856static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
857 0, 1);
858static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
859 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Guenter Roeck929c6a52012-12-19 22:17:00 +0100861static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
862static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
863 1, 1);
864static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
865 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Guenter Roeck929c6a52012-12-19 22:17:00 +0100867static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
868static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
869 2, 1);
870static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
871 2, 2);
872
873static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
874static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
875 3, 1);
876static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
877 3, 2);
878
879static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
880static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
881 4, 1);
882static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
883 4, 2);
884
885static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
886static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
887 5, 1);
888static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
889 5, 2);
890
891static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
892static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
893 6, 1);
894static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
895 6, 2);
896
897static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
898static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
899 7, 1);
900static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
901 7, 2);
902
903static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100904static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Guenter Roeckf838aa22015-04-01 20:09:36 -0700905static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
906static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
907static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Guenter Roeckcc18da72015-04-01 10:03:01 -0700909/* Up to 6 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200910static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100911 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100913 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
914 int nr = sattr->nr;
915 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200917
Guenter Roeck60ca3852012-12-19 22:17:00 +0100918 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200920
Guenter Roeck60ca3852012-12-19 22:17:00 +0100921static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
922 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100924 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
925 int nr = sattr->nr;
926 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200927 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100928 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100929 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100930
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100931 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100932 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100934 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100935
936 switch (index) {
937 default:
938 case 1:
939 reg = IT87_REG_TEMP_LOW(nr);
940 break;
941 case 2:
942 reg = IT87_REG_TEMP_HIGH(nr);
943 break;
944 case 3:
945 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
946 if (!(regval & 0x80)) {
947 regval |= 0x80;
948 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
949 }
950 data->valid = 0;
951 reg = IT87_REG_TEMP_OFFSET[nr];
952 break;
953 }
954
Guenter Roeck60ca3852012-12-19 22:17:00 +0100955 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100956 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100957 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return count;
959}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200960
Guenter Roeck60ca3852012-12-19 22:17:00 +0100961static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
962static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
963 0, 1);
964static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
965 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100966static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
967 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100968static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
969static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
970 1, 1);
971static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
972 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100973static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
974 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100975static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
976static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
977 2, 1);
978static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
979 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100980static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
981 set_temp, 2, 3);
Guenter Roeckcc18da72015-04-01 10:03:01 -0700982static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
983static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
984static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Guenter Roeck2cece012012-12-19 22:17:01 +0100986static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
987 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200989 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
990 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800992 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100993 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100994
Guenter Roeckc9620242015-04-04 09:05:57 -0700995 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1)) ||
996 (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100997 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (reg & (1 << nr))
999 return sprintf(buf, "3\n"); /* thermal diode */
1000 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +02001001 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return sprintf(buf, "0\n"); /* disabled */
1003}
Guenter Roeck2cece012012-12-19 22:17:01 +01001004
1005static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
1006 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001008 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1009 int nr = sensor_attr->index;
1010
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001011 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001012 long val;
Guenter Roeck19529782012-12-19 22:17:02 +01001013 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +01001014
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001015 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001016 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Jean Delvare8acf07c2010-04-14 16:14:09 +02001018 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1019 reg &= ~(1 << nr);
1020 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001021 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1022 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +01001023 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
1024 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1025 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +02001026 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001027 dev_warn(dev,
1028 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +02001029 val = 4;
1030 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001031 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +02001033 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +02001034 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +02001035 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001036 else if (has_temp_peci(data, nr) && val == 6)
1037 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +01001038 else if (has_temp_old_peci(data, nr) && val == 6)
1039 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +02001040 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +02001042
1043 mutex_lock(&data->update_lock);
1044 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +01001045 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001046 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +01001047 if (has_temp_old_peci(data, nr))
1048 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +02001049 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001050 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return count;
1052}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Guenter Roeck2cece012012-12-19 22:17:01 +01001054static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1055 set_temp_type, 0);
1056static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1057 set_temp_type, 1);
1058static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1059 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001061/* 6 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +01001062
1063static int pwm_mode(const struct it87_data *data, int nr)
1064{
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001065 if (data->type != it8603 && nr < 3 && !(data->fan_main_ctrl & BIT(nr)))
1066 return 0; /* Full speed */
1067 if (data->pwm_ctrl[nr] & 0x80)
1068 return 2; /* Automatic mode */
1069 if ((data->type == it8603 || nr >= 3) &&
1070 data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1071 return 0; /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +01001072
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001073 return 1; /* Manual mode */
Jean Delvareb99883d2010-03-05 22:17:15 +01001074}
1075
Jean Delvare20ad93d2005-06-05 11:53:25 +02001076static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +01001077 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001079 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1080 int nr = sattr->nr;
1081 int index = sattr->index;
1082 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001084
Guenter Roecke1169ba2012-12-19 22:17:01 +01001085 speed = has_16bit_fans(data) ?
1086 FAN16_FROM_REG(data->fan[nr][index]) :
1087 FAN_FROM_REG(data->fan[nr][index],
1088 DIV_FROM_REG(data->fan_div[nr]));
1089 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001091
Jean Delvare20ad93d2005-06-05 11:53:25 +02001092static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001093 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001095 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckc9620242015-04-04 09:05:57 -07001096 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001097 int nr = sensor_attr->index;
1098
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001099 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
Guenter Roeckc9620242015-04-04 09:05:57 -07001101
Jean Delvare5f2dc792010-03-05 22:17:18 +01001102static ssize_t show_pwm_enable(struct device *dev,
Guenter Roeckc9620242015-04-04 09:05:57 -07001103 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001105 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckc9620242015-04-04 09:05:57 -07001106 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001107 int nr = sensor_attr->index;
1108
Jean Delvareb99883d2010-03-05 22:17:15 +01001109 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
Guenter Roeckc9620242015-04-04 09:05:57 -07001111
Jean Delvare20ad93d2005-06-05 11:53:25 +02001112static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001113 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001115 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckc9620242015-04-04 09:05:57 -07001116 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001117 int nr = sensor_attr->index;
1118
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001119 return sprintf(buf, "%d\n",
1120 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
Guenter Roeckc9620242015-04-04 09:05:57 -07001122
Jean Delvaref8d0c192007-02-14 21:15:02 +01001123static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001124 char *buf)
Jean Delvaref8d0c192007-02-14 21:15:02 +01001125{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001126 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001127 struct it87_data *data = it87_update_device(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001128 int nr = sensor_attr->index;
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001129 unsigned int freq;
Guenter Roeck60878bc2015-03-26 19:57:42 -07001130 int index;
1131
1132 if (has_pwm_freq2(data) && nr == 1)
1133 index = (data->extra >> 4) & 0x07;
1134 else
1135 index = (data->fan_ctl >> 4) & 0x07;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001136
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001137 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1138
1139 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001140}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001141
1142static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1143 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001145 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1146 int nr = sattr->nr;
1147 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +02001148
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001149 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001150 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +01001151 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001153 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001154 return -EINVAL;
1155
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001156 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +01001157
1158 if (has_16bit_fans(data)) {
1159 data->fan[nr][index] = FAN16_TO_REG(val);
1160 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1161 data->fan[nr][index] & 0xff);
1162 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1163 data->fan[nr][index] >> 8);
1164 } else {
1165 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1166 switch (nr) {
1167 case 0:
1168 data->fan_div[nr] = reg & 0x07;
1169 break;
1170 case 1:
1171 data->fan_div[nr] = (reg >> 3) & 0x07;
1172 break;
1173 case 2:
1174 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1175 break;
1176 }
1177 data->fan[nr][index] =
1178 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1179 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1180 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -08001181 }
1182
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001183 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return count;
1185}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001186
Jean Delvare20ad93d2005-06-05 11:53:25 +02001187static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001188 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001190 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001191 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001192 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001193 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001194 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 u8 old;
1196
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001197 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001198 return -EINVAL;
1199
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001200 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001201 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001203 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001204 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 switch (nr) {
1207 case 0:
1208 case 1:
1209 data->fan_div[nr] = DIV_TO_REG(val);
1210 break;
1211 case 2:
1212 if (val < 8)
1213 data->fan_div[nr] = 1;
1214 else
1215 data->fan_div[nr] = 3;
1216 }
1217 val = old & 0x80;
1218 val |= (data->fan_div[0] & 0x07);
1219 val |= (data->fan_div[1] & 0x07) << 3;
1220 if (data->fan_div[2] == 3)
1221 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001222 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001224 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001225 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1226 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001227
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001228 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return count;
1230}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001231
1232/* Returns 0 if OK, -EINVAL otherwise */
1233static int check_trip_points(struct device *dev, int nr)
1234{
1235 const struct it87_data *data = dev_get_drvdata(dev);
1236 int i, err = 0;
1237
1238 if (has_old_autopwm(data)) {
1239 for (i = 0; i < 3; i++) {
1240 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1241 err = -EINVAL;
1242 }
1243 for (i = 0; i < 2; i++) {
1244 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1245 err = -EINVAL;
1246 }
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001247 } else if (has_newer_autopwm(data)) {
1248 for (i = 1; i < 3; i++) {
1249 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1250 err = -EINVAL;
1251 }
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001252 }
1253
1254 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001255 dev_err(dev,
1256 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001257 dev_err(dev, "Adjust the trip points and try again\n");
1258 }
1259 return err;
1260}
1261
Guenter Roeckc9620242015-04-04 09:05:57 -07001262static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1263 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001265 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001266 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001267 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001268 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001270 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001271 return -EINVAL;
1272
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001273 /* Check trip points before switching to automatic mode */
1274 if (val == 2) {
1275 if (check_trip_points(dev, nr) < 0)
1276 return -EINVAL;
1277 }
1278
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001279 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 if (val == 0) {
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001282 if (nr < 3 && data->type != it8603) {
1283 int tmp;
1284 /* make sure the fan is on when in on/off mode */
1285 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1286 it87_write_value(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1287 /* set on/off mode */
1288 data->fan_main_ctrl &= ~BIT(nr);
1289 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1290 data->fan_main_ctrl);
1291 } else {
1292 /* No on/off mode, set maximum pwm value */
1293 data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1294 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1295 data->pwm_duty[nr]);
1296 /* and set manual mode */
1297 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1298 data->pwm_temp_map[nr] :
1299 data->pwm_duty[nr];
1300 it87_write_value(data, IT87_REG_PWM[nr],
1301 data->pwm_ctrl[nr]);
1302 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001303 } else {
1304 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001305 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001306 data->pwm_temp_map[nr] :
1307 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001308 else /* Automatic mode */
1309 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001310 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001311
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001312 if (data->type != it8603 && nr < 3) {
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001313 /* set SmartGuardian mode */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001314 data->fan_main_ctrl |= BIT(nr);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001315 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1316 data->fan_main_ctrl);
1317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001320 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return count;
1322}
Guenter Roeckc9620242015-04-04 09:05:57 -07001323
Jean Delvare20ad93d2005-06-05 11:53:25 +02001324static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001325 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001327 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001328 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001329 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001330 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001332 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return -EINVAL;
1334
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001335 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001336 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001337 /*
1338 * If we are in automatic mode, the PWM duty cycle register
1339 * is read-only so we can't write the value.
1340 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001341 if (data->pwm_ctrl[nr] & 0x80) {
1342 mutex_unlock(&data->update_lock);
1343 return -EBUSY;
1344 }
1345 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001346 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001347 data->pwm_duty[nr]);
1348 } else {
1349 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001350 /*
1351 * If we are in manual mode, write the duty cycle immediately;
1352 * otherwise, just store it for later use.
1353 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001354 if (!(data->pwm_ctrl[nr] & 0x80)) {
1355 data->pwm_ctrl[nr] = data->pwm_duty[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001356 it87_write_value(data, IT87_REG_PWM[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001357 data->pwm_ctrl[nr]);
1358 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001359 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001360 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return count;
1362}
Guenter Roeckc9620242015-04-04 09:05:57 -07001363
1364static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1365 const char *buf, size_t count)
Jean Delvaref8d0c192007-02-14 21:15:02 +01001366{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001367 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001368 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001369 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001370 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001371 int i;
1372
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001373 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001374 return -EINVAL;
1375
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001376 val = clamp_val(val, 0, 1000000);
1377 val *= has_newer_autopwm(data) ? 256 : 128;
1378
Jean Delvaref8d0c192007-02-14 21:15:02 +01001379 /* Search for the nearest available frequency */
1380 for (i = 0; i < 7; i++) {
Guenter Roeckc9620242015-04-04 09:05:57 -07001381 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
Jean Delvaref8d0c192007-02-14 21:15:02 +01001382 break;
1383 }
1384
1385 mutex_lock(&data->update_lock);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001386 if (nr == 0) {
1387 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1388 data->fan_ctl |= i << 4;
1389 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1390 } else {
1391 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1392 data->extra |= i << 4;
1393 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1394 }
Jean Delvaref8d0c192007-02-14 21:15:02 +01001395 mutex_unlock(&data->update_lock);
1396
1397 return count;
1398}
Guenter Roeckc9620242015-04-04 09:05:57 -07001399
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001400static ssize_t show_pwm_temp_map(struct device *dev,
Guenter Roeckc9620242015-04-04 09:05:57 -07001401 struct device_attribute *attr, char *buf)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001402{
1403 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001404 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001405 int nr = sensor_attr->index;
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001406 int map;
1407
Guenter Roeck0624d862015-04-06 21:04:18 -07001408 map = data->pwm_temp_map[nr];
1409 if (map >= 3)
1410 map = 0; /* Should never happen */
1411 if (nr >= 3) /* pwm channels 3..6 map to temp4..6 */
1412 map += 3;
1413
1414 return sprintf(buf, "%d\n", (int)BIT(map));
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001415}
Guenter Roeckc9620242015-04-04 09:05:57 -07001416
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001417static ssize_t set_pwm_temp_map(struct device *dev,
Guenter Roeckc9620242015-04-04 09:05:57 -07001418 struct device_attribute *attr, const char *buf,
1419 size_t count)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001420{
1421 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001422 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001423 int nr = sensor_attr->index;
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001424 long val;
1425 u8 reg;
1426
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001427 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001428 return -EINVAL;
1429
Guenter Roeck0624d862015-04-06 21:04:18 -07001430 if (nr >= 3)
1431 val -= 3;
1432
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001433 switch (val) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001434 case BIT(0):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001435 reg = 0x00;
1436 break;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001437 case BIT(1):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001438 reg = 0x01;
1439 break;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001440 case BIT(2):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001441 reg = 0x02;
1442 break;
1443 default:
1444 return -EINVAL;
1445 }
1446
1447 mutex_lock(&data->update_lock);
1448 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001449 /*
1450 * If we are in automatic mode, write the temp mapping immediately;
1451 * otherwise, just store it for later use.
1452 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001453 if (data->pwm_ctrl[nr] & 0x80) {
1454 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001455 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001456 }
1457 mutex_unlock(&data->update_lock);
1458 return count;
1459}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Guenter Roeckc9620242015-04-04 09:05:57 -07001461static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1462 char *buf)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001463{
1464 struct it87_data *data = it87_update_device(dev);
1465 struct sensor_device_attribute_2 *sensor_attr =
1466 to_sensor_dev_attr_2(attr);
1467 int nr = sensor_attr->nr;
1468 int point = sensor_attr->index;
1469
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001470 return sprintf(buf, "%d\n",
1471 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001472}
1473
Guenter Roeckc9620242015-04-04 09:05:57 -07001474static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
1475 const char *buf, size_t count)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001476{
1477 struct it87_data *data = dev_get_drvdata(dev);
1478 struct sensor_device_attribute_2 *sensor_attr =
1479 to_sensor_dev_attr_2(attr);
1480 int nr = sensor_attr->nr;
1481 int point = sensor_attr->index;
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001482 int regaddr;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001483 long val;
1484
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001485 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001486 return -EINVAL;
1487
1488 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001489 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001490 if (has_newer_autopwm(data))
1491 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
1492 else
1493 regaddr = IT87_REG_AUTO_PWM(nr, point);
1494 it87_write_value(data, regaddr, data->auto_pwm[nr][point]);
1495 mutex_unlock(&data->update_lock);
1496 return count;
1497}
1498
1499static ssize_t show_auto_pwm_slope(struct device *dev,
1500 struct device_attribute *attr, char *buf)
1501{
1502 struct it87_data *data = it87_update_device(dev);
1503 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1504 int nr = sensor_attr->index;
1505
1506 return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
1507}
1508
1509static ssize_t set_auto_pwm_slope(struct device *dev,
1510 struct device_attribute *attr,
1511 const char *buf, size_t count)
1512{
1513 struct it87_data *data = dev_get_drvdata(dev);
1514 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1515 int nr = sensor_attr->index;
1516 unsigned long val;
1517
1518 if (kstrtoul(buf, 10, &val) < 0 || val > 127)
1519 return -EINVAL;
1520
1521 mutex_lock(&data->update_lock);
1522 data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
1523 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 4),
1524 data->auto_pwm[nr][1]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001525 mutex_unlock(&data->update_lock);
1526 return count;
1527}
1528
Guenter Roeckc9620242015-04-04 09:05:57 -07001529static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
1530 char *buf)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001531{
1532 struct it87_data *data = it87_update_device(dev);
1533 struct sensor_device_attribute_2 *sensor_attr =
1534 to_sensor_dev_attr_2(attr);
1535 int nr = sensor_attr->nr;
1536 int point = sensor_attr->index;
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001537 int reg;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001538
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001539 if (has_old_autopwm(data) || point)
1540 reg = data->auto_temp[nr][point];
1541 else
1542 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
1543
1544 return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001545}
1546
Guenter Roeckc9620242015-04-04 09:05:57 -07001547static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
1548 const char *buf, size_t count)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001549{
1550 struct it87_data *data = dev_get_drvdata(dev);
1551 struct sensor_device_attribute_2 *sensor_attr =
1552 to_sensor_dev_attr_2(attr);
1553 int nr = sensor_attr->nr;
1554 int point = sensor_attr->index;
1555 long val;
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001556 int reg;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001557
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001558 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001559 return -EINVAL;
1560
1561 mutex_lock(&data->update_lock);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001562 if (has_newer_autopwm(data) && !point) {
1563 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
1564 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
1565 data->auto_temp[nr][0] = reg;
1566 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
1567 } else {
1568 reg = TEMP_TO_REG(val);
1569 data->auto_temp[nr][point] = reg;
1570 if (has_newer_autopwm(data))
1571 point--;
1572 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point), reg);
1573 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001574 mutex_unlock(&data->update_lock);
1575 return count;
1576}
1577
Guenter Roecke1169ba2012-12-19 22:17:01 +01001578static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1579static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1580 0, 1);
1581static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1582 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Guenter Roecke1169ba2012-12-19 22:17:01 +01001584static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1585static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1586 1, 1);
1587static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1588 set_fan_div, 1);
1589
1590static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1591static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1592 2, 1);
1593static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1594 set_fan_div, 2);
1595
1596static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1597static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1598 3, 1);
1599
1600static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1601static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1602 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001604static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1605static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1606 5, 1);
1607
Guenter Roeckc4458db2012-12-19 22:17:02 +01001608static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1609 show_pwm_enable, set_pwm_enable, 0);
1610static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001611static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1612 set_pwm_freq, 0);
Guenter Roeck5c391262013-03-30 15:02:12 -07001613static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001614 show_pwm_temp_map, set_pwm_temp_map, 0);
1615static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1616 show_auto_pwm, set_auto_pwm, 0, 0);
1617static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1618 show_auto_pwm, set_auto_pwm, 0, 1);
1619static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1620 show_auto_pwm, set_auto_pwm, 0, 2);
1621static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1622 show_auto_pwm, NULL, 0, 3);
1623static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1624 show_auto_temp, set_auto_temp, 0, 1);
1625static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1626 show_auto_temp, set_auto_temp, 0, 0);
1627static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1628 show_auto_temp, set_auto_temp, 0, 2);
1629static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1630 show_auto_temp, set_auto_temp, 0, 3);
1631static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1632 show_auto_temp, set_auto_temp, 0, 4);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001633static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, S_IRUGO | S_IWUSR,
1634 show_auto_pwm, set_auto_pwm, 0, 0);
1635static SENSOR_DEVICE_ATTR(pwm1_auto_slope, S_IRUGO | S_IWUSR,
1636 show_auto_pwm_slope, set_auto_pwm_slope, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Guenter Roeckc4458db2012-12-19 22:17:02 +01001638static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1639 show_pwm_enable, set_pwm_enable, 1);
1640static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001641static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
Guenter Roeck5c391262013-03-30 15:02:12 -07001642static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001643 show_pwm_temp_map, set_pwm_temp_map, 1);
1644static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1645 show_auto_pwm, set_auto_pwm, 1, 0);
1646static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1647 show_auto_pwm, set_auto_pwm, 1, 1);
1648static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1649 show_auto_pwm, set_auto_pwm, 1, 2);
1650static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1651 show_auto_pwm, NULL, 1, 3);
1652static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1653 show_auto_temp, set_auto_temp, 1, 1);
1654static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1655 show_auto_temp, set_auto_temp, 1, 0);
1656static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1657 show_auto_temp, set_auto_temp, 1, 2);
1658static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1659 show_auto_temp, set_auto_temp, 1, 3);
1660static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1661 show_auto_temp, set_auto_temp, 1, 4);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001662static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, S_IRUGO | S_IWUSR,
1663 show_auto_pwm, set_auto_pwm, 1, 0);
1664static SENSOR_DEVICE_ATTR(pwm2_auto_slope, S_IRUGO | S_IWUSR,
1665 show_auto_pwm_slope, set_auto_pwm_slope, 1);
Guenter Roeckc4458db2012-12-19 22:17:02 +01001666
1667static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1668 show_pwm_enable, set_pwm_enable, 2);
1669static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001670static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
Guenter Roeck5c391262013-03-30 15:02:12 -07001671static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001672 show_pwm_temp_map, set_pwm_temp_map, 2);
1673static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1674 show_auto_pwm, set_auto_pwm, 2, 0);
1675static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1676 show_auto_pwm, set_auto_pwm, 2, 1);
1677static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1678 show_auto_pwm, set_auto_pwm, 2, 2);
1679static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1680 show_auto_pwm, NULL, 2, 3);
1681static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1682 show_auto_temp, set_auto_temp, 2, 1);
1683static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1684 show_auto_temp, set_auto_temp, 2, 0);
1685static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1686 show_auto_temp, set_auto_temp, 2, 2);
1687static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1688 show_auto_temp, set_auto_temp, 2, 3);
1689static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1690 show_auto_temp, set_auto_temp, 2, 4);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001691static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, S_IRUGO | S_IWUSR,
1692 show_auto_pwm, set_auto_pwm, 2, 0);
1693static SENSOR_DEVICE_ATTR(pwm3_auto_slope, S_IRUGO | S_IWUSR,
1694 show_auto_pwm_slope, set_auto_pwm_slope, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Guenter Roeck36c4d982015-03-26 18:18:19 -07001696static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1697 show_pwm_enable, set_pwm_enable, 3);
1698static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001699static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
Guenter Roeck5c391262013-03-30 15:02:12 -07001700static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001701 show_pwm_temp_map, set_pwm_temp_map, 3);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001702static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, S_IRUGO | S_IWUSR,
1703 show_auto_temp, set_auto_temp, 2, 1);
1704static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1705 show_auto_temp, set_auto_temp, 2, 0);
1706static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, S_IRUGO | S_IWUSR,
1707 show_auto_temp, set_auto_temp, 2, 2);
1708static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, S_IRUGO | S_IWUSR,
1709 show_auto_temp, set_auto_temp, 2, 3);
1710static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, S_IRUGO | S_IWUSR,
1711 show_auto_pwm, set_auto_pwm, 3, 0);
1712static SENSOR_DEVICE_ATTR(pwm4_auto_slope, S_IRUGO | S_IWUSR,
1713 show_auto_pwm_slope, set_auto_pwm_slope, 3);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001714
1715static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1716 show_pwm_enable, set_pwm_enable, 4);
1717static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001718static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
Guenter Roeck5c391262013-03-30 15:02:12 -07001719static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001720 show_pwm_temp_map, set_pwm_temp_map, 4);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001721static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, S_IRUGO | S_IWUSR,
1722 show_auto_temp, set_auto_temp, 2, 1);
1723static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1724 show_auto_temp, set_auto_temp, 2, 0);
1725static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, S_IRUGO | S_IWUSR,
1726 show_auto_temp, set_auto_temp, 2, 2);
1727static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, S_IRUGO | S_IWUSR,
1728 show_auto_temp, set_auto_temp, 2, 3);
1729static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, S_IRUGO | S_IWUSR,
1730 show_auto_pwm, set_auto_pwm, 4, 0);
1731static SENSOR_DEVICE_ATTR(pwm5_auto_slope, S_IRUGO | S_IWUSR,
1732 show_auto_pwm_slope, set_auto_pwm_slope, 4);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001733
1734static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1735 show_pwm_enable, set_pwm_enable, 5);
1736static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001737static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
Guenter Roeck5c391262013-03-30 15:02:12 -07001738static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001739 show_pwm_temp_map, set_pwm_temp_map, 5);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001740static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, S_IRUGO | S_IWUSR,
1741 show_auto_temp, set_auto_temp, 2, 1);
1742static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1743 show_auto_temp, set_auto_temp, 2, 0);
1744static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, S_IRUGO | S_IWUSR,
1745 show_auto_temp, set_auto_temp, 2, 2);
1746static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, S_IRUGO | S_IWUSR,
1747 show_auto_temp, set_auto_temp, 2, 3);
1748static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, S_IRUGO | S_IWUSR,
1749 show_auto_pwm, set_auto_pwm, 5, 0);
1750static SENSOR_DEVICE_ATTR(pwm6_auto_slope, S_IRUGO | S_IWUSR,
1751 show_auto_pwm_slope, set_auto_pwm_slope, 5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001754static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001755 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
1757 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001758
Jean Delvare68188ba2005-05-16 18:52:38 +02001759 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
Jean Delvare1d66c642005-04-18 21:16:59 -07001761static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Jean Delvare0124dd72007-11-25 16:16:41 +01001763static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001764 char *buf)
Jean Delvare0124dd72007-11-25 16:16:41 +01001765{
Jean Delvare0124dd72007-11-25 16:16:41 +01001766 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001767 int bitnr = to_sensor_dev_attr(attr)->index;
1768
Jean Delvare0124dd72007-11-25 16:16:41 +01001769 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1770}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001771
Guenter Roeckc9620242015-04-04 09:05:57 -07001772static ssize_t clear_intrusion(struct device *dev,
1773 struct device_attribute *attr, const char *buf,
1774 size_t count)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001775{
1776 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001777 int config;
Guenter Roeckc9620242015-04-04 09:05:57 -07001778 long val;
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001779
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001780 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001781 return -EINVAL;
1782
1783 mutex_lock(&data->update_lock);
1784 config = it87_read_value(data, IT87_REG_CONFIG);
1785 if (config < 0) {
1786 count = config;
1787 } else {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001788 config |= BIT(5);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001789 it87_write_value(data, IT87_REG_CONFIG, config);
1790 /* Invalidate cache to force re-read */
1791 data->valid = 0;
1792 }
1793 mutex_unlock(&data->update_lock);
1794
1795 return count;
1796}
1797
Jean Delvare0124dd72007-11-25 16:16:41 +01001798static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1799static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1800static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1801static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1802static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1803static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1804static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1805static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1806static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1807static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1808static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1809static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1810static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001811static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001812static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1813static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1814static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001815static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1816 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001817
Jean Delvared9b327c2010-03-05 22:17:17 +01001818static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001819 char *buf)
Jean Delvared9b327c2010-03-05 22:17:17 +01001820{
Jean Delvared9b327c2010-03-05 22:17:17 +01001821 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001822 int bitnr = to_sensor_dev_attr(attr)->index;
1823
Jean Delvared9b327c2010-03-05 22:17:17 +01001824 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1825}
Guenter Roeckc9620242015-04-04 09:05:57 -07001826
Jean Delvared9b327c2010-03-05 22:17:17 +01001827static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001828 const char *buf, size_t count)
Jean Delvared9b327c2010-03-05 22:17:17 +01001829{
1830 int bitnr = to_sensor_dev_attr(attr)->index;
1831 struct it87_data *data = dev_get_drvdata(dev);
1832 long val;
1833
Guenter Roeckc9620242015-04-04 09:05:57 -07001834 if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
Jean Delvared9b327c2010-03-05 22:17:17 +01001835 return -EINVAL;
1836
1837 mutex_lock(&data->update_lock);
1838 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1839 if (val)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001840 data->beeps |= BIT(bitnr);
Jean Delvared9b327c2010-03-05 22:17:17 +01001841 else
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001842 data->beeps &= ~BIT(bitnr);
Jean Delvared9b327c2010-03-05 22:17:17 +01001843 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1844 mutex_unlock(&data->update_lock);
1845 return count;
1846}
1847
1848static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1849 show_beep, set_beep, 1);
1850static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1851static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1852static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1853static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1854static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1855static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1856static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1857/* fanX_beep writability is set later */
1858static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1859static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1860static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1861static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1862static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001863static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001864static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1865 show_beep, set_beep, 2);
1866static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1867static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1868
Jean Delvare5f2dc792010-03-05 22:17:18 +01001869static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001870 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Jean Delvare90d66192007-10-08 18:24:35 +02001872 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001873
Jean Delvarea7be58a2005-12-18 16:40:14 +01001874 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
Guenter Roeckc9620242015-04-04 09:05:57 -07001876
Jean Delvare5f2dc792010-03-05 22:17:18 +01001877static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001878 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001880 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001881 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001883 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001884 return -EINVAL;
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 data->vrm = val;
1887
1888 return count;
1889}
1890static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Jean Delvare5f2dc792010-03-05 22:17:18 +01001892static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001893 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001896
1897 return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001900
Jean Delvare738e5e02010-08-14 21:08:50 +02001901static ssize_t show_label(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001902 char *buf)
Jean Delvare738e5e02010-08-14 21:08:50 +02001903{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001904 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001905 "+5V",
1906 "5VSB",
1907 "Vbat",
1908 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001909 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001910 "+3.3V",
1911 "3VSB",
1912 "Vbat",
1913 };
1914 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001915 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001916 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001917
Justin Maggardead80802015-08-05 12:53:08 -07001918 if (has_12mv_adc(data) || has_10_9mv_adc(data))
1919 label = labels_it8721[nr];
1920 else
1921 label = labels[nr];
1922
1923 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001924}
1925static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1926static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1927static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001928/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001929static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001930
Guenter Roeck52929712013-03-30 14:00:08 -07001931static umode_t it87_in_is_visible(struct kobject *kobj,
1932 struct attribute *attr, int index)
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001933{
Guenter Roeck52929712013-03-30 14:00:08 -07001934 struct device *dev = container_of(kobj, struct device, kobj);
1935 struct it87_data *data = dev_get_drvdata(dev);
1936 int i = index / 5; /* voltage index */
1937 int a = index % 5; /* attribute index */
1938
Guenter Roeckf838aa22015-04-01 20:09:36 -07001939 if (index >= 40) { /* in8 and higher only have input attributes */
Guenter Roeck52929712013-03-30 14:00:08 -07001940 i = index - 40 + 8;
1941 a = 0;
1942 }
1943
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001944 if (!(data->has_in & BIT(i)))
Guenter Roeck52929712013-03-30 14:00:08 -07001945 return 0;
1946
1947 if (a == 4 && !data->has_beep)
1948 return 0;
1949
1950 return attr->mode;
1951}
1952
1953static struct attribute *it87_attributes_in[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001954 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001955 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001956 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001957 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001958 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
1959
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001960 &sensor_dev_attr_in1_input.dev_attr.attr,
1961 &sensor_dev_attr_in1_min.dev_attr.attr,
1962 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001963 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001964 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
1965
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001966 &sensor_dev_attr_in2_input.dev_attr.attr,
1967 &sensor_dev_attr_in2_min.dev_attr.attr,
1968 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001969 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001970 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
1971
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001972 &sensor_dev_attr_in3_input.dev_attr.attr,
1973 &sensor_dev_attr_in3_min.dev_attr.attr,
1974 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001975 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001976 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
1977
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001978 &sensor_dev_attr_in4_input.dev_attr.attr,
1979 &sensor_dev_attr_in4_min.dev_attr.attr,
1980 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001981 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001982 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
1983
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001984 &sensor_dev_attr_in5_input.dev_attr.attr,
1985 &sensor_dev_attr_in5_min.dev_attr.attr,
1986 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001987 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001988 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
1989
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001990 &sensor_dev_attr_in6_input.dev_attr.attr,
1991 &sensor_dev_attr_in6_min.dev_attr.attr,
1992 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001993 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001994 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
1995
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001996 &sensor_dev_attr_in7_input.dev_attr.attr,
1997 &sensor_dev_attr_in7_min.dev_attr.attr,
1998 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001999 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07002000 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
Jean Delvare87808be2006-09-24 21:17:13 +02002001
Guenter Roeck52929712013-03-30 14:00:08 -07002002 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
Guenter Roeck52929712013-03-30 14:00:08 -07002003 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
Guenter Roeckf838aa22015-04-01 20:09:36 -07002004 &sensor_dev_attr_in10_input.dev_attr.attr, /* 41 */
2005 &sensor_dev_attr_in11_input.dev_attr.attr, /* 41 */
2006 &sensor_dev_attr_in12_input.dev_attr.attr, /* 41 */
Guenter Roeck52929712013-03-30 14:00:08 -07002007};
2008
2009static const struct attribute_group it87_group_in = {
2010 .attrs = it87_attributes_in,
2011 .is_visible = it87_in_is_visible,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002012};
2013
Guenter Roeck87533772013-03-30 14:23:20 -07002014static umode_t it87_temp_is_visible(struct kobject *kobj,
2015 struct attribute *attr, int index)
Guenter Roeck4573acb2012-03-26 16:17:41 -07002016{
Guenter Roeck87533772013-03-30 14:23:20 -07002017 struct device *dev = container_of(kobj, struct device, kobj);
2018 struct it87_data *data = dev_get_drvdata(dev);
2019 int i = index / 7; /* temperature index */
2020 int a = index % 7; /* attribute index */
2021
Guenter Roeckcc18da72015-04-01 10:03:01 -07002022 if (index >= 21) {
2023 i = index - 21 + 3;
2024 a = 0;
2025 }
2026
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002027 if (!(data->has_temp & BIT(i)))
Guenter Roeck87533772013-03-30 14:23:20 -07002028 return 0;
2029
2030 if (a == 5 && !has_temp_offset(data))
2031 return 0;
2032
2033 if (a == 6 && !data->has_beep)
2034 return 0;
2035
2036 return attr->mode;
2037}
2038
2039static struct attribute *it87_attributes_temp[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02002040 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002041 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002042 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002043 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002044 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07002045 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
2046 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
2047
Guenter Roeckcc18da72015-04-01 10:03:01 -07002048 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002049 &sensor_dev_attr_temp2_max.dev_attr.attr,
2050 &sensor_dev_attr_temp2_min.dev_attr.attr,
2051 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002052 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07002053 &sensor_dev_attr_temp2_offset.dev_attr.attr,
2054 &sensor_dev_attr_temp2_beep.dev_attr.attr,
2055
Guenter Roeckcc18da72015-04-01 10:03:01 -07002056 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002057 &sensor_dev_attr_temp3_max.dev_attr.attr,
2058 &sensor_dev_attr_temp3_min.dev_attr.attr,
2059 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002060 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07002061 &sensor_dev_attr_temp3_offset.dev_attr.attr,
2062 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002063
Guenter Roeckcc18da72015-04-01 10:03:01 -07002064 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
2065 &sensor_dev_attr_temp5_input.dev_attr.attr,
2066 &sensor_dev_attr_temp6_input.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07002067 NULL
Guenter Roeck4573acb2012-03-26 16:17:41 -07002068};
2069
Guenter Roeck87533772013-03-30 14:23:20 -07002070static const struct attribute_group it87_group_temp = {
2071 .attrs = it87_attributes_temp,
2072 .is_visible = it87_temp_is_visible,
Guenter Roeck161d8982012-12-19 22:17:01 +01002073};
2074
Guenter Roeckd3766842013-03-30 15:47:21 -07002075static umode_t it87_is_visible(struct kobject *kobj,
2076 struct attribute *attr, int index)
2077{
2078 struct device *dev = container_of(kobj, struct device, kobj);
2079 struct it87_data *data = dev_get_drvdata(dev);
2080
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002081 if ((index == 2 || index == 3) && !data->has_vid)
Guenter Roeckd3766842013-03-30 15:47:21 -07002082 return 0;
2083
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002084 if (index > 3 && !(data->in_internal & BIT(index - 4)))
Guenter Roeckd3766842013-03-30 15:47:21 -07002085 return 0;
2086
2087 return attr->mode;
2088}
2089
Guenter Roeck4573acb2012-03-26 16:17:41 -07002090static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02002091 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02002092 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002093 &dev_attr_vrm.attr, /* 2 */
2094 &dev_attr_cpu0_vid.attr, /* 3 */
2095 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
Guenter Roeckd3766842013-03-30 15:47:21 -07002096 &sensor_dev_attr_in7_label.dev_attr.attr,
2097 &sensor_dev_attr_in8_label.dev_attr.attr,
2098 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002099 NULL
2100};
2101
2102static const struct attribute_group it87_group = {
2103 .attrs = it87_attributes,
Guenter Roeckd3766842013-03-30 15:47:21 -07002104 .is_visible = it87_is_visible,
Jean Delvare87808be2006-09-24 21:17:13 +02002105};
2106
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002107static umode_t it87_fan_is_visible(struct kobject *kobj,
2108 struct attribute *attr, int index)
2109{
2110 struct device *dev = container_of(kobj, struct device, kobj);
2111 struct it87_data *data = dev_get_drvdata(dev);
2112 int i = index / 5; /* fan index */
2113 int a = index % 5; /* attribute index */
2114
2115 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
2116 i = (index - 15) / 4 + 3;
2117 a = (index - 15) % 4;
2118 }
2119
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002120 if (!(data->has_fan & BIT(i)))
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002121 return 0;
2122
2123 if (a == 3) { /* beep */
2124 if (!data->has_beep)
2125 return 0;
2126 /* first fan beep attribute is writable */
2127 if (i == __ffs(data->has_fan))
2128 return attr->mode | S_IWUSR;
2129 }
2130
2131 if (a == 4 && has_16bit_fans(data)) /* divisor */
2132 return 0;
2133
2134 return attr->mode;
2135}
2136
2137static struct attribute *it87_attributes_fan[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02002138 &sensor_dev_attr_fan1_input.dev_attr.attr,
2139 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01002140 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002141 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
2142 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
2143
Jean Delvare87808be2006-09-24 21:17:13 +02002144 &sensor_dev_attr_fan2_input.dev_attr.attr,
2145 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01002146 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002147 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2148 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
2149
Jean Delvare87808be2006-09-24 21:17:13 +02002150 &sensor_dev_attr_fan3_input.dev_attr.attr,
2151 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002152 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002153 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2154 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
2155
2156 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01002157 &sensor_dev_attr_fan4_min.dev_attr.attr,
2158 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002159 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2160
2161 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01002162 &sensor_dev_attr_fan5_min.dev_attr.attr,
2163 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002164 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2165
2166 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002167 &sensor_dev_attr_fan6_min.dev_attr.attr,
2168 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002169 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002170 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002171};
2172
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002173static const struct attribute_group it87_group_fan = {
2174 .attrs = it87_attributes_fan,
2175 .is_visible = it87_fan_is_visible,
Guenter Roecke1169ba2012-12-19 22:17:01 +01002176};
Jean Delvare723a0aa2010-03-05 22:17:16 +01002177
Guenter Roeck5c391262013-03-30 15:02:12 -07002178static umode_t it87_pwm_is_visible(struct kobject *kobj,
2179 struct attribute *attr, int index)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002180{
2181 struct device *dev = container_of(kobj, struct device, kobj);
2182 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck5c391262013-03-30 15:02:12 -07002183 int i = index / 4; /* pwm index */
2184 int a = index % 4; /* attribute index */
Guenter Roeck60878bc2015-03-26 19:57:42 -07002185
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002186 if (!(data->has_pwm & BIT(i)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002187 return 0;
2188
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002189 /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2190 if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002191 return attr->mode | S_IWUSR;
2192
2193 /* pwm2_freq is writable if there are two pwm frequency selects */
2194 if (has_pwm_freq2(data) && i == 1 && a == 2)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002195 return attr->mode | S_IWUSR;
2196
2197 return attr->mode;
2198}
2199
Guenter Roeck5c391262013-03-30 15:02:12 -07002200static struct attribute *it87_attributes_pwm[] = {
2201 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2202 &sensor_dev_attr_pwm1.dev_attr.attr,
2203 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2204 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2205
2206 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2207 &sensor_dev_attr_pwm2.dev_attr.attr,
2208 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2209 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2210
2211 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2212 &sensor_dev_attr_pwm3.dev_attr.attr,
2213 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2214 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2215
2216 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2217 &sensor_dev_attr_pwm4.dev_attr.attr,
2218 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2219 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2220
2221 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2222 &sensor_dev_attr_pwm5.dev_attr.attr,
2223 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2224 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2225
2226 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2227 &sensor_dev_attr_pwm6.dev_attr.attr,
2228 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2229 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2230
2231 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002232};
2233
Guenter Roeck5c391262013-03-30 15:02:12 -07002234static const struct attribute_group it87_group_pwm = {
2235 .attrs = it87_attributes_pwm,
2236 .is_visible = it87_pwm_is_visible,
2237};
2238
2239static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2240 struct attribute *attr, int index)
2241{
2242 struct device *dev = container_of(kobj, struct device, kobj);
2243 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002244 int i = index / 11; /* pwm index */
2245 int a = index % 11; /* attribute index */
2246
2247 if (index >= 33) { /* pwm 4..6 */
2248 i = (index - 33) / 6 + 3;
2249 a = (index - 33) % 6 + 4;
2250 }
Guenter Roeck5c391262013-03-30 15:02:12 -07002251
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002252 if (!(data->has_pwm & BIT(i)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002253 return 0;
2254
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002255 if (has_newer_autopwm(data)) {
2256 if (a < 4) /* no auto point pwm */
2257 return 0;
2258 if (a == 8) /* no auto_point4 */
2259 return 0;
2260 }
2261 if (has_old_autopwm(data)) {
2262 if (a >= 9) /* no pwm_auto_start, pwm_auto_slope */
2263 return 0;
2264 }
2265
Guenter Roeck5c391262013-03-30 15:02:12 -07002266 return attr->mode;
2267}
2268
2269static struct attribute *it87_attributes_auto_pwm[] = {
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002270 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2271 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2272 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2273 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2274 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2275 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2276 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2277 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2278 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002279 &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2280 &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002281
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002282 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, /* 11 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002283 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2284 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2285 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2286 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2287 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2288 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2289 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2290 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002291 &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2292 &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002293
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002294 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, /* 22 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002295 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2296 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2297 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2298 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2299 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2300 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2301 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2302 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002303 &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2304 &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2305
2306 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, /* 33 */
2307 &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2308 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2309 &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2310 &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2311 &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2312
2313 &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2314 &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2315 &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2316 &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2317 &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2318 &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2319
2320 &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2321 &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2322 &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2323 &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2324 &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2325 &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002326
Guenter Roeck5c391262013-03-30 15:02:12 -07002327 NULL,
2328};
2329
2330static const struct attribute_group it87_group_auto_pwm = {
2331 .attrs = it87_attributes_auto_pwm,
2332 .is_visible = it87_auto_pwm_is_visible,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002333};
2334
Jean Delvare2d8672c2005-07-19 23:56:35 +02002335/* SuperIO detection - will change isa_address if a chip is found */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002336static int __init it87_find(int sioaddr, unsigned short *address,
2337 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002339 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002340 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002341 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002342 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002344 err = superio_enter(sioaddr);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002345 if (err)
2346 return err;
2347
2348 err = -ENODEV;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002349 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002350
2351 switch (chip_type) {
2352 case IT8705F_DEVID:
2353 sio_data->type = it87;
2354 break;
2355 case IT8712F_DEVID:
2356 sio_data->type = it8712;
2357 break;
2358 case IT8716F_DEVID:
2359 case IT8726F_DEVID:
2360 sio_data->type = it8716;
2361 break;
2362 case IT8718F_DEVID:
2363 sio_data->type = it8718;
2364 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002365 case IT8720F_DEVID:
2366 sio_data->type = it8720;
2367 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002368 case IT8721F_DEVID:
2369 sio_data->type = it8721;
2370 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01002371 case IT8728F_DEVID:
2372 sio_data->type = it8728;
2373 break;
Justin Maggardead80802015-08-05 12:53:08 -07002374 case IT8732F_DEVID:
2375 sio_data->type = it8732;
2376 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06002377 case IT8771E_DEVID:
2378 sio_data->type = it8771;
2379 break;
2380 case IT8772E_DEVID:
2381 sio_data->type = it8772;
2382 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002383 case IT8781F_DEVID:
2384 sio_data->type = it8781;
2385 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08002386 case IT8782F_DEVID:
2387 sio_data->type = it8782;
2388 break;
2389 case IT8783E_DEVID:
2390 sio_data->type = it8783;
2391 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002392 case IT8786E_DEVID:
2393 sio_data->type = it8786;
2394 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07002395 case IT8790E_DEVID:
2396 sio_data->type = it8790;
2397 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02002398 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02002399 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002400 sio_data->type = it8603;
2401 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002402 case IT8620E_DEVID:
2403 sio_data->type = it8620;
2404 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002405 case 0xffff: /* No device at all */
2406 goto exit;
2407 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01002408 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002409 goto exit;
2410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002412 superio_select(sioaddr, PME);
2413 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002414 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 goto exit;
2416 }
2417
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002418 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002420 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 goto exit;
2422 }
2423
2424 err = 0;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002425 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07002426 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2427 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002428 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002430 config = &it87_devices[sio_data->type];
2431
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002432 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002433 if (has_in7_internal(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002434 sio_data->internal |= BIT(1);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002435
Jean Delvare738e5e02010-08-14 21:08:50 +02002436 /* in8 (Vbat) is always internal */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002437 sio_data->internal |= BIT(2);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002438
Guenter Roeck73055402015-03-26 09:16:32 -07002439 /* in9 (AVCC3), always internal if supported */
2440 if (has_avcc3(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002441 sio_data->internal |= BIT(3); /* in9 is AVCC */
Guenter Roeck73055402015-03-26 09:16:32 -07002442 else
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002443 sio_data->skip_in |= BIT(9);
Jean Delvare738e5e02010-08-14 21:08:50 +02002444
Guenter Roeck36c4d982015-03-26 18:18:19 -07002445 if (!has_six_pwm(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002446 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002447
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002448 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01002449 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01002450
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002451 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2452 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01002453 /* The IT8705F has a different LD number for GPIO */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002454 superio_select(sioaddr, 5);
2455 sio_data->beep_pin = superio_inb(sioaddr,
2456 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002457 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002458 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002459
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002460 superio_select(sioaddr, GPIO);
Guenter Roeck0531d982012-03-02 11:46:44 -08002461
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002462 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2463 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2464 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2465 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2466 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002467
Guenter Roeck0531d982012-03-02 11:46:44 -08002468 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002469 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
2470 sio_data->skip_fan |= BIT(2);
Guenter Roeckc9620242015-04-04 09:05:57 -07002471 if ((reg25 & BIT(4)) ||
2472 (!(reg2a & BIT(1)) && (regef & BIT(0))))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002473 sio_data->skip_pwm |= BIT(2);
Guenter Roeck0531d982012-03-02 11:46:44 -08002474
2475 /* Check if fan2 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002476 if (reg27 & BIT(7))
2477 sio_data->skip_fan |= BIT(1);
2478 if (reg27 & BIT(3))
2479 sio_data->skip_pwm |= BIT(1);
Guenter Roeck0531d982012-03-02 11:46:44 -08002480
2481 /* VIN5 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002482 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
2483 sio_data->skip_in |= BIT(5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002484
2485 /* VIN6 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002486 if (reg27 & BIT(1))
2487 sio_data->skip_in |= BIT(6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002488
2489 /*
2490 * VIN7
2491 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2492 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002493 if (reg27 & BIT(2)) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002494 /*
2495 * The data sheet is a bit unclear regarding the
2496 * internal voltage divider for VCCH5V. It says
2497 * "This bit enables and switches VIN7 (pin 91) to the
2498 * internal voltage divider for VCCH5V".
2499 * This is different to other chips, where the internal
2500 * voltage divider would connect VIN7 to an internal
2501 * voltage source. Maybe that is the case here as well.
2502 *
2503 * Since we don't know for sure, re-route it if that is
2504 * not the case, and ask the user to report if the
2505 * resulting voltage is sane.
2506 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002507 if (!(reg2c & BIT(1))) {
2508 reg2c |= BIT(1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002509 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2510 reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002511 pr_notice("Routing internal VCCH5V to in7.\n");
2512 }
2513 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2514 pr_notice("Please report if it displays a reasonable voltage.\n");
2515 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002516
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002517 if (reg2c & BIT(0))
2518 sio_data->internal |= BIT(0);
2519 if (reg2c & BIT(1))
2520 sio_data->internal |= BIT(1);
Guenter Roeck0531d982012-03-02 11:46:44 -08002521
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002522 sio_data->beep_pin = superio_inb(sioaddr,
2523 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002524 } else if (sio_data->type == it8603) {
2525 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002526
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002527 superio_select(sioaddr, GPIO);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002528
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002529 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002530
2531 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002532 if (reg27 & BIT(6))
2533 sio_data->skip_pwm |= BIT(2);
2534 if (reg27 & BIT(7))
2535 sio_data->skip_fan |= BIT(2);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002536
2537 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002538 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002539 if (reg29 & BIT(1))
2540 sio_data->skip_pwm |= BIT(1);
2541 if (reg29 & BIT(2))
2542 sio_data->skip_fan |= BIT(1);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002543
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002544 sio_data->skip_in |= BIT(5); /* No VIN5 */
2545 sio_data->skip_in |= BIT(6); /* No VIN6 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002546
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002547 sio_data->beep_pin = superio_inb(sioaddr,
2548 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002549 } else if (sio_data->type == it8620) {
2550 int reg;
2551
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002552 superio_select(sioaddr, GPIO);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002553
Guenter Roeck36c4d982015-03-26 18:18:19 -07002554 /* Check for pwm5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002555 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002556 if (reg & BIT(6))
2557 sio_data->skip_pwm |= BIT(4);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002558
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002559 /* Check for fan4, fan5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002560 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002561 if (!(reg & BIT(5)))
2562 sio_data->skip_fan |= BIT(3);
2563 if (!(reg & BIT(4)))
2564 sio_data->skip_fan |= BIT(4);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002565
2566 /* Check for pwm3, fan3 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002567 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002568 if (reg & BIT(6))
2569 sio_data->skip_pwm |= BIT(2);
2570 if (reg & BIT(7))
2571 sio_data->skip_fan |= BIT(2);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002572
Guenter Roeck36c4d982015-03-26 18:18:19 -07002573 /* Check for pwm4 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002574 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002575 if (!(reg & BIT(2)))
2576 sio_data->skip_pwm |= BIT(3);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002577
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002578 /* Check for pwm2, fan2 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002579 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002580 if (reg & BIT(1))
2581 sio_data->skip_pwm |= BIT(1);
2582 if (reg & BIT(2))
2583 sio_data->skip_fan |= BIT(1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002584 /* Check for pwm6, fan6 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002585 if (!(reg & BIT(7))) {
2586 sio_data->skip_pwm |= BIT(5);
2587 sio_data->skip_fan |= BIT(5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002588 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002589
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002590 sio_data->beep_pin = superio_inb(sioaddr,
2591 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002592 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002593 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002594 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002595
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002596 superio_select(sioaddr, GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002597
Guenter Roecka0df9262015-04-05 16:51:36 -07002598 /* Check for fan4, fan5 */
2599 if (has_five_fans(config)) {
2600 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2601 switch (sio_data->type) {
2602 case it8718:
2603 if (reg & BIT(5))
2604 sio_data->skip_fan |= BIT(3);
2605 if (reg & BIT(4))
2606 sio_data->skip_fan |= BIT(4);
2607 break;
2608 case it8720:
2609 case it8721:
2610 case it8728:
2611 if (!(reg & BIT(5)))
2612 sio_data->skip_fan |= BIT(3);
2613 if (!(reg & BIT(4)))
2614 sio_data->skip_fan |= BIT(4);
2615 break;
2616 default:
2617 break;
2618 }
2619 }
2620
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002621 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002622 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002623 /* We need at least 4 VID pins */
2624 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002625 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002626 sio_data->skip_vid = 1;
2627 }
Jean Delvare895ff262009-12-09 20:35:47 +01002628 }
2629
Jean Delvare591ec652009-12-09 20:35:48 +01002630 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002631 if (reg & BIT(6))
2632 sio_data->skip_pwm |= BIT(2);
2633 if (reg & BIT(7))
2634 sio_data->skip_fan |= BIT(2);
Jean Delvare591ec652009-12-09 20:35:48 +01002635
2636 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002637 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002638 if (reg & BIT(1))
2639 sio_data->skip_pwm |= BIT(1);
2640 if (reg & BIT(2))
2641 sio_data->skip_fan |= BIT(1);
Jean Delvare591ec652009-12-09 20:35:48 +01002642
Guenter Roeckc9620242015-04-04 09:05:57 -07002643 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
2644 !(sio_data->skip_vid))
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002645 sio_data->vid_value = superio_inb(sioaddr,
2646 IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002647
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002648 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002649
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002650 uart6 = sio_data->type == it8782 && (reg & BIT(2));
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002651
Jean Delvare436cad22010-07-09 16:22:48 +02002652 /*
2653 * The IT8720F has no VIN7 pin, so VCCH should always be
2654 * routed internally to VIN7 with an internal divider.
2655 * Curiously, there still is a configuration bit to control
2656 * this, which means it can be set incorrectly. And even
2657 * more curiously, many boards out there are improperly
2658 * configured, even though the IT8720F datasheet claims
2659 * that the internal routing of VCCH to VIN7 is the default
2660 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002661 *
2662 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002663 * If UART6 is enabled, re-route VIN7 to the internal divider
2664 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002665 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002666 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
2667 reg |= BIT(1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002668 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002669 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002670 }
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002671 if (reg & BIT(0))
2672 sio_data->internal |= BIT(0);
2673 if (reg & BIT(1))
2674 sio_data->internal |= BIT(1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002675
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002676 /*
2677 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2678 * While VIN7 can be routed to the internal voltage divider,
2679 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002680 *
2681 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2682 * is the temperature source. Since we can not read the
2683 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002684 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002685 if (uart6) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002686 sio_data->skip_in |= BIT(5) | BIT(6);
2687 sio_data->skip_temp |= BIT(2);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002688 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002689
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002690 sio_data->beep_pin = superio_inb(sioaddr,
2691 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002692 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002693 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002694 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002695
Jean Delvare98dd22c2008-10-09 15:33:58 +02002696 /* Disable specific features based on DMI strings */
2697 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2698 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2699 if (board_vendor && board_name) {
Guenter Roeckc9620242015-04-04 09:05:57 -07002700 if (strcmp(board_vendor, "nVIDIA") == 0 &&
2701 strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002702 /*
2703 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2704 * connected to a fan, but to something else. One user
2705 * has reported instant system power-off when changing
2706 * the PWM2 duty cycle, so we disable it.
2707 * I use the board name string as the trigger in case
2708 * the same board is ever used in other systems.
2709 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002710 pr_info("Disabling pwm2 due to hardware constraints\n");
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002711 sio_data->skip_pwm = BIT(1);
Jean Delvare98dd22c2008-10-09 15:33:58 +02002712 }
2713 }
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715exit:
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002716 superio_exit(sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 return err;
2718}
2719
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002720/* Called when we have found a new IT87. */
2721static void it87_init_device(struct platform_device *pdev)
2722{
2723 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2724 struct it87_data *data = platform_get_drvdata(pdev);
2725 int tmp, i;
2726 u8 mask;
2727
2728 /*
2729 * For each PWM channel:
2730 * - If it is in automatic mode, setting to manual mode should set
2731 * the fan to full speed by default.
2732 * - If it is in manual mode, we need a mapping to temperature
2733 * channels to use when later setting to automatic mode later.
2734 * Use a 1:1 mapping by default (we are clueless.)
2735 * In both cases, the value can (and should) be changed by the user
2736 * prior to switching to a different mode.
2737 * Note that this is no longer needed for the IT8721F and later, as
2738 * these have separate registers for the temperature mapping and the
2739 * manual duty cycle.
2740 */
Guenter Roeck23100482015-04-02 08:23:45 -07002741 for (i = 0; i < NUM_AUTO_PWM; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002742 data->pwm_temp_map[i] = i;
2743 data->pwm_duty[i] = 0x7f; /* Full speed */
2744 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2745 }
2746
2747 /*
2748 * Some chips seem to have default value 0xff for all limit
2749 * registers. For low voltage limits it makes no sense and triggers
2750 * alarms, so change to 0 instead. For high temperature limits, it
2751 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2752 * but is still confusing, so change to 127 degrees C.
2753 */
Guenter Roeck23100482015-04-02 08:23:45 -07002754 for (i = 0; i < NUM_VIN_LIMIT; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002755 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2756 if (tmp == 0xff)
2757 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2758 }
Guenter Roeck23100482015-04-02 08:23:45 -07002759 for (i = 0; i < NUM_TEMP_LIMIT; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002760 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2761 if (tmp == 0xff)
2762 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2763 }
2764
2765 /*
2766 * Temperature channels are not forcibly enabled, as they can be
2767 * set to two different sensor types and we can't guess which one
2768 * is correct for a given system. These channels can be enabled at
2769 * run-time through the temp{1-3}_type sysfs accessors if needed.
2770 */
2771
2772 /* Check if voltage monitors are reset manually or by some reason */
2773 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2774 if ((tmp & 0xff) == 0) {
2775 /* Enable all voltage monitors */
2776 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2777 }
2778
2779 /* Check if tachometers are reset manually or by some reason */
2780 mask = 0x70 & ~(sio_data->skip_fan << 4);
2781 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2782 if ((data->fan_main_ctrl & mask) == 0) {
2783 /* Enable all fan tachometers */
2784 data->fan_main_ctrl |= mask;
2785 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2786 data->fan_main_ctrl);
2787 }
2788 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2789
2790 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2791
2792 /* Set tachometers to 16-bit mode if needed */
2793 if (has_fan16_config(data)) {
2794 if (~tmp & 0x07 & data->has_fan) {
2795 dev_dbg(&pdev->dev,
2796 "Setting fan1-3 to 16-bit mode\n");
2797 it87_write_value(data, IT87_REG_FAN_16BIT,
2798 tmp | 0x07);
2799 }
2800 }
2801
2802 /* Check for additional fans */
2803 if (has_five_fans(data)) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002804 if (tmp & BIT(4))
2805 data->has_fan |= BIT(3); /* fan4 enabled */
2806 if (tmp & BIT(5))
2807 data->has_fan |= BIT(4); /* fan5 enabled */
2808 if (has_six_fans(data) && (tmp & BIT(2)))
2809 data->has_fan |= BIT(5); /* fan6 enabled */
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002810 }
2811
2812 /* Fan input pins may be used for alternative functions */
2813 data->has_fan &= ~sio_data->skip_fan;
2814
2815 /* Check if pwm5, pwm6 are enabled */
2816 if (has_six_pwm(data)) {
2817 /* The following code may be IT8620E specific */
2818 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2819 if ((tmp & 0xc0) == 0xc0)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002820 sio_data->skip_pwm |= BIT(4);
2821 if (!(tmp & BIT(3)))
2822 sio_data->skip_pwm |= BIT(5);
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002823 }
2824
2825 /* Start monitoring */
2826 it87_write_value(data, IT87_REG_CONFIG,
2827 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2828 | (update_vbat ? 0x41 : 0x01));
2829}
2830
2831/* Return 1 if and only if the PWM interface is safe to use */
2832static int it87_check_pwm(struct device *dev)
2833{
2834 struct it87_data *data = dev_get_drvdata(dev);
2835 /*
2836 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2837 * and polarity set to active low is sign that this is the case so we
2838 * disable pwm control to protect the user.
2839 */
2840 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2841
2842 if ((tmp & 0x87) == 0) {
2843 if (fix_pwm_polarity) {
2844 /*
2845 * The user asks us to attempt a chip reconfiguration.
2846 * This means switching to active high polarity and
2847 * inverting all fan speed values.
2848 */
2849 int i;
2850 u8 pwm[3];
2851
Guenter Roeck23100482015-04-02 08:23:45 -07002852 for (i = 0; i < ARRAY_SIZE(pwm); i++)
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002853 pwm[i] = it87_read_value(data,
2854 IT87_REG_PWM[i]);
2855
2856 /*
2857 * If any fan is in automatic pwm mode, the polarity
2858 * might be correct, as suspicious as it seems, so we
2859 * better don't change anything (but still disable the
2860 * PWM interface).
2861 */
2862 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2863 dev_info(dev,
2864 "Reconfiguring PWM to active high polarity\n");
2865 it87_write_value(data, IT87_REG_FAN_CTL,
2866 tmp | 0x87);
2867 for (i = 0; i < 3; i++)
2868 it87_write_value(data,
2869 IT87_REG_PWM[i],
2870 0x7f & ~pwm[i]);
2871 return 1;
2872 }
2873
2874 dev_info(dev,
2875 "PWM configuration is too broken to be fixed\n");
2876 }
2877
2878 dev_info(dev,
2879 "Detected broken BIOS defaults, disabling PWM interface\n");
2880 return 0;
2881 } else if (fix_pwm_polarity) {
2882 dev_info(dev,
2883 "PWM configuration looks sane, won't touch\n");
2884 }
2885
2886 return 1;
2887}
2888
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002889static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002892 struct resource *res;
2893 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002894 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 int enable_pwm_interface;
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002896 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002898 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002899 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2900 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002901 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2902 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002903 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002904 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Guenter Roeck62a1d052012-03-24 21:54:41 -07002907 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2908 if (!data)
2909 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002911 data->addr = res->start;
2912 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002913 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002914 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002915 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002916 /*
2917 * IT8705F Datasheet 0.4.1, 3h == Version G.
2918 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2919 * These are the first revisions with 16-bit tachometer support.
2920 */
2921 switch (data->type) {
2922 case it87:
2923 if (sio_data->revision >= 0x03) {
2924 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002925 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002926 }
2927 break;
2928 case it8712:
2929 if (sio_data->revision >= 0x08) {
2930 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002931 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2932 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002933 }
2934 break;
2935 default:
2936 break;
2937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 /* Now, we do the remaining detection. */
Guenter Roeckc9620242015-04-04 09:05:57 -07002940 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) ||
2941 it87_read_value(data, IT87_REG_CHIPID) != 0x90)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002942 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002944 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002946 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002949 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002951 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002952 if (has_12mv_adc(data)) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002953 if (sio_data->internal & BIT(0))
2954 data->in_scaled |= BIT(3); /* in3 is AVCC */
2955 if (sio_data->internal & BIT(1))
2956 data->in_scaled |= BIT(7); /* in7 is VSB */
2957 if (sio_data->internal & BIT(2))
2958 data->in_scaled |= BIT(8); /* in8 is Vbat */
2959 if (sio_data->internal & BIT(3))
2960 data->in_scaled |= BIT(9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002961 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2962 sio_data->type == it8783) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002963 if (sio_data->internal & BIT(0))
2964 data->in_scaled |= BIT(3); /* in3 is VCC5V */
2965 if (sio_data->internal & BIT(1))
2966 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002967 }
2968
Guenter Roeck4573acb2012-03-26 16:17:41 -07002969 data->has_temp = 0x07;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002970 if (sio_data->skip_temp & BIT(2)) {
Guenter Roeckc9620242015-04-04 09:05:57 -07002971 if (sio_data->type == it8782 &&
2972 !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002973 data->has_temp &= ~BIT(2);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002974 }
2975
Guenter Roeckd3766842013-03-30 15:47:21 -07002976 data->in_internal = sio_data->internal;
Guenter Roeck52929712013-03-30 14:00:08 -07002977 data->has_in = 0x3ff & ~sio_data->skip_in;
2978
Guenter Roeckcc18da72015-04-01 10:03:01 -07002979 if (has_six_temp(data)) {
2980 u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
2981
Guenter Roeckf838aa22015-04-01 20:09:36 -07002982 /* Check for additional temperature sensors */
Guenter Roeckcc18da72015-04-01 10:03:01 -07002983 if ((reg & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002984 data->has_temp |= BIT(3);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002985 if (((reg >> 2) & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002986 data->has_temp |= BIT(4);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002987 if (((reg >> 4) & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002988 data->has_temp |= BIT(5);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002989
2990 /* Check for additional voltage sensors */
2991 if ((reg & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002992 data->has_in |= BIT(10);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002993 if (((reg >> 2) & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002994 data->has_in |= BIT(11);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002995 if (((reg >> 4) & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002996 data->has_in |= BIT(12);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002997 }
2998
Guenter Roeck52929712013-03-30 14:00:08 -07002999 data->has_beep = !!sio_data->beep_pin;
3000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003002 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Guenter Roeckd3766842013-03-30 15:47:21 -07003004 if (!sio_data->skip_vid) {
3005 data->has_vid = true;
3006 data->vrm = vid_which_vrm();
3007 /* VID reading from Super-I/O config space if available */
3008 data->vid = sio_data->vid_value;
3009 }
3010
Guenter Roeck8638d0a2015-03-30 11:13:49 -07003011 /* Prepare for sysfs hooks */
3012 data->groups[0] = &it87_group;
3013 data->groups[1] = &it87_group_in;
3014 data->groups[2] = &it87_group_temp;
3015 data->groups[3] = &it87_group_fan;
Jean Delvare17d648b2006-08-28 14:23:46 +02003016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if (enable_pwm_interface) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07003018 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
Guenter Roeck5c391262013-03-30 15:02:12 -07003019 data->has_pwm &= ~sio_data->skip_pwm;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01003020
Guenter Roeck8638d0a2015-03-30 11:13:49 -07003021 data->groups[4] = &it87_group_pwm;
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07003022 if (has_old_autopwm(data) || has_newer_autopwm(data))
Guenter Roeck8638d0a2015-03-30 11:13:49 -07003023 data->groups[5] = &it87_group_auto_pwm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
3025
Guenter Roeck8638d0a2015-03-30 11:13:49 -07003026 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
3027 it87_devices[sio_data->type].name,
3028 data, data->groups);
3029 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030}
3031
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07003032static struct platform_driver it87_driver = {
3033 .driver = {
3034 .name = DRVNAME,
3035 },
3036 .probe = it87_probe,
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07003037};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Guenter Roecke84bd952015-03-28 08:24:29 -07003039static int __init it87_device_add(int index, unsigned short address,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003040 const struct it87_sio_data *sio_data)
3041{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07003042 struct platform_device *pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003043 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05003044 .start = address + IT87_EC_OFFSET,
3045 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003046 .name = DRVNAME,
3047 .flags = IORESOURCE_IO,
3048 };
3049 int err;
3050
Jean Delvareb9acb642009-01-07 16:37:35 +01003051 err = acpi_check_resource_conflict(&res);
3052 if (err)
Guenter Roeck5cae84a2015-03-28 07:44:59 -07003053 return err;
Jean Delvareb9acb642009-01-07 16:37:35 +01003054
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003055 pdev = platform_device_alloc(DRVNAME, address);
Guenter Roeck5cae84a2015-03-28 07:44:59 -07003056 if (!pdev)
3057 return -ENOMEM;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003058
3059 err = platform_device_add_resources(pdev, &res, 1);
3060 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01003061 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003062 goto exit_device_put;
3063 }
3064
3065 err = platform_device_add_data(pdev, sio_data,
3066 sizeof(struct it87_sio_data));
3067 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01003068 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003069 goto exit_device_put;
3070 }
3071
3072 err = platform_device_add(pdev);
3073 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01003074 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003075 goto exit_device_put;
3076 }
3077
Guenter Roecke84bd952015-03-28 08:24:29 -07003078 it87_pdev[index] = pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003079 return 0;
3080
3081exit_device_put:
3082 platform_device_put(pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003083 return err;
3084}
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086static int __init sm_it87_init(void)
3087{
Guenter Roecke84bd952015-03-28 08:24:29 -07003088 int sioaddr[2] = { REG_2E, REG_4E };
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003089 struct it87_sio_data sio_data;
Guenter Roecke84bd952015-03-28 08:24:29 -07003090 unsigned short isa_address;
3091 bool found = false;
3092 int i, err;
Jean Delvarefde09502005-07-19 23:51:07 +02003093
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003094 err = platform_driver_register(&it87_driver);
3095 if (err)
3096 return err;
3097
Guenter Roecke84bd952015-03-28 08:24:29 -07003098 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
3099 memset(&sio_data, 0, sizeof(struct it87_sio_data));
3100 isa_address = 0;
3101 err = it87_find(sioaddr[i], &isa_address, &sio_data);
3102 if (err || isa_address == 0)
3103 continue;
3104
3105 err = it87_device_add(i, isa_address, &sio_data);
3106 if (err)
3107 goto exit_dev_unregister;
3108 found = true;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003109 }
3110
Guenter Roecke84bd952015-03-28 08:24:29 -07003111 if (!found) {
3112 err = -ENODEV;
3113 goto exit_unregister;
3114 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003115 return 0;
Guenter Roecke84bd952015-03-28 08:24:29 -07003116
3117exit_dev_unregister:
3118 /* NULL check handled by platform_device_unregister */
3119 platform_device_unregister(it87_pdev[0]);
3120exit_unregister:
3121 platform_driver_unregister(&it87_driver);
3122 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
3125static void __exit sm_it87_exit(void)
3126{
Guenter Roecke84bd952015-03-28 08:24:29 -07003127 /* NULL check handled by platform_device_unregister */
3128 platform_device_unregister(it87_pdev[1]);
3129 platform_device_unregister(it87_pdev[0]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003130 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
Jean Delvare7c81c602014-01-29 20:40:08 +01003133MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02003134MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135module_param(update_vbat, bool, 0);
3136MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
3137module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01003138MODULE_PARM_DESC(fix_pwm_polarity,
3139 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140MODULE_LICENSE("GPL");
3141
3142module_init(sm_it87_init);
3143module_exit(sm_it87_exit);