blob: 915d206c79eb2e6fd8cb046bb88f3ffdd328f236 [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
Rudolf Marek574e9bd2014-04-04 18:01:35 +020014 * IT8623E Super I/O chip w/LPC interface
Rudolf Marekc145d5c2014-01-29 20:40:08 +010015 * IT8705F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010016 * IT8712F Super I/O chip w/LPC interface
17 * IT8716F Super I/O chip w/LPC interface
18 * IT8718F Super I/O chip w/LPC interface
19 * IT8720F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020020 * IT8721F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010021 * IT8726F Super I/O chip w/LPC interface
Jean Delvare16b5dda2012-01-16 22:51:48 +010022 * IT8728F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020023 * IT8758E Super I/O chip w/LPC interface
Guenter Roeckb0636702012-09-07 17:34:41 -060024 * IT8771E Super I/O chip w/LPC interface
25 * IT8772E Super I/O chip w/LPC interface
Guenter Roeck7bc32d22015-01-17 14:10:24 -080026 * IT8781F Super I/O chip w/LPC interface
Guenter Roeck0531d982012-03-02 11:46:44 -080027 * IT8782F Super I/O chip w/LPC interface
28 * IT8783E/F Super I/O chip w/LPC interface
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +010029 * IT8786E Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010030 * Sis950 A clone of the IT8705F
31 *
32 * Copyright (C) 2001 Chris Gauthron
Jean Delvare7c81c602014-01-29 20:40:08 +010033 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
Jean Delvare5f2dc792010-03-05 22:17:18 +010034 *
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License
46 * along with this program; if not, write to the Free Software
47 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
48 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Joe Perchesa8ca1032011-01-12 21:55:10 +010050#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
51
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
Guenter Roeckb0636702012-09-07 17:34:41 -060070enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8771,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +010071 it8772, it8781, it8782, it8783, it8786, it8603 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Jean Delvare67b671b2007-12-06 23:13:42 +010073static unsigned short force_id;
74module_param(force_id, ushort, 0);
75MODULE_PARM_DESC(force_id, "Override the detected device ID");
76
corentin.labbeb74f3fd2007-06-13 20:27:36 +020077static struct platform_device *pdev;
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define REG 0x2e /* The register to read/write */
80#define DEV 0x07 /* Register: Logical device select */
81#define VAL 0x2f /* The value to read/write */
82#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010083
84/* The device with the IT8718F/IT8720F VID value in it */
85#define GPIO 0x07
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define DEVID 0x20 /* Register: Device ID */
88#define DEVREV 0x22 /* Register: Device Revision */
89
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020090static inline int superio_inb(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 outb(reg, REG);
93 return inb(VAL);
94}
95
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020096static inline void superio_outb(int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +020097{
98 outb(reg, REG);
99 outb(val, VAL);
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int superio_inw(int reg)
103{
104 int val;
105 outb(reg++, REG);
106 val = inb(VAL) << 8;
107 outb(reg, REG);
108 val |= inb(VAL);
109 return val;
110}
111
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200112static inline void superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200115 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200118static inline int superio_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200120 /*
121 * Try to reserve REG and REG + 1 for exclusive access.
122 */
123 if (!request_muxed_region(REG, 2, DRVNAME))
124 return -EBUSY;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 outb(0x87, REG);
127 outb(0x01, REG);
128 outb(0x55, REG);
129 outb(0x55, REG);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200133static inline void superio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 outb(0x02, REG);
136 outb(0x02, VAL);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200137 release_region(REG, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Jean Delvare87673dd2006-08-28 14:37:19 +0200140/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define IT8712F_DEVID 0x8712
142#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200143#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200144#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100145#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200146#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400147#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100148#define IT8728F_DEVID 0x8728
Guenter Roeckb0636702012-09-07 17:34:41 -0600149#define IT8771E_DEVID 0x8771
150#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800151#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800152#define IT8782F_DEVID 0x8782
153#define IT8783E_DEVID 0x8783
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100154#define IT8786E_DEVID 0x8786
Rudolf Marek7183ae82014-04-04 18:01:35 +0200155#define IT8603E_DEVID 0x8603
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200156#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define IT87_ACT_REG 0x30
158#define IT87_BASE_REG 0x60
159
Jean Delvare87673dd2006-08-28 14:37:19 +0200160/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800161#define IT87_SIO_GPIO1_REG 0x25
Jean Delvare895ff262009-12-09 20:35:47 +0100162#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100163#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800164#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200165#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800166#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200167#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100168#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030171static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030174static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/* Many IT87 constants specified below */
177
178/* Length of ISA address segment */
179#define IT87_EXTENT 8
180
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500181/* Length of ISA address segment for Environmental Controller */
182#define IT87_EC_EXTENT 2
183
184/* Offset of EC registers from ISA base address */
185#define IT87_EC_OFFSET 5
186
187/* Where are the ISA address/data registers relative to the EC base address */
188#define IT87_ADDR_REG_OFFSET 0
189#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/*----- The IT87 registers -----*/
192
193#define IT87_REG_CONFIG 0x00
194
195#define IT87_REG_ALARM1 0x01
196#define IT87_REG_ALARM2 0x02
197#define IT87_REG_ALARM3 0x03
198
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800199/*
200 * The IT8718F and IT8720F have the VID value in a different register, in
201 * Super-I/O configuration space.
202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800204/*
205 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
206 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
207 * mode.
208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200210#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
213
Jean Delvarec7f1f712007-09-03 17:11:46 +0200214static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
215static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
216static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
217static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100218static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define IT87_REG_FAN_MAIN_CTRL 0x13
221#define IT87_REG_FAN_CTL 0x14
222#define IT87_REG_PWM(nr) (0x15 + (nr))
Jean Delvare6229cdb2010-12-08 16:27:22 +0100223#define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225#define IT87_REG_VIN(nr) (0x20 + (nr))
226#define IT87_REG_TEMP(nr) (0x29 + (nr))
227
228#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
229#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
230#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
231#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#define IT87_REG_VIN_ENABLE 0x50
234#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700235#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100236#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238#define IT87_REG_CHIPID 0x58
239
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100240#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
241#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
242
Guenter Roeck483db432012-12-19 22:17:02 +0100243struct it87_devices {
244 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700245 const char * const suffix;
Guenter Roeck483db432012-12-19 22:17:02 +0100246 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100247 u8 peci_mask;
248 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100249};
250
251#define FEAT_12MV_ADC (1 << 0)
252#define FEAT_NEWER_AUTOPWM (1 << 1)
253#define FEAT_OLD_AUTOPWM (1 << 2)
254#define FEAT_16BIT_FANS (1 << 3)
255#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100256#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100257#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800258#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
259#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800260#define FEAT_VID (1 << 9) /* Set if chip supports VID */
Guenter Roeck483db432012-12-19 22:17:02 +0100261
262static const struct it87_devices it87_devices[] = {
263 [it87] = {
264 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700265 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100266 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
267 },
268 [it8712] = {
269 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700270 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800271 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
272 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100273 },
274 [it8716] = {
275 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700276 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800277 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800278 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck483db432012-12-19 22:17:02 +0100279 },
280 [it8718] = {
281 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700282 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800283 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800284 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100285 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100286 },
287 [it8720] = {
288 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700289 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800290 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800291 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100292 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100293 },
294 [it8721] = {
295 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700296 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100297 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800298 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
299 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100300 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100301 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100302 },
303 [it8728] = {
304 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700305 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100306 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800307 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100308 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100309 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600310 [it8771] = {
311 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700312 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600313 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
314 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800315 /* PECI: guesswork */
316 /* 12mV ADC (OHM) */
317 /* 16 bit fans (OHM) */
318 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600319 .peci_mask = 0x07,
320 },
321 [it8772] = {
322 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700323 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600324 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
325 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800326 /* PECI (coreboot) */
327 /* 12mV ADC (HWSensors4, OHM) */
328 /* 16 bit fans (HWSensors4, OHM) */
329 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600330 .peci_mask = 0x07,
331 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800332 [it8781] = {
333 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700334 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800335 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800336 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800337 .old_peci_mask = 0x4,
338 },
Guenter Roeck483db432012-12-19 22:17:02 +0100339 [it8782] = {
340 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700341 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100342 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800343 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100344 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100345 },
346 [it8783] = {
347 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700348 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100349 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800350 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100351 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100352 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100353 [it8786] = {
354 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700355 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100356 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
357 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
358 .peci_mask = 0x07,
359 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100360 [it8603] = {
361 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700362 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100363 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
364 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
365 .peci_mask = 0x07,
366 },
Guenter Roeck483db432012-12-19 22:17:02 +0100367};
368
369#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
370#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
371#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
372#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
373#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100374#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
375 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100376#define has_temp_old_peci(data, nr) \
377 (((data)->features & FEAT_TEMP_OLD_PECI) && \
378 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800379#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
380#define has_five_fans(data) ((data)->features & FEAT_FIVE_FANS)
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800381#define has_vid(data) ((data)->features & FEAT_VID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200383struct it87_sio_data {
384 enum chips type;
385 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200386 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200387 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100388 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200389 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100390 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700391 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100392 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100393 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200394 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700395 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200396};
397
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800398/*
399 * For each registered chip, we need to keep some data in memory.
400 * The structure is dynamically allocated.
401 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700403 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100405 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100406 u8 peci_mask;
407 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200409 unsigned short addr;
410 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100411 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 char valid; /* !=0 if following fields are valid */
413 unsigned long last_updated; /* In jiffies */
414
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200415 u16 in_scaled; /* Internal voltage sensors are scaled */
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100416 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200417 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100418 u16 fan[5][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700419 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck161d8982012-12-19 22:17:01 +0100420 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100421 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
422 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 u8 fan_div[3]; /* Register encoding, shifted right */
424 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100425 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100427 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100429 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100430
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800431 /*
432 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100433 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
434 * 7, and we want to preserve settings on mode changes, so we have
435 * to track all values separately.
436 * Starting with the IT8721F, the manual PWM duty cycles are stored
437 * in separate registers (8-bit values), so the separate tracking
438 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800439 * simple.
440 */
Jean Delvareb99883d2010-03-05 22:17:15 +0100441 u8 pwm_ctrl[3]; /* Register value */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100442 u8 pwm_duty[3]; /* Manual PWM value set by user */
Jean Delvareb99883d2010-03-05 22:17:15 +0100443 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100444
445 /* Automatic fan speed control registers */
446 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
447 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448};
449
Guenter Roeck0531d982012-03-02 11:46:44 -0800450static int adc_lsb(const struct it87_data *data, int nr)
451{
452 int lsb = has_12mv_adc(data) ? 12 : 16;
453 if (data->in_scaled & (1 << nr))
454 lsb <<= 1;
455 return lsb;
456}
457
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200458static u8 in_to_reg(const struct it87_data *data, int nr, long val)
459{
Guenter Roeck0531d982012-03-02 11:46:44 -0800460 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800461 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200462}
463
464static int in_from_reg(const struct it87_data *data, int nr, int val)
465{
Guenter Roeck0531d982012-03-02 11:46:44 -0800466 return val * adc_lsb(data, nr);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200467}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200468
469static inline u8 FAN_TO_REG(long rpm, int div)
470{
471 if (rpm == 0)
472 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800473 rpm = clamp_val(rpm, 1, 1000000);
474 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200475}
476
477static inline u16 FAN16_TO_REG(long rpm)
478{
479 if (rpm == 0)
480 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800481 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200482}
483
484#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
485 1350000 / ((val) * (div)))
486/* The divider is fixed to 2 in 16-bit mode */
487#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
488 1350000 / ((val) * 2))
489
Guenter Roeck2a844c12013-01-09 08:09:34 -0800490#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
491 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200492#define TEMP_FROM_REG(val) ((val) * 1000)
493
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200494static u8 pwm_to_reg(const struct it87_data *data, long val)
495{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100496 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200497 return val;
498 else
499 return val >> 1;
500}
501
502static int pwm_from_reg(const struct it87_data *data, u8 reg)
503{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100504 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200505 return reg;
506 else
507 return (reg & 0x7f) << 1;
508}
509
Jean Delvare0df6454d2010-10-28 20:31:51 +0200510
511static int DIV_TO_REG(int val)
512{
513 int answer = 0;
514 while (answer < 7 && (val >>= 1))
515 answer++;
516 return answer;
517}
518#define DIV_FROM_REG(val) (1 << (val))
519
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700520/*
521 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
522 * depending on the chip type, to calculate the actual PWM frequency.
523 *
524 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
525 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
526 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
527 * sometimes just one. It is unknown if this is a datasheet error or real,
528 * so this is ignored for now.
529 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200530static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700531 48000000,
532 24000000,
533 12000000,
534 8000000,
535 6000000,
536 3000000,
537 1500000,
538 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200539};
540
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200541static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500542static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200544static int it87_read_value(struct it87_data *data, u8 reg);
545static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200547static int it87_check_pwm(struct device *dev);
548static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200551static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100552 .driver = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200553 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100554 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200555 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500556 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200557};
558
Jean Delvare20ad93d2005-06-05 11:53:25 +0200559static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100560 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100562 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
563 int nr = sattr->nr;
564 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100567 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Guenter Roeck929c6a52012-12-19 22:17:00 +0100570static ssize_t set_in(struct device *dev, struct device_attribute *attr,
571 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100573 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
574 int nr = sattr->nr;
575 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200576
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200577 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100578 unsigned long val;
579
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100580 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100581 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100583 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100584 data->in[nr][index] = in_to_reg(data, nr, val);
585 it87_write_value(data,
586 index == 1 ? IT87_REG_VIN_MIN(nr)
587 : IT87_REG_VIN_MAX(nr),
588 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100589 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return count;
591}
592
Guenter Roeck929c6a52012-12-19 22:17:00 +0100593static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
594static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
595 0, 1);
596static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
597 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Guenter Roeck929c6a52012-12-19 22:17:00 +0100599static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
600static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
601 1, 1);
602static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
603 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Guenter Roeck929c6a52012-12-19 22:17:00 +0100605static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
606static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
607 2, 1);
608static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
609 2, 2);
610
611static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
612static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
613 3, 1);
614static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
615 3, 2);
616
617static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
618static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
619 4, 1);
620static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
621 4, 2);
622
623static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
624static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
625 5, 1);
626static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
627 5, 2);
628
629static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
630static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
631 6, 1);
632static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
633 6, 2);
634
635static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
636static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
637 7, 1);
638static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
639 7, 2);
640
641static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100642static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200645static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100646 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100648 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
649 int nr = sattr->nr;
650 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200652
Guenter Roeck60ca3852012-12-19 22:17:00 +0100653 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200655
Guenter Roeck60ca3852012-12-19 22:17:00 +0100656static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
657 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100659 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
660 int nr = sattr->nr;
661 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200662 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100663 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100664 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100665
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100666 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100667 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100669 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100670
671 switch (index) {
672 default:
673 case 1:
674 reg = IT87_REG_TEMP_LOW(nr);
675 break;
676 case 2:
677 reg = IT87_REG_TEMP_HIGH(nr);
678 break;
679 case 3:
680 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
681 if (!(regval & 0x80)) {
682 regval |= 0x80;
683 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
684 }
685 data->valid = 0;
686 reg = IT87_REG_TEMP_OFFSET[nr];
687 break;
688 }
689
Guenter Roeck60ca3852012-12-19 22:17:00 +0100690 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100691 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100692 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return count;
694}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200695
Guenter Roeck60ca3852012-12-19 22:17:00 +0100696static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
697static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
698 0, 1);
699static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
700 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100701static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
702 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100703static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
704static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
705 1, 1);
706static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
707 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100708static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
709 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100710static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
711static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
712 2, 1);
713static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
714 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100715static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
716 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Guenter Roeck2cece012012-12-19 22:17:01 +0100718static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
719 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200721 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
722 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800724 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100725 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100726
Guenter Roeck19529782012-12-19 22:17:02 +0100727 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
728 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100729 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (reg & (1 << nr))
731 return sprintf(buf, "3\n"); /* thermal diode */
732 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200733 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return sprintf(buf, "0\n"); /* disabled */
735}
Guenter Roeck2cece012012-12-19 22:17:01 +0100736
737static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
738 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200740 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
741 int nr = sensor_attr->index;
742
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200743 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100744 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100745 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100746
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100747 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100748 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Jean Delvare8acf07c2010-04-14 16:14:09 +0200750 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
751 reg &= ~(1 << nr);
752 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100753 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
754 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100755 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
756 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
757 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200758 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100759 dev_warn(dev,
760 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200761 val = 4;
762 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100763 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200765 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200766 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200767 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100768 else if (has_temp_peci(data, nr) && val == 6)
769 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100770 else if (has_temp_old_peci(data, nr) && val == 6)
771 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200772 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200774
775 mutex_lock(&data->update_lock);
776 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100777 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200778 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100779 if (has_temp_old_peci(data, nr))
780 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200781 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100782 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return count;
784}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Guenter Roeck2cece012012-12-19 22:17:01 +0100786static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
787 set_temp_type, 0);
788static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
789 set_temp_type, 1);
790static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
791 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100794
795static int pwm_mode(const struct it87_data *data, int nr)
796{
797 int ctrl = data->fan_main_ctrl & (1 << nr);
798
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100799 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +0100800 return 0;
801 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
802 return 2;
803 else /* Manual mode */
804 return 1;
805}
806
Jean Delvare20ad93d2005-06-05 11:53:25 +0200807static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +0100808 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100810 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
811 int nr = sattr->nr;
812 int index = sattr->index;
813 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200815
Guenter Roecke1169ba2012-12-19 22:17:01 +0100816 speed = has_16bit_fans(data) ?
817 FAN16_FROM_REG(data->fan[nr][index]) :
818 FAN_FROM_REG(data->fan[nr][index],
819 DIV_FROM_REG(data->fan_div[nr]));
820 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100822
Jean Delvare20ad93d2005-06-05 11:53:25 +0200823static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
824 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200826 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
827 int nr = sensor_attr->index;
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 struct it87_data *data = it87_update_device(dev);
830 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
831}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100832static ssize_t show_pwm_enable(struct device *dev,
833 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200835 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
836 int nr = sensor_attr->index;
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100839 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200841static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
842 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200844 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
845 int nr = sensor_attr->index;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200848 return sprintf(buf, "%d\n",
849 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100851static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
852 char *buf)
853{
854 struct it87_data *data = it87_update_device(dev);
855 int index = (data->fan_ctl >> 4) & 0x07;
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700856 unsigned int freq;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100857
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700858 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
859
860 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100861}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100862
863static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
864 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100866 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
867 int nr = sattr->nr;
868 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200869
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200870 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100871 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100872 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100874 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100875 return -EINVAL;
876
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100877 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +0100878
879 if (has_16bit_fans(data)) {
880 data->fan[nr][index] = FAN16_TO_REG(val);
881 it87_write_value(data, IT87_REG_FAN_MIN[nr],
882 data->fan[nr][index] & 0xff);
883 it87_write_value(data, IT87_REG_FANX_MIN[nr],
884 data->fan[nr][index] >> 8);
885 } else {
886 reg = it87_read_value(data, IT87_REG_FAN_DIV);
887 switch (nr) {
888 case 0:
889 data->fan_div[nr] = reg & 0x07;
890 break;
891 case 1:
892 data->fan_div[nr] = (reg >> 3) & 0x07;
893 break;
894 case 2:
895 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
896 break;
897 }
898 data->fan[nr][index] =
899 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
900 it87_write_value(data, IT87_REG_FAN_MIN[nr],
901 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -0800902 }
903
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100904 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return count;
906}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100907
Jean Delvare20ad93d2005-06-05 11:53:25 +0200908static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
909 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200911 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
912 int nr = sensor_attr->index;
913
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200914 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100915 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200916 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 u8 old;
918
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100919 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100920 return -EINVAL;
921
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100922 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200923 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200925 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100926 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 switch (nr) {
929 case 0:
930 case 1:
931 data->fan_div[nr] = DIV_TO_REG(val);
932 break;
933 case 2:
934 if (val < 8)
935 data->fan_div[nr] = 1;
936 else
937 data->fan_div[nr] = 3;
938 }
939 val = old & 0x80;
940 val |= (data->fan_div[0] & 0x07);
941 val |= (data->fan_div[1] & 0x07) << 3;
942 if (data->fan_div[2] == 3)
943 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200944 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200946 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100947 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
948 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200949
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100950 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return count;
952}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100953
954/* Returns 0 if OK, -EINVAL otherwise */
955static int check_trip_points(struct device *dev, int nr)
956{
957 const struct it87_data *data = dev_get_drvdata(dev);
958 int i, err = 0;
959
960 if (has_old_autopwm(data)) {
961 for (i = 0; i < 3; i++) {
962 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
963 err = -EINVAL;
964 }
965 for (i = 0; i < 2; i++) {
966 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
967 err = -EINVAL;
968 }
969 }
970
971 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100972 dev_err(dev,
973 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100974 dev_err(dev, "Adjust the trip points and try again\n");
975 }
976 return err;
977}
978
Jean Delvare20ad93d2005-06-05 11:53:25 +0200979static ssize_t set_pwm_enable(struct device *dev,
980 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200982 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
983 int nr = sensor_attr->index;
984
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200985 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100986 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100988 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100989 return -EINVAL;
990
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100991 /* Check trip points before switching to automatic mode */
992 if (val == 2) {
993 if (check_trip_points(dev, nr) < 0)
994 return -EINVAL;
995 }
996
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100997 /* IT8603E does not have on/off mode */
998 if (val == 0 && data->type == it8603)
999 return -EINVAL;
1000
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001001 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (val == 0) {
1004 int tmp;
1005 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001006 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1007 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* set on/off mode */
1009 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +01001010 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1011 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +01001012 } else {
1013 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001014 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001015 data->pwm_temp_map[nr] :
1016 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001017 else /* Automatic mode */
1018 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1019 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001020
1021 if (data->type != it8603) {
1022 /* set SmartGuardian mode */
1023 data->fan_main_ctrl |= (1 << nr);
1024 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1025 data->fan_main_ctrl);
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001029 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return count;
1031}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001032static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1033 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001035 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1036 int nr = sensor_attr->index;
1037
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001038 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001039 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001041 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return -EINVAL;
1043
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001044 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001045 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001046 /*
1047 * If we are in automatic mode, the PWM duty cycle register
1048 * is read-only so we can't write the value.
1049 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001050 if (data->pwm_ctrl[nr] & 0x80) {
1051 mutex_unlock(&data->update_lock);
1052 return -EBUSY;
1053 }
1054 data->pwm_duty[nr] = pwm_to_reg(data, val);
1055 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
1056 data->pwm_duty[nr]);
1057 } else {
1058 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001059 /*
1060 * If we are in manual mode, write the duty cycle immediately;
1061 * otherwise, just store it for later use.
1062 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001063 if (!(data->pwm_ctrl[nr] & 0x80)) {
1064 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1065 it87_write_value(data, IT87_REG_PWM(nr),
1066 data->pwm_ctrl[nr]);
1067 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001068 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001069 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return count;
1071}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001072static ssize_t set_pwm_freq(struct device *dev,
1073 struct device_attribute *attr, const char *buf, size_t count)
1074{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001075 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001076 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001077 int i;
1078
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001079 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001080 return -EINVAL;
1081
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001082 val = clamp_val(val, 0, 1000000);
1083 val *= has_newer_autopwm(data) ? 256 : 128;
1084
Jean Delvaref8d0c192007-02-14 21:15:02 +01001085 /* Search for the nearest available frequency */
1086 for (i = 0; i < 7; i++) {
1087 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1088 break;
1089 }
1090
1091 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001092 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001093 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001094 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001095 mutex_unlock(&data->update_lock);
1096
1097 return count;
1098}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001099static ssize_t show_pwm_temp_map(struct device *dev,
1100 struct device_attribute *attr, char *buf)
1101{
1102 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1103 int nr = sensor_attr->index;
1104
1105 struct it87_data *data = it87_update_device(dev);
1106 int map;
1107
1108 if (data->pwm_temp_map[nr] < 3)
1109 map = 1 << data->pwm_temp_map[nr];
1110 else
1111 map = 0; /* Should never happen */
1112 return sprintf(buf, "%d\n", map);
1113}
1114static ssize_t set_pwm_temp_map(struct device *dev,
1115 struct device_attribute *attr, const char *buf, size_t count)
1116{
1117 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1118 int nr = sensor_attr->index;
1119
1120 struct it87_data *data = dev_get_drvdata(dev);
1121 long val;
1122 u8 reg;
1123
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001124 /*
1125 * This check can go away if we ever support automatic fan speed
1126 * control on newer chips.
1127 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001128 if (!has_old_autopwm(data)) {
1129 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1130 return -EINVAL;
1131 }
1132
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001133 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001134 return -EINVAL;
1135
1136 switch (val) {
1137 case (1 << 0):
1138 reg = 0x00;
1139 break;
1140 case (1 << 1):
1141 reg = 0x01;
1142 break;
1143 case (1 << 2):
1144 reg = 0x02;
1145 break;
1146 default:
1147 return -EINVAL;
1148 }
1149
1150 mutex_lock(&data->update_lock);
1151 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001152 /*
1153 * If we are in automatic mode, write the temp mapping immediately;
1154 * otherwise, just store it for later use.
1155 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001156 if (data->pwm_ctrl[nr] & 0x80) {
1157 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1158 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1159 }
1160 mutex_unlock(&data->update_lock);
1161 return count;
1162}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001164static ssize_t show_auto_pwm(struct device *dev,
1165 struct device_attribute *attr, char *buf)
1166{
1167 struct it87_data *data = it87_update_device(dev);
1168 struct sensor_device_attribute_2 *sensor_attr =
1169 to_sensor_dev_attr_2(attr);
1170 int nr = sensor_attr->nr;
1171 int point = sensor_attr->index;
1172
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001173 return sprintf(buf, "%d\n",
1174 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001175}
1176
1177static ssize_t set_auto_pwm(struct device *dev,
1178 struct device_attribute *attr, const char *buf, size_t count)
1179{
1180 struct it87_data *data = dev_get_drvdata(dev);
1181 struct sensor_device_attribute_2 *sensor_attr =
1182 to_sensor_dev_attr_2(attr);
1183 int nr = sensor_attr->nr;
1184 int point = sensor_attr->index;
1185 long val;
1186
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001187 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001188 return -EINVAL;
1189
1190 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001191 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001192 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1193 data->auto_pwm[nr][point]);
1194 mutex_unlock(&data->update_lock);
1195 return count;
1196}
1197
1198static ssize_t show_auto_temp(struct device *dev,
1199 struct device_attribute *attr, char *buf)
1200{
1201 struct it87_data *data = it87_update_device(dev);
1202 struct sensor_device_attribute_2 *sensor_attr =
1203 to_sensor_dev_attr_2(attr);
1204 int nr = sensor_attr->nr;
1205 int point = sensor_attr->index;
1206
1207 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1208}
1209
1210static ssize_t set_auto_temp(struct device *dev,
1211 struct device_attribute *attr, const char *buf, size_t count)
1212{
1213 struct it87_data *data = dev_get_drvdata(dev);
1214 struct sensor_device_attribute_2 *sensor_attr =
1215 to_sensor_dev_attr_2(attr);
1216 int nr = sensor_attr->nr;
1217 int point = sensor_attr->index;
1218 long val;
1219
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001220 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001221 return -EINVAL;
1222
1223 mutex_lock(&data->update_lock);
1224 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1225 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1226 data->auto_temp[nr][point]);
1227 mutex_unlock(&data->update_lock);
1228 return count;
1229}
1230
Guenter Roecke1169ba2012-12-19 22:17:01 +01001231static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1232static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1233 0, 1);
1234static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1235 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Guenter Roecke1169ba2012-12-19 22:17:01 +01001237static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1238static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1239 1, 1);
1240static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1241 set_fan_div, 1);
1242
1243static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1244static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1245 2, 1);
1246static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1247 set_fan_div, 2);
1248
1249static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1250static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1251 3, 1);
1252
1253static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1254static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1255 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Guenter Roeckc4458db2012-12-19 22:17:02 +01001257static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1258 show_pwm_enable, set_pwm_enable, 0);
1259static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1260static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1261static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1262 show_pwm_temp_map, set_pwm_temp_map, 0);
1263static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1264 show_auto_pwm, set_auto_pwm, 0, 0);
1265static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1266 show_auto_pwm, set_auto_pwm, 0, 1);
1267static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1268 show_auto_pwm, set_auto_pwm, 0, 2);
1269static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1270 show_auto_pwm, NULL, 0, 3);
1271static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1272 show_auto_temp, set_auto_temp, 0, 1);
1273static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1274 show_auto_temp, set_auto_temp, 0, 0);
1275static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1276 show_auto_temp, set_auto_temp, 0, 2);
1277static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1278 show_auto_temp, set_auto_temp, 0, 3);
1279static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1280 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Guenter Roeckc4458db2012-12-19 22:17:02 +01001282static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1283 show_pwm_enable, set_pwm_enable, 1);
1284static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1285static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1286static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1287 show_pwm_temp_map, set_pwm_temp_map, 1);
1288static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1289 show_auto_pwm, set_auto_pwm, 1, 0);
1290static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1291 show_auto_pwm, set_auto_pwm, 1, 1);
1292static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1293 show_auto_pwm, set_auto_pwm, 1, 2);
1294static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1295 show_auto_pwm, NULL, 1, 3);
1296static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1297 show_auto_temp, set_auto_temp, 1, 1);
1298static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1299 show_auto_temp, set_auto_temp, 1, 0);
1300static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1301 show_auto_temp, set_auto_temp, 1, 2);
1302static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1303 show_auto_temp, set_auto_temp, 1, 3);
1304static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1305 show_auto_temp, set_auto_temp, 1, 4);
1306
1307static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1308 show_pwm_enable, set_pwm_enable, 2);
1309static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1310static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1311static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1312 show_pwm_temp_map, set_pwm_temp_map, 2);
1313static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1314 show_auto_pwm, set_auto_pwm, 2, 0);
1315static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1316 show_auto_pwm, set_auto_pwm, 2, 1);
1317static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1318 show_auto_pwm, set_auto_pwm, 2, 2);
1319static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1320 show_auto_pwm, NULL, 2, 3);
1321static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1322 show_auto_temp, set_auto_temp, 2, 1);
1323static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1324 show_auto_temp, set_auto_temp, 2, 0);
1325static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1326 show_auto_temp, set_auto_temp, 2, 2);
1327static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1328 show_auto_temp, set_auto_temp, 2, 3);
1329static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1330 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001333static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1334 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001337 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
Jean Delvare1d66c642005-04-18 21:16:59 -07001339static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Jean Delvare0124dd72007-11-25 16:16:41 +01001341static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1342 char *buf)
1343{
1344 int bitnr = to_sensor_dev_attr(attr)->index;
1345 struct it87_data *data = it87_update_device(dev);
1346 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1347}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001348
1349static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1350 *attr, const char *buf, size_t count)
1351{
1352 struct it87_data *data = dev_get_drvdata(dev);
1353 long val;
1354 int config;
1355
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001356 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001357 return -EINVAL;
1358
1359 mutex_lock(&data->update_lock);
1360 config = it87_read_value(data, IT87_REG_CONFIG);
1361 if (config < 0) {
1362 count = config;
1363 } else {
1364 config |= 1 << 5;
1365 it87_write_value(data, IT87_REG_CONFIG, config);
1366 /* Invalidate cache to force re-read */
1367 data->valid = 0;
1368 }
1369 mutex_unlock(&data->update_lock);
1370
1371 return count;
1372}
1373
Jean Delvare0124dd72007-11-25 16:16:41 +01001374static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1375static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1376static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1377static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1378static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1379static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1380static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1381static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1382static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1383static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1384static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1385static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1386static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1387static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1388static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1389static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001390static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1391 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001392
Jean Delvared9b327c2010-03-05 22:17:17 +01001393static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1394 char *buf)
1395{
1396 int bitnr = to_sensor_dev_attr(attr)->index;
1397 struct it87_data *data = it87_update_device(dev);
1398 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1399}
1400static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1401 const char *buf, size_t count)
1402{
1403 int bitnr = to_sensor_dev_attr(attr)->index;
1404 struct it87_data *data = dev_get_drvdata(dev);
1405 long val;
1406
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001407 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001408 || (val != 0 && val != 1))
1409 return -EINVAL;
1410
1411 mutex_lock(&data->update_lock);
1412 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1413 if (val)
1414 data->beeps |= (1 << bitnr);
1415 else
1416 data->beeps &= ~(1 << bitnr);
1417 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1418 mutex_unlock(&data->update_lock);
1419 return count;
1420}
1421
1422static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1423 show_beep, set_beep, 1);
1424static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1425static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1426static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1427static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1428static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1429static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1430static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1431/* fanX_beep writability is set later */
1432static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1433static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1434static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1435static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1436static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1437static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1438 show_beep, set_beep, 2);
1439static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1440static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1441
Jean Delvare5f2dc792010-03-05 22:17:18 +01001442static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1443 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Jean Delvare90d66192007-10-08 18:24:35 +02001445 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001446 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001448static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1449 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001451 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001452 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001454 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001455 return -EINVAL;
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 data->vrm = val;
1458
1459 return count;
1460}
1461static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Jean Delvare5f2dc792010-03-05 22:17:18 +01001463static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1464 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 struct it87_data *data = it87_update_device(dev);
1467 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1468}
1469static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001470
Jean Delvare738e5e02010-08-14 21:08:50 +02001471static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1472 char *buf)
1473{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001474 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001475 "+5V",
1476 "5VSB",
1477 "Vbat",
1478 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001479 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001480 "+3.3V",
1481 "3VSB",
1482 "Vbat",
1483 };
1484 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001485 int nr = to_sensor_dev_attr(attr)->index;
1486
Jean Delvare16b5dda2012-01-16 22:51:48 +01001487 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1488 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001489}
1490static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1491static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1492static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Rudolf Marek7183ae82014-04-04 18:01:35 +02001493/* special AVCC3 IT8603E in9 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001494static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001495
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001496static ssize_t show_name(struct device *dev, struct device_attribute
1497 *devattr, char *buf)
1498{
1499 struct it87_data *data = dev_get_drvdata(dev);
1500 return sprintf(buf, "%s\n", data->name);
1501}
1502static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1503
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001504static struct attribute *it87_attributes_in[10][5] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001505{
Jean Delvare87808be2006-09-24 21:17:13 +02001506 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001507 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001508 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001509 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001510 NULL
1511}, {
1512 &sensor_dev_attr_in1_input.dev_attr.attr,
1513 &sensor_dev_attr_in1_min.dev_attr.attr,
1514 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001515 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001516 NULL
1517}, {
1518 &sensor_dev_attr_in2_input.dev_attr.attr,
1519 &sensor_dev_attr_in2_min.dev_attr.attr,
1520 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001521 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001522 NULL
1523}, {
1524 &sensor_dev_attr_in3_input.dev_attr.attr,
1525 &sensor_dev_attr_in3_min.dev_attr.attr,
1526 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001527 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001528 NULL
1529}, {
1530 &sensor_dev_attr_in4_input.dev_attr.attr,
1531 &sensor_dev_attr_in4_min.dev_attr.attr,
1532 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001533 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001534 NULL
1535}, {
1536 &sensor_dev_attr_in5_input.dev_attr.attr,
1537 &sensor_dev_attr_in5_min.dev_attr.attr,
1538 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001539 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001540 NULL
1541}, {
1542 &sensor_dev_attr_in6_input.dev_attr.attr,
1543 &sensor_dev_attr_in6_min.dev_attr.attr,
1544 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001545 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001546 NULL
1547}, {
1548 &sensor_dev_attr_in7_input.dev_attr.attr,
1549 &sensor_dev_attr_in7_min.dev_attr.attr,
1550 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001551 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001552 NULL
1553}, {
1554 &sensor_dev_attr_in8_input.dev_attr.attr,
1555 NULL
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001556}, {
1557 &sensor_dev_attr_in9_input.dev_attr.attr,
1558 NULL
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001559} };
Jean Delvare87808be2006-09-24 21:17:13 +02001560
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001561static const struct attribute_group it87_group_in[10] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001562 { .attrs = it87_attributes_in[0] },
1563 { .attrs = it87_attributes_in[1] },
1564 { .attrs = it87_attributes_in[2] },
1565 { .attrs = it87_attributes_in[3] },
1566 { .attrs = it87_attributes_in[4] },
1567 { .attrs = it87_attributes_in[5] },
1568 { .attrs = it87_attributes_in[6] },
1569 { .attrs = it87_attributes_in[7] },
1570 { .attrs = it87_attributes_in[8] },
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001571 { .attrs = it87_attributes_in[9] },
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001572};
1573
Guenter Roeck4573acb2012-03-26 16:17:41 -07001574static struct attribute *it87_attributes_temp[3][6] = {
1575{
Jean Delvare87808be2006-09-24 21:17:13 +02001576 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001577 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001578 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001579 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001580 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001581 NULL
1582} , {
1583 &sensor_dev_attr_temp2_input.dev_attr.attr,
1584 &sensor_dev_attr_temp2_max.dev_attr.attr,
1585 &sensor_dev_attr_temp2_min.dev_attr.attr,
1586 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001587 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001588 NULL
1589} , {
1590 &sensor_dev_attr_temp3_input.dev_attr.attr,
1591 &sensor_dev_attr_temp3_max.dev_attr.attr,
1592 &sensor_dev_attr_temp3_min.dev_attr.attr,
1593 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001594 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001595 NULL
1596} };
Jean Delvare87808be2006-09-24 21:17:13 +02001597
Guenter Roeck4573acb2012-03-26 16:17:41 -07001598static const struct attribute_group it87_group_temp[3] = {
1599 { .attrs = it87_attributes_temp[0] },
1600 { .attrs = it87_attributes_temp[1] },
1601 { .attrs = it87_attributes_temp[2] },
1602};
1603
Guenter Roeck161d8982012-12-19 22:17:01 +01001604static struct attribute *it87_attributes_temp_offset[] = {
1605 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1606 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1607 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1608};
1609
Guenter Roeck4573acb2012-03-26 16:17:41 -07001610static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001611 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001612 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001613 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001614 NULL
1615};
1616
1617static const struct attribute_group it87_group = {
1618 .attrs = it87_attributes,
1619};
1620
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001621static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001622 &sensor_dev_attr_in0_beep.dev_attr.attr,
1623 &sensor_dev_attr_in1_beep.dev_attr.attr,
1624 &sensor_dev_attr_in2_beep.dev_attr.attr,
1625 &sensor_dev_attr_in3_beep.dev_attr.attr,
1626 &sensor_dev_attr_in4_beep.dev_attr.attr,
1627 &sensor_dev_attr_in5_beep.dev_attr.attr,
1628 &sensor_dev_attr_in6_beep.dev_attr.attr,
1629 &sensor_dev_attr_in7_beep.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001630 NULL,
1631 NULL,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001632};
Jean Delvared9b327c2010-03-05 22:17:17 +01001633
Guenter Roeck4573acb2012-03-26 16:17:41 -07001634static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001635 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1636 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1637 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001638};
1639
Guenter Roecke1169ba2012-12-19 22:17:01 +01001640static struct attribute *it87_attributes_fan[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001641 &sensor_dev_attr_fan1_input.dev_attr.attr,
1642 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001643 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1644 NULL
1645}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001646 &sensor_dev_attr_fan2_input.dev_attr.attr,
1647 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001648 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1649 NULL
1650}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001651 &sensor_dev_attr_fan3_input.dev_attr.attr,
1652 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001653 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001654 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001655}, {
1656 &sensor_dev_attr_fan4_input.dev_attr.attr,
1657 &sensor_dev_attr_fan4_min.dev_attr.attr,
1658 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1659 NULL
1660}, {
1661 &sensor_dev_attr_fan5_input.dev_attr.attr,
1662 &sensor_dev_attr_fan5_min.dev_attr.attr,
1663 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1664 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001665} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001666
Guenter Roecke1169ba2012-12-19 22:17:01 +01001667static const struct attribute_group it87_group_fan[5] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001668 { .attrs = it87_attributes_fan[0] },
1669 { .attrs = it87_attributes_fan[1] },
1670 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001671 { .attrs = it87_attributes_fan[3] },
1672 { .attrs = it87_attributes_fan[4] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001673};
1674
Guenter Roecke1169ba2012-12-19 22:17:01 +01001675static const struct attribute *it87_attributes_fan_div[] = {
1676 &sensor_dev_attr_fan1_div.dev_attr.attr,
1677 &sensor_dev_attr_fan2_div.dev_attr.attr,
1678 &sensor_dev_attr_fan3_div.dev_attr.attr,
1679};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001680
1681static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001682 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001683 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001684 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001685 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001686 NULL
1687}, {
1688 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1689 &sensor_dev_attr_pwm2.dev_attr.attr,
1690 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001691 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001692 NULL
1693}, {
1694 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1695 &sensor_dev_attr_pwm3.dev_attr.attr,
1696 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001697 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001698 NULL
1699} };
Jean Delvare87808be2006-09-24 21:17:13 +02001700
Jean Delvare723a0aa2010-03-05 22:17:16 +01001701static const struct attribute_group it87_group_pwm[3] = {
1702 { .attrs = it87_attributes_pwm[0] },
1703 { .attrs = it87_attributes_pwm[1] },
1704 { .attrs = it87_attributes_pwm[2] },
1705};
1706
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001707static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1708 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1709 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1710 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1711 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1712 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1713 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1714 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1715 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1716 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1717 NULL
1718}, {
1719 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1720 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1721 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1722 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1723 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1724 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1725 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1726 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1727 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1728 NULL
1729}, {
1730 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1731 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1732 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1733 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1734 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1735 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1736 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1737 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1738 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1739 NULL
1740} };
1741
1742static const struct attribute_group it87_group_autopwm[3] = {
1743 { .attrs = it87_attributes_autopwm[0] },
1744 { .attrs = it87_attributes_autopwm[1] },
1745 { .attrs = it87_attributes_autopwm[2] },
1746};
1747
Jean Delvared9b327c2010-03-05 22:17:17 +01001748static struct attribute *it87_attributes_fan_beep[] = {
1749 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1750 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1751 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1752 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1753 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1754};
1755
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001756static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001757 &dev_attr_vrm.attr,
1758 &dev_attr_cpu0_vid.attr,
1759 NULL
1760};
1761
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001762static const struct attribute_group it87_group_vid = {
1763 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001764};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Jean Delvare738e5e02010-08-14 21:08:50 +02001766static struct attribute *it87_attributes_label[] = {
1767 &sensor_dev_attr_in3_label.dev_attr.attr,
1768 &sensor_dev_attr_in7_label.dev_attr.attr,
1769 &sensor_dev_attr_in8_label.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001770 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare738e5e02010-08-14 21:08:50 +02001771 NULL
1772};
1773
1774static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001775 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001776};
1777
Jean Delvare2d8672c2005-07-19 23:56:35 +02001778/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001779static int __init it87_find(unsigned short *address,
1780 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001782 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001783 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001784 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001786 err = superio_enter();
1787 if (err)
1788 return err;
1789
1790 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001791 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001792
1793 switch (chip_type) {
1794 case IT8705F_DEVID:
1795 sio_data->type = it87;
1796 break;
1797 case IT8712F_DEVID:
1798 sio_data->type = it8712;
1799 break;
1800 case IT8716F_DEVID:
1801 case IT8726F_DEVID:
1802 sio_data->type = it8716;
1803 break;
1804 case IT8718F_DEVID:
1805 sio_data->type = it8718;
1806 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001807 case IT8720F_DEVID:
1808 sio_data->type = it8720;
1809 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001810 case IT8721F_DEVID:
1811 sio_data->type = it8721;
1812 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001813 case IT8728F_DEVID:
1814 sio_data->type = it8728;
1815 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06001816 case IT8771E_DEVID:
1817 sio_data->type = it8771;
1818 break;
1819 case IT8772E_DEVID:
1820 sio_data->type = it8772;
1821 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08001822 case IT8781F_DEVID:
1823 sio_data->type = it8781;
1824 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001825 case IT8782F_DEVID:
1826 sio_data->type = it8782;
1827 break;
1828 case IT8783E_DEVID:
1829 sio_data->type = it8783;
1830 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001831 case IT8786E_DEVID:
1832 sio_data->type = it8786;
1833 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02001834 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02001835 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001836 sio_data->type = it8603;
1837 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001838 case 0xffff: /* No device at all */
1839 goto exit;
1840 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001841 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001842 goto exit;
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Jean Delvare87673dd2006-08-28 14:37:19 +02001845 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001847 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 goto exit;
1849 }
1850
1851 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1852 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001853 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 goto exit;
1855 }
1856
1857 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001858 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07001859 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
1860 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001861 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Jean Delvare738e5e02010-08-14 21:08:50 +02001863 /* in8 (Vbat) is always internal */
1864 sio_data->internal = (1 << 2);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001865 /* Only the IT8603E has in9 */
1866 if (sio_data->type != it8603)
1867 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02001868
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001869 if (!(it87_devices[sio_data->type].features & FEAT_VID))
Jean Delvare895ff262009-12-09 20:35:47 +01001870 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001871
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001872 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1873 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01001874 /* The IT8705F has a different LD number for GPIO */
1875 superio_select(5);
1876 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08001877 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001878 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08001879
Guenter Roeck0531d982012-03-02 11:46:44 -08001880 superio_select(GPIO);
1881
1882 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1883 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001884 reg2a = superio_inb(IT87_SIO_PINX1_REG);
1885 reg2c = superio_inb(IT87_SIO_PINX2_REG);
1886 regef = superio_inb(IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001887
Guenter Roeck0531d982012-03-02 11:46:44 -08001888 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001889 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08001890 sio_data->skip_fan |= (1 << 2);
1891 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001892 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08001893 sio_data->skip_pwm |= (1 << 2);
1894
1895 /* Check if fan2 is there or not */
1896 if (reg27 & (1 << 7))
1897 sio_data->skip_fan |= (1 << 1);
1898 if (reg27 & (1 << 3))
1899 sio_data->skip_pwm |= (1 << 1);
1900
1901 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001902 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001903 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001904
1905 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001906 if (reg27 & (1 << 1))
1907 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001908
1909 /*
1910 * VIN7
1911 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1912 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001913 if (reg27 & (1 << 2)) {
1914 /*
1915 * The data sheet is a bit unclear regarding the
1916 * internal voltage divider for VCCH5V. It says
1917 * "This bit enables and switches VIN7 (pin 91) to the
1918 * internal voltage divider for VCCH5V".
1919 * This is different to other chips, where the internal
1920 * voltage divider would connect VIN7 to an internal
1921 * voltage source. Maybe that is the case here as well.
1922 *
1923 * Since we don't know for sure, re-route it if that is
1924 * not the case, and ask the user to report if the
1925 * resulting voltage is sane.
1926 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001927 if (!(reg2c & (1 << 1))) {
1928 reg2c |= (1 << 1);
1929 superio_outb(IT87_SIO_PINX2_REG, reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001930 pr_notice("Routing internal VCCH5V to in7.\n");
1931 }
1932 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1933 pr_notice("Please report if it displays a reasonable voltage.\n");
1934 }
Guenter Roeck0531d982012-03-02 11:46:44 -08001935
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001936 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08001937 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001938 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08001939 sio_data->internal |= (1 << 1);
1940
1941 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001942 } else if (sio_data->type == it8603) {
1943 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08001944
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001945 superio_select(GPIO);
1946
1947 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1948
1949 /* Check if fan3 is there or not */
1950 if (reg27 & (1 << 6))
1951 sio_data->skip_pwm |= (1 << 2);
1952 if (reg27 & (1 << 7))
1953 sio_data->skip_fan |= (1 << 2);
1954
1955 /* Check if fan2 is there or not */
1956 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
1957 if (reg29 & (1 << 1))
1958 sio_data->skip_pwm |= (1 << 1);
1959 if (reg29 & (1 << 2))
1960 sio_data->skip_fan |= (1 << 1);
1961
1962 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1963 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1964
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001965 sio_data->internal |= (1 << 1); /* in7 is VSB */
1966 sio_data->internal |= (1 << 3); /* in9 is AVCC */
1967
1968 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01001969 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001970 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001971 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02001972
1973 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001974
Jean Delvare895ff262009-12-09 20:35:47 +01001975 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001976 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001977 /* We need at least 4 VID pins */
1978 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001979 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001980 sio_data->skip_vid = 1;
1981 }
Jean Delvare895ff262009-12-09 20:35:47 +01001982 }
1983
Jean Delvare591ec652009-12-09 20:35:48 +01001984 /* Check if fan3 is there or not */
1985 if (reg & (1 << 6))
1986 sio_data->skip_pwm |= (1 << 2);
1987 if (reg & (1 << 7))
1988 sio_data->skip_fan |= (1 << 2);
1989
1990 /* Check if fan2 is there or not */
1991 reg = superio_inb(IT87_SIO_GPIO5_REG);
1992 if (reg & (1 << 1))
1993 sio_data->skip_pwm |= (1 << 1);
1994 if (reg & (1 << 2))
1995 sio_data->skip_fan |= (1 << 1);
1996
Jean Delvare895ff262009-12-09 20:35:47 +01001997 if ((sio_data->type == it8718 || sio_data->type == it8720)
1998 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001999 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002000
2001 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002002
2003 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2004
Jean Delvare436cad22010-07-09 16:22:48 +02002005 /*
2006 * The IT8720F has no VIN7 pin, so VCCH should always be
2007 * routed internally to VIN7 with an internal divider.
2008 * Curiously, there still is a configuration bit to control
2009 * this, which means it can be set incorrectly. And even
2010 * more curiously, many boards out there are improperly
2011 * configured, even though the IT8720F datasheet claims
2012 * that the internal routing of VCCH to VIN7 is the default
2013 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002014 *
2015 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002016 * If UART6 is enabled, re-route VIN7 to the internal divider
2017 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002018 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002019 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02002020 reg |= (1 << 1);
2021 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002022 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002023 }
Jean Delvare87673dd2006-08-28 14:37:19 +02002024 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02002025 sio_data->internal |= (1 << 0);
Jean Delvare16b5dda2012-01-16 22:51:48 +01002026 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002027 sio_data->type == it8728 || sio_data->type == it8771 ||
2028 sio_data->type == it8772 || sio_data->type == it8786)
Jean Delvare738e5e02010-08-14 21:08:50 +02002029 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002030
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002031 /*
2032 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2033 * While VIN7 can be routed to the internal voltage divider,
2034 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002035 *
2036 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2037 * is the temperature source. Since we can not read the
2038 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002039 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002040 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002041 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002042 sio_data->skip_temp |= (1 << 2);
2043 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002044
Jean Delvared9b327c2010-03-05 22:17:17 +01002045 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002046 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002047 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002048 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002049
Jean Delvare98dd22c2008-10-09 15:33:58 +02002050 /* Disable specific features based on DMI strings */
2051 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2052 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2053 if (board_vendor && board_name) {
2054 if (strcmp(board_vendor, "nVIDIA") == 0
2055 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002056 /*
2057 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2058 * connected to a fan, but to something else. One user
2059 * has reported instant system power-off when changing
2060 * the PWM2 duty cycle, so we disable it.
2061 * I use the board name string as the trigger in case
2062 * the same board is ever used in other systems.
2063 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002064 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002065 sio_data->skip_pwm = (1 << 1);
2066 }
2067 }
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069exit:
2070 superio_exit();
2071 return err;
2072}
2073
Jean Delvare723a0aa2010-03-05 22:17:16 +01002074static void it87_remove_files(struct device *dev)
2075{
2076 struct it87_data *data = platform_get_drvdata(pdev);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002077 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002078 int i;
2079
2080 sysfs_remove_group(&dev->kobj, &it87_group);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002081 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002082 if (sio_data->skip_in & (1 << i))
2083 continue;
2084 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2085 if (it87_attributes_in_beep[i])
2086 sysfs_remove_file(&dev->kobj,
2087 it87_attributes_in_beep[i]);
2088 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002089 for (i = 0; i < 3; i++) {
2090 if (!(data->has_temp & (1 << i)))
2091 continue;
2092 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01002093 if (has_temp_offset(data))
2094 sysfs_remove_file(&dev->kobj,
2095 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002096 if (sio_data->beep_pin)
2097 sysfs_remove_file(&dev->kobj,
2098 it87_attributes_temp_beep[i]);
2099 }
Jean Delvare723a0aa2010-03-05 22:17:16 +01002100 for (i = 0; i < 5; i++) {
2101 if (!(data->has_fan & (1 << i)))
2102 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002103 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002104 if (sio_data->beep_pin)
2105 sysfs_remove_file(&dev->kobj,
2106 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002107 if (i < 3 && !has_16bit_fans(data))
2108 sysfs_remove_file(&dev->kobj,
2109 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002110 }
2111 for (i = 0; i < 3; i++) {
Guenter Roeck1696d1d2015-03-27 06:03:41 -07002112 if (sio_data->skip_pwm & (1 << i))
Jean Delvare723a0aa2010-03-05 22:17:16 +01002113 continue;
2114 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002115 if (has_old_autopwm(data))
2116 sysfs_remove_group(&dev->kobj,
2117 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002118 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002119 if (!sio_data->skip_vid)
2120 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02002121 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002122}
2123
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002124static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002127 struct resource *res;
2128 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002129 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002130 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01002132 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002134 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002135 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2136 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002137 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2138 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002139 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002140 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Guenter Roeck62a1d052012-03-24 21:54:41 -07002143 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2144 if (!data)
2145 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002147 data->addr = res->start;
2148 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002149 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002150 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002151 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002152 data->name = it87_devices[sio_data->type].name;
2153 /*
2154 * IT8705F Datasheet 0.4.1, 3h == Version G.
2155 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2156 * These are the first revisions with 16-bit tachometer support.
2157 */
2158 switch (data->type) {
2159 case it87:
2160 if (sio_data->revision >= 0x03) {
2161 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002162 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002163 }
2164 break;
2165 case it8712:
2166 if (sio_data->revision >= 0x08) {
2167 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002168 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2169 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002170 }
2171 break;
2172 default:
2173 break;
2174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002177 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002178 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2179 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002181 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002183 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002186 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002188 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002189 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002190 if (sio_data->internal & (1 << 0))
2191 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2192 if (sio_data->internal & (1 << 1))
2193 data->in_scaled |= (1 << 7); /* in7 is VSB */
2194 if (sio_data->internal & (1 << 2))
2195 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002196 if (sio_data->internal & (1 << 3))
2197 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002198 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2199 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002200 if (sio_data->internal & (1 << 0))
2201 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2202 if (sio_data->internal & (1 << 1))
2203 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002204 }
2205
Guenter Roeck4573acb2012-03-26 16:17:41 -07002206 data->has_temp = 0x07;
2207 if (sio_data->skip_temp & (1 << 2)) {
2208 if (sio_data->type == it8782
2209 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2210 data->has_temp &= ~(1 << 2);
2211 }
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002214 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002217 err = sysfs_create_group(&dev->kobj, &it87_group);
2218 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002219 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002220
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002221 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002222 if (sio_data->skip_in & (1 << i))
2223 continue;
2224 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2225 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002226 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002227 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2228 err = sysfs_create_file(&dev->kobj,
2229 it87_attributes_in_beep[i]);
2230 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002231 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002232 }
2233 }
2234
Guenter Roeck4573acb2012-03-26 16:17:41 -07002235 for (i = 0; i < 3; i++) {
2236 if (!(data->has_temp & (1 << i)))
2237 continue;
2238 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002239 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002240 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002241 if (has_temp_offset(data)) {
2242 err = sysfs_create_file(&dev->kobj,
2243 it87_attributes_temp_offset[i]);
2244 if (err)
2245 goto error;
2246 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002247 if (sio_data->beep_pin) {
2248 err = sysfs_create_file(&dev->kobj,
2249 it87_attributes_temp_beep[i]);
2250 if (err)
2251 goto error;
2252 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002253 }
2254
Jean Delvare9060f8b2006-08-28 14:24:17 +02002255 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002256 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01002257 for (i = 0; i < 5; i++) {
2258 if (!(data->has_fan & (1 << i)))
2259 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002260 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002261 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002262 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002263
Guenter Roecke1169ba2012-12-19 22:17:01 +01002264 if (i < 3 && !has_16bit_fans(data)) {
2265 err = sysfs_create_file(&dev->kobj,
2266 it87_attributes_fan_div[i]);
2267 if (err)
2268 goto error;
2269 }
2270
Jean Delvared9b327c2010-03-05 22:17:17 +01002271 if (sio_data->beep_pin) {
2272 err = sysfs_create_file(&dev->kobj,
2273 it87_attributes_fan_beep[i]);
2274 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002275 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002276 if (!fan_beep_need_rw)
2277 continue;
2278
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002279 /*
2280 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002281 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002282 * for it.
2283 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002284 if (sysfs_chmod_file(&dev->kobj,
2285 it87_attributes_fan_beep[i],
2286 S_IRUGO | S_IWUSR))
2287 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2288 i + 1);
2289 fan_beep_need_rw = 0;
2290 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002291 }
2292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002294 for (i = 0; i < 3; i++) {
2295 if (sio_data->skip_pwm & (1 << i))
2296 continue;
2297 err = sysfs_create_group(&dev->kobj,
2298 &it87_group_pwm[i]);
2299 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002300 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002301
2302 if (!has_old_autopwm(data))
2303 continue;
2304 err = sysfs_create_group(&dev->kobj,
2305 &it87_group_autopwm[i]);
2306 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002307 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310
Jean Delvare895ff262009-12-09 20:35:47 +01002311 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002312 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002313 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002314 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002315 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2316 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002317 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002318 }
2319
Jean Delvare738e5e02010-08-14 21:08:50 +02002320 /* Export labels for internal sensors */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002321 for (i = 0; i < 4; i++) {
Jean Delvare738e5e02010-08-14 21:08:50 +02002322 if (!(sio_data->internal & (1 << i)))
2323 continue;
2324 err = sysfs_create_file(&dev->kobj,
2325 it87_attributes_label[i]);
2326 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002327 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002328 }
2329
Tony Jones1beeffe2007-08-20 13:46:20 -07002330 data->hwmon_dev = hwmon_device_register(dev);
2331 if (IS_ERR(data->hwmon_dev)) {
2332 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002333 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335
2336 return 0;
2337
Guenter Roeck62a1d052012-03-24 21:54:41 -07002338error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002339 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return err;
2341}
2342
Bill Pemberton281dfd02012-11-19 13:25:51 -05002343static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002345 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Tony Jones1beeffe2007-08-20 13:46:20 -07002347 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002348 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return 0;
2351}
2352
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002353/*
2354 * Must be called with data->update_lock held, except during initialization.
2355 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2356 * would slow down the IT87 access and should not be necessary.
2357 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002358static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002360 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2361 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
2363
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002364/*
2365 * Must be called with data->update_lock held, except during initialization.
2366 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2367 * would slow down the IT87 access and should not be necessary.
2368 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002369static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002371 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2372 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
2375/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002376static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002378 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002379 /*
2380 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002382 * disable pwm control to protect the user.
2383 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002384 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if ((tmp & 0x87) == 0) {
2386 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002387 /*
2388 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002390 * inverting all fan speed values.
2391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 int i;
2393 u8 pwm[3];
2394
2395 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002396 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 IT87_REG_PWM(i));
2398
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002399 /*
2400 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 * might be correct, as suspicious as it seems, so we
2402 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002403 * PWM interface).
2404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002406 dev_info(dev,
2407 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002408 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 tmp | 0x87);
2410 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002411 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 IT87_REG_PWM(i),
2413 0x7f & ~pwm[i]);
2414 return 1;
2415 }
2416
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002417 dev_info(dev,
2418 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002421 dev_info(dev,
2422 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return 0;
2424 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002425 dev_info(dev,
2426 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428
2429 return 1;
2430}
2431
2432/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002433static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002435 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002436 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002438 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002440 /*
2441 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002442 * - If it is in automatic mode, setting to manual mode should set
2443 * the fan to full speed by default.
2444 * - If it is in manual mode, we need a mapping to temperature
2445 * channels to use when later setting to automatic mode later.
2446 * Use a 1:1 mapping by default (we are clueless.)
2447 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002448 * prior to switching to a different mode.
2449 * Note that this is no longer needed for the IT8721F and later, as
2450 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002451 * manual duty cycle.
2452 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002454 data->pwm_temp_map[i] = i;
2455 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002456 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
2458
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002459 /*
2460 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002461 * registers. For low voltage limits it makes no sense and triggers
2462 * alarms, so change to 0 instead. For high temperature limits, it
2463 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002464 * but is still confusing, so change to 127 degrees C.
2465 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002466 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002467 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002468 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002469 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002470 }
2471 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002472 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002473 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002474 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002475 }
2476
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002477 /*
2478 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002479 * set to two different sensor types and we can't guess which one
2480 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002481 * run-time through the temp{1-3}_type sysfs accessors if needed.
2482 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002485 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if ((tmp & 0xff) == 0) {
2487 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002488 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
2490
2491 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002492 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002493 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002494 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002496 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002497 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2498 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002500 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002502 /* Set tachometers to 16-bit mode if needed */
2503 if (has_fan16_config(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002504 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002505 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002506 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002507 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002508 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002509 tmp | 0x07);
2510 }
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002511 }
2512
2513 /* Check for additional fans */
2514 if (has_five_fans(data)) {
2515 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2516 if (tmp & (1 << 4))
2517 data->has_fan |= (1 << 3); /* fan4 enabled */
2518 if (tmp & (1 << 5))
2519 data->has_fan |= (1 << 4); /* fan5 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02002520 }
2521
Jean Delvare591ec652009-12-09 20:35:48 +01002522 /* Fan input pins may be used for alternative functions */
2523 data->has_fan &= ~sio_data->skip_fan;
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002526 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002527 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 | (update_vbat ? 0x41 : 0x01));
2529}
2530
Jean Delvareb99883d2010-03-05 22:17:15 +01002531static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2532{
2533 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002534 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002535 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002536 data->pwm_duty[nr] = it87_read_value(data,
2537 IT87_REG_PWM_DUTY(nr));
2538 } else {
2539 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2540 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2541 else /* Manual mode */
2542 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2543 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002544
2545 if (has_old_autopwm(data)) {
2546 int i;
2547
2548 for (i = 0; i < 5 ; i++)
2549 data->auto_temp[nr][i] = it87_read_value(data,
2550 IT87_REG_AUTO_TEMP(nr, i));
2551 for (i = 0; i < 3 ; i++)
2552 data->auto_pwm[nr][i] = it87_read_value(data,
2553 IT87_REG_AUTO_PWM(nr, i));
2554 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002555}
2556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557static struct it87_data *it87_update_device(struct device *dev)
2558{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002559 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 int i;
2561
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002562 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
2564 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2565 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002567 /*
2568 * Cleared after each update, so reenable. Value
2569 * returned by this read will be previous value
2570 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002571 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002572 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
2574 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002575 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002576 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002577 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002578 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002579 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002580 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
Jean Delvare3543a532006-08-28 14:27:25 +02002582 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002583 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002584 if (data->type == it8603)
2585 data->in[9][0] = it87_read_value(data, 0x2f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Jean Delvarec7f1f712007-09-03 17:11:46 +02002587 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002588 /* Skip disabled fans */
2589 if (!(data->has_fan & (1 << i)))
2590 continue;
2591
Guenter Roecke1169ba2012-12-19 22:17:01 +01002592 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002593 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002594 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002595 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002596 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002597 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002598 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002599 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002600 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002601 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
2604 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002605 if (!(data->has_temp & (1 << i)))
2606 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002607 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002608 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002609 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002610 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002611 data->temp[i][2] =
2612 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002613 if (has_temp_offset(data))
2614 data->temp[i][3] =
2615 it87_read_value(data,
2616 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618
Jean Delvare17d648b2006-08-28 14:23:46 +02002619 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002620 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002621 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002622 data->fan_div[0] = i & 0x07;
2623 data->fan_div[1] = (i >> 3) & 0x07;
2624 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002628 it87_read_value(data, IT87_REG_ALARM1) |
2629 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2630 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002631 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002632
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002633 data->fan_main_ctrl = it87_read_value(data,
2634 IT87_REG_FAN_MAIN_CTRL);
2635 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002636 for (i = 0; i < 3; i++)
2637 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002639 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002640 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002641 /*
2642 * The IT8705F does not have VID capability.
2643 * The IT8718F and later don't use IT87_REG_VID for the
2644 * same purpose.
2645 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002646 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002647 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002648 /*
2649 * The older IT8712F revisions had only 5 VID pins,
2650 * but we assume it is always safe to read 6 bits.
2651 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002652 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654 data->last_updated = jiffies;
2655 data->valid = 1;
2656 }
2657
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002658 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 return data;
2661}
2662
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002663static int __init it87_device_add(unsigned short address,
2664 const struct it87_sio_data *sio_data)
2665{
2666 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002667 .start = address + IT87_EC_OFFSET,
2668 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002669 .name = DRVNAME,
2670 .flags = IORESOURCE_IO,
2671 };
2672 int err;
2673
Jean Delvareb9acb642009-01-07 16:37:35 +01002674 err = acpi_check_resource_conflict(&res);
2675 if (err)
2676 goto exit;
2677
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002678 pdev = platform_device_alloc(DRVNAME, address);
2679 if (!pdev) {
2680 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002681 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002682 goto exit;
2683 }
2684
2685 err = platform_device_add_resources(pdev, &res, 1);
2686 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002687 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002688 goto exit_device_put;
2689 }
2690
2691 err = platform_device_add_data(pdev, sio_data,
2692 sizeof(struct it87_sio_data));
2693 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002694 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002695 goto exit_device_put;
2696 }
2697
2698 err = platform_device_add(pdev);
2699 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002700 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002701 goto exit_device_put;
2702 }
2703
2704 return 0;
2705
2706exit_device_put:
2707 platform_device_put(pdev);
2708exit:
2709 return err;
2710}
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712static int __init sm_it87_init(void)
2713{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002714 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002715 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002716 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002717
Jean Delvare98dd22c2008-10-09 15:33:58 +02002718 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002719 err = it87_find(&isa_address, &sio_data);
2720 if (err)
2721 return err;
2722 err = platform_driver_register(&it87_driver);
2723 if (err)
2724 return err;
2725
2726 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002727 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002728 platform_driver_unregister(&it87_driver);
2729 return err;
2730 }
2731
2732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}
2734
2735static void __exit sm_it87_exit(void)
2736{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002737 platform_device_unregister(pdev);
2738 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739}
2740
2741
Jean Delvare7c81c602014-01-29 20:40:08 +01002742MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002743MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744module_param(update_vbat, bool, 0);
2745MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2746module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002747MODULE_PARM_DESC(fix_pwm_polarity,
2748 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749MODULE_LICENSE("GPL");
2750
2751module_init(sm_it87_init);
2752module_exit(sm_it87_exit);