blob: 48b48939d8938a6c7ce9387e2f5c0d939b6eda6e [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
Jean Delvare5f2dc792010-03-05 22:17:18 +010029 * Sis950 A clone of the IT8705F
30 *
31 * Copyright (C) 2001 Chris Gauthron
Jean Delvare7c81c602014-01-29 20:40:08 +010032 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
Jean Delvare5f2dc792010-03-05 22:17:18 +010033 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License
45 * along with this program; if not, write to the Free Software
46 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
47 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Joe Perchesa8ca1032011-01-12 21:55:10 +010049#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/slab.h>
54#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020055#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040056#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020057#include <linux/hwmon-sysfs.h>
58#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040059#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010060#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020061#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020062#include <linux/string.h>
63#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010064#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020065#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
corentin.labbeb74f3fd2007-06-13 20:27:36 +020067#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Guenter Roeckb0636702012-09-07 17:34:41 -060069enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8771,
Guenter Roeck7bc32d22015-01-17 14:10:24 -080070 it8772, it8781, it8782, it8783, it8603 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jean Delvare67b671b2007-12-06 23:13:42 +010072static unsigned short force_id;
73module_param(force_id, ushort, 0);
74MODULE_PARM_DESC(force_id, "Override the detected device ID");
75
corentin.labbeb74f3fd2007-06-13 20:27:36 +020076static struct platform_device *pdev;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define REG 0x2e /* The register to read/write */
79#define DEV 0x07 /* Register: Logical device select */
80#define VAL 0x2f /* The value to read/write */
81#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010082
83/* The device with the IT8718F/IT8720F VID value in it */
84#define GPIO 0x07
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define DEVID 0x20 /* Register: Device ID */
87#define DEVREV 0x22 /* Register: Device Revision */
88
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020089static inline int superio_inb(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 outb(reg, REG);
92 return inb(VAL);
93}
94
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020095static inline void superio_outb(int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +020096{
97 outb(reg, REG);
98 outb(val, VAL);
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int superio_inw(int reg)
102{
103 int val;
104 outb(reg++, REG);
105 val = inb(VAL) << 8;
106 outb(reg, REG);
107 val |= inb(VAL);
108 return val;
109}
110
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200111static inline void superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200114 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200117static inline int superio_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200119 /*
120 * Try to reserve REG and REG + 1 for exclusive access.
121 */
122 if (!request_muxed_region(REG, 2, DRVNAME))
123 return -EBUSY;
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 outb(0x87, REG);
126 outb(0x01, REG);
127 outb(0x55, REG);
128 outb(0x55, REG);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200132static inline void superio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 outb(0x02, REG);
135 outb(0x02, VAL);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200136 release_region(REG, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Jean Delvare87673dd2006-08-28 14:37:19 +0200139/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#define IT8712F_DEVID 0x8712
141#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200142#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200143#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100144#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200145#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400146#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100147#define IT8728F_DEVID 0x8728
Guenter Roeckb0636702012-09-07 17:34:41 -0600148#define IT8771E_DEVID 0x8771
149#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800150#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800151#define IT8782F_DEVID 0x8782
152#define IT8783E_DEVID 0x8783
Rudolf Marek7183ae82014-04-04 18:01:35 +0200153#define IT8603E_DEVID 0x8603
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200154#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define IT87_ACT_REG 0x30
156#define IT87_BASE_REG 0x60
157
Jean Delvare87673dd2006-08-28 14:37:19 +0200158/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800159#define IT87_SIO_GPIO1_REG 0x25
Jean Delvare895ff262009-12-09 20:35:47 +0100160#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100161#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800162#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200163#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800164#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200165#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100166#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030169static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030172static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/* Many IT87 constants specified below */
175
176/* Length of ISA address segment */
177#define IT87_EXTENT 8
178
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500179/* Length of ISA address segment for Environmental Controller */
180#define IT87_EC_EXTENT 2
181
182/* Offset of EC registers from ISA base address */
183#define IT87_EC_OFFSET 5
184
185/* Where are the ISA address/data registers relative to the EC base address */
186#define IT87_ADDR_REG_OFFSET 0
187#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189/*----- The IT87 registers -----*/
190
191#define IT87_REG_CONFIG 0x00
192
193#define IT87_REG_ALARM1 0x01
194#define IT87_REG_ALARM2 0x02
195#define IT87_REG_ALARM3 0x03
196
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800197/*
198 * The IT8718F and IT8720F have the VID value in a different register, in
199 * Super-I/O configuration space.
200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800202/*
203 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
204 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
205 * mode.
206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200208#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
211
Jean Delvarec7f1f712007-09-03 17:11:46 +0200212static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
213static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
214static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
215static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100216static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#define IT87_REG_FAN_MAIN_CTRL 0x13
219#define IT87_REG_FAN_CTL 0x14
220#define IT87_REG_PWM(nr) (0x15 + (nr))
Jean Delvare6229cdb2010-12-08 16:27:22 +0100221#define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223#define IT87_REG_VIN(nr) (0x20 + (nr))
224#define IT87_REG_TEMP(nr) (0x29 + (nr))
225
226#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
227#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
228#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
229#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#define IT87_REG_VIN_ENABLE 0x50
232#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700233#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100234#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236#define IT87_REG_CHIPID 0x58
237
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100238#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
239#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
240
Guenter Roeck483db432012-12-19 22:17:02 +0100241struct it87_devices {
242 const char *name;
243 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100244 u8 peci_mask;
245 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100246};
247
248#define FEAT_12MV_ADC (1 << 0)
249#define FEAT_NEWER_AUTOPWM (1 << 1)
250#define FEAT_OLD_AUTOPWM (1 << 2)
251#define FEAT_16BIT_FANS (1 << 3)
252#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100253#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100254#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800255#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
256#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck483db432012-12-19 22:17:02 +0100257
258static const struct it87_devices it87_devices[] = {
259 [it87] = {
260 .name = "it87",
261 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
262 },
263 [it8712] = {
264 .name = "it8712",
265 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
266 },
267 [it8716] = {
268 .name = "it8716",
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800269 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
270 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck483db432012-12-19 22:17:02 +0100271 },
272 [it8718] = {
273 .name = "it8718",
Guenter Roeck19529782012-12-19 22:17:02 +0100274 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800275 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100276 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100277 },
278 [it8720] = {
279 .name = "it8720",
Guenter Roeck19529782012-12-19 22:17:02 +0100280 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800281 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100282 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100283 },
284 [it8721] = {
285 .name = "it8721",
286 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800287 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
288 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100289 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100290 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100291 },
292 [it8728] = {
293 .name = "it8728",
294 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800295 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100296 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100297 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600298 [it8771] = {
299 .name = "it8771",
300 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
301 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800302 /* PECI: guesswork */
303 /* 12mV ADC (OHM) */
304 /* 16 bit fans (OHM) */
305 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600306 .peci_mask = 0x07,
307 },
308 [it8772] = {
309 .name = "it8772",
310 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
311 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800312 /* PECI (coreboot) */
313 /* 12mV ADC (HWSensors4, OHM) */
314 /* 16 bit fans (HWSensors4, OHM) */
315 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600316 .peci_mask = 0x07,
317 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800318 [it8781] = {
319 .name = "it8781",
320 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800321 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800322 .old_peci_mask = 0x4,
323 },
Guenter Roeck483db432012-12-19 22:17:02 +0100324 [it8782] = {
325 .name = "it8782",
Guenter Roeck19529782012-12-19 22:17:02 +0100326 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800327 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100328 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100329 },
330 [it8783] = {
331 .name = "it8783",
Guenter Roeck19529782012-12-19 22:17:02 +0100332 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800333 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100334 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100335 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100336 [it8603] = {
337 .name = "it8603",
338 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
339 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
340 .peci_mask = 0x07,
341 },
Guenter Roeck483db432012-12-19 22:17:02 +0100342};
343
344#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
345#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
346#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
347#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
348#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100349#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
350 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100351#define has_temp_old_peci(data, nr) \
352 (((data)->features & FEAT_TEMP_OLD_PECI) && \
353 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800354#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
355#define has_five_fans(data) ((data)->features & FEAT_FIVE_FANS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200357struct it87_sio_data {
358 enum chips type;
359 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200360 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200361 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100362 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200363 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100364 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700365 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100366 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100367 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200368 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700369 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200370};
371
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800372/*
373 * For each registered chip, we need to keep some data in memory.
374 * The structure is dynamically allocated.
375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700377 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100379 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100380 u8 peci_mask;
381 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200383 unsigned short addr;
384 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100385 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 char valid; /* !=0 if following fields are valid */
387 unsigned long last_updated; /* In jiffies */
388
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200389 u16 in_scaled; /* Internal voltage sensors are scaled */
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100390 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200391 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100392 u16 fan[5][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700393 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck161d8982012-12-19 22:17:01 +0100394 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100395 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
396 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 u8 fan_div[3]; /* Register encoding, shifted right */
398 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100399 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100401 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100403 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100404
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800405 /*
406 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100407 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
408 * 7, and we want to preserve settings on mode changes, so we have
409 * to track all values separately.
410 * Starting with the IT8721F, the manual PWM duty cycles are stored
411 * in separate registers (8-bit values), so the separate tracking
412 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800413 * simple.
414 */
Jean Delvareb99883d2010-03-05 22:17:15 +0100415 u8 pwm_ctrl[3]; /* Register value */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100416 u8 pwm_duty[3]; /* Manual PWM value set by user */
Jean Delvareb99883d2010-03-05 22:17:15 +0100417 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100418
419 /* Automatic fan speed control registers */
420 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
421 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422};
423
Guenter Roeck0531d982012-03-02 11:46:44 -0800424static int adc_lsb(const struct it87_data *data, int nr)
425{
426 int lsb = has_12mv_adc(data) ? 12 : 16;
427 if (data->in_scaled & (1 << nr))
428 lsb <<= 1;
429 return lsb;
430}
431
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200432static u8 in_to_reg(const struct it87_data *data, int nr, long val)
433{
Guenter Roeck0531d982012-03-02 11:46:44 -0800434 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800435 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200436}
437
438static int in_from_reg(const struct it87_data *data, int nr, int val)
439{
Guenter Roeck0531d982012-03-02 11:46:44 -0800440 return val * adc_lsb(data, nr);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200441}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200442
443static inline u8 FAN_TO_REG(long rpm, int div)
444{
445 if (rpm == 0)
446 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800447 rpm = clamp_val(rpm, 1, 1000000);
448 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200449}
450
451static inline u16 FAN16_TO_REG(long rpm)
452{
453 if (rpm == 0)
454 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800455 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200456}
457
458#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
459 1350000 / ((val) * (div)))
460/* The divider is fixed to 2 in 16-bit mode */
461#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
462 1350000 / ((val) * 2))
463
Guenter Roeck2a844c12013-01-09 08:09:34 -0800464#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
465 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200466#define TEMP_FROM_REG(val) ((val) * 1000)
467
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200468static u8 pwm_to_reg(const struct it87_data *data, long val)
469{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100470 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200471 return val;
472 else
473 return val >> 1;
474}
475
476static int pwm_from_reg(const struct it87_data *data, u8 reg)
477{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100478 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200479 return reg;
480 else
481 return (reg & 0x7f) << 1;
482}
483
Jean Delvare0df6454d2010-10-28 20:31:51 +0200484
485static int DIV_TO_REG(int val)
486{
487 int answer = 0;
488 while (answer < 7 && (val >>= 1))
489 answer++;
490 return answer;
491}
492#define DIV_FROM_REG(val) (1 << (val))
493
494static const unsigned int pwm_freq[8] = {
495 48000000 / 128,
496 24000000 / 128,
497 12000000 / 128,
498 8000000 / 128,
499 6000000 / 128,
500 3000000 / 128,
501 1500000 / 128,
502 750000 / 128,
503};
504
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200505static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500506static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200508static int it87_read_value(struct it87_data *data, u8 reg);
509static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200511static int it87_check_pwm(struct device *dev);
512static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200515static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100516 .driver = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200517 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100518 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200519 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500520 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200521};
522
Jean Delvare20ad93d2005-06-05 11:53:25 +0200523static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100524 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100526 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
527 int nr = sattr->nr;
528 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100531 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Guenter Roeck929c6a52012-12-19 22:17:00 +0100534static ssize_t set_in(struct device *dev, struct device_attribute *attr,
535 const char *buf, size_t count)
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
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200541 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100542 unsigned long val;
543
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100544 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100545 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100547 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100548 data->in[nr][index] = in_to_reg(data, nr, val);
549 it87_write_value(data,
550 index == 1 ? IT87_REG_VIN_MIN(nr)
551 : IT87_REG_VIN_MAX(nr),
552 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100553 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return count;
555}
556
Guenter Roeck929c6a52012-12-19 22:17:00 +0100557static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
558static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
559 0, 1);
560static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
561 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Guenter Roeck929c6a52012-12-19 22:17:00 +0100563static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
564static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
565 1, 1);
566static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
567 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Guenter Roeck929c6a52012-12-19 22:17:00 +0100569static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
570static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
571 2, 1);
572static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
573 2, 2);
574
575static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
576static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
577 3, 1);
578static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
579 3, 2);
580
581static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
582static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
583 4, 1);
584static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
585 4, 2);
586
587static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
588static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
589 5, 1);
590static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
591 5, 2);
592
593static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
594static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
595 6, 1);
596static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
597 6, 2);
598
599static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
600static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
601 7, 1);
602static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
603 7, 2);
604
605static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100606static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200609static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100610 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100612 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
613 int nr = sattr->nr;
614 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200616
Guenter Roeck60ca3852012-12-19 22:17:00 +0100617 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200619
Guenter Roeck60ca3852012-12-19 22:17:00 +0100620static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
621 const char *buf, size_t count)
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;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200626 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100627 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100628 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100629
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100630 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100631 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100633 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100634
635 switch (index) {
636 default:
637 case 1:
638 reg = IT87_REG_TEMP_LOW(nr);
639 break;
640 case 2:
641 reg = IT87_REG_TEMP_HIGH(nr);
642 break;
643 case 3:
644 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
645 if (!(regval & 0x80)) {
646 regval |= 0x80;
647 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
648 }
649 data->valid = 0;
650 reg = IT87_REG_TEMP_OFFSET[nr];
651 break;
652 }
653
Guenter Roeck60ca3852012-12-19 22:17:00 +0100654 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100655 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100656 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return count;
658}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200659
Guenter Roeck60ca3852012-12-19 22:17:00 +0100660static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
661static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
662 0, 1);
663static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
664 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100665static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
666 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100667static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
668static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
669 1, 1);
670static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
671 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100672static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
673 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100674static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
675static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
676 2, 1);
677static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
678 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100679static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
680 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Guenter Roeck2cece012012-12-19 22:17:01 +0100682static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
683 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200685 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
686 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800688 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100689 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100690
Guenter Roeck19529782012-12-19 22:17:02 +0100691 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
692 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100693 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (reg & (1 << nr))
695 return sprintf(buf, "3\n"); /* thermal diode */
696 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200697 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return sprintf(buf, "0\n"); /* disabled */
699}
Guenter Roeck2cece012012-12-19 22:17:01 +0100700
701static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
702 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200704 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
705 int nr = sensor_attr->index;
706
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200707 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100708 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100709 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100710
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100711 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100712 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Jean Delvare8acf07c2010-04-14 16:14:09 +0200714 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
715 reg &= ~(1 << nr);
716 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100717 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
718 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100719 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
720 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
721 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200722 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100723 dev_warn(dev,
724 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200725 val = 4;
726 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100727 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200729 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200730 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200731 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100732 else if (has_temp_peci(data, nr) && val == 6)
733 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100734 else if (has_temp_old_peci(data, nr) && val == 6)
735 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200736 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200738
739 mutex_lock(&data->update_lock);
740 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100741 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200742 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100743 if (has_temp_old_peci(data, nr))
744 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200745 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100746 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return count;
748}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Guenter Roeck2cece012012-12-19 22:17:01 +0100750static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
751 set_temp_type, 0);
752static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
753 set_temp_type, 1);
754static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
755 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100758
759static int pwm_mode(const struct it87_data *data, int nr)
760{
761 int ctrl = data->fan_main_ctrl & (1 << nr);
762
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100763 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +0100764 return 0;
765 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
766 return 2;
767 else /* Manual mode */
768 return 1;
769}
770
Jean Delvare20ad93d2005-06-05 11:53:25 +0200771static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +0100772 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100774 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
775 int nr = sattr->nr;
776 int index = sattr->index;
777 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200779
Guenter Roecke1169ba2012-12-19 22:17:01 +0100780 speed = has_16bit_fans(data) ?
781 FAN16_FROM_REG(data->fan[nr][index]) :
782 FAN_FROM_REG(data->fan[nr][index],
783 DIV_FROM_REG(data->fan_div[nr]));
784 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100786
Jean Delvare20ad93d2005-06-05 11:53:25 +0200787static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
788 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200790 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
791 int nr = sensor_attr->index;
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct it87_data *data = it87_update_device(dev);
794 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
795}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100796static ssize_t show_pwm_enable(struct device *dev,
797 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200799 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
800 int nr = sensor_attr->index;
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100803 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200805static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
806 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200808 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
809 int nr = sensor_attr->index;
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200812 return sprintf(buf, "%d\n",
813 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100815static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
816 char *buf)
817{
818 struct it87_data *data = it87_update_device(dev);
819 int index = (data->fan_ctl >> 4) & 0x07;
820
821 return sprintf(buf, "%u\n", pwm_freq[index]);
822}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100823
824static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
825 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100827 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
828 int nr = sattr->nr;
829 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200830
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200831 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100832 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100833 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100835 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100836 return -EINVAL;
837
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100838 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +0100839
840 if (has_16bit_fans(data)) {
841 data->fan[nr][index] = FAN16_TO_REG(val);
842 it87_write_value(data, IT87_REG_FAN_MIN[nr],
843 data->fan[nr][index] & 0xff);
844 it87_write_value(data, IT87_REG_FANX_MIN[nr],
845 data->fan[nr][index] >> 8);
846 } else {
847 reg = it87_read_value(data, IT87_REG_FAN_DIV);
848 switch (nr) {
849 case 0:
850 data->fan_div[nr] = reg & 0x07;
851 break;
852 case 1:
853 data->fan_div[nr] = (reg >> 3) & 0x07;
854 break;
855 case 2:
856 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
857 break;
858 }
859 data->fan[nr][index] =
860 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
861 it87_write_value(data, IT87_REG_FAN_MIN[nr],
862 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -0800863 }
864
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100865 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return count;
867}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100868
Jean Delvare20ad93d2005-06-05 11:53:25 +0200869static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
870 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200872 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
873 int nr = sensor_attr->index;
874
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200875 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100876 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200877 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 u8 old;
879
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100880 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100881 return -EINVAL;
882
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100883 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200884 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200886 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100887 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 switch (nr) {
890 case 0:
891 case 1:
892 data->fan_div[nr] = DIV_TO_REG(val);
893 break;
894 case 2:
895 if (val < 8)
896 data->fan_div[nr] = 1;
897 else
898 data->fan_div[nr] = 3;
899 }
900 val = old & 0x80;
901 val |= (data->fan_div[0] & 0x07);
902 val |= (data->fan_div[1] & 0x07) << 3;
903 if (data->fan_div[2] == 3)
904 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200905 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200907 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100908 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
909 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200910
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100911 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return count;
913}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100914
915/* Returns 0 if OK, -EINVAL otherwise */
916static int check_trip_points(struct device *dev, int nr)
917{
918 const struct it87_data *data = dev_get_drvdata(dev);
919 int i, err = 0;
920
921 if (has_old_autopwm(data)) {
922 for (i = 0; i < 3; i++) {
923 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
924 err = -EINVAL;
925 }
926 for (i = 0; i < 2; i++) {
927 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
928 err = -EINVAL;
929 }
930 }
931
932 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100933 dev_err(dev,
934 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100935 dev_err(dev, "Adjust the trip points and try again\n");
936 }
937 return err;
938}
939
Jean Delvare20ad93d2005-06-05 11:53:25 +0200940static ssize_t set_pwm_enable(struct device *dev,
941 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200943 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
944 int nr = sensor_attr->index;
945
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200946 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100947 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100949 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100950 return -EINVAL;
951
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100952 /* Check trip points before switching to automatic mode */
953 if (val == 2) {
954 if (check_trip_points(dev, nr) < 0)
955 return -EINVAL;
956 }
957
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100958 /* IT8603E does not have on/off mode */
959 if (val == 0 && data->type == it8603)
960 return -EINVAL;
961
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100962 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 if (val == 0) {
965 int tmp;
966 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200967 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
968 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* set on/off mode */
970 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100971 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
972 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100973 } else {
974 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +0100975 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +0100976 data->pwm_temp_map[nr] :
977 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +0100978 else /* Automatic mode */
979 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
980 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100981
982 if (data->type != it8603) {
983 /* set SmartGuardian mode */
984 data->fan_main_ctrl |= (1 << nr);
985 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
986 data->fan_main_ctrl);
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100990 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return count;
992}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200993static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
994 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200996 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
997 int nr = sensor_attr->index;
998
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200999 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001000 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001002 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return -EINVAL;
1004
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001005 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001006 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001007 /*
1008 * If we are in automatic mode, the PWM duty cycle register
1009 * is read-only so we can't write the value.
1010 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001011 if (data->pwm_ctrl[nr] & 0x80) {
1012 mutex_unlock(&data->update_lock);
1013 return -EBUSY;
1014 }
1015 data->pwm_duty[nr] = pwm_to_reg(data, val);
1016 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
1017 data->pwm_duty[nr]);
1018 } else {
1019 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001020 /*
1021 * If we are in manual mode, write the duty cycle immediately;
1022 * otherwise, just store it for later use.
1023 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001024 if (!(data->pwm_ctrl[nr] & 0x80)) {
1025 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1026 it87_write_value(data, IT87_REG_PWM(nr),
1027 data->pwm_ctrl[nr]);
1028 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001029 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001030 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return count;
1032}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001033static ssize_t set_pwm_freq(struct device *dev,
1034 struct device_attribute *attr, const char *buf, size_t count)
1035{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001036 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001037 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001038 int i;
1039
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001040 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001041 return -EINVAL;
1042
Jean Delvaref8d0c192007-02-14 21:15:02 +01001043 /* Search for the nearest available frequency */
1044 for (i = 0; i < 7; i++) {
1045 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1046 break;
1047 }
1048
1049 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001050 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001051 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001052 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001053 mutex_unlock(&data->update_lock);
1054
1055 return count;
1056}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001057static ssize_t show_pwm_temp_map(struct device *dev,
1058 struct device_attribute *attr, char *buf)
1059{
1060 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1061 int nr = sensor_attr->index;
1062
1063 struct it87_data *data = it87_update_device(dev);
1064 int map;
1065
1066 if (data->pwm_temp_map[nr] < 3)
1067 map = 1 << data->pwm_temp_map[nr];
1068 else
1069 map = 0; /* Should never happen */
1070 return sprintf(buf, "%d\n", map);
1071}
1072static ssize_t set_pwm_temp_map(struct device *dev,
1073 struct device_attribute *attr, const char *buf, size_t count)
1074{
1075 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1076 int nr = sensor_attr->index;
1077
1078 struct it87_data *data = dev_get_drvdata(dev);
1079 long val;
1080 u8 reg;
1081
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001082 /*
1083 * This check can go away if we ever support automatic fan speed
1084 * control on newer chips.
1085 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001086 if (!has_old_autopwm(data)) {
1087 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1088 return -EINVAL;
1089 }
1090
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001091 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001092 return -EINVAL;
1093
1094 switch (val) {
1095 case (1 << 0):
1096 reg = 0x00;
1097 break;
1098 case (1 << 1):
1099 reg = 0x01;
1100 break;
1101 case (1 << 2):
1102 reg = 0x02;
1103 break;
1104 default:
1105 return -EINVAL;
1106 }
1107
1108 mutex_lock(&data->update_lock);
1109 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001110 /*
1111 * If we are in automatic mode, write the temp mapping immediately;
1112 * otherwise, just store it for later use.
1113 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001114 if (data->pwm_ctrl[nr] & 0x80) {
1115 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1116 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1117 }
1118 mutex_unlock(&data->update_lock);
1119 return count;
1120}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001122static ssize_t show_auto_pwm(struct device *dev,
1123 struct device_attribute *attr, char *buf)
1124{
1125 struct it87_data *data = it87_update_device(dev);
1126 struct sensor_device_attribute_2 *sensor_attr =
1127 to_sensor_dev_attr_2(attr);
1128 int nr = sensor_attr->nr;
1129 int point = sensor_attr->index;
1130
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001131 return sprintf(buf, "%d\n",
1132 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001133}
1134
1135static ssize_t set_auto_pwm(struct device *dev,
1136 struct device_attribute *attr, const char *buf, size_t count)
1137{
1138 struct it87_data *data = dev_get_drvdata(dev);
1139 struct sensor_device_attribute_2 *sensor_attr =
1140 to_sensor_dev_attr_2(attr);
1141 int nr = sensor_attr->nr;
1142 int point = sensor_attr->index;
1143 long val;
1144
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001145 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001146 return -EINVAL;
1147
1148 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001149 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001150 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1151 data->auto_pwm[nr][point]);
1152 mutex_unlock(&data->update_lock);
1153 return count;
1154}
1155
1156static ssize_t show_auto_temp(struct device *dev,
1157 struct device_attribute *attr, char *buf)
1158{
1159 struct it87_data *data = it87_update_device(dev);
1160 struct sensor_device_attribute_2 *sensor_attr =
1161 to_sensor_dev_attr_2(attr);
1162 int nr = sensor_attr->nr;
1163 int point = sensor_attr->index;
1164
1165 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1166}
1167
1168static ssize_t set_auto_temp(struct device *dev,
1169 struct device_attribute *attr, const char *buf, size_t count)
1170{
1171 struct it87_data *data = dev_get_drvdata(dev);
1172 struct sensor_device_attribute_2 *sensor_attr =
1173 to_sensor_dev_attr_2(attr);
1174 int nr = sensor_attr->nr;
1175 int point = sensor_attr->index;
1176 long val;
1177
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001178 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001179 return -EINVAL;
1180
1181 mutex_lock(&data->update_lock);
1182 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1183 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1184 data->auto_temp[nr][point]);
1185 mutex_unlock(&data->update_lock);
1186 return count;
1187}
1188
Guenter Roecke1169ba2012-12-19 22:17:01 +01001189static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1190static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1191 0, 1);
1192static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1193 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Guenter Roecke1169ba2012-12-19 22:17:01 +01001195static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1196static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1197 1, 1);
1198static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1199 set_fan_div, 1);
1200
1201static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1202static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1203 2, 1);
1204static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1205 set_fan_div, 2);
1206
1207static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1208static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1209 3, 1);
1210
1211static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1212static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1213 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Guenter Roeckc4458db2012-12-19 22:17:02 +01001215static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1216 show_pwm_enable, set_pwm_enable, 0);
1217static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1218static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1219static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1220 show_pwm_temp_map, set_pwm_temp_map, 0);
1221static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1222 show_auto_pwm, set_auto_pwm, 0, 0);
1223static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1224 show_auto_pwm, set_auto_pwm, 0, 1);
1225static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1226 show_auto_pwm, set_auto_pwm, 0, 2);
1227static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1228 show_auto_pwm, NULL, 0, 3);
1229static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1230 show_auto_temp, set_auto_temp, 0, 1);
1231static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1232 show_auto_temp, set_auto_temp, 0, 0);
1233static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1234 show_auto_temp, set_auto_temp, 0, 2);
1235static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1236 show_auto_temp, set_auto_temp, 0, 3);
1237static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1238 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Guenter Roeckc4458db2012-12-19 22:17:02 +01001240static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1241 show_pwm_enable, set_pwm_enable, 1);
1242static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1243static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1244static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1245 show_pwm_temp_map, set_pwm_temp_map, 1);
1246static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1247 show_auto_pwm, set_auto_pwm, 1, 0);
1248static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1249 show_auto_pwm, set_auto_pwm, 1, 1);
1250static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1251 show_auto_pwm, set_auto_pwm, 1, 2);
1252static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1253 show_auto_pwm, NULL, 1, 3);
1254static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1255 show_auto_temp, set_auto_temp, 1, 1);
1256static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1257 show_auto_temp, set_auto_temp, 1, 0);
1258static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1259 show_auto_temp, set_auto_temp, 1, 2);
1260static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1261 show_auto_temp, set_auto_temp, 1, 3);
1262static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1263 show_auto_temp, set_auto_temp, 1, 4);
1264
1265static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1266 show_pwm_enable, set_pwm_enable, 2);
1267static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1268static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1269static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1270 show_pwm_temp_map, set_pwm_temp_map, 2);
1271static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1272 show_auto_pwm, set_auto_pwm, 2, 0);
1273static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1274 show_auto_pwm, set_auto_pwm, 2, 1);
1275static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1276 show_auto_pwm, set_auto_pwm, 2, 2);
1277static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1278 show_auto_pwm, NULL, 2, 3);
1279static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1280 show_auto_temp, set_auto_temp, 2, 1);
1281static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1282 show_auto_temp, set_auto_temp, 2, 0);
1283static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1284 show_auto_temp, set_auto_temp, 2, 2);
1285static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1286 show_auto_temp, set_auto_temp, 2, 3);
1287static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1288 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001291static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1292 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001295 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
Jean Delvare1d66c642005-04-18 21:16:59 -07001297static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Jean Delvare0124dd72007-11-25 16:16:41 +01001299static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1300 char *buf)
1301{
1302 int bitnr = to_sensor_dev_attr(attr)->index;
1303 struct it87_data *data = it87_update_device(dev);
1304 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1305}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001306
1307static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1308 *attr, const char *buf, size_t count)
1309{
1310 struct it87_data *data = dev_get_drvdata(dev);
1311 long val;
1312 int config;
1313
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001314 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001315 return -EINVAL;
1316
1317 mutex_lock(&data->update_lock);
1318 config = it87_read_value(data, IT87_REG_CONFIG);
1319 if (config < 0) {
1320 count = config;
1321 } else {
1322 config |= 1 << 5;
1323 it87_write_value(data, IT87_REG_CONFIG, config);
1324 /* Invalidate cache to force re-read */
1325 data->valid = 0;
1326 }
1327 mutex_unlock(&data->update_lock);
1328
1329 return count;
1330}
1331
Jean Delvare0124dd72007-11-25 16:16:41 +01001332static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1333static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1334static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1335static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1336static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1337static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1338static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1339static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1340static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1341static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1342static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1343static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1344static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1345static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1346static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1347static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001348static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1349 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001350
Jean Delvared9b327c2010-03-05 22:17:17 +01001351static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1352 char *buf)
1353{
1354 int bitnr = to_sensor_dev_attr(attr)->index;
1355 struct it87_data *data = it87_update_device(dev);
1356 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1357}
1358static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1359 const char *buf, size_t count)
1360{
1361 int bitnr = to_sensor_dev_attr(attr)->index;
1362 struct it87_data *data = dev_get_drvdata(dev);
1363 long val;
1364
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001365 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001366 || (val != 0 && val != 1))
1367 return -EINVAL;
1368
1369 mutex_lock(&data->update_lock);
1370 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1371 if (val)
1372 data->beeps |= (1 << bitnr);
1373 else
1374 data->beeps &= ~(1 << bitnr);
1375 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1376 mutex_unlock(&data->update_lock);
1377 return count;
1378}
1379
1380static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1381 show_beep, set_beep, 1);
1382static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1383static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1384static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1385static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1386static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1387static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1388static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1389/* fanX_beep writability is set later */
1390static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1391static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1392static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1393static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1394static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1395static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1396 show_beep, set_beep, 2);
1397static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1398static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1399
Jean Delvare5f2dc792010-03-05 22:17:18 +01001400static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1401 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Jean Delvare90d66192007-10-08 18:24:35 +02001403 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001404 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001406static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1407 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001409 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001410 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001412 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001413 return -EINVAL;
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 data->vrm = val;
1416
1417 return count;
1418}
1419static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Jean Delvare5f2dc792010-03-05 22:17:18 +01001421static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1422 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 struct it87_data *data = it87_update_device(dev);
1425 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1426}
1427static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001428
Jean Delvare738e5e02010-08-14 21:08:50 +02001429static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1430 char *buf)
1431{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001432 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001433 "+5V",
1434 "5VSB",
1435 "Vbat",
1436 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001437 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001438 "+3.3V",
1439 "3VSB",
1440 "Vbat",
1441 };
1442 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001443 int nr = to_sensor_dev_attr(attr)->index;
1444
Jean Delvare16b5dda2012-01-16 22:51:48 +01001445 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1446 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001447}
1448static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1449static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1450static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Rudolf Marek7183ae82014-04-04 18:01:35 +02001451/* special AVCC3 IT8603E in9 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001452static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001453
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001454static ssize_t show_name(struct device *dev, struct device_attribute
1455 *devattr, char *buf)
1456{
1457 struct it87_data *data = dev_get_drvdata(dev);
1458 return sprintf(buf, "%s\n", data->name);
1459}
1460static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1461
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001462static struct attribute *it87_attributes_in[10][5] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001463{
Jean Delvare87808be2006-09-24 21:17:13 +02001464 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001465 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001466 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001467 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001468 NULL
1469}, {
1470 &sensor_dev_attr_in1_input.dev_attr.attr,
1471 &sensor_dev_attr_in1_min.dev_attr.attr,
1472 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001473 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001474 NULL
1475}, {
1476 &sensor_dev_attr_in2_input.dev_attr.attr,
1477 &sensor_dev_attr_in2_min.dev_attr.attr,
1478 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001479 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001480 NULL
1481}, {
1482 &sensor_dev_attr_in3_input.dev_attr.attr,
1483 &sensor_dev_attr_in3_min.dev_attr.attr,
1484 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001485 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001486 NULL
1487}, {
1488 &sensor_dev_attr_in4_input.dev_attr.attr,
1489 &sensor_dev_attr_in4_min.dev_attr.attr,
1490 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001491 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001492 NULL
1493}, {
1494 &sensor_dev_attr_in5_input.dev_attr.attr,
1495 &sensor_dev_attr_in5_min.dev_attr.attr,
1496 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001497 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001498 NULL
1499}, {
1500 &sensor_dev_attr_in6_input.dev_attr.attr,
1501 &sensor_dev_attr_in6_min.dev_attr.attr,
1502 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001503 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001504 NULL
1505}, {
1506 &sensor_dev_attr_in7_input.dev_attr.attr,
1507 &sensor_dev_attr_in7_min.dev_attr.attr,
1508 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001509 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001510 NULL
1511}, {
1512 &sensor_dev_attr_in8_input.dev_attr.attr,
1513 NULL
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001514}, {
1515 &sensor_dev_attr_in9_input.dev_attr.attr,
1516 NULL
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001517} };
Jean Delvare87808be2006-09-24 21:17:13 +02001518
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001519static const struct attribute_group it87_group_in[10] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001520 { .attrs = it87_attributes_in[0] },
1521 { .attrs = it87_attributes_in[1] },
1522 { .attrs = it87_attributes_in[2] },
1523 { .attrs = it87_attributes_in[3] },
1524 { .attrs = it87_attributes_in[4] },
1525 { .attrs = it87_attributes_in[5] },
1526 { .attrs = it87_attributes_in[6] },
1527 { .attrs = it87_attributes_in[7] },
1528 { .attrs = it87_attributes_in[8] },
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001529 { .attrs = it87_attributes_in[9] },
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001530};
1531
Guenter Roeck4573acb2012-03-26 16:17:41 -07001532static struct attribute *it87_attributes_temp[3][6] = {
1533{
Jean Delvare87808be2006-09-24 21:17:13 +02001534 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001535 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001536 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001537 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001538 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001539 NULL
1540} , {
1541 &sensor_dev_attr_temp2_input.dev_attr.attr,
1542 &sensor_dev_attr_temp2_max.dev_attr.attr,
1543 &sensor_dev_attr_temp2_min.dev_attr.attr,
1544 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001545 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001546 NULL
1547} , {
1548 &sensor_dev_attr_temp3_input.dev_attr.attr,
1549 &sensor_dev_attr_temp3_max.dev_attr.attr,
1550 &sensor_dev_attr_temp3_min.dev_attr.attr,
1551 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001552 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001553 NULL
1554} };
Jean Delvare87808be2006-09-24 21:17:13 +02001555
Guenter Roeck4573acb2012-03-26 16:17:41 -07001556static const struct attribute_group it87_group_temp[3] = {
1557 { .attrs = it87_attributes_temp[0] },
1558 { .attrs = it87_attributes_temp[1] },
1559 { .attrs = it87_attributes_temp[2] },
1560};
1561
Guenter Roeck161d8982012-12-19 22:17:01 +01001562static struct attribute *it87_attributes_temp_offset[] = {
1563 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1564 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1565 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1566};
1567
Guenter Roeck4573acb2012-03-26 16:17:41 -07001568static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001569 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001570 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001571 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001572 NULL
1573};
1574
1575static const struct attribute_group it87_group = {
1576 .attrs = it87_attributes,
1577};
1578
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001579static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001580 &sensor_dev_attr_in0_beep.dev_attr.attr,
1581 &sensor_dev_attr_in1_beep.dev_attr.attr,
1582 &sensor_dev_attr_in2_beep.dev_attr.attr,
1583 &sensor_dev_attr_in3_beep.dev_attr.attr,
1584 &sensor_dev_attr_in4_beep.dev_attr.attr,
1585 &sensor_dev_attr_in5_beep.dev_attr.attr,
1586 &sensor_dev_attr_in6_beep.dev_attr.attr,
1587 &sensor_dev_attr_in7_beep.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001588 NULL,
1589 NULL,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001590};
Jean Delvared9b327c2010-03-05 22:17:17 +01001591
Guenter Roeck4573acb2012-03-26 16:17:41 -07001592static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001593 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1594 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1595 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001596};
1597
Guenter Roecke1169ba2012-12-19 22:17:01 +01001598static struct attribute *it87_attributes_fan[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001599 &sensor_dev_attr_fan1_input.dev_attr.attr,
1600 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001601 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1602 NULL
1603}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001604 &sensor_dev_attr_fan2_input.dev_attr.attr,
1605 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001606 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1607 NULL
1608}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001609 &sensor_dev_attr_fan3_input.dev_attr.attr,
1610 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001611 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001612 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001613}, {
1614 &sensor_dev_attr_fan4_input.dev_attr.attr,
1615 &sensor_dev_attr_fan4_min.dev_attr.attr,
1616 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1617 NULL
1618}, {
1619 &sensor_dev_attr_fan5_input.dev_attr.attr,
1620 &sensor_dev_attr_fan5_min.dev_attr.attr,
1621 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1622 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001623} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001624
Guenter Roecke1169ba2012-12-19 22:17:01 +01001625static const struct attribute_group it87_group_fan[5] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001626 { .attrs = it87_attributes_fan[0] },
1627 { .attrs = it87_attributes_fan[1] },
1628 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001629 { .attrs = it87_attributes_fan[3] },
1630 { .attrs = it87_attributes_fan[4] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001631};
1632
Guenter Roecke1169ba2012-12-19 22:17:01 +01001633static const struct attribute *it87_attributes_fan_div[] = {
1634 &sensor_dev_attr_fan1_div.dev_attr.attr,
1635 &sensor_dev_attr_fan2_div.dev_attr.attr,
1636 &sensor_dev_attr_fan3_div.dev_attr.attr,
1637};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001638
1639static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001640 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001641 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001642 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001643 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001644 NULL
1645}, {
1646 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1647 &sensor_dev_attr_pwm2.dev_attr.attr,
1648 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001649 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001650 NULL
1651}, {
1652 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1653 &sensor_dev_attr_pwm3.dev_attr.attr,
1654 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001655 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001656 NULL
1657} };
Jean Delvare87808be2006-09-24 21:17:13 +02001658
Jean Delvare723a0aa2010-03-05 22:17:16 +01001659static const struct attribute_group it87_group_pwm[3] = {
1660 { .attrs = it87_attributes_pwm[0] },
1661 { .attrs = it87_attributes_pwm[1] },
1662 { .attrs = it87_attributes_pwm[2] },
1663};
1664
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001665static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1666 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1667 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1668 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1669 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1670 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1671 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1672 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1673 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1674 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1675 NULL
1676}, {
1677 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1678 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1679 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1680 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1681 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1682 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1683 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1684 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1685 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1686 NULL
1687}, {
1688 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1689 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1690 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1691 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1692 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1693 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1694 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1695 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1696 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1697 NULL
1698} };
1699
1700static const struct attribute_group it87_group_autopwm[3] = {
1701 { .attrs = it87_attributes_autopwm[0] },
1702 { .attrs = it87_attributes_autopwm[1] },
1703 { .attrs = it87_attributes_autopwm[2] },
1704};
1705
Jean Delvared9b327c2010-03-05 22:17:17 +01001706static struct attribute *it87_attributes_fan_beep[] = {
1707 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1708 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1709 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1710 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1711 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1712};
1713
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001714static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001715 &dev_attr_vrm.attr,
1716 &dev_attr_cpu0_vid.attr,
1717 NULL
1718};
1719
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001720static const struct attribute_group it87_group_vid = {
1721 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001722};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Jean Delvare738e5e02010-08-14 21:08:50 +02001724static struct attribute *it87_attributes_label[] = {
1725 &sensor_dev_attr_in3_label.dev_attr.attr,
1726 &sensor_dev_attr_in7_label.dev_attr.attr,
1727 &sensor_dev_attr_in8_label.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001728 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare738e5e02010-08-14 21:08:50 +02001729 NULL
1730};
1731
1732static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001733 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001734};
1735
Jean Delvare2d8672c2005-07-19 23:56:35 +02001736/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001737static int __init it87_find(unsigned short *address,
1738 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001740 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001741 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001742 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001744 err = superio_enter();
1745 if (err)
1746 return err;
1747
1748 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001749 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001750
1751 switch (chip_type) {
1752 case IT8705F_DEVID:
1753 sio_data->type = it87;
1754 break;
1755 case IT8712F_DEVID:
1756 sio_data->type = it8712;
1757 break;
1758 case IT8716F_DEVID:
1759 case IT8726F_DEVID:
1760 sio_data->type = it8716;
1761 break;
1762 case IT8718F_DEVID:
1763 sio_data->type = it8718;
1764 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001765 case IT8720F_DEVID:
1766 sio_data->type = it8720;
1767 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001768 case IT8721F_DEVID:
1769 sio_data->type = it8721;
1770 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001771 case IT8728F_DEVID:
1772 sio_data->type = it8728;
1773 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06001774 case IT8771E_DEVID:
1775 sio_data->type = it8771;
1776 break;
1777 case IT8772E_DEVID:
1778 sio_data->type = it8772;
1779 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08001780 case IT8781F_DEVID:
1781 sio_data->type = it8781;
1782 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001783 case IT8782F_DEVID:
1784 sio_data->type = it8782;
1785 break;
1786 case IT8783E_DEVID:
1787 sio_data->type = it8783;
1788 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02001789 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02001790 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001791 sio_data->type = it8603;
1792 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001793 case 0xffff: /* No device at all */
1794 goto exit;
1795 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001796 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001797 goto exit;
1798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Jean Delvare87673dd2006-08-28 14:37:19 +02001800 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001802 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 goto exit;
1804 }
1805
1806 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1807 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001808 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 goto exit;
1810 }
1811
1812 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001813 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001814 pr_info("Found IT%04x%c chip at 0x%x, revision %d\n", chip_type,
Jean Delvareb523bb72014-01-29 20:40:08 +01001815 chip_type == 0x8771 || chip_type == 0x8772 ||
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001816 chip_type == 0x8603 ? 'E' : 'F', *address,
1817 sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Jean Delvare738e5e02010-08-14 21:08:50 +02001819 /* in8 (Vbat) is always internal */
1820 sio_data->internal = (1 << 2);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001821 /* Only the IT8603E has in9 */
1822 if (sio_data->type != it8603)
1823 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02001824
Jean Delvare87673dd2006-08-28 14:37:19 +02001825 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001826 if (sio_data->type == it87) {
1827 /* The IT8705F doesn't have VID pins at all */
1828 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001829
1830 /* The IT8705F has a different LD number for GPIO */
1831 superio_select(5);
1832 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08001833 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001834 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08001835
1836 sio_data->skip_vid = 1; /* No VID */
1837
1838 superio_select(GPIO);
1839
1840 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1841 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001842 reg2a = superio_inb(IT87_SIO_PINX1_REG);
1843 reg2c = superio_inb(IT87_SIO_PINX2_REG);
1844 regef = superio_inb(IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001845
Guenter Roeck0531d982012-03-02 11:46:44 -08001846 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001847 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08001848 sio_data->skip_fan |= (1 << 2);
1849 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001850 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08001851 sio_data->skip_pwm |= (1 << 2);
1852
1853 /* Check if fan2 is there or not */
1854 if (reg27 & (1 << 7))
1855 sio_data->skip_fan |= (1 << 1);
1856 if (reg27 & (1 << 3))
1857 sio_data->skip_pwm |= (1 << 1);
1858
1859 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001860 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001861 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001862
1863 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001864 if (reg27 & (1 << 1))
1865 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001866
1867 /*
1868 * VIN7
1869 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1870 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001871 if (reg27 & (1 << 2)) {
1872 /*
1873 * The data sheet is a bit unclear regarding the
1874 * internal voltage divider for VCCH5V. It says
1875 * "This bit enables and switches VIN7 (pin 91) to the
1876 * internal voltage divider for VCCH5V".
1877 * This is different to other chips, where the internal
1878 * voltage divider would connect VIN7 to an internal
1879 * voltage source. Maybe that is the case here as well.
1880 *
1881 * Since we don't know for sure, re-route it if that is
1882 * not the case, and ask the user to report if the
1883 * resulting voltage is sane.
1884 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001885 if (!(reg2c & (1 << 1))) {
1886 reg2c |= (1 << 1);
1887 superio_outb(IT87_SIO_PINX2_REG, reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001888 pr_notice("Routing internal VCCH5V to in7.\n");
1889 }
1890 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1891 pr_notice("Please report if it displays a reasonable voltage.\n");
1892 }
Guenter Roeck0531d982012-03-02 11:46:44 -08001893
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001894 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08001895 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001896 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08001897 sio_data->internal |= (1 << 1);
1898
1899 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001900 } else if (sio_data->type == it8603) {
1901 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08001902
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001903 sio_data->skip_vid = 1; /* No VID */
1904 superio_select(GPIO);
1905
1906 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1907
1908 /* Check if fan3 is there or not */
1909 if (reg27 & (1 << 6))
1910 sio_data->skip_pwm |= (1 << 2);
1911 if (reg27 & (1 << 7))
1912 sio_data->skip_fan |= (1 << 2);
1913
1914 /* Check if fan2 is there or not */
1915 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
1916 if (reg29 & (1 << 1))
1917 sio_data->skip_pwm |= (1 << 1);
1918 if (reg29 & (1 << 2))
1919 sio_data->skip_fan |= (1 << 1);
1920
1921 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1922 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1923
1924 /* no fan4 */
1925 sio_data->skip_pwm |= (1 << 3);
1926 sio_data->skip_fan |= (1 << 3);
1927
1928 sio_data->internal |= (1 << 1); /* in7 is VSB */
1929 sio_data->internal |= (1 << 3); /* in9 is AVCC */
1930
1931 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01001932 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001933 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001934 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02001935
1936 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001937
Jean Delvare895ff262009-12-09 20:35:47 +01001938 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001939 if (sio_data->type == it8721 || sio_data->type == it8728 ||
Guenter Roeckb0636702012-09-07 17:34:41 -06001940 sio_data->type == it8771 || sio_data->type == it8772 ||
Guenter Roeck7bc32d22015-01-17 14:10:24 -08001941 sio_data->type == it8781 || sio_data->type == it8782) {
Jean Delvare16b5dda2012-01-16 22:51:48 +01001942 /*
Guenter Roeck7bc32d22015-01-17 14:10:24 -08001943 * IT8721F/IT8758E, IT8728F, IT8772F, IT8781F, and
1944 * IT8782F don't have VID pins at all, not sure about
1945 * the IT8771F.
Jean Delvare16b5dda2012-01-16 22:51:48 +01001946 */
Jean Delvare895ff262009-12-09 20:35:47 +01001947 sio_data->skip_vid = 1;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001948 } else {
1949 /* We need at least 4 VID pins */
1950 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001951 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001952 sio_data->skip_vid = 1;
1953 }
Jean Delvare895ff262009-12-09 20:35:47 +01001954 }
1955
Jean Delvare591ec652009-12-09 20:35:48 +01001956 /* Check if fan3 is there or not */
1957 if (reg & (1 << 6))
1958 sio_data->skip_pwm |= (1 << 2);
1959 if (reg & (1 << 7))
1960 sio_data->skip_fan |= (1 << 2);
1961
1962 /* Check if fan2 is there or not */
1963 reg = superio_inb(IT87_SIO_GPIO5_REG);
1964 if (reg & (1 << 1))
1965 sio_data->skip_pwm |= (1 << 1);
1966 if (reg & (1 << 2))
1967 sio_data->skip_fan |= (1 << 1);
1968
Jean Delvare895ff262009-12-09 20:35:47 +01001969 if ((sio_data->type == it8718 || sio_data->type == it8720)
1970 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001971 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001972
1973 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001974
1975 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1976
Jean Delvare436cad22010-07-09 16:22:48 +02001977 /*
1978 * The IT8720F has no VIN7 pin, so VCCH should always be
1979 * routed internally to VIN7 with an internal divider.
1980 * Curiously, there still is a configuration bit to control
1981 * this, which means it can be set incorrectly. And even
1982 * more curiously, many boards out there are improperly
1983 * configured, even though the IT8720F datasheet claims
1984 * that the internal routing of VCCH to VIN7 is the default
1985 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08001986 *
1987 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001988 * If UART6 is enabled, re-route VIN7 to the internal divider
1989 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02001990 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001991 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02001992 reg |= (1 << 1);
1993 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01001994 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02001995 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001996 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001997 sio_data->internal |= (1 << 0);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001998 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
Guenter Roeckb0636702012-09-07 17:34:41 -06001999 sio_data->type == it8728 ||
2000 sio_data->type == it8771 ||
2001 sio_data->type == it8772)
Jean Delvare738e5e02010-08-14 21:08:50 +02002002 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002003
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002004 /*
2005 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2006 * While VIN7 can be routed to the internal voltage divider,
2007 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002008 *
2009 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2010 * is the temperature source. Since we can not read the
2011 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002012 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002013 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002014 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002015 sio_data->skip_temp |= (1 << 2);
2016 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002017
Jean Delvared9b327c2010-03-05 22:17:17 +01002018 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002019 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002020 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002021 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002022
Jean Delvare98dd22c2008-10-09 15:33:58 +02002023 /* Disable specific features based on DMI strings */
2024 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2025 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2026 if (board_vendor && board_name) {
2027 if (strcmp(board_vendor, "nVIDIA") == 0
2028 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002029 /*
2030 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2031 * connected to a fan, but to something else. One user
2032 * has reported instant system power-off when changing
2033 * the PWM2 duty cycle, so we disable it.
2034 * I use the board name string as the trigger in case
2035 * the same board is ever used in other systems.
2036 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002037 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002038 sio_data->skip_pwm = (1 << 1);
2039 }
2040 }
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042exit:
2043 superio_exit();
2044 return err;
2045}
2046
Jean Delvare723a0aa2010-03-05 22:17:16 +01002047static void it87_remove_files(struct device *dev)
2048{
2049 struct it87_data *data = platform_get_drvdata(pdev);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002050 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002051 int i;
2052
2053 sysfs_remove_group(&dev->kobj, &it87_group);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002054 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002055 if (sio_data->skip_in & (1 << i))
2056 continue;
2057 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2058 if (it87_attributes_in_beep[i])
2059 sysfs_remove_file(&dev->kobj,
2060 it87_attributes_in_beep[i]);
2061 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002062 for (i = 0; i < 3; i++) {
2063 if (!(data->has_temp & (1 << i)))
2064 continue;
2065 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01002066 if (has_temp_offset(data))
2067 sysfs_remove_file(&dev->kobj,
2068 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002069 if (sio_data->beep_pin)
2070 sysfs_remove_file(&dev->kobj,
2071 it87_attributes_temp_beep[i]);
2072 }
Jean Delvare723a0aa2010-03-05 22:17:16 +01002073 for (i = 0; i < 5; i++) {
2074 if (!(data->has_fan & (1 << i)))
2075 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002076 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002077 if (sio_data->beep_pin)
2078 sysfs_remove_file(&dev->kobj,
2079 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002080 if (i < 3 && !has_16bit_fans(data))
2081 sysfs_remove_file(&dev->kobj,
2082 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002083 }
2084 for (i = 0; i < 3; i++) {
2085 if (sio_data->skip_pwm & (1 << 0))
2086 continue;
2087 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002088 if (has_old_autopwm(data))
2089 sysfs_remove_group(&dev->kobj,
2090 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002091 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002092 if (!sio_data->skip_vid)
2093 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02002094 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002095}
2096
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002097static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002100 struct resource *res;
2101 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002102 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002103 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01002105 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002107 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002108 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2109 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002110 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2111 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002112 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002113 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Guenter Roeck62a1d052012-03-24 21:54:41 -07002116 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2117 if (!data)
2118 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002120 data->addr = res->start;
2121 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002122 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002123 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002124 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002125 data->name = it87_devices[sio_data->type].name;
2126 /*
2127 * IT8705F Datasheet 0.4.1, 3h == Version G.
2128 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2129 * These are the first revisions with 16-bit tachometer support.
2130 */
2131 switch (data->type) {
2132 case it87:
2133 if (sio_data->revision >= 0x03) {
2134 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002135 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002136 }
2137 break;
2138 case it8712:
2139 if (sio_data->revision >= 0x08) {
2140 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002141 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2142 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002143 }
2144 break;
2145 default:
2146 break;
2147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002150 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002151 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2152 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002154 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002156 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002159 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002161 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002162 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002163 if (sio_data->internal & (1 << 0))
2164 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2165 if (sio_data->internal & (1 << 1))
2166 data->in_scaled |= (1 << 7); /* in7 is VSB */
2167 if (sio_data->internal & (1 << 2))
2168 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002169 if (sio_data->internal & (1 << 3))
2170 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002171 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2172 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002173 if (sio_data->internal & (1 << 0))
2174 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2175 if (sio_data->internal & (1 << 1))
2176 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002177 }
2178
Guenter Roeck4573acb2012-03-26 16:17:41 -07002179 data->has_temp = 0x07;
2180 if (sio_data->skip_temp & (1 << 2)) {
2181 if (sio_data->type == it8782
2182 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2183 data->has_temp &= ~(1 << 2);
2184 }
2185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002187 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002190 err = sysfs_create_group(&dev->kobj, &it87_group);
2191 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002192 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002193
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002194 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002195 if (sio_data->skip_in & (1 << i))
2196 continue;
2197 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2198 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002199 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002200 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2201 err = sysfs_create_file(&dev->kobj,
2202 it87_attributes_in_beep[i]);
2203 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002204 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002205 }
2206 }
2207
Guenter Roeck4573acb2012-03-26 16:17:41 -07002208 for (i = 0; i < 3; i++) {
2209 if (!(data->has_temp & (1 << i)))
2210 continue;
2211 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002212 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002213 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002214 if (has_temp_offset(data)) {
2215 err = sysfs_create_file(&dev->kobj,
2216 it87_attributes_temp_offset[i]);
2217 if (err)
2218 goto error;
2219 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002220 if (sio_data->beep_pin) {
2221 err = sysfs_create_file(&dev->kobj,
2222 it87_attributes_temp_beep[i]);
2223 if (err)
2224 goto error;
2225 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002226 }
2227
Jean Delvare9060f8b2006-08-28 14:24:17 +02002228 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002229 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01002230 for (i = 0; i < 5; i++) {
2231 if (!(data->has_fan & (1 << i)))
2232 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002233 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002234 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002235 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002236
Guenter Roecke1169ba2012-12-19 22:17:01 +01002237 if (i < 3 && !has_16bit_fans(data)) {
2238 err = sysfs_create_file(&dev->kobj,
2239 it87_attributes_fan_div[i]);
2240 if (err)
2241 goto error;
2242 }
2243
Jean Delvared9b327c2010-03-05 22:17:17 +01002244 if (sio_data->beep_pin) {
2245 err = sysfs_create_file(&dev->kobj,
2246 it87_attributes_fan_beep[i]);
2247 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002248 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002249 if (!fan_beep_need_rw)
2250 continue;
2251
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002252 /*
2253 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002254 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002255 * for it.
2256 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002257 if (sysfs_chmod_file(&dev->kobj,
2258 it87_attributes_fan_beep[i],
2259 S_IRUGO | S_IWUSR))
2260 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2261 i + 1);
2262 fan_beep_need_rw = 0;
2263 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002264 }
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002267 for (i = 0; i < 3; i++) {
2268 if (sio_data->skip_pwm & (1 << i))
2269 continue;
2270 err = sysfs_create_group(&dev->kobj,
2271 &it87_group_pwm[i]);
2272 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002273 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002274
2275 if (!has_old_autopwm(data))
2276 continue;
2277 err = sysfs_create_group(&dev->kobj,
2278 &it87_group_autopwm[i]);
2279 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002280 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283
Jean Delvare895ff262009-12-09 20:35:47 +01002284 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002285 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002286 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002287 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002288 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2289 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002290 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002291 }
2292
Jean Delvare738e5e02010-08-14 21:08:50 +02002293 /* Export labels for internal sensors */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002294 for (i = 0; i < 4; i++) {
Jean Delvare738e5e02010-08-14 21:08:50 +02002295 if (!(sio_data->internal & (1 << i)))
2296 continue;
2297 err = sysfs_create_file(&dev->kobj,
2298 it87_attributes_label[i]);
2299 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002300 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002301 }
2302
Tony Jones1beeffe2007-08-20 13:46:20 -07002303 data->hwmon_dev = hwmon_device_register(dev);
2304 if (IS_ERR(data->hwmon_dev)) {
2305 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002306 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
2308
2309 return 0;
2310
Guenter Roeck62a1d052012-03-24 21:54:41 -07002311error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002312 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return err;
2314}
2315
Bill Pemberton281dfd02012-11-19 13:25:51 -05002316static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002318 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Tony Jones1beeffe2007-08-20 13:46:20 -07002320 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002321 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return 0;
2324}
2325
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002326/*
2327 * Must be called with data->update_lock held, except during initialization.
2328 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2329 * would slow down the IT87 access and should not be necessary.
2330 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002331static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002333 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2334 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002337/*
2338 * Must be called with data->update_lock held, except during initialization.
2339 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2340 * would slow down the IT87 access and should not be necessary.
2341 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002342static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002344 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2345 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
2348/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002349static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002351 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002352 /*
2353 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002355 * disable pwm control to protect the user.
2356 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002357 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if ((tmp & 0x87) == 0) {
2359 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002360 /*
2361 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002363 * inverting all fan speed values.
2364 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 int i;
2366 u8 pwm[3];
2367
2368 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002369 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 IT87_REG_PWM(i));
2371
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002372 /*
2373 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 * might be correct, as suspicious as it seems, so we
2375 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002376 * PWM interface).
2377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002379 dev_info(dev,
2380 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002381 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 tmp | 0x87);
2383 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002384 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 IT87_REG_PWM(i),
2386 0x7f & ~pwm[i]);
2387 return 1;
2388 }
2389
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002390 dev_info(dev,
2391 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002394 dev_info(dev,
2395 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return 0;
2397 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002398 dev_info(dev,
2399 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401
2402 return 1;
2403}
2404
2405/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002406static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002408 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002409 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002411 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002413 /*
2414 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002415 * - If it is in automatic mode, setting to manual mode should set
2416 * the fan to full speed by default.
2417 * - If it is in manual mode, we need a mapping to temperature
2418 * channels to use when later setting to automatic mode later.
2419 * Use a 1:1 mapping by default (we are clueless.)
2420 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002421 * prior to switching to a different mode.
2422 * Note that this is no longer needed for the IT8721F and later, as
2423 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002424 * manual duty cycle.
2425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002427 data->pwm_temp_map[i] = i;
2428 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002429 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
2431
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002432 /*
2433 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002434 * registers. For low voltage limits it makes no sense and triggers
2435 * alarms, so change to 0 instead. For high temperature limits, it
2436 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002437 * but is still confusing, so change to 127 degrees C.
2438 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002439 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002440 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002441 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002442 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002443 }
2444 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002445 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002446 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002447 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002448 }
2449
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002450 /*
2451 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002452 * set to two different sensor types and we can't guess which one
2453 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002454 * run-time through the temp{1-3}_type sysfs accessors if needed.
2455 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002458 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if ((tmp & 0xff) == 0) {
2460 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002461 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463
2464 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002465 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002466 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002467 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002469 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002470 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2471 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002473 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002475 /* Set tachometers to 16-bit mode if needed */
2476 if (has_fan16_config(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002477 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002478 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002479 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002480 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002481 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002482 tmp | 0x07);
2483 }
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002484 }
2485
2486 /* Check for additional fans */
2487 if (has_five_fans(data)) {
2488 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2489 if (tmp & (1 << 4))
2490 data->has_fan |= (1 << 3); /* fan4 enabled */
2491 if (tmp & (1 << 5))
2492 data->has_fan |= (1 << 4); /* fan5 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02002493 }
2494
Jean Delvare591ec652009-12-09 20:35:48 +01002495 /* Fan input pins may be used for alternative functions */
2496 data->has_fan &= ~sio_data->skip_fan;
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002499 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002500 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 | (update_vbat ? 0x41 : 0x01));
2502}
2503
Jean Delvareb99883d2010-03-05 22:17:15 +01002504static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2505{
2506 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002507 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002508 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002509 data->pwm_duty[nr] = it87_read_value(data,
2510 IT87_REG_PWM_DUTY(nr));
2511 } else {
2512 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2513 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2514 else /* Manual mode */
2515 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2516 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002517
2518 if (has_old_autopwm(data)) {
2519 int i;
2520
2521 for (i = 0; i < 5 ; i++)
2522 data->auto_temp[nr][i] = it87_read_value(data,
2523 IT87_REG_AUTO_TEMP(nr, i));
2524 for (i = 0; i < 3 ; i++)
2525 data->auto_pwm[nr][i] = it87_read_value(data,
2526 IT87_REG_AUTO_PWM(nr, i));
2527 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002528}
2529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530static struct it87_data *it87_update_device(struct device *dev)
2531{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002532 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 int i;
2534
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002535 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2538 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002540 /*
2541 * Cleared after each update, so reenable. Value
2542 * returned by this read will be previous value
2543 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002544 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002545 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
2547 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002548 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002549 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002550 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002551 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002552 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002553 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
Jean Delvare3543a532006-08-28 14:27:25 +02002555 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002556 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002557 if (data->type == it8603)
2558 data->in[9][0] = it87_read_value(data, 0x2f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Jean Delvarec7f1f712007-09-03 17:11:46 +02002560 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002561 /* Skip disabled fans */
2562 if (!(data->has_fan & (1 << i)))
2563 continue;
2564
Guenter Roecke1169ba2012-12-19 22:17:01 +01002565 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002566 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002567 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002568 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002569 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002570 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002571 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002572 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002573 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002574 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002578 if (!(data->has_temp & (1 << i)))
2579 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002580 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002581 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002582 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002583 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002584 data->temp[i][2] =
2585 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002586 if (has_temp_offset(data))
2587 data->temp[i][3] =
2588 it87_read_value(data,
2589 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
2591
Jean Delvare17d648b2006-08-28 14:23:46 +02002592 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002593 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002594 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002595 data->fan_div[0] = i & 0x07;
2596 data->fan_div[1] = (i >> 3) & 0x07;
2597 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
2600 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002601 it87_read_value(data, IT87_REG_ALARM1) |
2602 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2603 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002604 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002605
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002606 data->fan_main_ctrl = it87_read_value(data,
2607 IT87_REG_FAN_MAIN_CTRL);
2608 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002609 for (i = 0; i < 3; i++)
2610 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002612 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002613 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002614 /*
2615 * The IT8705F does not have VID capability.
2616 * The IT8718F and later don't use IT87_REG_VID for the
2617 * same purpose.
2618 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002619 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002620 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002621 /*
2622 * The older IT8712F revisions had only 5 VID pins,
2623 * but we assume it is always safe to read 6 bits.
2624 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002625 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
2627 data->last_updated = jiffies;
2628 data->valid = 1;
2629 }
2630
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002631 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 return data;
2634}
2635
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002636static int __init it87_device_add(unsigned short address,
2637 const struct it87_sio_data *sio_data)
2638{
2639 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002640 .start = address + IT87_EC_OFFSET,
2641 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002642 .name = DRVNAME,
2643 .flags = IORESOURCE_IO,
2644 };
2645 int err;
2646
Jean Delvareb9acb642009-01-07 16:37:35 +01002647 err = acpi_check_resource_conflict(&res);
2648 if (err)
2649 goto exit;
2650
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002651 pdev = platform_device_alloc(DRVNAME, address);
2652 if (!pdev) {
2653 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002654 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002655 goto exit;
2656 }
2657
2658 err = platform_device_add_resources(pdev, &res, 1);
2659 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002660 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002661 goto exit_device_put;
2662 }
2663
2664 err = platform_device_add_data(pdev, sio_data,
2665 sizeof(struct it87_sio_data));
2666 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002667 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002668 goto exit_device_put;
2669 }
2670
2671 err = platform_device_add(pdev);
2672 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002673 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002674 goto exit_device_put;
2675 }
2676
2677 return 0;
2678
2679exit_device_put:
2680 platform_device_put(pdev);
2681exit:
2682 return err;
2683}
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685static int __init sm_it87_init(void)
2686{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002687 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002688 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002689 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002690
Jean Delvare98dd22c2008-10-09 15:33:58 +02002691 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002692 err = it87_find(&isa_address, &sio_data);
2693 if (err)
2694 return err;
2695 err = platform_driver_register(&it87_driver);
2696 if (err)
2697 return err;
2698
2699 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002700 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002701 platform_driver_unregister(&it87_driver);
2702 return err;
2703 }
2704
2705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
2708static void __exit sm_it87_exit(void)
2709{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002710 platform_device_unregister(pdev);
2711 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
2713
2714
Jean Delvare7c81c602014-01-29 20:40:08 +01002715MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002716MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717module_param(update_vbat, bool, 0);
2718MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2719module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002720MODULE_PARM_DESC(fix_pwm_polarity,
2721 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722MODULE_LICENSE("GPL");
2723
2724module_init(sm_it87_init);
2725module_exit(sm_it87_exit);