blob: d0ee556e8ce00447d9d16d8c3adb81de4160bfae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jean Delvare5f2dc792010-03-05 22:17:18 +01002 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring.
4 *
5 * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 * addition to an Environment Controller (Enhanced Hardware Monitor and
8 * Fan Controller)
9 *
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
12 *
Rudolf Marekc145d5c2014-01-29 20:40:08 +010013 * Supports: IT8603E Super I/O chip w/LPC interface
Guenter Roeck3ba9d972015-02-13 20:13:20 -080014 * IT8620E Super I/O chip w/LPC interface
Rudolf Marek574e9bd2014-04-04 18:01:35 +020015 * IT8623E Super I/O chip w/LPC interface
Rudolf Marekc145d5c2014-01-29 20:40:08 +010016 * IT8705F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010017 * IT8712F Super I/O chip w/LPC interface
18 * IT8716F Super I/O chip w/LPC interface
19 * IT8718F Super I/O chip w/LPC interface
20 * IT8720F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020021 * IT8721F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010022 * IT8726F Super I/O chip w/LPC interface
Jean Delvare16b5dda2012-01-16 22:51:48 +010023 * IT8728F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020024 * IT8758E Super I/O chip w/LPC interface
Guenter Roeckb0636702012-09-07 17:34:41 -060025 * IT8771E Super I/O chip w/LPC interface
26 * IT8772E Super I/O chip w/LPC interface
Guenter Roeck7bc32d22015-01-17 14:10:24 -080027 * IT8781F Super I/O chip w/LPC interface
Guenter Roeck0531d982012-03-02 11:46:44 -080028 * IT8782F Super I/O chip w/LPC interface
29 * IT8783E/F Super I/O chip w/LPC interface
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +010030 * IT8786E Super I/O chip w/LPC interface
Guenter Roeck4ee07152015-03-25 23:26:28 -070031 * IT8790E Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010032 * Sis950 A clone of the IT8705F
33 *
34 * Copyright (C) 2001 Chris Gauthron
Jean Delvare7c81c602014-01-29 20:40:08 +010035 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
Jean Delvare5f2dc792010-03-05 22:17:18 +010036 *
37 * This program is free software; you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation; either version 2 of the License, or
40 * (at your option) any later version.
41 *
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
46 *
47 * You should have received a copy of the GNU General Public License
48 * along with this program; if not, write to the Free Software
49 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
50 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Joe Perchesa8ca1032011-01-12 21:55:10 +010052#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/module.h>
55#include <linux/init.h>
56#include <linux/slab.h>
57#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020058#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040059#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020060#include <linux/hwmon-sysfs.h>
61#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040062#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010063#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020064#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020065#include <linux/string.h>
66#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010067#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020068#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
corentin.labbeb74f3fd2007-06-13 20:27:36 +020070#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Guenter Roeckb0636702012-09-07 17:34:41 -060072enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8771,
Guenter Roeck3ba9d972015-02-13 20:13:20 -080073 it8772, it8781, it8782, it8783, it8786, it8790, it8603, it8620 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Jean Delvare67b671b2007-12-06 23:13:42 +010075static unsigned short force_id;
76module_param(force_id, ushort, 0);
77MODULE_PARM_DESC(force_id, "Override the detected device ID");
78
corentin.labbeb74f3fd2007-06-13 20:27:36 +020079static struct platform_device *pdev;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define REG 0x2e /* The register to read/write */
82#define DEV 0x07 /* Register: Logical device select */
83#define VAL 0x2f /* The value to read/write */
84#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010085
86/* The device with the IT8718F/IT8720F VID value in it */
87#define GPIO 0x07
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define DEVID 0x20 /* Register: Device ID */
90#define DEVREV 0x22 /* Register: Device Revision */
91
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020092static inline int superio_inb(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 outb(reg, REG);
95 return inb(VAL);
96}
97
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020098static inline void superio_outb(int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +020099{
100 outb(reg, REG);
101 outb(val, VAL);
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static int superio_inw(int reg)
105{
106 int val;
107 outb(reg++, REG);
108 val = inb(VAL) << 8;
109 outb(reg, REG);
110 val |= inb(VAL);
111 return val;
112}
113
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200114static inline void superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200117 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200120static inline int superio_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200122 /*
123 * Try to reserve REG and REG + 1 for exclusive access.
124 */
125 if (!request_muxed_region(REG, 2, DRVNAME))
126 return -EBUSY;
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 outb(0x87, REG);
129 outb(0x01, REG);
130 outb(0x55, REG);
131 outb(0x55, REG);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200135static inline void superio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 outb(0x02, REG);
138 outb(0x02, VAL);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200139 release_region(REG, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Jean Delvare87673dd2006-08-28 14:37:19 +0200142/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#define IT8712F_DEVID 0x8712
144#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200145#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200146#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100147#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200148#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400149#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100150#define IT8728F_DEVID 0x8728
Guenter Roeckb0636702012-09-07 17:34:41 -0600151#define IT8771E_DEVID 0x8771
152#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800153#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800154#define IT8782F_DEVID 0x8782
155#define IT8783E_DEVID 0x8783
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100156#define IT8786E_DEVID 0x8786
Guenter Roeck4ee07152015-03-25 23:26:28 -0700157#define IT8790E_DEVID 0x8790
Rudolf Marek7183ae82014-04-04 18:01:35 +0200158#define IT8603E_DEVID 0x8603
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800159#define IT8620E_DEVID 0x8620
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200160#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define IT87_ACT_REG 0x30
162#define IT87_BASE_REG 0x60
163
Jean Delvare87673dd2006-08-28 14:37:19 +0200164/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800165#define IT87_SIO_GPIO1_REG 0x25
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800166#define IT87_SIO_GPIO2_REG 0x26
Jean Delvare895ff262009-12-09 20:35:47 +0100167#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100168#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800169#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200170#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800171#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200172#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100173#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030176static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030179static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* Many IT87 constants specified below */
182
183/* Length of ISA address segment */
184#define IT87_EXTENT 8
185
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500186/* Length of ISA address segment for Environmental Controller */
187#define IT87_EC_EXTENT 2
188
189/* Offset of EC registers from ISA base address */
190#define IT87_EC_OFFSET 5
191
192/* Where are the ISA address/data registers relative to the EC base address */
193#define IT87_ADDR_REG_OFFSET 0
194#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196/*----- The IT87 registers -----*/
197
198#define IT87_REG_CONFIG 0x00
199
200#define IT87_REG_ALARM1 0x01
201#define IT87_REG_ALARM2 0x02
202#define IT87_REG_ALARM3 0x03
203
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800204/*
205 * The IT8718F and IT8720F have the VID value in a different register, in
206 * Super-I/O configuration space.
207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800209/*
210 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
211 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
212 * mode.
213 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200215#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
218
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700219static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
220static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
221static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
222static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
223static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#define IT87_REG_FAN_MAIN_CTRL 0x13
226#define IT87_REG_FAN_CTL 0x14
227#define IT87_REG_PWM(nr) (0x15 + (nr))
Jean Delvare6229cdb2010-12-08 16:27:22 +0100228#define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230#define IT87_REG_VIN(nr) (0x20 + (nr))
231#define IT87_REG_TEMP(nr) (0x29 + (nr))
232
233#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
234#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
235#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
236#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#define IT87_REG_VIN_ENABLE 0x50
239#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700240#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100241#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243#define IT87_REG_CHIPID 0x58
244
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100245#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
246#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
247
Guenter Roeck483db432012-12-19 22:17:02 +0100248struct it87_devices {
249 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700250 const char * const suffix;
Guenter Roeck483db432012-12-19 22:17:02 +0100251 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100252 u8 peci_mask;
253 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100254};
255
256#define FEAT_12MV_ADC (1 << 0)
257#define FEAT_NEWER_AUTOPWM (1 << 1)
258#define FEAT_OLD_AUTOPWM (1 << 2)
259#define FEAT_16BIT_FANS (1 << 3)
260#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100261#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100262#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800263#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
264#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800265#define FEAT_VID (1 << 9) /* Set if chip supports VID */
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700266#define FEAT_IN7_INTERNAL (1 << 10) /* Set if in7 is internal */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700267#define FEAT_SIX_FANS (1 << 11) /* Supports six fans */
Guenter Roeck483db432012-12-19 22:17:02 +0100268
269static const struct it87_devices it87_devices[] = {
270 [it87] = {
271 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700272 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100273 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
274 },
275 [it8712] = {
276 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700277 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800278 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
279 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100280 },
281 [it8716] = {
282 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700283 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800284 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800285 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck483db432012-12-19 22:17:02 +0100286 },
287 [it8718] = {
288 .name = "it8718",
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 [it8720] = {
295 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700296 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800297 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800298 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100299 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100300 },
301 [it8721] = {
302 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700303 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100304 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800305 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700306 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100307 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100308 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100309 },
310 [it8728] = {
311 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700312 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100313 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700314 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
315 | FEAT_IN7_INTERNAL,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100316 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100317 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600318 [it8771] = {
319 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700320 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600321 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700322 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800323 /* PECI: guesswork */
324 /* 12mV ADC (OHM) */
325 /* 16 bit fans (OHM) */
326 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600327 .peci_mask = 0x07,
328 },
329 [it8772] = {
330 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700331 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600332 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700333 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800334 /* PECI (coreboot) */
335 /* 12mV ADC (HWSensors4, OHM) */
336 /* 16 bit fans (HWSensors4, OHM) */
337 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600338 .peci_mask = 0x07,
339 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800340 [it8781] = {
341 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700342 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800343 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800344 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800345 .old_peci_mask = 0x4,
346 },
Guenter Roeck483db432012-12-19 22:17:02 +0100347 [it8782] = {
348 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700349 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100350 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800351 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100352 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100353 },
354 [it8783] = {
355 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700356 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100357 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800358 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100359 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100360 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100361 [it8786] = {
362 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700363 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100364 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700365 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100366 .peci_mask = 0x07,
367 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700368 [it8790] = {
369 .name = "it8790",
370 .suffix = "E",
371 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
372 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
373 .peci_mask = 0x07,
374 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100375 [it8603] = {
376 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700377 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100378 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700379 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100380 .peci_mask = 0x07,
381 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800382 [it8620] = {
383 .name = "it8620",
384 .suffix = "E",
385 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700386 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800387 | FEAT_IN7_INTERNAL,
388 .peci_mask = 0x07,
389 },
Guenter Roeck483db432012-12-19 22:17:02 +0100390};
391
392#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
393#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
394#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
395#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
396#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100397#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
398 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100399#define has_temp_old_peci(data, nr) \
400 (((data)->features & FEAT_TEMP_OLD_PECI) && \
401 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800402#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700403#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
404 FEAT_SIX_FANS))
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800405#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700406#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700407#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200409struct it87_sio_data {
410 enum chips type;
411 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200412 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200413 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100414 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200415 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100416 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700417 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100418 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100419 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200420 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700421 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200422};
423
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800424/*
425 * For each registered chip, we need to keep some data in memory.
426 * The structure is dynamically allocated.
427 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700429 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100431 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100432 u8 peci_mask;
433 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200435 unsigned short addr;
436 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100437 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 char valid; /* !=0 if following fields are valid */
439 unsigned long last_updated; /* In jiffies */
440
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200441 u16 in_scaled; /* Internal voltage sensors are scaled */
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100442 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200443 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700444 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700445 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck161d8982012-12-19 22:17:01 +0100446 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100447 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
448 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 u8 fan_div[3]; /* Register encoding, shifted right */
450 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100451 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100453 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100455 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100456
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800457 /*
458 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100459 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
460 * 7, and we want to preserve settings on mode changes, so we have
461 * to track all values separately.
462 * Starting with the IT8721F, the manual PWM duty cycles are stored
463 * in separate registers (8-bit values), so the separate tracking
464 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800465 * simple.
466 */
Jean Delvareb99883d2010-03-05 22:17:15 +0100467 u8 pwm_ctrl[3]; /* Register value */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100468 u8 pwm_duty[3]; /* Manual PWM value set by user */
Jean Delvareb99883d2010-03-05 22:17:15 +0100469 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100470
471 /* Automatic fan speed control registers */
472 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
473 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474};
475
Guenter Roeck0531d982012-03-02 11:46:44 -0800476static int adc_lsb(const struct it87_data *data, int nr)
477{
478 int lsb = has_12mv_adc(data) ? 12 : 16;
479 if (data->in_scaled & (1 << nr))
480 lsb <<= 1;
481 return lsb;
482}
483
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200484static u8 in_to_reg(const struct it87_data *data, int nr, long val)
485{
Guenter Roeck0531d982012-03-02 11:46:44 -0800486 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800487 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200488}
489
490static int in_from_reg(const struct it87_data *data, int nr, int val)
491{
Guenter Roeck0531d982012-03-02 11:46:44 -0800492 return val * adc_lsb(data, nr);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200493}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200494
495static inline u8 FAN_TO_REG(long rpm, int div)
496{
497 if (rpm == 0)
498 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800499 rpm = clamp_val(rpm, 1, 1000000);
500 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200501}
502
503static inline u16 FAN16_TO_REG(long rpm)
504{
505 if (rpm == 0)
506 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800507 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200508}
509
510#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
511 1350000 / ((val) * (div)))
512/* The divider is fixed to 2 in 16-bit mode */
513#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
514 1350000 / ((val) * 2))
515
Guenter Roeck2a844c12013-01-09 08:09:34 -0800516#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
517 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200518#define TEMP_FROM_REG(val) ((val) * 1000)
519
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200520static u8 pwm_to_reg(const struct it87_data *data, long val)
521{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100522 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200523 return val;
524 else
525 return val >> 1;
526}
527
528static int pwm_from_reg(const struct it87_data *data, u8 reg)
529{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100530 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200531 return reg;
532 else
533 return (reg & 0x7f) << 1;
534}
535
Jean Delvare0df6454d2010-10-28 20:31:51 +0200536
537static int DIV_TO_REG(int val)
538{
539 int answer = 0;
540 while (answer < 7 && (val >>= 1))
541 answer++;
542 return answer;
543}
544#define DIV_FROM_REG(val) (1 << (val))
545
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700546/*
547 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
548 * depending on the chip type, to calculate the actual PWM frequency.
549 *
550 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
551 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
552 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
553 * sometimes just one. It is unknown if this is a datasheet error or real,
554 * so this is ignored for now.
555 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200556static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700557 48000000,
558 24000000,
559 12000000,
560 8000000,
561 6000000,
562 3000000,
563 1500000,
564 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200565};
566
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200567static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500568static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200570static int it87_read_value(struct it87_data *data, u8 reg);
571static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200573static int it87_check_pwm(struct device *dev);
574static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200577static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100578 .driver = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200579 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100580 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200581 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500582 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200583};
584
Jean Delvare20ad93d2005-06-05 11:53:25 +0200585static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100586 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100588 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
589 int nr = sattr->nr;
590 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100593 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Guenter Roeck929c6a52012-12-19 22:17:00 +0100596static ssize_t set_in(struct device *dev, struct device_attribute *attr,
597 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100599 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
600 int nr = sattr->nr;
601 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200602
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200603 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100604 unsigned long val;
605
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100606 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100607 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100609 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100610 data->in[nr][index] = in_to_reg(data, nr, val);
611 it87_write_value(data,
612 index == 1 ? IT87_REG_VIN_MIN(nr)
613 : IT87_REG_VIN_MAX(nr),
614 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100615 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return count;
617}
618
Guenter Roeck929c6a52012-12-19 22:17:00 +0100619static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
620static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
621 0, 1);
622static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
623 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Guenter Roeck929c6a52012-12-19 22:17:00 +0100625static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
626static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
627 1, 1);
628static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
629 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Guenter Roeck929c6a52012-12-19 22:17:00 +0100631static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
632static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
633 2, 1);
634static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
635 2, 2);
636
637static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
638static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
639 3, 1);
640static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
641 3, 2);
642
643static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
644static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
645 4, 1);
646static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
647 4, 2);
648
649static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
650static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
651 5, 1);
652static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
653 5, 2);
654
655static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
656static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
657 6, 1);
658static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
659 6, 2);
660
661static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
662static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
663 7, 1);
664static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
665 7, 2);
666
667static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100668static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200671static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100672 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100674 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
675 int nr = sattr->nr;
676 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200678
Guenter Roeck60ca3852012-12-19 22:17:00 +0100679 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200681
Guenter Roeck60ca3852012-12-19 22:17:00 +0100682static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
683 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100685 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
686 int nr = sattr->nr;
687 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200688 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100689 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100690 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100691
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100692 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100693 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100695 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100696
697 switch (index) {
698 default:
699 case 1:
700 reg = IT87_REG_TEMP_LOW(nr);
701 break;
702 case 2:
703 reg = IT87_REG_TEMP_HIGH(nr);
704 break;
705 case 3:
706 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
707 if (!(regval & 0x80)) {
708 regval |= 0x80;
709 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
710 }
711 data->valid = 0;
712 reg = IT87_REG_TEMP_OFFSET[nr];
713 break;
714 }
715
Guenter Roeck60ca3852012-12-19 22:17:00 +0100716 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100717 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100718 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return count;
720}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200721
Guenter Roeck60ca3852012-12-19 22:17:00 +0100722static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
723static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
724 0, 1);
725static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
726 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100727static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
728 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100729static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
730static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
731 1, 1);
732static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
733 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100734static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
735 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100736static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
737static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
738 2, 1);
739static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
740 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100741static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
742 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Guenter Roeck2cece012012-12-19 22:17:01 +0100744static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
745 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200747 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
748 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800750 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100751 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100752
Guenter Roeck19529782012-12-19 22:17:02 +0100753 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
754 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100755 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (reg & (1 << nr))
757 return sprintf(buf, "3\n"); /* thermal diode */
758 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200759 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return sprintf(buf, "0\n"); /* disabled */
761}
Guenter Roeck2cece012012-12-19 22:17:01 +0100762
763static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
764 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200766 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
767 int nr = sensor_attr->index;
768
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200769 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100770 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100771 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100772
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100773 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100774 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Jean Delvare8acf07c2010-04-14 16:14:09 +0200776 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
777 reg &= ~(1 << nr);
778 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100779 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
780 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100781 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
782 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
783 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200784 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100785 dev_warn(dev,
786 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200787 val = 4;
788 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100789 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200791 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200792 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200793 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100794 else if (has_temp_peci(data, nr) && val == 6)
795 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100796 else if (has_temp_old_peci(data, nr) && val == 6)
797 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200798 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200800
801 mutex_lock(&data->update_lock);
802 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100803 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200804 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100805 if (has_temp_old_peci(data, nr))
806 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200807 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100808 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return count;
810}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Guenter Roeck2cece012012-12-19 22:17:01 +0100812static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
813 set_temp_type, 0);
814static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
815 set_temp_type, 1);
816static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
817 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100820
821static int pwm_mode(const struct it87_data *data, int nr)
822{
823 int ctrl = data->fan_main_ctrl & (1 << nr);
824
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100825 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +0100826 return 0;
827 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
828 return 2;
829 else /* Manual mode */
830 return 1;
831}
832
Jean Delvare20ad93d2005-06-05 11:53:25 +0200833static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +0100834 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100836 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
837 int nr = sattr->nr;
838 int index = sattr->index;
839 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200841
Guenter Roecke1169ba2012-12-19 22:17:01 +0100842 speed = has_16bit_fans(data) ?
843 FAN16_FROM_REG(data->fan[nr][index]) :
844 FAN_FROM_REG(data->fan[nr][index],
845 DIV_FROM_REG(data->fan_div[nr]));
846 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100848
Jean Delvare20ad93d2005-06-05 11:53:25 +0200849static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
850 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200852 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
853 int nr = sensor_attr->index;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct it87_data *data = it87_update_device(dev);
856 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
857}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100858static ssize_t show_pwm_enable(struct device *dev,
859 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200861 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
862 int nr = sensor_attr->index;
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100865 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200867static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
868 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200870 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
871 int nr = sensor_attr->index;
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200874 return sprintf(buf, "%d\n",
875 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100877static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
878 char *buf)
879{
880 struct it87_data *data = it87_update_device(dev);
881 int index = (data->fan_ctl >> 4) & 0x07;
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700882 unsigned int freq;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100883
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700884 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
885
886 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100887}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100888
889static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
890 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100892 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
893 int nr = sattr->nr;
894 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200895
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200896 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100897 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100898 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100900 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100901 return -EINVAL;
902
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100903 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +0100904
905 if (has_16bit_fans(data)) {
906 data->fan[nr][index] = FAN16_TO_REG(val);
907 it87_write_value(data, IT87_REG_FAN_MIN[nr],
908 data->fan[nr][index] & 0xff);
909 it87_write_value(data, IT87_REG_FANX_MIN[nr],
910 data->fan[nr][index] >> 8);
911 } else {
912 reg = it87_read_value(data, IT87_REG_FAN_DIV);
913 switch (nr) {
914 case 0:
915 data->fan_div[nr] = reg & 0x07;
916 break;
917 case 1:
918 data->fan_div[nr] = (reg >> 3) & 0x07;
919 break;
920 case 2:
921 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
922 break;
923 }
924 data->fan[nr][index] =
925 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
926 it87_write_value(data, IT87_REG_FAN_MIN[nr],
927 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -0800928 }
929
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100930 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return count;
932}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100933
Jean Delvare20ad93d2005-06-05 11:53:25 +0200934static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
935 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200937 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
938 int nr = sensor_attr->index;
939
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200940 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100941 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200942 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 u8 old;
944
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100945 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100946 return -EINVAL;
947
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100948 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200949 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200951 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100952 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 switch (nr) {
955 case 0:
956 case 1:
957 data->fan_div[nr] = DIV_TO_REG(val);
958 break;
959 case 2:
960 if (val < 8)
961 data->fan_div[nr] = 1;
962 else
963 data->fan_div[nr] = 3;
964 }
965 val = old & 0x80;
966 val |= (data->fan_div[0] & 0x07);
967 val |= (data->fan_div[1] & 0x07) << 3;
968 if (data->fan_div[2] == 3)
969 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200970 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200972 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100973 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
974 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200975
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100976 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return count;
978}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100979
980/* Returns 0 if OK, -EINVAL otherwise */
981static int check_trip_points(struct device *dev, int nr)
982{
983 const struct it87_data *data = dev_get_drvdata(dev);
984 int i, err = 0;
985
986 if (has_old_autopwm(data)) {
987 for (i = 0; i < 3; i++) {
988 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
989 err = -EINVAL;
990 }
991 for (i = 0; i < 2; i++) {
992 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
993 err = -EINVAL;
994 }
995 }
996
997 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100998 dev_err(dev,
999 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001000 dev_err(dev, "Adjust the trip points and try again\n");
1001 }
1002 return err;
1003}
1004
Jean Delvare20ad93d2005-06-05 11:53:25 +02001005static ssize_t set_pwm_enable(struct device *dev,
1006 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001008 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1009 int nr = sensor_attr->index;
1010
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001011 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001012 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001014 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001015 return -EINVAL;
1016
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001017 /* Check trip points before switching to automatic mode */
1018 if (val == 2) {
1019 if (check_trip_points(dev, nr) < 0)
1020 return -EINVAL;
1021 }
1022
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001023 /* IT8603E does not have on/off mode */
1024 if (val == 0 && data->type == it8603)
1025 return -EINVAL;
1026
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001027 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (val == 0) {
1030 int tmp;
1031 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001032 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1033 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* set on/off mode */
1035 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +01001036 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1037 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +01001038 } else {
1039 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001040 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001041 data->pwm_temp_map[nr] :
1042 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001043 else /* Automatic mode */
1044 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1045 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001046
1047 if (data->type != it8603) {
1048 /* set SmartGuardian mode */
1049 data->fan_main_ctrl |= (1 << nr);
1050 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1051 data->fan_main_ctrl);
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001055 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return count;
1057}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001058static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1059 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001061 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1062 int nr = sensor_attr->index;
1063
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001064 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001065 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001067 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return -EINVAL;
1069
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001070 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001071 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001072 /*
1073 * If we are in automatic mode, the PWM duty cycle register
1074 * is read-only so we can't write the value.
1075 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001076 if (data->pwm_ctrl[nr] & 0x80) {
1077 mutex_unlock(&data->update_lock);
1078 return -EBUSY;
1079 }
1080 data->pwm_duty[nr] = pwm_to_reg(data, val);
1081 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
1082 data->pwm_duty[nr]);
1083 } else {
1084 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001085 /*
1086 * If we are in manual mode, write the duty cycle immediately;
1087 * otherwise, just store it for later use.
1088 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001089 if (!(data->pwm_ctrl[nr] & 0x80)) {
1090 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1091 it87_write_value(data, IT87_REG_PWM(nr),
1092 data->pwm_ctrl[nr]);
1093 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001094 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001095 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return count;
1097}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001098static ssize_t set_pwm_freq(struct device *dev,
1099 struct device_attribute *attr, const char *buf, size_t count)
1100{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001101 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001102 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001103 int i;
1104
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001105 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001106 return -EINVAL;
1107
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001108 val = clamp_val(val, 0, 1000000);
1109 val *= has_newer_autopwm(data) ? 256 : 128;
1110
Jean Delvaref8d0c192007-02-14 21:15:02 +01001111 /* Search for the nearest available frequency */
1112 for (i = 0; i < 7; i++) {
1113 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1114 break;
1115 }
1116
1117 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001118 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001119 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001120 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001121 mutex_unlock(&data->update_lock);
1122
1123 return count;
1124}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001125static ssize_t show_pwm_temp_map(struct device *dev,
1126 struct device_attribute *attr, char *buf)
1127{
1128 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1129 int nr = sensor_attr->index;
1130
1131 struct it87_data *data = it87_update_device(dev);
1132 int map;
1133
1134 if (data->pwm_temp_map[nr] < 3)
1135 map = 1 << data->pwm_temp_map[nr];
1136 else
1137 map = 0; /* Should never happen */
1138 return sprintf(buf, "%d\n", map);
1139}
1140static ssize_t set_pwm_temp_map(struct device *dev,
1141 struct device_attribute *attr, const char *buf, size_t count)
1142{
1143 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1144 int nr = sensor_attr->index;
1145
1146 struct it87_data *data = dev_get_drvdata(dev);
1147 long val;
1148 u8 reg;
1149
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001150 /*
1151 * This check can go away if we ever support automatic fan speed
1152 * control on newer chips.
1153 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001154 if (!has_old_autopwm(data)) {
1155 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1156 return -EINVAL;
1157 }
1158
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001159 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001160 return -EINVAL;
1161
1162 switch (val) {
1163 case (1 << 0):
1164 reg = 0x00;
1165 break;
1166 case (1 << 1):
1167 reg = 0x01;
1168 break;
1169 case (1 << 2):
1170 reg = 0x02;
1171 break;
1172 default:
1173 return -EINVAL;
1174 }
1175
1176 mutex_lock(&data->update_lock);
1177 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001178 /*
1179 * If we are in automatic mode, write the temp mapping immediately;
1180 * otherwise, just store it for later use.
1181 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001182 if (data->pwm_ctrl[nr] & 0x80) {
1183 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1184 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1185 }
1186 mutex_unlock(&data->update_lock);
1187 return count;
1188}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001190static ssize_t show_auto_pwm(struct device *dev,
1191 struct device_attribute *attr, char *buf)
1192{
1193 struct it87_data *data = it87_update_device(dev);
1194 struct sensor_device_attribute_2 *sensor_attr =
1195 to_sensor_dev_attr_2(attr);
1196 int nr = sensor_attr->nr;
1197 int point = sensor_attr->index;
1198
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001199 return sprintf(buf, "%d\n",
1200 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001201}
1202
1203static ssize_t set_auto_pwm(struct device *dev,
1204 struct device_attribute *attr, const char *buf, size_t count)
1205{
1206 struct it87_data *data = dev_get_drvdata(dev);
1207 struct sensor_device_attribute_2 *sensor_attr =
1208 to_sensor_dev_attr_2(attr);
1209 int nr = sensor_attr->nr;
1210 int point = sensor_attr->index;
1211 long val;
1212
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001213 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001214 return -EINVAL;
1215
1216 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001217 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001218 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1219 data->auto_pwm[nr][point]);
1220 mutex_unlock(&data->update_lock);
1221 return count;
1222}
1223
1224static ssize_t show_auto_temp(struct device *dev,
1225 struct device_attribute *attr, char *buf)
1226{
1227 struct it87_data *data = it87_update_device(dev);
1228 struct sensor_device_attribute_2 *sensor_attr =
1229 to_sensor_dev_attr_2(attr);
1230 int nr = sensor_attr->nr;
1231 int point = sensor_attr->index;
1232
1233 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1234}
1235
1236static ssize_t set_auto_temp(struct device *dev,
1237 struct device_attribute *attr, const char *buf, size_t count)
1238{
1239 struct it87_data *data = dev_get_drvdata(dev);
1240 struct sensor_device_attribute_2 *sensor_attr =
1241 to_sensor_dev_attr_2(attr);
1242 int nr = sensor_attr->nr;
1243 int point = sensor_attr->index;
1244 long val;
1245
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001246 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001247 return -EINVAL;
1248
1249 mutex_lock(&data->update_lock);
1250 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1251 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1252 data->auto_temp[nr][point]);
1253 mutex_unlock(&data->update_lock);
1254 return count;
1255}
1256
Guenter Roecke1169ba2012-12-19 22:17:01 +01001257static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1258static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1259 0, 1);
1260static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1261 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Guenter Roecke1169ba2012-12-19 22:17:01 +01001263static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1264static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1265 1, 1);
1266static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1267 set_fan_div, 1);
1268
1269static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1270static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1271 2, 1);
1272static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1273 set_fan_div, 2);
1274
1275static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1276static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1277 3, 1);
1278
1279static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1280static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1281 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001283static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1284static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1285 5, 1);
1286
Guenter Roeckc4458db2012-12-19 22:17:02 +01001287static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1288 show_pwm_enable, set_pwm_enable, 0);
1289static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1290static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1291static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1292 show_pwm_temp_map, set_pwm_temp_map, 0);
1293static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1294 show_auto_pwm, set_auto_pwm, 0, 0);
1295static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1296 show_auto_pwm, set_auto_pwm, 0, 1);
1297static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1298 show_auto_pwm, set_auto_pwm, 0, 2);
1299static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1300 show_auto_pwm, NULL, 0, 3);
1301static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1302 show_auto_temp, set_auto_temp, 0, 1);
1303static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1304 show_auto_temp, set_auto_temp, 0, 0);
1305static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1306 show_auto_temp, set_auto_temp, 0, 2);
1307static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1308 show_auto_temp, set_auto_temp, 0, 3);
1309static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1310 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Guenter Roeckc4458db2012-12-19 22:17:02 +01001312static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1313 show_pwm_enable, set_pwm_enable, 1);
1314static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1315static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1316static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1317 show_pwm_temp_map, set_pwm_temp_map, 1);
1318static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1319 show_auto_pwm, set_auto_pwm, 1, 0);
1320static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1321 show_auto_pwm, set_auto_pwm, 1, 1);
1322static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1323 show_auto_pwm, set_auto_pwm, 1, 2);
1324static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1325 show_auto_pwm, NULL, 1, 3);
1326static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1327 show_auto_temp, set_auto_temp, 1, 1);
1328static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1329 show_auto_temp, set_auto_temp, 1, 0);
1330static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1331 show_auto_temp, set_auto_temp, 1, 2);
1332static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1333 show_auto_temp, set_auto_temp, 1, 3);
1334static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1335 show_auto_temp, set_auto_temp, 1, 4);
1336
1337static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1338 show_pwm_enable, set_pwm_enable, 2);
1339static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1340static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1341static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1342 show_pwm_temp_map, set_pwm_temp_map, 2);
1343static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1344 show_auto_pwm, set_auto_pwm, 2, 0);
1345static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1346 show_auto_pwm, set_auto_pwm, 2, 1);
1347static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1348 show_auto_pwm, set_auto_pwm, 2, 2);
1349static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1350 show_auto_pwm, NULL, 2, 3);
1351static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1352 show_auto_temp, set_auto_temp, 2, 1);
1353static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1354 show_auto_temp, set_auto_temp, 2, 0);
1355static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1356 show_auto_temp, set_auto_temp, 2, 2);
1357static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1358 show_auto_temp, set_auto_temp, 2, 3);
1359static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1360 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001363static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1364 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001367 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
Jean Delvare1d66c642005-04-18 21:16:59 -07001369static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Jean Delvare0124dd72007-11-25 16:16:41 +01001371static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1372 char *buf)
1373{
1374 int bitnr = to_sensor_dev_attr(attr)->index;
1375 struct it87_data *data = it87_update_device(dev);
1376 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1377}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001378
1379static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1380 *attr, const char *buf, size_t count)
1381{
1382 struct it87_data *data = dev_get_drvdata(dev);
1383 long val;
1384 int config;
1385
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001386 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001387 return -EINVAL;
1388
1389 mutex_lock(&data->update_lock);
1390 config = it87_read_value(data, IT87_REG_CONFIG);
1391 if (config < 0) {
1392 count = config;
1393 } else {
1394 config |= 1 << 5;
1395 it87_write_value(data, IT87_REG_CONFIG, config);
1396 /* Invalidate cache to force re-read */
1397 data->valid = 0;
1398 }
1399 mutex_unlock(&data->update_lock);
1400
1401 return count;
1402}
1403
Jean Delvare0124dd72007-11-25 16:16:41 +01001404static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1405static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1406static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1407static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1408static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1409static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1410static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1411static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1412static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1413static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1414static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1415static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1416static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001417static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001418static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1419static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1420static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001421static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1422 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001423
Jean Delvared9b327c2010-03-05 22:17:17 +01001424static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1425 char *buf)
1426{
1427 int bitnr = to_sensor_dev_attr(attr)->index;
1428 struct it87_data *data = it87_update_device(dev);
1429 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1430}
1431static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1432 const char *buf, size_t count)
1433{
1434 int bitnr = to_sensor_dev_attr(attr)->index;
1435 struct it87_data *data = dev_get_drvdata(dev);
1436 long val;
1437
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001438 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001439 || (val != 0 && val != 1))
1440 return -EINVAL;
1441
1442 mutex_lock(&data->update_lock);
1443 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1444 if (val)
1445 data->beeps |= (1 << bitnr);
1446 else
1447 data->beeps &= ~(1 << bitnr);
1448 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1449 mutex_unlock(&data->update_lock);
1450 return count;
1451}
1452
1453static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1454 show_beep, set_beep, 1);
1455static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1456static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1457static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1458static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1459static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1460static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1461static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1462/* fanX_beep writability is set later */
1463static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1464static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1465static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1466static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1467static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001468static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001469static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1470 show_beep, set_beep, 2);
1471static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1472static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1473
Jean Delvare5f2dc792010-03-05 22:17:18 +01001474static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1475 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Jean Delvare90d66192007-10-08 18:24:35 +02001477 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001478 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001480static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1481 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001483 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001484 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001486 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001487 return -EINVAL;
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 data->vrm = val;
1490
1491 return count;
1492}
1493static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Jean Delvare5f2dc792010-03-05 22:17:18 +01001495static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1496 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 struct it87_data *data = it87_update_device(dev);
1499 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1500}
1501static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001502
Jean Delvare738e5e02010-08-14 21:08:50 +02001503static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1504 char *buf)
1505{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001506 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001507 "+5V",
1508 "5VSB",
1509 "Vbat",
1510 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001511 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001512 "+3.3V",
1513 "3VSB",
1514 "Vbat",
1515 };
1516 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001517 int nr = to_sensor_dev_attr(attr)->index;
1518
Jean Delvare16b5dda2012-01-16 22:51:48 +01001519 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1520 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001521}
1522static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1523static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1524static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Rudolf Marek7183ae82014-04-04 18:01:35 +02001525/* special AVCC3 IT8603E in9 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001526static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001527
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001528static ssize_t show_name(struct device *dev, struct device_attribute
1529 *devattr, char *buf)
1530{
1531 struct it87_data *data = dev_get_drvdata(dev);
1532 return sprintf(buf, "%s\n", data->name);
1533}
1534static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1535
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001536static struct attribute *it87_attributes_in[10][5] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001537{
Jean Delvare87808be2006-09-24 21:17:13 +02001538 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001539 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001540 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001541 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001542 NULL
1543}, {
1544 &sensor_dev_attr_in1_input.dev_attr.attr,
1545 &sensor_dev_attr_in1_min.dev_attr.attr,
1546 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001547 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001548 NULL
1549}, {
1550 &sensor_dev_attr_in2_input.dev_attr.attr,
1551 &sensor_dev_attr_in2_min.dev_attr.attr,
1552 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001553 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001554 NULL
1555}, {
1556 &sensor_dev_attr_in3_input.dev_attr.attr,
1557 &sensor_dev_attr_in3_min.dev_attr.attr,
1558 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001559 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001560 NULL
1561}, {
1562 &sensor_dev_attr_in4_input.dev_attr.attr,
1563 &sensor_dev_attr_in4_min.dev_attr.attr,
1564 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001565 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001566 NULL
1567}, {
1568 &sensor_dev_attr_in5_input.dev_attr.attr,
1569 &sensor_dev_attr_in5_min.dev_attr.attr,
1570 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001571 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001572 NULL
1573}, {
1574 &sensor_dev_attr_in6_input.dev_attr.attr,
1575 &sensor_dev_attr_in6_min.dev_attr.attr,
1576 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001577 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001578 NULL
1579}, {
1580 &sensor_dev_attr_in7_input.dev_attr.attr,
1581 &sensor_dev_attr_in7_min.dev_attr.attr,
1582 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001583 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001584 NULL
1585}, {
1586 &sensor_dev_attr_in8_input.dev_attr.attr,
1587 NULL
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001588}, {
1589 &sensor_dev_attr_in9_input.dev_attr.attr,
1590 NULL
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001591} };
Jean Delvare87808be2006-09-24 21:17:13 +02001592
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001593static const struct attribute_group it87_group_in[10] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001594 { .attrs = it87_attributes_in[0] },
1595 { .attrs = it87_attributes_in[1] },
1596 { .attrs = it87_attributes_in[2] },
1597 { .attrs = it87_attributes_in[3] },
1598 { .attrs = it87_attributes_in[4] },
1599 { .attrs = it87_attributes_in[5] },
1600 { .attrs = it87_attributes_in[6] },
1601 { .attrs = it87_attributes_in[7] },
1602 { .attrs = it87_attributes_in[8] },
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001603 { .attrs = it87_attributes_in[9] },
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001604};
1605
Guenter Roeck4573acb2012-03-26 16:17:41 -07001606static struct attribute *it87_attributes_temp[3][6] = {
1607{
Jean Delvare87808be2006-09-24 21:17:13 +02001608 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001609 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001610 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001611 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001612 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001613 NULL
1614} , {
1615 &sensor_dev_attr_temp2_input.dev_attr.attr,
1616 &sensor_dev_attr_temp2_max.dev_attr.attr,
1617 &sensor_dev_attr_temp2_min.dev_attr.attr,
1618 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001619 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001620 NULL
1621} , {
1622 &sensor_dev_attr_temp3_input.dev_attr.attr,
1623 &sensor_dev_attr_temp3_max.dev_attr.attr,
1624 &sensor_dev_attr_temp3_min.dev_attr.attr,
1625 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001626 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001627 NULL
1628} };
Jean Delvare87808be2006-09-24 21:17:13 +02001629
Guenter Roeck4573acb2012-03-26 16:17:41 -07001630static const struct attribute_group it87_group_temp[3] = {
1631 { .attrs = it87_attributes_temp[0] },
1632 { .attrs = it87_attributes_temp[1] },
1633 { .attrs = it87_attributes_temp[2] },
1634};
1635
Guenter Roeck161d8982012-12-19 22:17:01 +01001636static struct attribute *it87_attributes_temp_offset[] = {
1637 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1638 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1639 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1640};
1641
Guenter Roeck4573acb2012-03-26 16:17:41 -07001642static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001643 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001644 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001645 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001646 NULL
1647};
1648
1649static const struct attribute_group it87_group = {
1650 .attrs = it87_attributes,
1651};
1652
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001653static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001654 &sensor_dev_attr_in0_beep.dev_attr.attr,
1655 &sensor_dev_attr_in1_beep.dev_attr.attr,
1656 &sensor_dev_attr_in2_beep.dev_attr.attr,
1657 &sensor_dev_attr_in3_beep.dev_attr.attr,
1658 &sensor_dev_attr_in4_beep.dev_attr.attr,
1659 &sensor_dev_attr_in5_beep.dev_attr.attr,
1660 &sensor_dev_attr_in6_beep.dev_attr.attr,
1661 &sensor_dev_attr_in7_beep.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001662 NULL,
1663 NULL,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001664};
Jean Delvared9b327c2010-03-05 22:17:17 +01001665
Guenter Roeck4573acb2012-03-26 16:17:41 -07001666static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001667 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1668 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1669 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001670};
1671
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001672static struct attribute *it87_attributes_fan[6][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001673 &sensor_dev_attr_fan1_input.dev_attr.attr,
1674 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001675 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1676 NULL
1677}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001678 &sensor_dev_attr_fan2_input.dev_attr.attr,
1679 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001680 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1681 NULL
1682}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001683 &sensor_dev_attr_fan3_input.dev_attr.attr,
1684 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001685 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001686 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001687}, {
1688 &sensor_dev_attr_fan4_input.dev_attr.attr,
1689 &sensor_dev_attr_fan4_min.dev_attr.attr,
1690 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1691 NULL
1692}, {
1693 &sensor_dev_attr_fan5_input.dev_attr.attr,
1694 &sensor_dev_attr_fan5_min.dev_attr.attr,
1695 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1696 NULL
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001697}, {
1698 &sensor_dev_attr_fan6_input.dev_attr.attr,
1699 &sensor_dev_attr_fan6_min.dev_attr.attr,
1700 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1701 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001702} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001703
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001704static const struct attribute_group it87_group_fan[6] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001705 { .attrs = it87_attributes_fan[0] },
1706 { .attrs = it87_attributes_fan[1] },
1707 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001708 { .attrs = it87_attributes_fan[3] },
1709 { .attrs = it87_attributes_fan[4] },
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001710 { .attrs = it87_attributes_fan[5] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001711};
1712
Guenter Roecke1169ba2012-12-19 22:17:01 +01001713static const struct attribute *it87_attributes_fan_div[] = {
1714 &sensor_dev_attr_fan1_div.dev_attr.attr,
1715 &sensor_dev_attr_fan2_div.dev_attr.attr,
1716 &sensor_dev_attr_fan3_div.dev_attr.attr,
1717};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001718
1719static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001720 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001721 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001722 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001723 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001724 NULL
1725}, {
1726 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1727 &sensor_dev_attr_pwm2.dev_attr.attr,
1728 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001729 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001730 NULL
1731}, {
1732 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1733 &sensor_dev_attr_pwm3.dev_attr.attr,
1734 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001735 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001736 NULL
1737} };
Jean Delvare87808be2006-09-24 21:17:13 +02001738
Jean Delvare723a0aa2010-03-05 22:17:16 +01001739static const struct attribute_group it87_group_pwm[3] = {
1740 { .attrs = it87_attributes_pwm[0] },
1741 { .attrs = it87_attributes_pwm[1] },
1742 { .attrs = it87_attributes_pwm[2] },
1743};
1744
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001745static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1746 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1747 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1748 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1749 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1750 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1751 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1752 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1753 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1754 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1755 NULL
1756}, {
1757 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1758 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1759 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1760 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1761 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1762 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1763 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1764 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1765 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1766 NULL
1767}, {
1768 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1769 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1770 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1771 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1772 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1773 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1774 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1775 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1776 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1777 NULL
1778} };
1779
1780static const struct attribute_group it87_group_autopwm[3] = {
1781 { .attrs = it87_attributes_autopwm[0] },
1782 { .attrs = it87_attributes_autopwm[1] },
1783 { .attrs = it87_attributes_autopwm[2] },
1784};
1785
Jean Delvared9b327c2010-03-05 22:17:17 +01001786static struct attribute *it87_attributes_fan_beep[] = {
1787 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1788 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1789 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1790 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1791 &sensor_dev_attr_fan5_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001792 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001793};
1794
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001795static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001796 &dev_attr_vrm.attr,
1797 &dev_attr_cpu0_vid.attr,
1798 NULL
1799};
1800
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001801static const struct attribute_group it87_group_vid = {
1802 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001803};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Jean Delvare738e5e02010-08-14 21:08:50 +02001805static struct attribute *it87_attributes_label[] = {
1806 &sensor_dev_attr_in3_label.dev_attr.attr,
1807 &sensor_dev_attr_in7_label.dev_attr.attr,
1808 &sensor_dev_attr_in8_label.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001809 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare738e5e02010-08-14 21:08:50 +02001810 NULL
1811};
1812
1813static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001814 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001815};
1816
Jean Delvare2d8672c2005-07-19 23:56:35 +02001817/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001818static int __init it87_find(unsigned short *address,
1819 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001821 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001822 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001823 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001824 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001826 err = superio_enter();
1827 if (err)
1828 return err;
1829
1830 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001831 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001832
1833 switch (chip_type) {
1834 case IT8705F_DEVID:
1835 sio_data->type = it87;
1836 break;
1837 case IT8712F_DEVID:
1838 sio_data->type = it8712;
1839 break;
1840 case IT8716F_DEVID:
1841 case IT8726F_DEVID:
1842 sio_data->type = it8716;
1843 break;
1844 case IT8718F_DEVID:
1845 sio_data->type = it8718;
1846 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001847 case IT8720F_DEVID:
1848 sio_data->type = it8720;
1849 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001850 case IT8721F_DEVID:
1851 sio_data->type = it8721;
1852 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001853 case IT8728F_DEVID:
1854 sio_data->type = it8728;
1855 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06001856 case IT8771E_DEVID:
1857 sio_data->type = it8771;
1858 break;
1859 case IT8772E_DEVID:
1860 sio_data->type = it8772;
1861 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08001862 case IT8781F_DEVID:
1863 sio_data->type = it8781;
1864 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001865 case IT8782F_DEVID:
1866 sio_data->type = it8782;
1867 break;
1868 case IT8783E_DEVID:
1869 sio_data->type = it8783;
1870 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001871 case IT8786E_DEVID:
1872 sio_data->type = it8786;
1873 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07001874 case IT8790E_DEVID:
1875 sio_data->type = it8790;
1876 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02001877 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02001878 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001879 sio_data->type = it8603;
1880 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08001881 case IT8620E_DEVID:
1882 sio_data->type = it8620;
1883 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001884 case 0xffff: /* No device at all */
1885 goto exit;
1886 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001887 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001888 goto exit;
1889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Jean Delvare87673dd2006-08-28 14:37:19 +02001891 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001893 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 goto exit;
1895 }
1896
1897 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1898 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001899 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 goto exit;
1901 }
1902
1903 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001904 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07001905 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
1906 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001907 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001909 config = &it87_devices[sio_data->type];
1910
Guenter Roeck7f5726c2015-03-26 08:49:18 -07001911 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001912 if (has_in7_internal(config))
Guenter Roeck7f5726c2015-03-26 08:49:18 -07001913 sio_data->internal |= (1 << 1);
1914
Jean Delvare738e5e02010-08-14 21:08:50 +02001915 /* in8 (Vbat) is always internal */
Guenter Roeck7f5726c2015-03-26 08:49:18 -07001916 sio_data->internal |= (1 << 2);
1917
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001918 /* Only the IT8603E has in9 */
1919 if (sio_data->type != it8603)
1920 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02001921
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07001922 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01001923 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001924
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001925 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1926 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01001927 /* The IT8705F has a different LD number for GPIO */
1928 superio_select(5);
1929 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08001930 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001931 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08001932
Guenter Roeck0531d982012-03-02 11:46:44 -08001933 superio_select(GPIO);
1934
1935 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1936 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001937 reg2a = superio_inb(IT87_SIO_PINX1_REG);
1938 reg2c = superio_inb(IT87_SIO_PINX2_REG);
1939 regef = superio_inb(IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001940
Guenter Roeck0531d982012-03-02 11:46:44 -08001941 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001942 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08001943 sio_data->skip_fan |= (1 << 2);
1944 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001945 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08001946 sio_data->skip_pwm |= (1 << 2);
1947
1948 /* Check if fan2 is there or not */
1949 if (reg27 & (1 << 7))
1950 sio_data->skip_fan |= (1 << 1);
1951 if (reg27 & (1 << 3))
1952 sio_data->skip_pwm |= (1 << 1);
1953
1954 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001955 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001956 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001957
1958 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001959 if (reg27 & (1 << 1))
1960 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001961
1962 /*
1963 * VIN7
1964 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1965 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001966 if (reg27 & (1 << 2)) {
1967 /*
1968 * The data sheet is a bit unclear regarding the
1969 * internal voltage divider for VCCH5V. It says
1970 * "This bit enables and switches VIN7 (pin 91) to the
1971 * internal voltage divider for VCCH5V".
1972 * This is different to other chips, where the internal
1973 * voltage divider would connect VIN7 to an internal
1974 * voltage source. Maybe that is the case here as well.
1975 *
1976 * Since we don't know for sure, re-route it if that is
1977 * not the case, and ask the user to report if the
1978 * resulting voltage is sane.
1979 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001980 if (!(reg2c & (1 << 1))) {
1981 reg2c |= (1 << 1);
1982 superio_outb(IT87_SIO_PINX2_REG, reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001983 pr_notice("Routing internal VCCH5V to in7.\n");
1984 }
1985 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1986 pr_notice("Please report if it displays a reasonable voltage.\n");
1987 }
Guenter Roeck0531d982012-03-02 11:46:44 -08001988
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001989 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08001990 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001991 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08001992 sio_data->internal |= (1 << 1);
1993
1994 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001995 } else if (sio_data->type == it8603) {
1996 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08001997
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001998 superio_select(GPIO);
1999
2000 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
2001
2002 /* Check if fan3 is there or not */
2003 if (reg27 & (1 << 6))
2004 sio_data->skip_pwm |= (1 << 2);
2005 if (reg27 & (1 << 7))
2006 sio_data->skip_fan |= (1 << 2);
2007
2008 /* Check if fan2 is there or not */
2009 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
2010 if (reg29 & (1 << 1))
2011 sio_data->skip_pwm |= (1 << 1);
2012 if (reg29 & (1 << 2))
2013 sio_data->skip_fan |= (1 << 1);
2014
2015 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2016 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2017
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002018 sio_data->internal |= (1 << 3); /* in9 is AVCC */
2019
2020 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002021 } else if (sio_data->type == it8620) {
2022 int reg;
2023
2024 superio_select(GPIO);
2025
2026 /* Check for fan4, fan5 */
2027 reg = superio_inb(IT87_SIO_GPIO2_REG);
2028 if (!(reg & (1 << 5)))
2029 sio_data->skip_fan |= (1 << 3);
2030 if (!(reg & (1 << 4)))
2031 sio_data->skip_fan |= (1 << 4);
2032
2033 /* Check for pwm3, fan3 */
2034 reg = superio_inb(IT87_SIO_GPIO3_REG);
2035 if (reg & (1 << 6))
2036 sio_data->skip_pwm |= (1 << 2);
2037 if (reg & (1 << 7))
2038 sio_data->skip_fan |= (1 << 2);
2039
2040 /* Check for pwm2, fan2 */
2041 reg = superio_inb(IT87_SIO_GPIO5_REG);
2042 if (reg & (1 << 1))
2043 sio_data->skip_pwm |= (1 << 1);
2044 if (reg & (1 << 2))
2045 sio_data->skip_fan |= (1 << 1);
2046
2047 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002048 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002049 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002050 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002051
2052 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002053
Jean Delvare895ff262009-12-09 20:35:47 +01002054 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002055 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002056 /* We need at least 4 VID pins */
2057 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002058 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002059 sio_data->skip_vid = 1;
2060 }
Jean Delvare895ff262009-12-09 20:35:47 +01002061 }
2062
Jean Delvare591ec652009-12-09 20:35:48 +01002063 /* Check if fan3 is there or not */
2064 if (reg & (1 << 6))
2065 sio_data->skip_pwm |= (1 << 2);
2066 if (reg & (1 << 7))
2067 sio_data->skip_fan |= (1 << 2);
2068
2069 /* Check if fan2 is there or not */
2070 reg = superio_inb(IT87_SIO_GPIO5_REG);
2071 if (reg & (1 << 1))
2072 sio_data->skip_pwm |= (1 << 1);
2073 if (reg & (1 << 2))
2074 sio_data->skip_fan |= (1 << 1);
2075
Jean Delvare895ff262009-12-09 20:35:47 +01002076 if ((sio_data->type == it8718 || sio_data->type == it8720)
2077 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002078 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002079
2080 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002081
2082 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2083
Jean Delvare436cad22010-07-09 16:22:48 +02002084 /*
2085 * The IT8720F has no VIN7 pin, so VCCH should always be
2086 * routed internally to VIN7 with an internal divider.
2087 * Curiously, there still is a configuration bit to control
2088 * this, which means it can be set incorrectly. And even
2089 * more curiously, many boards out there are improperly
2090 * configured, even though the IT8720F datasheet claims
2091 * that the internal routing of VCCH to VIN7 is the default
2092 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002093 *
2094 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002095 * If UART6 is enabled, re-route VIN7 to the internal divider
2096 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002097 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002098 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02002099 reg |= (1 << 1);
2100 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002101 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002102 }
Jean Delvare87673dd2006-08-28 14:37:19 +02002103 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02002104 sio_data->internal |= (1 << 0);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002105 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02002106 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002107
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002108 /*
2109 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2110 * While VIN7 can be routed to the internal voltage divider,
2111 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002112 *
2113 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2114 * is the temperature source. Since we can not read the
2115 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002116 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002117 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002118 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002119 sio_data->skip_temp |= (1 << 2);
2120 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002121
Jean Delvared9b327c2010-03-05 22:17:17 +01002122 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002123 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002124 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002125 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002126
Jean Delvare98dd22c2008-10-09 15:33:58 +02002127 /* Disable specific features based on DMI strings */
2128 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2129 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2130 if (board_vendor && board_name) {
2131 if (strcmp(board_vendor, "nVIDIA") == 0
2132 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002133 /*
2134 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2135 * connected to a fan, but to something else. One user
2136 * has reported instant system power-off when changing
2137 * the PWM2 duty cycle, so we disable it.
2138 * I use the board name string as the trigger in case
2139 * the same board is ever used in other systems.
2140 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002141 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002142 sio_data->skip_pwm = (1 << 1);
2143 }
2144 }
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146exit:
2147 superio_exit();
2148 return err;
2149}
2150
Jean Delvare723a0aa2010-03-05 22:17:16 +01002151static void it87_remove_files(struct device *dev)
2152{
2153 struct it87_data *data = platform_get_drvdata(pdev);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002154 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002155 int i;
2156
2157 sysfs_remove_group(&dev->kobj, &it87_group);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002158 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002159 if (sio_data->skip_in & (1 << i))
2160 continue;
2161 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2162 if (it87_attributes_in_beep[i])
2163 sysfs_remove_file(&dev->kobj,
2164 it87_attributes_in_beep[i]);
2165 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002166 for (i = 0; i < 3; i++) {
2167 if (!(data->has_temp & (1 << i)))
2168 continue;
2169 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01002170 if (has_temp_offset(data))
2171 sysfs_remove_file(&dev->kobj,
2172 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002173 if (sio_data->beep_pin)
2174 sysfs_remove_file(&dev->kobj,
2175 it87_attributes_temp_beep[i]);
2176 }
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002177 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002178 if (!(data->has_fan & (1 << i)))
2179 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002180 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002181 if (sio_data->beep_pin)
2182 sysfs_remove_file(&dev->kobj,
2183 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002184 if (i < 3 && !has_16bit_fans(data))
2185 sysfs_remove_file(&dev->kobj,
2186 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002187 }
2188 for (i = 0; i < 3; i++) {
Guenter Roeck1696d1d2015-03-27 06:03:41 -07002189 if (sio_data->skip_pwm & (1 << i))
Jean Delvare723a0aa2010-03-05 22:17:16 +01002190 continue;
2191 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002192 if (has_old_autopwm(data))
2193 sysfs_remove_group(&dev->kobj,
2194 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002195 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002196 if (!sio_data->skip_vid)
2197 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02002198 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002199}
2200
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002201static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002204 struct resource *res;
2205 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002206 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002207 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01002209 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002211 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002212 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2213 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002214 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2215 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002216 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002217 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Guenter Roeck62a1d052012-03-24 21:54:41 -07002220 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2221 if (!data)
2222 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002224 data->addr = res->start;
2225 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002226 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002227 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002228 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002229 data->name = it87_devices[sio_data->type].name;
2230 /*
2231 * IT8705F Datasheet 0.4.1, 3h == Version G.
2232 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2233 * These are the first revisions with 16-bit tachometer support.
2234 */
2235 switch (data->type) {
2236 case it87:
2237 if (sio_data->revision >= 0x03) {
2238 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002239 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002240 }
2241 break;
2242 case it8712:
2243 if (sio_data->revision >= 0x08) {
2244 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002245 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2246 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002247 }
2248 break;
2249 default:
2250 break;
2251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002254 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002255 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2256 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002258 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002260 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002263 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002265 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002266 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002267 if (sio_data->internal & (1 << 0))
2268 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2269 if (sio_data->internal & (1 << 1))
2270 data->in_scaled |= (1 << 7); /* in7 is VSB */
2271 if (sio_data->internal & (1 << 2))
2272 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002273 if (sio_data->internal & (1 << 3))
2274 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002275 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2276 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002277 if (sio_data->internal & (1 << 0))
2278 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2279 if (sio_data->internal & (1 << 1))
2280 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002281 }
2282
Guenter Roeck4573acb2012-03-26 16:17:41 -07002283 data->has_temp = 0x07;
2284 if (sio_data->skip_temp & (1 << 2)) {
2285 if (sio_data->type == it8782
2286 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2287 data->has_temp &= ~(1 << 2);
2288 }
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002291 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002294 err = sysfs_create_group(&dev->kobj, &it87_group);
2295 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002296 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002297
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002298 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002299 if (sio_data->skip_in & (1 << i))
2300 continue;
2301 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2302 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002303 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002304 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2305 err = sysfs_create_file(&dev->kobj,
2306 it87_attributes_in_beep[i]);
2307 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002308 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002309 }
2310 }
2311
Guenter Roeck4573acb2012-03-26 16:17:41 -07002312 for (i = 0; i < 3; i++) {
2313 if (!(data->has_temp & (1 << i)))
2314 continue;
2315 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002316 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002317 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002318 if (has_temp_offset(data)) {
2319 err = sysfs_create_file(&dev->kobj,
2320 it87_attributes_temp_offset[i]);
2321 if (err)
2322 goto error;
2323 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002324 if (sio_data->beep_pin) {
2325 err = sysfs_create_file(&dev->kobj,
2326 it87_attributes_temp_beep[i]);
2327 if (err)
2328 goto error;
2329 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002330 }
2331
Jean Delvare9060f8b2006-08-28 14:24:17 +02002332 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002333 fan_beep_need_rw = 1;
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002334 for (i = 0; i < 6; i++) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002335 if (!(data->has_fan & (1 << i)))
2336 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002337 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002338 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002339 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002340
Guenter Roecke1169ba2012-12-19 22:17:01 +01002341 if (i < 3 && !has_16bit_fans(data)) {
2342 err = sysfs_create_file(&dev->kobj,
2343 it87_attributes_fan_div[i]);
2344 if (err)
2345 goto error;
2346 }
2347
Jean Delvared9b327c2010-03-05 22:17:17 +01002348 if (sio_data->beep_pin) {
2349 err = sysfs_create_file(&dev->kobj,
2350 it87_attributes_fan_beep[i]);
2351 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002352 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002353 if (!fan_beep_need_rw)
2354 continue;
2355
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002356 /*
2357 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002358 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002359 * for it.
2360 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002361 if (sysfs_chmod_file(&dev->kobj,
2362 it87_attributes_fan_beep[i],
2363 S_IRUGO | S_IWUSR))
2364 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2365 i + 1);
2366 fan_beep_need_rw = 0;
2367 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002368 }
2369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002371 for (i = 0; i < 3; i++) {
2372 if (sio_data->skip_pwm & (1 << i))
2373 continue;
2374 err = sysfs_create_group(&dev->kobj,
2375 &it87_group_pwm[i]);
2376 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002377 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002378
2379 if (!has_old_autopwm(data))
2380 continue;
2381 err = sysfs_create_group(&dev->kobj,
2382 &it87_group_autopwm[i]);
2383 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002384 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387
Jean Delvare895ff262009-12-09 20:35:47 +01002388 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002389 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002390 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002391 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002392 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2393 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002394 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002395 }
2396
Jean Delvare738e5e02010-08-14 21:08:50 +02002397 /* Export labels for internal sensors */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002398 for (i = 0; i < 4; i++) {
Jean Delvare738e5e02010-08-14 21:08:50 +02002399 if (!(sio_data->internal & (1 << i)))
2400 continue;
2401 err = sysfs_create_file(&dev->kobj,
2402 it87_attributes_label[i]);
2403 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002404 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002405 }
2406
Tony Jones1beeffe2007-08-20 13:46:20 -07002407 data->hwmon_dev = hwmon_device_register(dev);
2408 if (IS_ERR(data->hwmon_dev)) {
2409 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002410 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
2412
2413 return 0;
2414
Guenter Roeck62a1d052012-03-24 21:54:41 -07002415error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002416 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return err;
2418}
2419
Bill Pemberton281dfd02012-11-19 13:25:51 -05002420static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002422 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Tony Jones1beeffe2007-08-20 13:46:20 -07002424 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002425 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return 0;
2428}
2429
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002430/*
2431 * Must be called with data->update_lock held, except during initialization.
2432 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2433 * would slow down the IT87 access and should not be necessary.
2434 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002435static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002437 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2438 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002441/*
2442 * Must be called with data->update_lock held, except during initialization.
2443 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2444 * would slow down the IT87 access and should not be necessary.
2445 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002446static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002448 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2449 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
2452/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002453static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002455 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002456 /*
2457 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002459 * disable pwm control to protect the user.
2460 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002461 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if ((tmp & 0x87) == 0) {
2463 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002464 /*
2465 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002467 * inverting all fan speed values.
2468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 int i;
2470 u8 pwm[3];
2471
2472 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002473 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 IT87_REG_PWM(i));
2475
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002476 /*
2477 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 * might be correct, as suspicious as it seems, so we
2479 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002480 * PWM interface).
2481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002483 dev_info(dev,
2484 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002485 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 tmp | 0x87);
2487 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002488 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 IT87_REG_PWM(i),
2490 0x7f & ~pwm[i]);
2491 return 1;
2492 }
2493
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002494 dev_info(dev,
2495 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
2497
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002498 dev_info(dev,
2499 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return 0;
2501 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002502 dev_info(dev,
2503 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 }
2505
2506 return 1;
2507}
2508
2509/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002510static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002512 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002513 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002515 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002517 /*
2518 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002519 * - If it is in automatic mode, setting to manual mode should set
2520 * the fan to full speed by default.
2521 * - If it is in manual mode, we need a mapping to temperature
2522 * channels to use when later setting to automatic mode later.
2523 * Use a 1:1 mapping by default (we are clueless.)
2524 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002525 * prior to switching to a different mode.
2526 * Note that this is no longer needed for the IT8721F and later, as
2527 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002528 * manual duty cycle.
2529 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002531 data->pwm_temp_map[i] = i;
2532 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002533 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002536 /*
2537 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002538 * registers. For low voltage limits it makes no sense and triggers
2539 * alarms, so change to 0 instead. For high temperature limits, it
2540 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002541 * but is still confusing, so change to 127 degrees C.
2542 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002543 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002544 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002545 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002546 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002547 }
2548 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002549 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002550 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002551 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002552 }
2553
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002554 /*
2555 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002556 * set to two different sensor types and we can't guess which one
2557 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002558 * run-time through the temp{1-3}_type sysfs accessors if needed.
2559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002562 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if ((tmp & 0xff) == 0) {
2564 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002565 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567
2568 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002569 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002570 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002571 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002573 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002574 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2575 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002577 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002579 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2580
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002581 /* Set tachometers to 16-bit mode if needed */
2582 if (has_fan16_config(data)) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002583 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002584 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002585 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002586 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002587 tmp | 0x07);
2588 }
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002589 }
2590
2591 /* Check for additional fans */
2592 if (has_five_fans(data)) {
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002593 if (tmp & (1 << 4))
2594 data->has_fan |= (1 << 3); /* fan4 enabled */
2595 if (tmp & (1 << 5))
2596 data->has_fan |= (1 << 4); /* fan5 enabled */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002597 if (has_six_fans(data) && (tmp & (1 << 2)))
2598 data->has_fan |= (1 << 5); /* fan6 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02002599 }
2600
Jean Delvare591ec652009-12-09 20:35:48 +01002601 /* Fan input pins may be used for alternative functions */
2602 data->has_fan &= ~sio_data->skip_fan;
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002605 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002606 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 | (update_vbat ? 0x41 : 0x01));
2608}
2609
Jean Delvareb99883d2010-03-05 22:17:15 +01002610static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2611{
2612 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002613 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002614 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002615 data->pwm_duty[nr] = it87_read_value(data,
2616 IT87_REG_PWM_DUTY(nr));
2617 } else {
2618 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2619 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2620 else /* Manual mode */
2621 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2622 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002623
2624 if (has_old_autopwm(data)) {
2625 int i;
2626
2627 for (i = 0; i < 5 ; i++)
2628 data->auto_temp[nr][i] = it87_read_value(data,
2629 IT87_REG_AUTO_TEMP(nr, i));
2630 for (i = 0; i < 3 ; i++)
2631 data->auto_pwm[nr][i] = it87_read_value(data,
2632 IT87_REG_AUTO_PWM(nr, i));
2633 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002634}
2635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636static struct it87_data *it87_update_device(struct device *dev)
2637{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002638 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 int i;
2640
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002641 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2644 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002646 /*
2647 * Cleared after each update, so reenable. Value
2648 * returned by this read will be previous value
2649 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002650 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002651 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 }
2653 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002654 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002655 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002656 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002657 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002658 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002659 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
Jean Delvare3543a532006-08-28 14:27:25 +02002661 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002662 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002663 if (data->type == it8603)
2664 data->in[9][0] = it87_read_value(data, 0x2f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002666 for (i = 0; i < 6; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002667 /* Skip disabled fans */
2668 if (!(data->has_fan & (1 << i)))
2669 continue;
2670
Guenter Roecke1169ba2012-12-19 22:17:01 +01002671 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002672 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002673 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002674 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002675 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002676 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002677 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002678 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002679 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002680 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
2683 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002684 if (!(data->has_temp & (1 << i)))
2685 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002686 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002687 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002688 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002689 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002690 data->temp[i][2] =
2691 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002692 if (has_temp_offset(data))
2693 data->temp[i][3] =
2694 it87_read_value(data,
2695 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 }
2697
Jean Delvare17d648b2006-08-28 14:23:46 +02002698 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002699 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002700 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002701 data->fan_div[0] = i & 0x07;
2702 data->fan_div[1] = (i >> 3) & 0x07;
2703 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002707 it87_read_value(data, IT87_REG_ALARM1) |
2708 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2709 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002710 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002711
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002712 data->fan_main_ctrl = it87_read_value(data,
2713 IT87_REG_FAN_MAIN_CTRL);
2714 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002715 for (i = 0; i < 3; i++)
2716 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002718 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002719 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002720 /*
2721 * The IT8705F does not have VID capability.
2722 * The IT8718F and later don't use IT87_REG_VID for the
2723 * same purpose.
2724 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002725 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002726 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002727 /*
2728 * The older IT8712F revisions had only 5 VID pins,
2729 * but we assume it is always safe to read 6 bits.
2730 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002731 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 }
2733 data->last_updated = jiffies;
2734 data->valid = 1;
2735 }
2736
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002737 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 return data;
2740}
2741
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002742static int __init it87_device_add(unsigned short address,
2743 const struct it87_sio_data *sio_data)
2744{
2745 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002746 .start = address + IT87_EC_OFFSET,
2747 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002748 .name = DRVNAME,
2749 .flags = IORESOURCE_IO,
2750 };
2751 int err;
2752
Jean Delvareb9acb642009-01-07 16:37:35 +01002753 err = acpi_check_resource_conflict(&res);
2754 if (err)
2755 goto exit;
2756
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002757 pdev = platform_device_alloc(DRVNAME, address);
2758 if (!pdev) {
2759 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002760 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002761 goto exit;
2762 }
2763
2764 err = platform_device_add_resources(pdev, &res, 1);
2765 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002766 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002767 goto exit_device_put;
2768 }
2769
2770 err = platform_device_add_data(pdev, sio_data,
2771 sizeof(struct it87_sio_data));
2772 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002773 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002774 goto exit_device_put;
2775 }
2776
2777 err = platform_device_add(pdev);
2778 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002779 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002780 goto exit_device_put;
2781 }
2782
2783 return 0;
2784
2785exit_device_put:
2786 platform_device_put(pdev);
2787exit:
2788 return err;
2789}
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791static int __init sm_it87_init(void)
2792{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002793 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002794 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002795 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002796
Jean Delvare98dd22c2008-10-09 15:33:58 +02002797 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002798 err = it87_find(&isa_address, &sio_data);
2799 if (err)
2800 return err;
2801 err = platform_driver_register(&it87_driver);
2802 if (err)
2803 return err;
2804
2805 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002806 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002807 platform_driver_unregister(&it87_driver);
2808 return err;
2809 }
2810
2811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
2814static void __exit sm_it87_exit(void)
2815{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002816 platform_device_unregister(pdev);
2817 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
2819
2820
Jean Delvare7c81c602014-01-29 20:40:08 +01002821MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002822MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823module_param(update_vbat, bool, 0);
2824MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2825module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002826MODULE_PARM_DESC(fix_pwm_polarity,
2827 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828MODULE_LICENSE("GPL");
2829
2830module_init(sm_it87_init);
2831module_exit(sm_it87_exit);