blob: 81a43db371f760ca6685a993ff1e7b9ddece2f73 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jean Delvare5f2dc792010-03-05 22:17:18 +01002 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring.
4 *
5 * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 * addition to an Environment Controller (Enhanced Hardware Monitor and
8 * Fan Controller)
9 *
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
12 *
Rudolf Marekc145d5c2014-01-29 20:40:08 +010013 * Supports: IT8603E Super I/O chip w/LPC interface
Rudolf Marek574e9bd2014-04-04 18:01:35 +020014 * IT8623E Super I/O chip w/LPC interface
Rudolf Marekc145d5c2014-01-29 20:40:08 +010015 * IT8705F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010016 * IT8712F Super I/O chip w/LPC interface
17 * IT8716F Super I/O chip w/LPC interface
18 * IT8718F Super I/O chip w/LPC interface
19 * IT8720F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020020 * IT8721F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010021 * IT8726F Super I/O chip w/LPC interface
Jean Delvare16b5dda2012-01-16 22:51:48 +010022 * IT8728F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020023 * IT8758E Super I/O chip w/LPC interface
Guenter Roeckb0636702012-09-07 17:34:41 -060024 * IT8771E Super I/O chip w/LPC interface
25 * IT8772E Super I/O chip w/LPC interface
Guenter Roeck7bc32d22015-01-17 14:10:24 -080026 * IT8781F Super I/O chip w/LPC interface
Guenter Roeck0531d982012-03-02 11:46:44 -080027 * IT8782F Super I/O chip w/LPC interface
28 * IT8783E/F Super I/O chip w/LPC interface
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +010029 * IT8786E Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010030 * Sis950 A clone of the IT8705F
31 *
32 * Copyright (C) 2001 Chris Gauthron
Jean Delvare7c81c602014-01-29 20:40:08 +010033 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
Jean Delvare5f2dc792010-03-05 22:17:18 +010034 *
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License
46 * along with this program; if not, write to the Free Software
47 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
48 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Joe Perchesa8ca1032011-01-12 21:55:10 +010050#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/module.h>
53#include <linux/init.h>
54#include <linux/slab.h>
55#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020056#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040057#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020058#include <linux/hwmon-sysfs.h>
59#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040060#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010061#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020062#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020063#include <linux/string.h>
64#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010065#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020066#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
corentin.labbeb74f3fd2007-06-13 20:27:36 +020068#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Guenter Roeckb0636702012-09-07 17:34:41 -060070enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8771,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +010071 it8772, it8781, it8782, it8783, it8786, it8603 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Jean Delvare67b671b2007-12-06 23:13:42 +010073static unsigned short force_id;
74module_param(force_id, ushort, 0);
75MODULE_PARM_DESC(force_id, "Override the detected device ID");
76
corentin.labbeb74f3fd2007-06-13 20:27:36 +020077static struct platform_device *pdev;
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define REG 0x2e /* The register to read/write */
80#define DEV 0x07 /* Register: Logical device select */
81#define VAL 0x2f /* The value to read/write */
82#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010083
84/* The device with the IT8718F/IT8720F VID value in it */
85#define GPIO 0x07
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define DEVID 0x20 /* Register: Device ID */
88#define DEVREV 0x22 /* Register: Device Revision */
89
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020090static inline int superio_inb(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 outb(reg, REG);
93 return inb(VAL);
94}
95
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020096static inline void superio_outb(int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +020097{
98 outb(reg, REG);
99 outb(val, VAL);
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int superio_inw(int reg)
103{
104 int val;
105 outb(reg++, REG);
106 val = inb(VAL) << 8;
107 outb(reg, REG);
108 val |= inb(VAL);
109 return val;
110}
111
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200112static inline void superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200115 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200118static inline int superio_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200120 /*
121 * Try to reserve REG and REG + 1 for exclusive access.
122 */
123 if (!request_muxed_region(REG, 2, DRVNAME))
124 return -EBUSY;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 outb(0x87, REG);
127 outb(0x01, REG);
128 outb(0x55, REG);
129 outb(0x55, REG);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200133static inline void superio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 outb(0x02, REG);
136 outb(0x02, VAL);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200137 release_region(REG, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Jean Delvare87673dd2006-08-28 14:37:19 +0200140/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define IT8712F_DEVID 0x8712
142#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200143#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200144#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100145#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200146#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400147#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100148#define IT8728F_DEVID 0x8728
Guenter Roeckb0636702012-09-07 17:34:41 -0600149#define IT8771E_DEVID 0x8771
150#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800151#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800152#define IT8782F_DEVID 0x8782
153#define IT8783E_DEVID 0x8783
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100154#define IT8786E_DEVID 0x8786
Rudolf Marek7183ae82014-04-04 18:01:35 +0200155#define IT8603E_DEVID 0x8603
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200156#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define IT87_ACT_REG 0x30
158#define IT87_BASE_REG 0x60
159
Jean Delvare87673dd2006-08-28 14:37:19 +0200160/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800161#define IT87_SIO_GPIO1_REG 0x25
Jean Delvare895ff262009-12-09 20:35:47 +0100162#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100163#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800164#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200165#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800166#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200167#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100168#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030171static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030174static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/* Many IT87 constants specified below */
177
178/* Length of ISA address segment */
179#define IT87_EXTENT 8
180
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500181/* Length of ISA address segment for Environmental Controller */
182#define IT87_EC_EXTENT 2
183
184/* Offset of EC registers from ISA base address */
185#define IT87_EC_OFFSET 5
186
187/* Where are the ISA address/data registers relative to the EC base address */
188#define IT87_ADDR_REG_OFFSET 0
189#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/*----- The IT87 registers -----*/
192
193#define IT87_REG_CONFIG 0x00
194
195#define IT87_REG_ALARM1 0x01
196#define IT87_REG_ALARM2 0x02
197#define IT87_REG_ALARM3 0x03
198
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800199/*
200 * The IT8718F and IT8720F have the VID value in a different register, in
201 * Super-I/O configuration space.
202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800204/*
205 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
206 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
207 * mode.
208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200210#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
213
Jean Delvarec7f1f712007-09-03 17:11:46 +0200214static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
215static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
216static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
217static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100218static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define IT87_REG_FAN_MAIN_CTRL 0x13
221#define IT87_REG_FAN_CTL 0x14
222#define IT87_REG_PWM(nr) (0x15 + (nr))
Jean Delvare6229cdb2010-12-08 16:27:22 +0100223#define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225#define IT87_REG_VIN(nr) (0x20 + (nr))
226#define IT87_REG_TEMP(nr) (0x29 + (nr))
227
228#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
229#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
230#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
231#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#define IT87_REG_VIN_ENABLE 0x50
234#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700235#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100236#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238#define IT87_REG_CHIPID 0x58
239
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100240#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
241#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
242
Guenter Roeck483db432012-12-19 22:17:02 +0100243struct it87_devices {
244 const char *name;
245 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100246 u8 peci_mask;
247 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100248};
249
250#define FEAT_12MV_ADC (1 << 0)
251#define FEAT_NEWER_AUTOPWM (1 << 1)
252#define FEAT_OLD_AUTOPWM (1 << 2)
253#define FEAT_16BIT_FANS (1 << 3)
254#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100255#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100256#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800257#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
258#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800259#define FEAT_VID (1 << 9) /* Set if chip supports VID */
Guenter Roeck483db432012-12-19 22:17:02 +0100260
261static const struct it87_devices it87_devices[] = {
262 [it87] = {
263 .name = "it87",
264 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
265 },
266 [it8712] = {
267 .name = "it8712",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800268 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
269 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100270 },
271 [it8716] = {
272 .name = "it8716",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800273 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800274 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck483db432012-12-19 22:17:02 +0100275 },
276 [it8718] = {
277 .name = "it8718",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800278 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800279 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100280 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100281 },
282 [it8720] = {
283 .name = "it8720",
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_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100286 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100287 },
288 [it8721] = {
289 .name = "it8721",
290 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800291 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
292 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100293 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100294 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100295 },
296 [it8728] = {
297 .name = "it8728",
298 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800299 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100300 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100301 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600302 [it8771] = {
303 .name = "it8771",
304 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
305 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800306 /* PECI: guesswork */
307 /* 12mV ADC (OHM) */
308 /* 16 bit fans (OHM) */
309 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600310 .peci_mask = 0x07,
311 },
312 [it8772] = {
313 .name = "it8772",
314 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
315 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800316 /* PECI (coreboot) */
317 /* 12mV ADC (HWSensors4, OHM) */
318 /* 16 bit fans (HWSensors4, OHM) */
319 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600320 .peci_mask = 0x07,
321 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800322 [it8781] = {
323 .name = "it8781",
324 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800325 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800326 .old_peci_mask = 0x4,
327 },
Guenter Roeck483db432012-12-19 22:17:02 +0100328 [it8782] = {
329 .name = "it8782",
Guenter Roeck19529782012-12-19 22:17:02 +0100330 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800331 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100332 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100333 },
334 [it8783] = {
335 .name = "it8783",
Guenter Roeck19529782012-12-19 22:17:02 +0100336 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800337 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100338 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100339 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100340 [it8786] = {
341 .name = "it8786",
342 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
343 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
344 .peci_mask = 0x07,
345 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100346 [it8603] = {
347 .name = "it8603",
348 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
349 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
350 .peci_mask = 0x07,
351 },
Guenter Roeck483db432012-12-19 22:17:02 +0100352};
353
354#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
355#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
356#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
357#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
358#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100359#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
360 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100361#define has_temp_old_peci(data, nr) \
362 (((data)->features & FEAT_TEMP_OLD_PECI) && \
363 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800364#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
365#define has_five_fans(data) ((data)->features & FEAT_FIVE_FANS)
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800366#define has_vid(data) ((data)->features & FEAT_VID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200368struct it87_sio_data {
369 enum chips type;
370 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200371 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200372 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100373 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200374 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100375 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700376 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100377 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100378 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200379 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700380 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200381};
382
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800383/*
384 * For each registered chip, we need to keep some data in memory.
385 * The structure is dynamically allocated.
386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700388 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100390 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100391 u8 peci_mask;
392 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200394 unsigned short addr;
395 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100396 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 char valid; /* !=0 if following fields are valid */
398 unsigned long last_updated; /* In jiffies */
399
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200400 u16 in_scaled; /* Internal voltage sensors are scaled */
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100401 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200402 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100403 u16 fan[5][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700404 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck161d8982012-12-19 22:17:01 +0100405 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100406 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
407 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 u8 fan_div[3]; /* Register encoding, shifted right */
409 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100410 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100412 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100414 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100415
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800416 /*
417 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100418 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
419 * 7, and we want to preserve settings on mode changes, so we have
420 * to track all values separately.
421 * Starting with the IT8721F, the manual PWM duty cycles are stored
422 * in separate registers (8-bit values), so the separate tracking
423 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800424 * simple.
425 */
Jean Delvareb99883d2010-03-05 22:17:15 +0100426 u8 pwm_ctrl[3]; /* Register value */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100427 u8 pwm_duty[3]; /* Manual PWM value set by user */
Jean Delvareb99883d2010-03-05 22:17:15 +0100428 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100429
430 /* Automatic fan speed control registers */
431 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
432 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433};
434
Guenter Roeck0531d982012-03-02 11:46:44 -0800435static int adc_lsb(const struct it87_data *data, int nr)
436{
437 int lsb = has_12mv_adc(data) ? 12 : 16;
438 if (data->in_scaled & (1 << nr))
439 lsb <<= 1;
440 return lsb;
441}
442
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200443static u8 in_to_reg(const struct it87_data *data, int nr, long val)
444{
Guenter Roeck0531d982012-03-02 11:46:44 -0800445 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800446 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200447}
448
449static int in_from_reg(const struct it87_data *data, int nr, int val)
450{
Guenter Roeck0531d982012-03-02 11:46:44 -0800451 return val * adc_lsb(data, nr);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200452}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200453
454static inline u8 FAN_TO_REG(long rpm, int div)
455{
456 if (rpm == 0)
457 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800458 rpm = clamp_val(rpm, 1, 1000000);
459 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200460}
461
462static inline u16 FAN16_TO_REG(long rpm)
463{
464 if (rpm == 0)
465 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800466 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200467}
468
469#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
470 1350000 / ((val) * (div)))
471/* The divider is fixed to 2 in 16-bit mode */
472#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
473 1350000 / ((val) * 2))
474
Guenter Roeck2a844c12013-01-09 08:09:34 -0800475#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
476 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200477#define TEMP_FROM_REG(val) ((val) * 1000)
478
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200479static u8 pwm_to_reg(const struct it87_data *data, long val)
480{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100481 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200482 return val;
483 else
484 return val >> 1;
485}
486
487static int pwm_from_reg(const struct it87_data *data, u8 reg)
488{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100489 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200490 return reg;
491 else
492 return (reg & 0x7f) << 1;
493}
494
Jean Delvare0df6454d2010-10-28 20:31:51 +0200495
496static int DIV_TO_REG(int val)
497{
498 int answer = 0;
499 while (answer < 7 && (val >>= 1))
500 answer++;
501 return answer;
502}
503#define DIV_FROM_REG(val) (1 << (val))
504
505static const unsigned int pwm_freq[8] = {
506 48000000 / 128,
507 24000000 / 128,
508 12000000 / 128,
509 8000000 / 128,
510 6000000 / 128,
511 3000000 / 128,
512 1500000 / 128,
513 750000 / 128,
514};
515
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200516static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500517static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200519static int it87_read_value(struct it87_data *data, u8 reg);
520static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200522static int it87_check_pwm(struct device *dev);
523static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200526static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100527 .driver = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200528 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100529 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200530 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500531 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200532};
533
Jean Delvare20ad93d2005-06-05 11:53:25 +0200534static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100535 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100537 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
538 int nr = sattr->nr;
539 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100542 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Guenter Roeck929c6a52012-12-19 22:17:00 +0100545static ssize_t set_in(struct device *dev, struct device_attribute *attr,
546 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100548 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
549 int nr = sattr->nr;
550 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200551
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200552 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100553 unsigned long val;
554
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100555 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100556 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100558 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100559 data->in[nr][index] = in_to_reg(data, nr, val);
560 it87_write_value(data,
561 index == 1 ? IT87_REG_VIN_MIN(nr)
562 : IT87_REG_VIN_MAX(nr),
563 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100564 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return count;
566}
567
Guenter Roeck929c6a52012-12-19 22:17:00 +0100568static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
569static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
570 0, 1);
571static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
572 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Guenter Roeck929c6a52012-12-19 22:17:00 +0100574static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
575static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
576 1, 1);
577static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
578 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Guenter Roeck929c6a52012-12-19 22:17:00 +0100580static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
581static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
582 2, 1);
583static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
584 2, 2);
585
586static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
587static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
588 3, 1);
589static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
590 3, 2);
591
592static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
593static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
594 4, 1);
595static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
596 4, 2);
597
598static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
599static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
600 5, 1);
601static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
602 5, 2);
603
604static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
605static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
606 6, 1);
607static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
608 6, 2);
609
610static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
611static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
612 7, 1);
613static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
614 7, 2);
615
616static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100617static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200620static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100621 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100623 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
624 int nr = sattr->nr;
625 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200627
Guenter Roeck60ca3852012-12-19 22:17:00 +0100628 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200630
Guenter Roeck60ca3852012-12-19 22:17:00 +0100631static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
632 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100634 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
635 int nr = sattr->nr;
636 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200637 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100638 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100639 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100640
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100641 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100642 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100644 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100645
646 switch (index) {
647 default:
648 case 1:
649 reg = IT87_REG_TEMP_LOW(nr);
650 break;
651 case 2:
652 reg = IT87_REG_TEMP_HIGH(nr);
653 break;
654 case 3:
655 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
656 if (!(regval & 0x80)) {
657 regval |= 0x80;
658 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
659 }
660 data->valid = 0;
661 reg = IT87_REG_TEMP_OFFSET[nr];
662 break;
663 }
664
Guenter Roeck60ca3852012-12-19 22:17:00 +0100665 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100666 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100667 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return count;
669}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200670
Guenter Roeck60ca3852012-12-19 22:17:00 +0100671static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
672static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
673 0, 1);
674static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
675 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100676static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
677 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100678static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
679static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
680 1, 1);
681static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
682 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100683static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
684 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100685static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
686static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
687 2, 1);
688static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
689 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100690static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
691 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Guenter Roeck2cece012012-12-19 22:17:01 +0100693static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
694 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200696 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
697 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800699 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100700 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100701
Guenter Roeck19529782012-12-19 22:17:02 +0100702 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
703 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100704 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (reg & (1 << nr))
706 return sprintf(buf, "3\n"); /* thermal diode */
707 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200708 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return sprintf(buf, "0\n"); /* disabled */
710}
Guenter Roeck2cece012012-12-19 22:17:01 +0100711
712static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
713 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200715 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
716 int nr = sensor_attr->index;
717
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200718 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100719 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100720 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100721
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100722 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100723 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Jean Delvare8acf07c2010-04-14 16:14:09 +0200725 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
726 reg &= ~(1 << nr);
727 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100728 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
729 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100730 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
731 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
732 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200733 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100734 dev_warn(dev,
735 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200736 val = 4;
737 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100738 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200740 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200741 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200742 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100743 else if (has_temp_peci(data, nr) && val == 6)
744 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100745 else if (has_temp_old_peci(data, nr) && val == 6)
746 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200747 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200749
750 mutex_lock(&data->update_lock);
751 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100752 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200753 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100754 if (has_temp_old_peci(data, nr))
755 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200756 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100757 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return count;
759}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Guenter Roeck2cece012012-12-19 22:17:01 +0100761static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
762 set_temp_type, 0);
763static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
764 set_temp_type, 1);
765static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
766 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100769
770static int pwm_mode(const struct it87_data *data, int nr)
771{
772 int ctrl = data->fan_main_ctrl & (1 << nr);
773
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100774 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +0100775 return 0;
776 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
777 return 2;
778 else /* Manual mode */
779 return 1;
780}
781
Jean Delvare20ad93d2005-06-05 11:53:25 +0200782static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +0100783 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100785 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
786 int nr = sattr->nr;
787 int index = sattr->index;
788 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200790
Guenter Roecke1169ba2012-12-19 22:17:01 +0100791 speed = has_16bit_fans(data) ?
792 FAN16_FROM_REG(data->fan[nr][index]) :
793 FAN_FROM_REG(data->fan[nr][index],
794 DIV_FROM_REG(data->fan_div[nr]));
795 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100797
Jean Delvare20ad93d2005-06-05 11:53:25 +0200798static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
799 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200801 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
802 int nr = sensor_attr->index;
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 struct it87_data *data = it87_update_device(dev);
805 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
806}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100807static ssize_t show_pwm_enable(struct device *dev,
808 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200810 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
811 int nr = sensor_attr->index;
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100814 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200816static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
817 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200819 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
820 int nr = sensor_attr->index;
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200823 return sprintf(buf, "%d\n",
824 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100826static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
827 char *buf)
828{
829 struct it87_data *data = it87_update_device(dev);
830 int index = (data->fan_ctl >> 4) & 0x07;
831
832 return sprintf(buf, "%u\n", pwm_freq[index]);
833}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100834
835static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
836 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100838 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
839 int nr = sattr->nr;
840 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200841
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200842 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100843 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100844 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100846 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100847 return -EINVAL;
848
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100849 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +0100850
851 if (has_16bit_fans(data)) {
852 data->fan[nr][index] = FAN16_TO_REG(val);
853 it87_write_value(data, IT87_REG_FAN_MIN[nr],
854 data->fan[nr][index] & 0xff);
855 it87_write_value(data, IT87_REG_FANX_MIN[nr],
856 data->fan[nr][index] >> 8);
857 } else {
858 reg = it87_read_value(data, IT87_REG_FAN_DIV);
859 switch (nr) {
860 case 0:
861 data->fan_div[nr] = reg & 0x07;
862 break;
863 case 1:
864 data->fan_div[nr] = (reg >> 3) & 0x07;
865 break;
866 case 2:
867 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
868 break;
869 }
870 data->fan[nr][index] =
871 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
872 it87_write_value(data, IT87_REG_FAN_MIN[nr],
873 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -0800874 }
875
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100876 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return count;
878}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100879
Jean Delvare20ad93d2005-06-05 11:53:25 +0200880static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
881 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200883 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
884 int nr = sensor_attr->index;
885
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200886 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100887 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200888 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 u8 old;
890
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100891 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100892 return -EINVAL;
893
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100894 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200895 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200897 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100898 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 switch (nr) {
901 case 0:
902 case 1:
903 data->fan_div[nr] = DIV_TO_REG(val);
904 break;
905 case 2:
906 if (val < 8)
907 data->fan_div[nr] = 1;
908 else
909 data->fan_div[nr] = 3;
910 }
911 val = old & 0x80;
912 val |= (data->fan_div[0] & 0x07);
913 val |= (data->fan_div[1] & 0x07) << 3;
914 if (data->fan_div[2] == 3)
915 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200916 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200918 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100919 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
920 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200921
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100922 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return count;
924}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100925
926/* Returns 0 if OK, -EINVAL otherwise */
927static int check_trip_points(struct device *dev, int nr)
928{
929 const struct it87_data *data = dev_get_drvdata(dev);
930 int i, err = 0;
931
932 if (has_old_autopwm(data)) {
933 for (i = 0; i < 3; i++) {
934 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
935 err = -EINVAL;
936 }
937 for (i = 0; i < 2; i++) {
938 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
939 err = -EINVAL;
940 }
941 }
942
943 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100944 dev_err(dev,
945 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100946 dev_err(dev, "Adjust the trip points and try again\n");
947 }
948 return err;
949}
950
Jean Delvare20ad93d2005-06-05 11:53:25 +0200951static ssize_t set_pwm_enable(struct device *dev,
952 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200954 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
955 int nr = sensor_attr->index;
956
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200957 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100958 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100960 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100961 return -EINVAL;
962
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100963 /* Check trip points before switching to automatic mode */
964 if (val == 2) {
965 if (check_trip_points(dev, nr) < 0)
966 return -EINVAL;
967 }
968
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100969 /* IT8603E does not have on/off mode */
970 if (val == 0 && data->type == it8603)
971 return -EINVAL;
972
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100973 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (val == 0) {
976 int tmp;
977 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200978 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
979 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* set on/off mode */
981 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100982 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
983 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100984 } else {
985 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +0100986 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +0100987 data->pwm_temp_map[nr] :
988 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +0100989 else /* Automatic mode */
990 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
991 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100992
993 if (data->type != it8603) {
994 /* set SmartGuardian mode */
995 data->fan_main_ctrl |= (1 << nr);
996 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
997 data->fan_main_ctrl);
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001001 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return count;
1003}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001004static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1005 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001007 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1008 int nr = sensor_attr->index;
1009
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001010 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001011 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001013 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return -EINVAL;
1015
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001016 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001017 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001018 /*
1019 * If we are in automatic mode, the PWM duty cycle register
1020 * is read-only so we can't write the value.
1021 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001022 if (data->pwm_ctrl[nr] & 0x80) {
1023 mutex_unlock(&data->update_lock);
1024 return -EBUSY;
1025 }
1026 data->pwm_duty[nr] = pwm_to_reg(data, val);
1027 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
1028 data->pwm_duty[nr]);
1029 } else {
1030 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001031 /*
1032 * If we are in manual mode, write the duty cycle immediately;
1033 * otherwise, just store it for later use.
1034 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001035 if (!(data->pwm_ctrl[nr] & 0x80)) {
1036 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1037 it87_write_value(data, IT87_REG_PWM(nr),
1038 data->pwm_ctrl[nr]);
1039 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001040 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001041 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return count;
1043}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001044static ssize_t set_pwm_freq(struct device *dev,
1045 struct device_attribute *attr, const char *buf, size_t count)
1046{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001047 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001048 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001049 int i;
1050
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001051 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001052 return -EINVAL;
1053
Jean Delvaref8d0c192007-02-14 21:15:02 +01001054 /* Search for the nearest available frequency */
1055 for (i = 0; i < 7; i++) {
1056 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1057 break;
1058 }
1059
1060 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001061 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001062 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001063 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001064 mutex_unlock(&data->update_lock);
1065
1066 return count;
1067}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001068static ssize_t show_pwm_temp_map(struct device *dev,
1069 struct device_attribute *attr, char *buf)
1070{
1071 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1072 int nr = sensor_attr->index;
1073
1074 struct it87_data *data = it87_update_device(dev);
1075 int map;
1076
1077 if (data->pwm_temp_map[nr] < 3)
1078 map = 1 << data->pwm_temp_map[nr];
1079 else
1080 map = 0; /* Should never happen */
1081 return sprintf(buf, "%d\n", map);
1082}
1083static ssize_t set_pwm_temp_map(struct device *dev,
1084 struct device_attribute *attr, const char *buf, size_t count)
1085{
1086 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1087 int nr = sensor_attr->index;
1088
1089 struct it87_data *data = dev_get_drvdata(dev);
1090 long val;
1091 u8 reg;
1092
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001093 /*
1094 * This check can go away if we ever support automatic fan speed
1095 * control on newer chips.
1096 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001097 if (!has_old_autopwm(data)) {
1098 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1099 return -EINVAL;
1100 }
1101
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001102 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001103 return -EINVAL;
1104
1105 switch (val) {
1106 case (1 << 0):
1107 reg = 0x00;
1108 break;
1109 case (1 << 1):
1110 reg = 0x01;
1111 break;
1112 case (1 << 2):
1113 reg = 0x02;
1114 break;
1115 default:
1116 return -EINVAL;
1117 }
1118
1119 mutex_lock(&data->update_lock);
1120 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001121 /*
1122 * If we are in automatic mode, write the temp mapping immediately;
1123 * otherwise, just store it for later use.
1124 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001125 if (data->pwm_ctrl[nr] & 0x80) {
1126 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1127 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1128 }
1129 mutex_unlock(&data->update_lock);
1130 return count;
1131}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001133static ssize_t show_auto_pwm(struct device *dev,
1134 struct device_attribute *attr, char *buf)
1135{
1136 struct it87_data *data = it87_update_device(dev);
1137 struct sensor_device_attribute_2 *sensor_attr =
1138 to_sensor_dev_attr_2(attr);
1139 int nr = sensor_attr->nr;
1140 int point = sensor_attr->index;
1141
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001142 return sprintf(buf, "%d\n",
1143 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001144}
1145
1146static ssize_t set_auto_pwm(struct device *dev,
1147 struct device_attribute *attr, const char *buf, size_t count)
1148{
1149 struct it87_data *data = dev_get_drvdata(dev);
1150 struct sensor_device_attribute_2 *sensor_attr =
1151 to_sensor_dev_attr_2(attr);
1152 int nr = sensor_attr->nr;
1153 int point = sensor_attr->index;
1154 long val;
1155
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001156 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001157 return -EINVAL;
1158
1159 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001160 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001161 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1162 data->auto_pwm[nr][point]);
1163 mutex_unlock(&data->update_lock);
1164 return count;
1165}
1166
1167static ssize_t show_auto_temp(struct device *dev,
1168 struct device_attribute *attr, char *buf)
1169{
1170 struct it87_data *data = it87_update_device(dev);
1171 struct sensor_device_attribute_2 *sensor_attr =
1172 to_sensor_dev_attr_2(attr);
1173 int nr = sensor_attr->nr;
1174 int point = sensor_attr->index;
1175
1176 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1177}
1178
1179static ssize_t set_auto_temp(struct device *dev,
1180 struct device_attribute *attr, const char *buf, size_t count)
1181{
1182 struct it87_data *data = dev_get_drvdata(dev);
1183 struct sensor_device_attribute_2 *sensor_attr =
1184 to_sensor_dev_attr_2(attr);
1185 int nr = sensor_attr->nr;
1186 int point = sensor_attr->index;
1187 long val;
1188
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001189 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001190 return -EINVAL;
1191
1192 mutex_lock(&data->update_lock);
1193 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1194 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1195 data->auto_temp[nr][point]);
1196 mutex_unlock(&data->update_lock);
1197 return count;
1198}
1199
Guenter Roecke1169ba2012-12-19 22:17:01 +01001200static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1201static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1202 0, 1);
1203static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1204 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Guenter Roecke1169ba2012-12-19 22:17:01 +01001206static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1207static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1208 1, 1);
1209static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1210 set_fan_div, 1);
1211
1212static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1213static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1214 2, 1);
1215static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1216 set_fan_div, 2);
1217
1218static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1219static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1220 3, 1);
1221
1222static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1223static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1224 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Guenter Roeckc4458db2012-12-19 22:17:02 +01001226static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1227 show_pwm_enable, set_pwm_enable, 0);
1228static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1229static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1230static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1231 show_pwm_temp_map, set_pwm_temp_map, 0);
1232static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1233 show_auto_pwm, set_auto_pwm, 0, 0);
1234static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1235 show_auto_pwm, set_auto_pwm, 0, 1);
1236static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1237 show_auto_pwm, set_auto_pwm, 0, 2);
1238static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1239 show_auto_pwm, NULL, 0, 3);
1240static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1241 show_auto_temp, set_auto_temp, 0, 1);
1242static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1243 show_auto_temp, set_auto_temp, 0, 0);
1244static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1245 show_auto_temp, set_auto_temp, 0, 2);
1246static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1247 show_auto_temp, set_auto_temp, 0, 3);
1248static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1249 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Guenter Roeckc4458db2012-12-19 22:17:02 +01001251static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1252 show_pwm_enable, set_pwm_enable, 1);
1253static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1254static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1255static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1256 show_pwm_temp_map, set_pwm_temp_map, 1);
1257static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1258 show_auto_pwm, set_auto_pwm, 1, 0);
1259static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1260 show_auto_pwm, set_auto_pwm, 1, 1);
1261static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1262 show_auto_pwm, set_auto_pwm, 1, 2);
1263static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1264 show_auto_pwm, NULL, 1, 3);
1265static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1266 show_auto_temp, set_auto_temp, 1, 1);
1267static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1268 show_auto_temp, set_auto_temp, 1, 0);
1269static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1270 show_auto_temp, set_auto_temp, 1, 2);
1271static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1272 show_auto_temp, set_auto_temp, 1, 3);
1273static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1274 show_auto_temp, set_auto_temp, 1, 4);
1275
1276static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1277 show_pwm_enable, set_pwm_enable, 2);
1278static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1279static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1280static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1281 show_pwm_temp_map, set_pwm_temp_map, 2);
1282static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1283 show_auto_pwm, set_auto_pwm, 2, 0);
1284static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1285 show_auto_pwm, set_auto_pwm, 2, 1);
1286static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1287 show_auto_pwm, set_auto_pwm, 2, 2);
1288static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1289 show_auto_pwm, NULL, 2, 3);
1290static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1291 show_auto_temp, set_auto_temp, 2, 1);
1292static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1293 show_auto_temp, set_auto_temp, 2, 0);
1294static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1295 show_auto_temp, set_auto_temp, 2, 2);
1296static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1297 show_auto_temp, set_auto_temp, 2, 3);
1298static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1299 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001302static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1303 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001306 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
Jean Delvare1d66c642005-04-18 21:16:59 -07001308static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Jean Delvare0124dd72007-11-25 16:16:41 +01001310static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1311 char *buf)
1312{
1313 int bitnr = to_sensor_dev_attr(attr)->index;
1314 struct it87_data *data = it87_update_device(dev);
1315 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1316}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001317
1318static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1319 *attr, const char *buf, size_t count)
1320{
1321 struct it87_data *data = dev_get_drvdata(dev);
1322 long val;
1323 int config;
1324
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001325 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001326 return -EINVAL;
1327
1328 mutex_lock(&data->update_lock);
1329 config = it87_read_value(data, IT87_REG_CONFIG);
1330 if (config < 0) {
1331 count = config;
1332 } else {
1333 config |= 1 << 5;
1334 it87_write_value(data, IT87_REG_CONFIG, config);
1335 /* Invalidate cache to force re-read */
1336 data->valid = 0;
1337 }
1338 mutex_unlock(&data->update_lock);
1339
1340 return count;
1341}
1342
Jean Delvare0124dd72007-11-25 16:16:41 +01001343static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1344static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1345static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1346static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1347static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1348static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1349static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1350static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1351static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1352static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1353static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1354static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1355static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1356static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1357static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1358static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001359static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1360 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001361
Jean Delvared9b327c2010-03-05 22:17:17 +01001362static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1363 char *buf)
1364{
1365 int bitnr = to_sensor_dev_attr(attr)->index;
1366 struct it87_data *data = it87_update_device(dev);
1367 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1368}
1369static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1370 const char *buf, size_t count)
1371{
1372 int bitnr = to_sensor_dev_attr(attr)->index;
1373 struct it87_data *data = dev_get_drvdata(dev);
1374 long val;
1375
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001376 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001377 || (val != 0 && val != 1))
1378 return -EINVAL;
1379
1380 mutex_lock(&data->update_lock);
1381 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1382 if (val)
1383 data->beeps |= (1 << bitnr);
1384 else
1385 data->beeps &= ~(1 << bitnr);
1386 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1387 mutex_unlock(&data->update_lock);
1388 return count;
1389}
1390
1391static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1392 show_beep, set_beep, 1);
1393static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1394static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1395static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1396static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1397static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1398static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1399static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1400/* fanX_beep writability is set later */
1401static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1402static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1403static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1404static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1405static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1406static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1407 show_beep, set_beep, 2);
1408static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1409static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1410
Jean Delvare5f2dc792010-03-05 22:17:18 +01001411static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1412 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Jean Delvare90d66192007-10-08 18:24:35 +02001414 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001415 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001417static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1418 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001420 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001421 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001423 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001424 return -EINVAL;
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 data->vrm = val;
1427
1428 return count;
1429}
1430static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Jean Delvare5f2dc792010-03-05 22:17:18 +01001432static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1433 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 struct it87_data *data = it87_update_device(dev);
1436 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1437}
1438static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001439
Jean Delvare738e5e02010-08-14 21:08:50 +02001440static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1441 char *buf)
1442{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001443 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001444 "+5V",
1445 "5VSB",
1446 "Vbat",
1447 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001448 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001449 "+3.3V",
1450 "3VSB",
1451 "Vbat",
1452 };
1453 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001454 int nr = to_sensor_dev_attr(attr)->index;
1455
Jean Delvare16b5dda2012-01-16 22:51:48 +01001456 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1457 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001458}
1459static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1460static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1461static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Rudolf Marek7183ae82014-04-04 18:01:35 +02001462/* special AVCC3 IT8603E in9 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001463static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001464
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001465static ssize_t show_name(struct device *dev, struct device_attribute
1466 *devattr, char *buf)
1467{
1468 struct it87_data *data = dev_get_drvdata(dev);
1469 return sprintf(buf, "%s\n", data->name);
1470}
1471static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1472
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001473static struct attribute *it87_attributes_in[10][5] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001474{
Jean Delvare87808be2006-09-24 21:17:13 +02001475 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001476 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001477 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001478 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001479 NULL
1480}, {
1481 &sensor_dev_attr_in1_input.dev_attr.attr,
1482 &sensor_dev_attr_in1_min.dev_attr.attr,
1483 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001484 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001485 NULL
1486}, {
1487 &sensor_dev_attr_in2_input.dev_attr.attr,
1488 &sensor_dev_attr_in2_min.dev_attr.attr,
1489 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001490 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001491 NULL
1492}, {
1493 &sensor_dev_attr_in3_input.dev_attr.attr,
1494 &sensor_dev_attr_in3_min.dev_attr.attr,
1495 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001496 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001497 NULL
1498}, {
1499 &sensor_dev_attr_in4_input.dev_attr.attr,
1500 &sensor_dev_attr_in4_min.dev_attr.attr,
1501 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001502 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001503 NULL
1504}, {
1505 &sensor_dev_attr_in5_input.dev_attr.attr,
1506 &sensor_dev_attr_in5_min.dev_attr.attr,
1507 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001508 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001509 NULL
1510}, {
1511 &sensor_dev_attr_in6_input.dev_attr.attr,
1512 &sensor_dev_attr_in6_min.dev_attr.attr,
1513 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001514 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001515 NULL
1516}, {
1517 &sensor_dev_attr_in7_input.dev_attr.attr,
1518 &sensor_dev_attr_in7_min.dev_attr.attr,
1519 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001520 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001521 NULL
1522}, {
1523 &sensor_dev_attr_in8_input.dev_attr.attr,
1524 NULL
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001525}, {
1526 &sensor_dev_attr_in9_input.dev_attr.attr,
1527 NULL
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001528} };
Jean Delvare87808be2006-09-24 21:17:13 +02001529
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001530static const struct attribute_group it87_group_in[10] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001531 { .attrs = it87_attributes_in[0] },
1532 { .attrs = it87_attributes_in[1] },
1533 { .attrs = it87_attributes_in[2] },
1534 { .attrs = it87_attributes_in[3] },
1535 { .attrs = it87_attributes_in[4] },
1536 { .attrs = it87_attributes_in[5] },
1537 { .attrs = it87_attributes_in[6] },
1538 { .attrs = it87_attributes_in[7] },
1539 { .attrs = it87_attributes_in[8] },
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001540 { .attrs = it87_attributes_in[9] },
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001541};
1542
Guenter Roeck4573acb2012-03-26 16:17:41 -07001543static struct attribute *it87_attributes_temp[3][6] = {
1544{
Jean Delvare87808be2006-09-24 21:17:13 +02001545 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001546 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001547 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001548 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001549 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001550 NULL
1551} , {
1552 &sensor_dev_attr_temp2_input.dev_attr.attr,
1553 &sensor_dev_attr_temp2_max.dev_attr.attr,
1554 &sensor_dev_attr_temp2_min.dev_attr.attr,
1555 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001556 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001557 NULL
1558} , {
1559 &sensor_dev_attr_temp3_input.dev_attr.attr,
1560 &sensor_dev_attr_temp3_max.dev_attr.attr,
1561 &sensor_dev_attr_temp3_min.dev_attr.attr,
1562 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001563 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001564 NULL
1565} };
Jean Delvare87808be2006-09-24 21:17:13 +02001566
Guenter Roeck4573acb2012-03-26 16:17:41 -07001567static const struct attribute_group it87_group_temp[3] = {
1568 { .attrs = it87_attributes_temp[0] },
1569 { .attrs = it87_attributes_temp[1] },
1570 { .attrs = it87_attributes_temp[2] },
1571};
1572
Guenter Roeck161d8982012-12-19 22:17:01 +01001573static struct attribute *it87_attributes_temp_offset[] = {
1574 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1575 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1576 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1577};
1578
Guenter Roeck4573acb2012-03-26 16:17:41 -07001579static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001580 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001581 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001582 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001583 NULL
1584};
1585
1586static const struct attribute_group it87_group = {
1587 .attrs = it87_attributes,
1588};
1589
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001590static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001591 &sensor_dev_attr_in0_beep.dev_attr.attr,
1592 &sensor_dev_attr_in1_beep.dev_attr.attr,
1593 &sensor_dev_attr_in2_beep.dev_attr.attr,
1594 &sensor_dev_attr_in3_beep.dev_attr.attr,
1595 &sensor_dev_attr_in4_beep.dev_attr.attr,
1596 &sensor_dev_attr_in5_beep.dev_attr.attr,
1597 &sensor_dev_attr_in6_beep.dev_attr.attr,
1598 &sensor_dev_attr_in7_beep.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001599 NULL,
1600 NULL,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001601};
Jean Delvared9b327c2010-03-05 22:17:17 +01001602
Guenter Roeck4573acb2012-03-26 16:17:41 -07001603static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001604 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1605 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1606 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001607};
1608
Guenter Roecke1169ba2012-12-19 22:17:01 +01001609static struct attribute *it87_attributes_fan[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001610 &sensor_dev_attr_fan1_input.dev_attr.attr,
1611 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001612 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1613 NULL
1614}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001615 &sensor_dev_attr_fan2_input.dev_attr.attr,
1616 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001617 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1618 NULL
1619}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001620 &sensor_dev_attr_fan3_input.dev_attr.attr,
1621 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001622 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001623 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001624}, {
1625 &sensor_dev_attr_fan4_input.dev_attr.attr,
1626 &sensor_dev_attr_fan4_min.dev_attr.attr,
1627 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1628 NULL
1629}, {
1630 &sensor_dev_attr_fan5_input.dev_attr.attr,
1631 &sensor_dev_attr_fan5_min.dev_attr.attr,
1632 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1633 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001634} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001635
Guenter Roecke1169ba2012-12-19 22:17:01 +01001636static const struct attribute_group it87_group_fan[5] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001637 { .attrs = it87_attributes_fan[0] },
1638 { .attrs = it87_attributes_fan[1] },
1639 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001640 { .attrs = it87_attributes_fan[3] },
1641 { .attrs = it87_attributes_fan[4] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001642};
1643
Guenter Roecke1169ba2012-12-19 22:17:01 +01001644static const struct attribute *it87_attributes_fan_div[] = {
1645 &sensor_dev_attr_fan1_div.dev_attr.attr,
1646 &sensor_dev_attr_fan2_div.dev_attr.attr,
1647 &sensor_dev_attr_fan3_div.dev_attr.attr,
1648};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001649
1650static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001651 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001652 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001653 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001654 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001655 NULL
1656}, {
1657 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1658 &sensor_dev_attr_pwm2.dev_attr.attr,
1659 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001660 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001661 NULL
1662}, {
1663 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1664 &sensor_dev_attr_pwm3.dev_attr.attr,
1665 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001666 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001667 NULL
1668} };
Jean Delvare87808be2006-09-24 21:17:13 +02001669
Jean Delvare723a0aa2010-03-05 22:17:16 +01001670static const struct attribute_group it87_group_pwm[3] = {
1671 { .attrs = it87_attributes_pwm[0] },
1672 { .attrs = it87_attributes_pwm[1] },
1673 { .attrs = it87_attributes_pwm[2] },
1674};
1675
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001676static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1677 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1678 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1679 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1680 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1681 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1682 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1683 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1684 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1685 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1686 NULL
1687}, {
1688 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1689 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1690 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1691 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1692 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1693 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1694 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1695 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1696 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1697 NULL
1698}, {
1699 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1700 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1701 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1702 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1703 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1704 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1705 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1706 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1707 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1708 NULL
1709} };
1710
1711static const struct attribute_group it87_group_autopwm[3] = {
1712 { .attrs = it87_attributes_autopwm[0] },
1713 { .attrs = it87_attributes_autopwm[1] },
1714 { .attrs = it87_attributes_autopwm[2] },
1715};
1716
Jean Delvared9b327c2010-03-05 22:17:17 +01001717static struct attribute *it87_attributes_fan_beep[] = {
1718 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1719 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1720 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1721 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1722 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1723};
1724
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001725static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001726 &dev_attr_vrm.attr,
1727 &dev_attr_cpu0_vid.attr,
1728 NULL
1729};
1730
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001731static const struct attribute_group it87_group_vid = {
1732 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001733};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Jean Delvare738e5e02010-08-14 21:08:50 +02001735static struct attribute *it87_attributes_label[] = {
1736 &sensor_dev_attr_in3_label.dev_attr.attr,
1737 &sensor_dev_attr_in7_label.dev_attr.attr,
1738 &sensor_dev_attr_in8_label.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001739 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare738e5e02010-08-14 21:08:50 +02001740 NULL
1741};
1742
1743static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001744 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001745};
1746
Jean Delvare2d8672c2005-07-19 23:56:35 +02001747/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001748static int __init it87_find(unsigned short *address,
1749 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001751 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001752 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001753 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001755 err = superio_enter();
1756 if (err)
1757 return err;
1758
1759 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001760 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001761
1762 switch (chip_type) {
1763 case IT8705F_DEVID:
1764 sio_data->type = it87;
1765 break;
1766 case IT8712F_DEVID:
1767 sio_data->type = it8712;
1768 break;
1769 case IT8716F_DEVID:
1770 case IT8726F_DEVID:
1771 sio_data->type = it8716;
1772 break;
1773 case IT8718F_DEVID:
1774 sio_data->type = it8718;
1775 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001776 case IT8720F_DEVID:
1777 sio_data->type = it8720;
1778 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001779 case IT8721F_DEVID:
1780 sio_data->type = it8721;
1781 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001782 case IT8728F_DEVID:
1783 sio_data->type = it8728;
1784 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06001785 case IT8771E_DEVID:
1786 sio_data->type = it8771;
1787 break;
1788 case IT8772E_DEVID:
1789 sio_data->type = it8772;
1790 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08001791 case IT8781F_DEVID:
1792 sio_data->type = it8781;
1793 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001794 case IT8782F_DEVID:
1795 sio_data->type = it8782;
1796 break;
1797 case IT8783E_DEVID:
1798 sio_data->type = it8783;
1799 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001800 case IT8786E_DEVID:
1801 sio_data->type = it8786;
1802 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02001803 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02001804 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001805 sio_data->type = it8603;
1806 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001807 case 0xffff: /* No device at all */
1808 goto exit;
1809 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001810 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001811 goto exit;
1812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Jean Delvare87673dd2006-08-28 14:37:19 +02001814 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001816 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 goto exit;
1818 }
1819
1820 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1821 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001822 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 goto exit;
1824 }
1825
1826 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001827 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001828 pr_info("Found IT%04x%c chip at 0x%x, revision %d\n", chip_type,
Jean Delvareb523bb72014-01-29 20:40:08 +01001829 chip_type == 0x8771 || chip_type == 0x8772 ||
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001830 chip_type == 0x8786 || chip_type == 0x8603 ? 'E' : 'F',
1831 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Jean Delvare738e5e02010-08-14 21:08:50 +02001833 /* in8 (Vbat) is always internal */
1834 sio_data->internal = (1 << 2);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001835 /* Only the IT8603E has in9 */
1836 if (sio_data->type != it8603)
1837 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02001838
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001839 if (!(it87_devices[sio_data->type].features & FEAT_VID))
Jean Delvare895ff262009-12-09 20:35:47 +01001840 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001841
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001842 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1843 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01001844 /* The IT8705F has a different LD number for GPIO */
1845 superio_select(5);
1846 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08001847 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001848 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08001849
Guenter Roeck0531d982012-03-02 11:46:44 -08001850 superio_select(GPIO);
1851
1852 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1853 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001854 reg2a = superio_inb(IT87_SIO_PINX1_REG);
1855 reg2c = superio_inb(IT87_SIO_PINX2_REG);
1856 regef = superio_inb(IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001857
Guenter Roeck0531d982012-03-02 11:46:44 -08001858 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001859 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08001860 sio_data->skip_fan |= (1 << 2);
1861 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001862 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08001863 sio_data->skip_pwm |= (1 << 2);
1864
1865 /* Check if fan2 is there or not */
1866 if (reg27 & (1 << 7))
1867 sio_data->skip_fan |= (1 << 1);
1868 if (reg27 & (1 << 3))
1869 sio_data->skip_pwm |= (1 << 1);
1870
1871 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001872 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001873 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001874
1875 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001876 if (reg27 & (1 << 1))
1877 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001878
1879 /*
1880 * VIN7
1881 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1882 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001883 if (reg27 & (1 << 2)) {
1884 /*
1885 * The data sheet is a bit unclear regarding the
1886 * internal voltage divider for VCCH5V. It says
1887 * "This bit enables and switches VIN7 (pin 91) to the
1888 * internal voltage divider for VCCH5V".
1889 * This is different to other chips, where the internal
1890 * voltage divider would connect VIN7 to an internal
1891 * voltage source. Maybe that is the case here as well.
1892 *
1893 * Since we don't know for sure, re-route it if that is
1894 * not the case, and ask the user to report if the
1895 * resulting voltage is sane.
1896 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001897 if (!(reg2c & (1 << 1))) {
1898 reg2c |= (1 << 1);
1899 superio_outb(IT87_SIO_PINX2_REG, reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001900 pr_notice("Routing internal VCCH5V to in7.\n");
1901 }
1902 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1903 pr_notice("Please report if it displays a reasonable voltage.\n");
1904 }
Guenter Roeck0531d982012-03-02 11:46:44 -08001905
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001906 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08001907 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001908 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08001909 sio_data->internal |= (1 << 1);
1910
1911 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001912 } else if (sio_data->type == it8603) {
1913 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08001914
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001915 superio_select(GPIO);
1916
1917 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1918
1919 /* Check if fan3 is there or not */
1920 if (reg27 & (1 << 6))
1921 sio_data->skip_pwm |= (1 << 2);
1922 if (reg27 & (1 << 7))
1923 sio_data->skip_fan |= (1 << 2);
1924
1925 /* Check if fan2 is there or not */
1926 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
1927 if (reg29 & (1 << 1))
1928 sio_data->skip_pwm |= (1 << 1);
1929 if (reg29 & (1 << 2))
1930 sio_data->skip_fan |= (1 << 1);
1931
1932 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1933 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1934
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001935 sio_data->internal |= (1 << 1); /* in7 is VSB */
1936 sio_data->internal |= (1 << 3); /* in9 is AVCC */
1937
1938 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01001939 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001940 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001941 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02001942
1943 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001944
Jean Delvare895ff262009-12-09 20:35:47 +01001945 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001946 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001947 /* We need at least 4 VID pins */
1948 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001949 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001950 sio_data->skip_vid = 1;
1951 }
Jean Delvare895ff262009-12-09 20:35:47 +01001952 }
1953
Jean Delvare591ec652009-12-09 20:35:48 +01001954 /* Check if fan3 is there or not */
1955 if (reg & (1 << 6))
1956 sio_data->skip_pwm |= (1 << 2);
1957 if (reg & (1 << 7))
1958 sio_data->skip_fan |= (1 << 2);
1959
1960 /* Check if fan2 is there or not */
1961 reg = superio_inb(IT87_SIO_GPIO5_REG);
1962 if (reg & (1 << 1))
1963 sio_data->skip_pwm |= (1 << 1);
1964 if (reg & (1 << 2))
1965 sio_data->skip_fan |= (1 << 1);
1966
Jean Delvare895ff262009-12-09 20:35:47 +01001967 if ((sio_data->type == it8718 || sio_data->type == it8720)
1968 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001969 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001970
1971 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001972
1973 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1974
Jean Delvare436cad22010-07-09 16:22:48 +02001975 /*
1976 * The IT8720F has no VIN7 pin, so VCCH should always be
1977 * routed internally to VIN7 with an internal divider.
1978 * Curiously, there still is a configuration bit to control
1979 * this, which means it can be set incorrectly. And even
1980 * more curiously, many boards out there are improperly
1981 * configured, even though the IT8720F datasheet claims
1982 * that the internal routing of VCCH to VIN7 is the default
1983 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08001984 *
1985 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001986 * If UART6 is enabled, re-route VIN7 to the internal divider
1987 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02001988 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001989 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02001990 reg |= (1 << 1);
1991 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01001992 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02001993 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001994 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001995 sio_data->internal |= (1 << 0);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001996 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001997 sio_data->type == it8728 || sio_data->type == it8771 ||
1998 sio_data->type == it8772 || sio_data->type == it8786)
Jean Delvare738e5e02010-08-14 21:08:50 +02001999 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002000
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002001 /*
2002 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2003 * While VIN7 can be routed to the internal voltage divider,
2004 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002005 *
2006 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2007 * is the temperature source. Since we can not read the
2008 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002009 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002010 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002011 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002012 sio_data->skip_temp |= (1 << 2);
2013 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002014
Jean Delvared9b327c2010-03-05 22:17:17 +01002015 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002016 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002017 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002018 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002019
Jean Delvare98dd22c2008-10-09 15:33:58 +02002020 /* Disable specific features based on DMI strings */
2021 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2022 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2023 if (board_vendor && board_name) {
2024 if (strcmp(board_vendor, "nVIDIA") == 0
2025 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002026 /*
2027 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2028 * connected to a fan, but to something else. One user
2029 * has reported instant system power-off when changing
2030 * the PWM2 duty cycle, so we disable it.
2031 * I use the board name string as the trigger in case
2032 * the same board is ever used in other systems.
2033 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002034 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002035 sio_data->skip_pwm = (1 << 1);
2036 }
2037 }
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039exit:
2040 superio_exit();
2041 return err;
2042}
2043
Jean Delvare723a0aa2010-03-05 22:17:16 +01002044static void it87_remove_files(struct device *dev)
2045{
2046 struct it87_data *data = platform_get_drvdata(pdev);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002047 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002048 int i;
2049
2050 sysfs_remove_group(&dev->kobj, &it87_group);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002051 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002052 if (sio_data->skip_in & (1 << i))
2053 continue;
2054 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2055 if (it87_attributes_in_beep[i])
2056 sysfs_remove_file(&dev->kobj,
2057 it87_attributes_in_beep[i]);
2058 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002059 for (i = 0; i < 3; i++) {
2060 if (!(data->has_temp & (1 << i)))
2061 continue;
2062 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01002063 if (has_temp_offset(data))
2064 sysfs_remove_file(&dev->kobj,
2065 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002066 if (sio_data->beep_pin)
2067 sysfs_remove_file(&dev->kobj,
2068 it87_attributes_temp_beep[i]);
2069 }
Jean Delvare723a0aa2010-03-05 22:17:16 +01002070 for (i = 0; i < 5; i++) {
2071 if (!(data->has_fan & (1 << i)))
2072 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002073 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002074 if (sio_data->beep_pin)
2075 sysfs_remove_file(&dev->kobj,
2076 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002077 if (i < 3 && !has_16bit_fans(data))
2078 sysfs_remove_file(&dev->kobj,
2079 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002080 }
2081 for (i = 0; i < 3; i++) {
Guenter Roeck1696d1d2015-03-27 06:03:41 -07002082 if (sio_data->skip_pwm & (1 << i))
Jean Delvare723a0aa2010-03-05 22:17:16 +01002083 continue;
2084 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002085 if (has_old_autopwm(data))
2086 sysfs_remove_group(&dev->kobj,
2087 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002088 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002089 if (!sio_data->skip_vid)
2090 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02002091 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002092}
2093
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002094static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002097 struct resource *res;
2098 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002099 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002100 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01002102 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002104 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002105 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2106 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002107 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2108 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002109 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002110 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Guenter Roeck62a1d052012-03-24 21:54:41 -07002113 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2114 if (!data)
2115 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002117 data->addr = res->start;
2118 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002119 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002120 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002121 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002122 data->name = it87_devices[sio_data->type].name;
2123 /*
2124 * IT8705F Datasheet 0.4.1, 3h == Version G.
2125 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2126 * These are the first revisions with 16-bit tachometer support.
2127 */
2128 switch (data->type) {
2129 case it87:
2130 if (sio_data->revision >= 0x03) {
2131 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002132 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002133 }
2134 break;
2135 case it8712:
2136 if (sio_data->revision >= 0x08) {
2137 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002138 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2139 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002140 }
2141 break;
2142 default:
2143 break;
2144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002147 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002148 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2149 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002151 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002153 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002156 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002158 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002159 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002160 if (sio_data->internal & (1 << 0))
2161 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2162 if (sio_data->internal & (1 << 1))
2163 data->in_scaled |= (1 << 7); /* in7 is VSB */
2164 if (sio_data->internal & (1 << 2))
2165 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002166 if (sio_data->internal & (1 << 3))
2167 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002168 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2169 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002170 if (sio_data->internal & (1 << 0))
2171 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2172 if (sio_data->internal & (1 << 1))
2173 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002174 }
2175
Guenter Roeck4573acb2012-03-26 16:17:41 -07002176 data->has_temp = 0x07;
2177 if (sio_data->skip_temp & (1 << 2)) {
2178 if (sio_data->type == it8782
2179 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2180 data->has_temp &= ~(1 << 2);
2181 }
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002184 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002187 err = sysfs_create_group(&dev->kobj, &it87_group);
2188 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002189 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002190
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002191 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002192 if (sio_data->skip_in & (1 << i))
2193 continue;
2194 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2195 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002196 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002197 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2198 err = sysfs_create_file(&dev->kobj,
2199 it87_attributes_in_beep[i]);
2200 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002201 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002202 }
2203 }
2204
Guenter Roeck4573acb2012-03-26 16:17:41 -07002205 for (i = 0; i < 3; i++) {
2206 if (!(data->has_temp & (1 << i)))
2207 continue;
2208 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002209 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002210 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002211 if (has_temp_offset(data)) {
2212 err = sysfs_create_file(&dev->kobj,
2213 it87_attributes_temp_offset[i]);
2214 if (err)
2215 goto error;
2216 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002217 if (sio_data->beep_pin) {
2218 err = sysfs_create_file(&dev->kobj,
2219 it87_attributes_temp_beep[i]);
2220 if (err)
2221 goto error;
2222 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002223 }
2224
Jean Delvare9060f8b2006-08-28 14:24:17 +02002225 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002226 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01002227 for (i = 0; i < 5; i++) {
2228 if (!(data->has_fan & (1 << i)))
2229 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002230 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002231 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002232 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002233
Guenter Roecke1169ba2012-12-19 22:17:01 +01002234 if (i < 3 && !has_16bit_fans(data)) {
2235 err = sysfs_create_file(&dev->kobj,
2236 it87_attributes_fan_div[i]);
2237 if (err)
2238 goto error;
2239 }
2240
Jean Delvared9b327c2010-03-05 22:17:17 +01002241 if (sio_data->beep_pin) {
2242 err = sysfs_create_file(&dev->kobj,
2243 it87_attributes_fan_beep[i]);
2244 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002245 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002246 if (!fan_beep_need_rw)
2247 continue;
2248
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002249 /*
2250 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002251 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002252 * for it.
2253 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002254 if (sysfs_chmod_file(&dev->kobj,
2255 it87_attributes_fan_beep[i],
2256 S_IRUGO | S_IWUSR))
2257 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2258 i + 1);
2259 fan_beep_need_rw = 0;
2260 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002261 }
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002264 for (i = 0; i < 3; i++) {
2265 if (sio_data->skip_pwm & (1 << i))
2266 continue;
2267 err = sysfs_create_group(&dev->kobj,
2268 &it87_group_pwm[i]);
2269 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002270 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002271
2272 if (!has_old_autopwm(data))
2273 continue;
2274 err = sysfs_create_group(&dev->kobj,
2275 &it87_group_autopwm[i]);
2276 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002277 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280
Jean Delvare895ff262009-12-09 20:35:47 +01002281 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002282 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002283 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002284 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002285 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2286 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002287 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002288 }
2289
Jean Delvare738e5e02010-08-14 21:08:50 +02002290 /* Export labels for internal sensors */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002291 for (i = 0; i < 4; i++) {
Jean Delvare738e5e02010-08-14 21:08:50 +02002292 if (!(sio_data->internal & (1 << i)))
2293 continue;
2294 err = sysfs_create_file(&dev->kobj,
2295 it87_attributes_label[i]);
2296 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002297 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002298 }
2299
Tony Jones1beeffe2007-08-20 13:46:20 -07002300 data->hwmon_dev = hwmon_device_register(dev);
2301 if (IS_ERR(data->hwmon_dev)) {
2302 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002303 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305
2306 return 0;
2307
Guenter Roeck62a1d052012-03-24 21:54:41 -07002308error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002309 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return err;
2311}
2312
Bill Pemberton281dfd02012-11-19 13:25:51 -05002313static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002315 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Tony Jones1beeffe2007-08-20 13:46:20 -07002317 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002318 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return 0;
2321}
2322
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002323/*
2324 * Must be called with data->update_lock held, except during initialization.
2325 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2326 * would slow down the IT87 access and should not be necessary.
2327 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002328static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002330 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2331 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002334/*
2335 * Must be called with data->update_lock held, except during initialization.
2336 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2337 * would slow down the IT87 access and should not be necessary.
2338 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002339static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002341 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2342 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
2345/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002346static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002348 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002349 /*
2350 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002352 * disable pwm control to protect the user.
2353 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002354 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if ((tmp & 0x87) == 0) {
2356 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002357 /*
2358 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002360 * inverting all fan speed values.
2361 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 int i;
2363 u8 pwm[3];
2364
2365 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002366 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 IT87_REG_PWM(i));
2368
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002369 /*
2370 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 * might be correct, as suspicious as it seems, so we
2372 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002373 * PWM interface).
2374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002376 dev_info(dev,
2377 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002378 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 tmp | 0x87);
2380 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002381 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 IT87_REG_PWM(i),
2383 0x7f & ~pwm[i]);
2384 return 1;
2385 }
2386
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002387 dev_info(dev,
2388 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002391 dev_info(dev,
2392 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 return 0;
2394 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002395 dev_info(dev,
2396 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398
2399 return 1;
2400}
2401
2402/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002403static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002405 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002406 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002408 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002410 /*
2411 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002412 * - If it is in automatic mode, setting to manual mode should set
2413 * the fan to full speed by default.
2414 * - If it is in manual mode, we need a mapping to temperature
2415 * channels to use when later setting to automatic mode later.
2416 * Use a 1:1 mapping by default (we are clueless.)
2417 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002418 * prior to switching to a different mode.
2419 * Note that this is no longer needed for the IT8721F and later, as
2420 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002421 * manual duty cycle.
2422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002424 data->pwm_temp_map[i] = i;
2425 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002426 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002429 /*
2430 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002431 * registers. For low voltage limits it makes no sense and triggers
2432 * alarms, so change to 0 instead. For high temperature limits, it
2433 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002434 * but is still confusing, so change to 127 degrees C.
2435 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002436 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002437 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002438 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002439 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002440 }
2441 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002442 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002443 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002444 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002445 }
2446
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002447 /*
2448 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002449 * set to two different sensor types and we can't guess which one
2450 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002451 * run-time through the temp{1-3}_type sysfs accessors if needed.
2452 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002455 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if ((tmp & 0xff) == 0) {
2457 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002458 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460
2461 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002462 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002463 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002464 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002466 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002467 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2468 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002470 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002472 /* Set tachometers to 16-bit mode if needed */
2473 if (has_fan16_config(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002474 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002475 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002476 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002477 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002478 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002479 tmp | 0x07);
2480 }
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002481 }
2482
2483 /* Check for additional fans */
2484 if (has_five_fans(data)) {
2485 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2486 if (tmp & (1 << 4))
2487 data->has_fan |= (1 << 3); /* fan4 enabled */
2488 if (tmp & (1 << 5))
2489 data->has_fan |= (1 << 4); /* fan5 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02002490 }
2491
Jean Delvare591ec652009-12-09 20:35:48 +01002492 /* Fan input pins may be used for alternative functions */
2493 data->has_fan &= ~sio_data->skip_fan;
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002496 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002497 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 | (update_vbat ? 0x41 : 0x01));
2499}
2500
Jean Delvareb99883d2010-03-05 22:17:15 +01002501static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2502{
2503 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002504 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002505 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002506 data->pwm_duty[nr] = it87_read_value(data,
2507 IT87_REG_PWM_DUTY(nr));
2508 } else {
2509 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2510 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2511 else /* Manual mode */
2512 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2513 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002514
2515 if (has_old_autopwm(data)) {
2516 int i;
2517
2518 for (i = 0; i < 5 ; i++)
2519 data->auto_temp[nr][i] = it87_read_value(data,
2520 IT87_REG_AUTO_TEMP(nr, i));
2521 for (i = 0; i < 3 ; i++)
2522 data->auto_pwm[nr][i] = it87_read_value(data,
2523 IT87_REG_AUTO_PWM(nr, i));
2524 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002525}
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527static struct it87_data *it87_update_device(struct device *dev)
2528{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002529 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 int i;
2531
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002532 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2535 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002537 /*
2538 * Cleared after each update, so reenable. Value
2539 * returned by this read will be previous value
2540 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002541 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002542 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002545 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002546 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002547 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002548 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002549 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002550 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
Jean Delvare3543a532006-08-28 14:27:25 +02002552 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002553 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002554 if (data->type == it8603)
2555 data->in[9][0] = it87_read_value(data, 0x2f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Jean Delvarec7f1f712007-09-03 17:11:46 +02002557 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002558 /* Skip disabled fans */
2559 if (!(data->has_fan & (1 << i)))
2560 continue;
2561
Guenter Roecke1169ba2012-12-19 22:17:01 +01002562 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002563 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002564 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002565 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002566 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002567 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002568 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002569 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002570 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002571 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
2574 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002575 if (!(data->has_temp & (1 << i)))
2576 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002577 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002578 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002579 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002580 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002581 data->temp[i][2] =
2582 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002583 if (has_temp_offset(data))
2584 data->temp[i][3] =
2585 it87_read_value(data,
2586 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
2588
Jean Delvare17d648b2006-08-28 14:23:46 +02002589 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002590 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002591 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002592 data->fan_div[0] = i & 0x07;
2593 data->fan_div[1] = (i >> 3) & 0x07;
2594 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002598 it87_read_value(data, IT87_REG_ALARM1) |
2599 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2600 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002601 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002602
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002603 data->fan_main_ctrl = it87_read_value(data,
2604 IT87_REG_FAN_MAIN_CTRL);
2605 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002606 for (i = 0; i < 3; i++)
2607 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002609 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002610 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002611 /*
2612 * The IT8705F does not have VID capability.
2613 * The IT8718F and later don't use IT87_REG_VID for the
2614 * same purpose.
2615 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002616 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002617 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002618 /*
2619 * The older IT8712F revisions had only 5 VID pins,
2620 * but we assume it is always safe to read 6 bits.
2621 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002622 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
2624 data->last_updated = jiffies;
2625 data->valid = 1;
2626 }
2627
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002628 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 return data;
2631}
2632
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002633static int __init it87_device_add(unsigned short address,
2634 const struct it87_sio_data *sio_data)
2635{
2636 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002637 .start = address + IT87_EC_OFFSET,
2638 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002639 .name = DRVNAME,
2640 .flags = IORESOURCE_IO,
2641 };
2642 int err;
2643
Jean Delvareb9acb642009-01-07 16:37:35 +01002644 err = acpi_check_resource_conflict(&res);
2645 if (err)
2646 goto exit;
2647
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002648 pdev = platform_device_alloc(DRVNAME, address);
2649 if (!pdev) {
2650 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002651 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002652 goto exit;
2653 }
2654
2655 err = platform_device_add_resources(pdev, &res, 1);
2656 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002657 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002658 goto exit_device_put;
2659 }
2660
2661 err = platform_device_add_data(pdev, sio_data,
2662 sizeof(struct it87_sio_data));
2663 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002664 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002665 goto exit_device_put;
2666 }
2667
2668 err = platform_device_add(pdev);
2669 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002670 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002671 goto exit_device_put;
2672 }
2673
2674 return 0;
2675
2676exit_device_put:
2677 platform_device_put(pdev);
2678exit:
2679 return err;
2680}
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682static int __init sm_it87_init(void)
2683{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002684 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002685 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002686 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002687
Jean Delvare98dd22c2008-10-09 15:33:58 +02002688 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002689 err = it87_find(&isa_address, &sio_data);
2690 if (err)
2691 return err;
2692 err = platform_driver_register(&it87_driver);
2693 if (err)
2694 return err;
2695
2696 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002697 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002698 platform_driver_unregister(&it87_driver);
2699 return err;
2700 }
2701
2702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
2705static void __exit sm_it87_exit(void)
2706{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002707 platform_device_unregister(pdev);
2708 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709}
2710
2711
Jean Delvare7c81c602014-01-29 20:40:08 +01002712MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002713MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714module_param(update_vbat, bool, 0);
2715MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2716module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002717MODULE_PARM_DESC(fix_pwm_polarity,
2718 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719MODULE_LICENSE("GPL");
2720
2721module_init(sm_it87_init);
2722module_exit(sm_it87_exit);