blob: f40d81a0a9fc55fe9474c8df85eb3478b65a3fe7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jean Delvare5f2dc792010-03-05 22:17:18 +01002 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring.
4 *
5 * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 * addition to an Environment Controller (Enhanced Hardware Monitor and
8 * Fan Controller)
9 *
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
12 *
Rudolf Marekc145d5c2014-01-29 20:40:08 +010013 * Supports: IT8603E Super I/O chip w/LPC interface
Guenter Roeck3ba9d972015-02-13 20:13:20 -080014 * IT8620E Super I/O chip w/LPC interface
Rudolf Marek574e9bd2014-04-04 18:01:35 +020015 * IT8623E Super I/O chip w/LPC interface
Rudolf Marekc145d5c2014-01-29 20:40:08 +010016 * IT8705F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010017 * IT8712F Super I/O chip w/LPC interface
18 * IT8716F Super I/O chip w/LPC interface
19 * IT8718F Super I/O chip w/LPC interface
20 * IT8720F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020021 * IT8721F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010022 * IT8726F Super I/O chip w/LPC interface
Jean Delvare16b5dda2012-01-16 22:51:48 +010023 * IT8728F Super I/O chip w/LPC interface
Justin Maggardead80802015-08-05 12:53:08 -070024 * IT8732F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020025 * IT8758E Super I/O chip w/LPC interface
Guenter Roeckb0636702012-09-07 17:34:41 -060026 * IT8771E Super I/O chip w/LPC interface
27 * IT8772E Super I/O chip w/LPC interface
Guenter Roeck7bc32d22015-01-17 14:10:24 -080028 * IT8781F Super I/O chip w/LPC interface
Guenter Roeck0531d982012-03-02 11:46:44 -080029 * IT8782F Super I/O chip w/LPC interface
30 * IT8783E/F Super I/O chip w/LPC interface
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +010031 * IT8786E Super I/O chip w/LPC interface
Guenter Roeck4ee07152015-03-25 23:26:28 -070032 * IT8790E Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010033 * Sis950 A clone of the IT8705F
34 *
35 * Copyright (C) 2001 Chris Gauthron
Jean Delvare7c81c602014-01-29 20:40:08 +010036 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
Jean Delvare5f2dc792010-03-05 22:17:18 +010037 *
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version.
42 *
43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
Jean Delvare5f2dc792010-03-05 22:17:18 +010047 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Joe Perchesa8ca1032011-01-12 21:55:10 +010049#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
50
Guenter Roeck48b2ae72015-04-02 08:06:12 -070051#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/module.h>
53#include <linux/init.h>
54#include <linux/slab.h>
55#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020056#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040057#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020058#include <linux/hwmon-sysfs.h>
59#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040060#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010061#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020062#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020063#include <linux/string.h>
64#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010065#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020066#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
corentin.labbeb74f3fd2007-06-13 20:27:36 +020068#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Justin Maggardead80802015-08-05 12:53:08 -070070enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
71 it8771, it8772, it8781, it8782, it8783, it8786, it8790, it8603,
72 it8620 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Jean Delvare67b671b2007-12-06 23:13:42 +010074static unsigned short force_id;
75module_param(force_id, ushort, 0);
76MODULE_PARM_DESC(force_id, "Override the detected device ID");
77
Guenter Roecke84bd952015-03-28 08:24:29 -070078static struct platform_device *it87_pdev[2];
corentin.labbeb74f3fd2007-06-13 20:27:36 +020079
Guenter Roeck3c2e3512015-03-28 08:03:10 -070080#define REG_2E 0x2e /* The register to read/write */
Guenter Roecke84bd952015-03-28 08:24:29 -070081#define REG_4E 0x4e /* Secondary register to read/write */
Guenter Roeck3c2e3512015-03-28 08:03:10 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define DEV 0x07 /* Register: Logical device select */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010085
86/* The device with the IT8718F/IT8720F VID value in it */
87#define GPIO 0x07
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define DEVID 0x20 /* Register: Device ID */
90#define DEVREV 0x22 /* Register: Device Revision */
91
Guenter Roeck3c2e3512015-03-28 08:03:10 -070092static inline int superio_inb(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Guenter Roeck3c2e3512015-03-28 08:03:10 -070094 outb(reg, ioreg);
95 return inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Guenter Roeck3c2e3512015-03-28 08:03:10 -070098static inline void superio_outb(int ioreg, int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +020099{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700100 outb(reg, ioreg);
101 outb(val, ioreg + 1);
Jean Delvare436cad22010-07-09 16:22:48 +0200102}
103
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700104static int superio_inw(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 int val;
Guenter Roeckc9620242015-04-04 09:05:57 -0700107
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700108 outb(reg++, ioreg);
109 val = inb(ioreg + 1) << 8;
110 outb(reg, ioreg);
111 val |= inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return val;
113}
114
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700115static inline void superio_select(int ioreg, int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700117 outb(DEV, ioreg);
118 outb(ldn, ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700121static inline int superio_enter(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200123 /*
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700124 * Try to reserve ioreg and ioreg + 1 for exclusive access.
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200125 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700126 if (!request_muxed_region(ioreg, 2, DRVNAME))
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200127 return -EBUSY;
128
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700129 outb(0x87, ioreg);
130 outb(0x01, ioreg);
131 outb(0x55, ioreg);
Guenter Roecke84bd952015-03-28 08:24:29 -0700132 outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700136static inline void superio_exit(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700138 outb(0x02, ioreg);
139 outb(0x02, ioreg + 1);
140 release_region(ioreg, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Jean Delvare87673dd2006-08-28 14:37:19 +0200143/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define IT8712F_DEVID 0x8712
145#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200146#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200147#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100148#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200149#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400150#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100151#define IT8728F_DEVID 0x8728
Justin Maggardead80802015-08-05 12:53:08 -0700152#define IT8732F_DEVID 0x8732
Guenter Roeckb0636702012-09-07 17:34:41 -0600153#define IT8771E_DEVID 0x8771
154#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800155#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800156#define IT8782F_DEVID 0x8782
157#define IT8783E_DEVID 0x8783
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100158#define IT8786E_DEVID 0x8786
Guenter Roeck4ee07152015-03-25 23:26:28 -0700159#define IT8790E_DEVID 0x8790
Rudolf Marek7183ae82014-04-04 18:01:35 +0200160#define IT8603E_DEVID 0x8603
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800161#define IT8620E_DEVID 0x8620
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200162#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define IT87_ACT_REG 0x30
164#define IT87_BASE_REG 0x60
165
Jean Delvare87673dd2006-08-28 14:37:19 +0200166/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800167#define IT87_SIO_GPIO1_REG 0x25
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800168#define IT87_SIO_GPIO2_REG 0x26
Jean Delvare895ff262009-12-09 20:35:47 +0100169#define IT87_SIO_GPIO3_REG 0x27
Guenter Roeck36c4d982015-03-26 18:18:19 -0700170#define IT87_SIO_GPIO4_REG 0x28
Jean Delvare591ec652009-12-09 20:35:48 +0100171#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800172#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200173#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800174#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200175#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100176#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030179static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030182static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/* Many IT87 constants specified below */
185
186/* Length of ISA address segment */
187#define IT87_EXTENT 8
188
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500189/* Length of ISA address segment for Environmental Controller */
190#define IT87_EC_EXTENT 2
191
192/* Offset of EC registers from ISA base address */
193#define IT87_EC_OFFSET 5
194
195/* Where are the ISA address/data registers relative to the EC base address */
196#define IT87_ADDR_REG_OFFSET 0
197#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199/*----- The IT87 registers -----*/
200
201#define IT87_REG_CONFIG 0x00
202
203#define IT87_REG_ALARM1 0x01
204#define IT87_REG_ALARM2 0x02
205#define IT87_REG_ALARM3 0x03
206
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800207/*
208 * The IT8718F and IT8720F have the VID value in a different register, in
209 * Super-I/O configuration space.
210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800212/*
213 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
214 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
215 * mode.
216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200218#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Guenter Roeckf838aa22015-04-01 20:09:36 -0700220/*
221 * Monitors:
222 * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
223 * - up to 6 temp (1 to 6)
224 * - up to 6 fan (1 to 6)
225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700227static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
228static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
229static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
230static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
231static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#define IT87_REG_FAN_MAIN_CTRL 0x13
234#define IT87_REG_FAN_CTL 0x14
Guenter Roeck36c4d982015-03-26 18:18:19 -0700235static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
236static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Guenter Roeck559313c2015-04-01 10:15:38 -0700238static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
Guenter Roeckf838aa22015-04-01 20:09:36 -0700239 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Guenter Roeck559313c2015-04-01 10:15:38 -0700241#define IT87_REG_TEMP(nr) (0x29 + (nr))
Guenter Roeck73055402015-03-26 09:16:32 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
244#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
245#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
246#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#define IT87_REG_VIN_ENABLE 0x50
249#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700250#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100251#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253#define IT87_REG_CHIPID 0x58
254
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100255#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
256#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
257
Guenter Roeckcc18da72015-04-01 10:03:01 -0700258#define IT87_REG_TEMP456_ENABLE 0x77
259
Guenter Roeck23100482015-04-02 08:23:45 -0700260#define NUM_VIN ARRAY_SIZE(IT87_REG_VIN)
261#define NUM_VIN_LIMIT 8
262#define NUM_TEMP 6
263#define NUM_TEMP_OFFSET ARRAY_SIZE(IT87_REG_TEMP_OFFSET)
264#define NUM_TEMP_LIMIT 3
265#define NUM_FAN ARRAY_SIZE(IT87_REG_FAN)
266#define NUM_FAN_DIV 3
267#define NUM_PWM ARRAY_SIZE(IT87_REG_PWM)
268#define NUM_AUTO_PWM ARRAY_SIZE(IT87_REG_PWM)
269
Guenter Roeck483db432012-12-19 22:17:02 +0100270struct it87_devices {
271 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700272 const char * const suffix;
Guenter Roeckcc18da72015-04-01 10:03:01 -0700273 u32 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100274 u8 peci_mask;
275 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100276};
277
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700278#define FEAT_12MV_ADC BIT(0)
279#define FEAT_NEWER_AUTOPWM BIT(1)
280#define FEAT_OLD_AUTOPWM BIT(2)
281#define FEAT_16BIT_FANS BIT(3)
282#define FEAT_TEMP_OFFSET BIT(4)
283#define FEAT_TEMP_PECI BIT(5)
284#define FEAT_TEMP_OLD_PECI BIT(6)
285#define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
286#define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
287#define FEAT_VID BIT(9) /* Set if chip supports VID */
288#define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
289#define FEAT_SIX_FANS BIT(11) /* Supports six fans */
290#define FEAT_10_9MV_ADC BIT(12)
291#define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
292#define FEAT_SIX_PWM BIT(14) /* Chip supports 6 pwm chn */
293#define FEAT_PWM_FREQ2 BIT(15) /* Separate pwm freq 2 */
294#define FEAT_SIX_TEMP BIT(16) /* Up to 6 temp sensors */
Guenter Roeck483db432012-12-19 22:17:02 +0100295
296static const struct it87_devices it87_devices[] = {
297 [it87] = {
298 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700299 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100300 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
301 },
302 [it8712] = {
303 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700304 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800305 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
306 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100307 },
308 [it8716] = {
309 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700310 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800311 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700312 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
Guenter Roeck483db432012-12-19 22:17:02 +0100313 },
314 [it8718] = {
315 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700316 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800317 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700318 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
319 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100320 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100321 },
322 [it8720] = {
323 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700324 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800325 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700326 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
327 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100328 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100329 },
330 [it8721] = {
331 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700332 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100333 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800334 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck60878bc2015-03-26 19:57:42 -0700335 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
336 | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100337 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100338 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100339 },
340 [it8728] = {
341 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700342 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100343 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700344 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700345 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100346 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100347 },
Justin Maggardead80802015-08-05 12:53:08 -0700348 [it8732] = {
349 .name = "it8732",
350 .suffix = "F",
351 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
352 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
353 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
354 .peci_mask = 0x07,
355 .old_peci_mask = 0x02, /* Actually reports PCH */
356 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600357 [it8771] = {
358 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700359 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600360 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700361 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
362 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800363 /* PECI: guesswork */
364 /* 12mV ADC (OHM) */
365 /* 16 bit fans (OHM) */
366 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600367 .peci_mask = 0x07,
368 },
369 [it8772] = {
370 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700371 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600372 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700373 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
374 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800375 /* PECI (coreboot) */
376 /* 12mV ADC (HWSensors4, OHM) */
377 /* 16 bit fans (HWSensors4, OHM) */
378 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600379 .peci_mask = 0x07,
380 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800381 [it8781] = {
382 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700383 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800384 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700385 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800386 .old_peci_mask = 0x4,
387 },
Guenter Roeck483db432012-12-19 22:17:02 +0100388 [it8782] = {
389 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700390 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100391 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700392 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100393 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100394 },
395 [it8783] = {
396 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700397 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100398 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700399 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100400 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100401 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100402 [it8786] = {
403 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700404 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100405 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700406 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
407 | FEAT_PWM_FREQ2,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100408 .peci_mask = 0x07,
409 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700410 [it8790] = {
411 .name = "it8790",
412 .suffix = "E",
413 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700414 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
415 | FEAT_PWM_FREQ2,
Guenter Roeck4ee07152015-03-25 23:26:28 -0700416 .peci_mask = 0x07,
417 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100418 [it8603] = {
419 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700420 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100421 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck73055402015-03-26 09:16:32 -0700422 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
Guenter Roeck60878bc2015-03-26 19:57:42 -0700423 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100424 .peci_mask = 0x07,
425 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800426 [it8620] = {
427 .name = "it8620",
428 .suffix = "E",
429 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700430 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeckcc18da72015-04-01 10:03:01 -0700431 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
432 | FEAT_SIX_TEMP,
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800433 .peci_mask = 0x07,
434 },
Guenter Roeck483db432012-12-19 22:17:02 +0100435};
436
437#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
438#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
Justin Maggardead80802015-08-05 12:53:08 -0700439#define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
Guenter Roeck483db432012-12-19 22:17:02 +0100440#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
441#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
442#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100443#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700444 ((data)->peci_mask & BIT(nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100445#define has_temp_old_peci(data, nr) \
446 (((data)->features & FEAT_TEMP_OLD_PECI) && \
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700447 ((data)->old_peci_mask & BIT(nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800448#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700449#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
450 FEAT_SIX_FANS))
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800451#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700452#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700453#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Guenter Roeck73055402015-03-26 09:16:32 -0700454#define has_avcc3(data) ((data)->features & FEAT_AVCC3)
Guenter Roeck36c4d982015-03-26 18:18:19 -0700455#define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
Guenter Roeck60878bc2015-03-26 19:57:42 -0700456#define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
Guenter Roeckcc18da72015-04-01 10:03:01 -0700457#define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200459struct it87_sio_data {
460 enum chips type;
461 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200462 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200463 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100464 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200465 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100466 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700467 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100468 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100469 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200470 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700471 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200472};
473
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800474/*
475 * For each registered chip, we need to keep some data in memory.
476 * The structure is dynamically allocated.
477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478struct it87_data {
Guenter Roeck8638d0a2015-03-30 11:13:49 -0700479 const struct attribute_group *groups[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100481 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100482 u8 peci_mask;
483 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200485 unsigned short addr;
486 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100487 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 char valid; /* !=0 if following fields are valid */
489 unsigned long last_updated; /* In jiffies */
490
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200491 u16 in_scaled; /* Internal voltage sensors are scaled */
Guenter Roeckd3766842013-03-30 15:47:21 -0700492 u16 in_internal; /* Bitfield, internal sensors (for labels) */
Guenter Roeck52929712013-03-30 14:00:08 -0700493 u16 has_in; /* Bitfield, voltage sensors enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700494 u8 in[NUM_VIN][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200495 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700496 u16 fan[NUM_FAN][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700497 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700498 s8 temp[NUM_TEMP][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100499 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
500 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Guenter Roeck23100482015-04-02 08:23:45 -0700501 u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
Guenter Roeckd3766842013-03-30 15:47:21 -0700502 bool has_vid; /* True if VID supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100504 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 u32 alarms; /* Register encoding, combined */
Guenter Roeck52929712013-03-30 14:00:08 -0700506 bool has_beep; /* true if beep supported */
Jean Delvared9b327c2010-03-05 22:17:17 +0100507 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100509 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100510
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800511 /*
512 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100513 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
514 * 7, and we want to preserve settings on mode changes, so we have
515 * to track all values separately.
516 * Starting with the IT8721F, the manual PWM duty cycles are stored
517 * in separate registers (8-bit values), so the separate tracking
518 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800519 * simple.
520 */
Guenter Roeck5c391262013-03-30 15:02:12 -0700521 u8 has_pwm; /* Bitfield, pwm control enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700522 u8 pwm_ctrl[NUM_PWM]; /* Register value */
523 u8 pwm_duty[NUM_PWM]; /* Manual PWM value set by user */
524 u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100525
526 /* Automatic fan speed control registers */
Guenter Roeck23100482015-04-02 08:23:45 -0700527 u8 auto_pwm[NUM_AUTO_PWM][4]; /* [nr][3] is hard-coded */
528 s8 auto_temp[NUM_AUTO_PWM][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529};
530
Guenter Roeck0531d982012-03-02 11:46:44 -0800531static int adc_lsb(const struct it87_data *data, int nr)
532{
Justin Maggardead80802015-08-05 12:53:08 -0700533 int lsb;
534
535 if (has_12mv_adc(data))
536 lsb = 120;
537 else if (has_10_9mv_adc(data))
538 lsb = 109;
539 else
540 lsb = 160;
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700541 if (data->in_scaled & BIT(nr))
Guenter Roeck0531d982012-03-02 11:46:44 -0800542 lsb <<= 1;
543 return lsb;
544}
545
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200546static u8 in_to_reg(const struct it87_data *data, int nr, long val)
547{
Justin Maggardead80802015-08-05 12:53:08 -0700548 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800549 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200550}
551
552static int in_from_reg(const struct it87_data *data, int nr, int val)
553{
Justin Maggardead80802015-08-05 12:53:08 -0700554 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200555}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200556
557static inline u8 FAN_TO_REG(long rpm, int div)
558{
559 if (rpm == 0)
560 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800561 rpm = clamp_val(rpm, 1, 1000000);
562 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200563}
564
565static inline u16 FAN16_TO_REG(long rpm)
566{
567 if (rpm == 0)
568 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800569 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200570}
571
572#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
573 1350000 / ((val) * (div)))
574/* The divider is fixed to 2 in 16-bit mode */
575#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
576 1350000 / ((val) * 2))
577
Guenter Roeck2a844c12013-01-09 08:09:34 -0800578#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
579 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200580#define TEMP_FROM_REG(val) ((val) * 1000)
581
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200582static u8 pwm_to_reg(const struct it87_data *data, long val)
583{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100584 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200585 return val;
586 else
587 return val >> 1;
588}
589
590static int pwm_from_reg(const struct it87_data *data, u8 reg)
591{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100592 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200593 return reg;
594 else
595 return (reg & 0x7f) << 1;
596}
597
Jean Delvare0df6454d2010-10-28 20:31:51 +0200598static int DIV_TO_REG(int val)
599{
600 int answer = 0;
Guenter Roeckc9620242015-04-04 09:05:57 -0700601
Jean Delvare0df6454d2010-10-28 20:31:51 +0200602 while (answer < 7 && (val >>= 1))
603 answer++;
604 return answer;
605}
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700606
607#define DIV_FROM_REG(val) BIT(val)
Jean Delvare0df6454d2010-10-28 20:31:51 +0200608
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700609/*
610 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
611 * depending on the chip type, to calculate the actual PWM frequency.
612 *
613 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
614 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
615 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
616 * sometimes just one. It is unknown if this is a datasheet error or real,
617 * so this is ignored for now.
618 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200619static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700620 48000000,
621 24000000,
622 12000000,
623 8000000,
624 6000000,
625 3000000,
626 1500000,
627 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200628};
629
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700630/*
631 * Must be called with data->update_lock held, except during initialization.
632 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
633 * would slow down the IT87 access and should not be necessary.
634 */
635static int it87_read_value(struct it87_data *data, u8 reg)
636{
637 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
638 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
639}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700641/*
642 * Must be called with data->update_lock held, except during initialization.
643 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
644 * would slow down the IT87 access and should not be necessary.
645 */
646static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
647{
648 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
649 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
650}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700652static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
653{
654 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
655 if (has_newer_autopwm(data)) {
656 data->pwm_temp_map[nr] = (data->pwm_ctrl[nr] & 0x03) +
657 nr < 3 ? 0 : 3;
658 data->pwm_duty[nr] = it87_read_value(data,
659 IT87_REG_PWM_DUTY[nr]);
660 } else {
661 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
662 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
663 else /* Manual mode */
664 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700667 if (has_old_autopwm(data)) {
668 int i;
669
670 for (i = 0; i < 5 ; i++)
671 data->auto_temp[nr][i] = it87_read_value(data,
672 IT87_REG_AUTO_TEMP(nr, i));
673 for (i = 0; i < 3 ; i++)
674 data->auto_pwm[nr][i] = it87_read_value(data,
675 IT87_REG_AUTO_PWM(nr, i));
676 }
677}
678
679static struct it87_data *it87_update_device(struct device *dev)
680{
681 struct it87_data *data = dev_get_drvdata(dev);
682 int i;
683
684 mutex_lock(&data->update_lock);
685
Guenter Roeckc9620242015-04-04 09:05:57 -0700686 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
687 !data->valid) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700688 if (update_vbat) {
689 /*
690 * Cleared after each update, so reenable. Value
691 * returned by this read will be previous value
692 */
693 it87_write_value(data, IT87_REG_CONFIG,
694 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
695 }
Guenter Roeck23100482015-04-02 08:23:45 -0700696 for (i = 0; i < NUM_VIN; i++) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700697 if (!(data->has_in & BIT(i)))
Guenter Roeck559313c2015-04-01 10:15:38 -0700698 continue;
699
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700700 data->in[i][0] =
Guenter Roeck559313c2015-04-01 10:15:38 -0700701 it87_read_value(data, IT87_REG_VIN[i]);
702
703 /* VBAT and AVCC don't have limit registers */
Guenter Roeck23100482015-04-02 08:23:45 -0700704 if (i >= NUM_VIN_LIMIT)
Guenter Roeck559313c2015-04-01 10:15:38 -0700705 continue;
706
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700707 data->in[i][1] =
708 it87_read_value(data, IT87_REG_VIN_MIN(i));
709 data->in[i][2] =
710 it87_read_value(data, IT87_REG_VIN_MAX(i));
711 }
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700712
Guenter Roeck23100482015-04-02 08:23:45 -0700713 for (i = 0; i < NUM_FAN; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700714 /* Skip disabled fans */
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700715 if (!(data->has_fan & BIT(i)))
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700716 continue;
717
718 data->fan[i][1] =
719 it87_read_value(data, IT87_REG_FAN_MIN[i]);
720 data->fan[i][0] = it87_read_value(data,
721 IT87_REG_FAN[i]);
722 /* Add high byte if in 16-bit mode */
723 if (has_16bit_fans(data)) {
724 data->fan[i][0] |= it87_read_value(data,
725 IT87_REG_FANX[i]) << 8;
726 data->fan[i][1] |= it87_read_value(data,
727 IT87_REG_FANX_MIN[i]) << 8;
728 }
729 }
Guenter Roeck23100482015-04-02 08:23:45 -0700730 for (i = 0; i < NUM_TEMP; i++) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700731 if (!(data->has_temp & BIT(i)))
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700732 continue;
733 data->temp[i][0] =
734 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeckcc18da72015-04-01 10:03:01 -0700735
Guenter Roeck23100482015-04-02 08:23:45 -0700736 if (has_temp_offset(data) && i < NUM_TEMP_OFFSET)
737 data->temp[i][3] =
738 it87_read_value(data,
739 IT87_REG_TEMP_OFFSET[i]);
740
741 if (i >= NUM_TEMP_LIMIT)
Guenter Roeckcc18da72015-04-01 10:03:01 -0700742 continue;
743
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700744 data->temp[i][1] =
745 it87_read_value(data, IT87_REG_TEMP_LOW(i));
746 data->temp[i][2] =
747 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700748 }
749
750 /* Newer chips don't have clock dividers */
751 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
752 i = it87_read_value(data, IT87_REG_FAN_DIV);
753 data->fan_div[0] = i & 0x07;
754 data->fan_div[1] = (i >> 3) & 0x07;
755 data->fan_div[2] = (i & 0x40) ? 3 : 1;
756 }
757
758 data->alarms =
759 it87_read_value(data, IT87_REG_ALARM1) |
760 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
761 (it87_read_value(data, IT87_REG_ALARM3) << 16);
762 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
763
764 data->fan_main_ctrl = it87_read_value(data,
765 IT87_REG_FAN_MAIN_CTRL);
766 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Guenter Roeck23100482015-04-02 08:23:45 -0700767 for (i = 0; i < NUM_PWM; i++)
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700768 it87_update_pwm_ctrl(data, i);
769
770 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
771 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
772 /*
773 * The IT8705F does not have VID capability.
774 * The IT8718F and later don't use IT87_REG_VID for the
775 * same purpose.
776 */
777 if (data->type == it8712 || data->type == it8716) {
778 data->vid = it87_read_value(data, IT87_REG_VID);
779 /*
780 * The older IT8712F revisions had only 5 VID pins,
781 * but we assume it is always safe to read 6 bits.
782 */
783 data->vid &= 0x3f;
784 }
785 data->last_updated = jiffies;
786 data->valid = 1;
787 }
788
789 mutex_unlock(&data->update_lock);
790
791 return data;
792}
Jean Delvarefde09502005-07-19 23:51:07 +0200793
Jean Delvare20ad93d2005-06-05 11:53:25 +0200794static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100795 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100797 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -0700799 int index = sattr->index;
800 int nr = sattr->nr;
801
Guenter Roeck929c6a52012-12-19 22:17:00 +0100802 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Guenter Roeck929c6a52012-12-19 22:17:00 +0100805static ssize_t set_in(struct device *dev, struct device_attribute *attr,
806 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100808 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200809 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -0700810 int index = sattr->index;
811 int nr = sattr->nr;
Jean Delvaref5f64502010-03-05 22:17:19 +0100812 unsigned long val;
813
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100814 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100815 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100817 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100818 data->in[nr][index] = in_to_reg(data, nr, val);
819 it87_write_value(data,
820 index == 1 ? IT87_REG_VIN_MIN(nr)
821 : IT87_REG_VIN_MAX(nr),
822 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100823 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return count;
825}
826
Guenter Roeck929c6a52012-12-19 22:17:00 +0100827static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
828static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
829 0, 1);
830static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
831 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Guenter Roeck929c6a52012-12-19 22:17:00 +0100833static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
834static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
835 1, 1);
836static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
837 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Guenter Roeck929c6a52012-12-19 22:17:00 +0100839static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
840static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
841 2, 1);
842static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
843 2, 2);
844
845static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
846static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
847 3, 1);
848static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
849 3, 2);
850
851static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
852static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
853 4, 1);
854static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
855 4, 2);
856
857static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
858static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
859 5, 1);
860static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
861 5, 2);
862
863static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
864static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
865 6, 1);
866static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
867 6, 2);
868
869static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
870static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
871 7, 1);
872static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
873 7, 2);
874
875static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100876static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Guenter Roeckf838aa22015-04-01 20:09:36 -0700877static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
878static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
879static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Guenter Roeckcc18da72015-04-01 10:03:01 -0700881/* Up to 6 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200882static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100883 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100885 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
886 int nr = sattr->nr;
887 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200889
Guenter Roeck60ca3852012-12-19 22:17:00 +0100890 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200892
Guenter Roeck60ca3852012-12-19 22:17:00 +0100893static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
894 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100896 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
897 int nr = sattr->nr;
898 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200899 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100900 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100901 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100902
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100903 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100904 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100906 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100907
908 switch (index) {
909 default:
910 case 1:
911 reg = IT87_REG_TEMP_LOW(nr);
912 break;
913 case 2:
914 reg = IT87_REG_TEMP_HIGH(nr);
915 break;
916 case 3:
917 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
918 if (!(regval & 0x80)) {
919 regval |= 0x80;
920 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
921 }
922 data->valid = 0;
923 reg = IT87_REG_TEMP_OFFSET[nr];
924 break;
925 }
926
Guenter Roeck60ca3852012-12-19 22:17:00 +0100927 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100928 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100929 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return count;
931}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200932
Guenter Roeck60ca3852012-12-19 22:17:00 +0100933static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
934static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
935 0, 1);
936static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
937 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100938static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
939 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100940static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
941static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
942 1, 1);
943static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
944 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100945static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
946 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100947static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
948static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
949 2, 1);
950static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
951 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100952static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
953 set_temp, 2, 3);
Guenter Roeckcc18da72015-04-01 10:03:01 -0700954static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
955static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
956static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Guenter Roeck2cece012012-12-19 22:17:01 +0100958static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
959 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200961 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
962 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800964 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100965 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100966
Guenter Roeckc9620242015-04-04 09:05:57 -0700967 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1)) ||
968 (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100969 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (reg & (1 << nr))
971 return sprintf(buf, "3\n"); /* thermal diode */
972 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200973 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return sprintf(buf, "0\n"); /* disabled */
975}
Guenter Roeck2cece012012-12-19 22:17:01 +0100976
977static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
978 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200980 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
981 int nr = sensor_attr->index;
982
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200983 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100984 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100985 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100986
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100987 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100988 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Jean Delvare8acf07c2010-04-14 16:14:09 +0200990 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
991 reg &= ~(1 << nr);
992 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100993 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
994 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100995 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
996 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
997 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200998 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100999 dev_warn(dev,
1000 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +02001001 val = 4;
1002 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001003 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +02001005 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +02001006 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +02001007 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001008 else if (has_temp_peci(data, nr) && val == 6)
1009 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +01001010 else if (has_temp_old_peci(data, nr) && val == 6)
1011 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +02001012 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +02001014
1015 mutex_lock(&data->update_lock);
1016 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +01001017 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001018 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +01001019 if (has_temp_old_peci(data, nr))
1020 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +02001021 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001022 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return count;
1024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Guenter Roeck2cece012012-12-19 22:17:01 +01001026static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1027 set_temp_type, 0);
1028static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1029 set_temp_type, 1);
1030static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1031 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001033/* 6 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +01001034
1035static int pwm_mode(const struct it87_data *data, int nr)
1036{
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001037 if (data->type != it8603 && nr < 3 && !(data->fan_main_ctrl & BIT(nr)))
1038 return 0; /* Full speed */
1039 if (data->pwm_ctrl[nr] & 0x80)
1040 return 2; /* Automatic mode */
1041 if ((data->type == it8603 || nr >= 3) &&
1042 data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1043 return 0; /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +01001044
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001045 return 1; /* Manual mode */
Jean Delvareb99883d2010-03-05 22:17:15 +01001046}
1047
Jean Delvare20ad93d2005-06-05 11:53:25 +02001048static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +01001049 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001051 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1052 int nr = sattr->nr;
1053 int index = sattr->index;
1054 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001056
Guenter Roecke1169ba2012-12-19 22:17:01 +01001057 speed = has_16bit_fans(data) ?
1058 FAN16_FROM_REG(data->fan[nr][index]) :
1059 FAN_FROM_REG(data->fan[nr][index],
1060 DIV_FROM_REG(data->fan_div[nr]));
1061 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001063
Jean Delvare20ad93d2005-06-05 11:53:25 +02001064static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001065 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001067 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckc9620242015-04-04 09:05:57 -07001068 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001069 int nr = sensor_attr->index;
1070
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001071 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
Guenter Roeckc9620242015-04-04 09:05:57 -07001073
Jean Delvare5f2dc792010-03-05 22:17:18 +01001074static ssize_t show_pwm_enable(struct device *dev,
Guenter Roeckc9620242015-04-04 09:05:57 -07001075 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001077 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckc9620242015-04-04 09:05:57 -07001078 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001079 int nr = sensor_attr->index;
1080
Jean Delvareb99883d2010-03-05 22:17:15 +01001081 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
Guenter Roeckc9620242015-04-04 09:05:57 -07001083
Jean Delvare20ad93d2005-06-05 11:53:25 +02001084static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001085 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001087 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckc9620242015-04-04 09:05:57 -07001088 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001089 int nr = sensor_attr->index;
1090
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001091 return sprintf(buf, "%d\n",
1092 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
Guenter Roeckc9620242015-04-04 09:05:57 -07001094
Jean Delvaref8d0c192007-02-14 21:15:02 +01001095static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001096 char *buf)
Jean Delvaref8d0c192007-02-14 21:15:02 +01001097{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001098 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001099 struct it87_data *data = it87_update_device(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001100 int nr = sensor_attr->index;
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001101 unsigned int freq;
Guenter Roeck60878bc2015-03-26 19:57:42 -07001102 int index;
1103
1104 if (has_pwm_freq2(data) && nr == 1)
1105 index = (data->extra >> 4) & 0x07;
1106 else
1107 index = (data->fan_ctl >> 4) & 0x07;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001108
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001109 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1110
1111 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001112}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001113
1114static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1115 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001117 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1118 int nr = sattr->nr;
1119 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +02001120
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001121 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001122 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +01001123 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001125 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001126 return -EINVAL;
1127
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001128 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +01001129
1130 if (has_16bit_fans(data)) {
1131 data->fan[nr][index] = FAN16_TO_REG(val);
1132 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1133 data->fan[nr][index] & 0xff);
1134 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1135 data->fan[nr][index] >> 8);
1136 } else {
1137 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1138 switch (nr) {
1139 case 0:
1140 data->fan_div[nr] = reg & 0x07;
1141 break;
1142 case 1:
1143 data->fan_div[nr] = (reg >> 3) & 0x07;
1144 break;
1145 case 2:
1146 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1147 break;
1148 }
1149 data->fan[nr][index] =
1150 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1151 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1152 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -08001153 }
1154
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001155 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return count;
1157}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001158
Jean Delvare20ad93d2005-06-05 11:53:25 +02001159static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001160 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001162 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001163 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001164 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001165 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001166 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 u8 old;
1168
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001169 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001170 return -EINVAL;
1171
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001172 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001173 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001175 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001176 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 switch (nr) {
1179 case 0:
1180 case 1:
1181 data->fan_div[nr] = DIV_TO_REG(val);
1182 break;
1183 case 2:
1184 if (val < 8)
1185 data->fan_div[nr] = 1;
1186 else
1187 data->fan_div[nr] = 3;
1188 }
1189 val = old & 0x80;
1190 val |= (data->fan_div[0] & 0x07);
1191 val |= (data->fan_div[1] & 0x07) << 3;
1192 if (data->fan_div[2] == 3)
1193 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001194 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001196 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001197 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1198 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001199
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001200 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return count;
1202}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001203
1204/* Returns 0 if OK, -EINVAL otherwise */
1205static int check_trip_points(struct device *dev, int nr)
1206{
1207 const struct it87_data *data = dev_get_drvdata(dev);
1208 int i, err = 0;
1209
1210 if (has_old_autopwm(data)) {
1211 for (i = 0; i < 3; i++) {
1212 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1213 err = -EINVAL;
1214 }
1215 for (i = 0; i < 2; i++) {
1216 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1217 err = -EINVAL;
1218 }
1219 }
1220
1221 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001222 dev_err(dev,
1223 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001224 dev_err(dev, "Adjust the trip points and try again\n");
1225 }
1226 return err;
1227}
1228
Guenter Roeckc9620242015-04-04 09:05:57 -07001229static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1230 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001232 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001233 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001234 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001235 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001237 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001238 return -EINVAL;
1239
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001240 /* Check trip points before switching to automatic mode */
1241 if (val == 2) {
1242 if (check_trip_points(dev, nr) < 0)
1243 return -EINVAL;
1244 }
1245
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001246 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 if (val == 0) {
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001249 if (nr < 3 && data->type != it8603) {
1250 int tmp;
1251 /* make sure the fan is on when in on/off mode */
1252 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1253 it87_write_value(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1254 /* set on/off mode */
1255 data->fan_main_ctrl &= ~BIT(nr);
1256 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1257 data->fan_main_ctrl);
1258 } else {
1259 /* No on/off mode, set maximum pwm value */
1260 data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1261 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1262 data->pwm_duty[nr]);
1263 /* and set manual mode */
1264 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1265 data->pwm_temp_map[nr] :
1266 data->pwm_duty[nr];
1267 it87_write_value(data, IT87_REG_PWM[nr],
1268 data->pwm_ctrl[nr]);
1269 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001270 } else {
1271 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001272 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001273 data->pwm_temp_map[nr] :
1274 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001275 else /* Automatic mode */
1276 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001277 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001278
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001279 if (data->type != it8603 && nr < 3) {
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001280 /* set SmartGuardian mode */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001281 data->fan_main_ctrl |= BIT(nr);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001282 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1283 data->fan_main_ctrl);
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001287 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return count;
1289}
Guenter Roeckc9620242015-04-04 09:05:57 -07001290
Jean Delvare20ad93d2005-06-05 11:53:25 +02001291static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001292 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001294 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001295 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001296 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001297 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001299 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return -EINVAL;
1301
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001302 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001303 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001304 /*
1305 * If we are in automatic mode, the PWM duty cycle register
1306 * is read-only so we can't write the value.
1307 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001308 if (data->pwm_ctrl[nr] & 0x80) {
1309 mutex_unlock(&data->update_lock);
1310 return -EBUSY;
1311 }
1312 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001313 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001314 data->pwm_duty[nr]);
1315 } else {
1316 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001317 /*
1318 * If we are in manual mode, write the duty cycle immediately;
1319 * otherwise, just store it for later use.
1320 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001321 if (!(data->pwm_ctrl[nr] & 0x80)) {
1322 data->pwm_ctrl[nr] = data->pwm_duty[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001323 it87_write_value(data, IT87_REG_PWM[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001324 data->pwm_ctrl[nr]);
1325 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001326 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001327 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return count;
1329}
Guenter Roeckc9620242015-04-04 09:05:57 -07001330
1331static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1332 const char *buf, size_t count)
Jean Delvaref8d0c192007-02-14 21:15:02 +01001333{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001334 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001335 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001336 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001337 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001338 int i;
1339
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001340 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001341 return -EINVAL;
1342
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001343 val = clamp_val(val, 0, 1000000);
1344 val *= has_newer_autopwm(data) ? 256 : 128;
1345
Jean Delvaref8d0c192007-02-14 21:15:02 +01001346 /* Search for the nearest available frequency */
1347 for (i = 0; i < 7; i++) {
Guenter Roeckc9620242015-04-04 09:05:57 -07001348 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
Jean Delvaref8d0c192007-02-14 21:15:02 +01001349 break;
1350 }
1351
1352 mutex_lock(&data->update_lock);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001353 if (nr == 0) {
1354 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1355 data->fan_ctl |= i << 4;
1356 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1357 } else {
1358 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1359 data->extra |= i << 4;
1360 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1361 }
Jean Delvaref8d0c192007-02-14 21:15:02 +01001362 mutex_unlock(&data->update_lock);
1363
1364 return count;
1365}
Guenter Roeckc9620242015-04-04 09:05:57 -07001366
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001367static ssize_t show_pwm_temp_map(struct device *dev,
Guenter Roeckc9620242015-04-04 09:05:57 -07001368 struct device_attribute *attr, char *buf)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001369{
1370 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001371 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001372 int nr = sensor_attr->index;
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001373 int map;
1374
1375 if (data->pwm_temp_map[nr] < 3)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001376 map = BIT(data->pwm_temp_map[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001377 else
1378 map = 0; /* Should never happen */
1379 return sprintf(buf, "%d\n", map);
1380}
Guenter Roeckc9620242015-04-04 09:05:57 -07001381
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001382static ssize_t set_pwm_temp_map(struct device *dev,
Guenter Roeckc9620242015-04-04 09:05:57 -07001383 struct device_attribute *attr, const char *buf,
1384 size_t count)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001385{
1386 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001387 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001388 int nr = sensor_attr->index;
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001389 long val;
1390 u8 reg;
1391
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001392 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001393 return -EINVAL;
1394
1395 switch (val) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001396 case BIT(0):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001397 reg = 0x00;
1398 break;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001399 case BIT(1):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001400 reg = 0x01;
1401 break;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001402 case BIT(2):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001403 reg = 0x02;
1404 break;
1405 default:
1406 return -EINVAL;
1407 }
1408
1409 mutex_lock(&data->update_lock);
1410 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001411 /*
1412 * If we are in automatic mode, write the temp mapping immediately;
1413 * otherwise, just store it for later use.
1414 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001415 if (data->pwm_ctrl[nr] & 0x80) {
1416 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001417 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001418 }
1419 mutex_unlock(&data->update_lock);
1420 return count;
1421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Guenter Roeckc9620242015-04-04 09:05:57 -07001423static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1424 char *buf)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001425{
1426 struct it87_data *data = it87_update_device(dev);
1427 struct sensor_device_attribute_2 *sensor_attr =
1428 to_sensor_dev_attr_2(attr);
1429 int nr = sensor_attr->nr;
1430 int point = sensor_attr->index;
1431
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001432 return sprintf(buf, "%d\n",
1433 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001434}
1435
Guenter Roeckc9620242015-04-04 09:05:57 -07001436static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
1437 const char *buf, size_t count)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001438{
1439 struct it87_data *data = dev_get_drvdata(dev);
1440 struct sensor_device_attribute_2 *sensor_attr =
1441 to_sensor_dev_attr_2(attr);
1442 int nr = sensor_attr->nr;
1443 int point = sensor_attr->index;
1444 long val;
1445
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001446 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001447 return -EINVAL;
1448
1449 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001450 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001451 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1452 data->auto_pwm[nr][point]);
1453 mutex_unlock(&data->update_lock);
1454 return count;
1455}
1456
Guenter Roeckc9620242015-04-04 09:05:57 -07001457static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
1458 char *buf)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001459{
1460 struct it87_data *data = it87_update_device(dev);
1461 struct sensor_device_attribute_2 *sensor_attr =
1462 to_sensor_dev_attr_2(attr);
1463 int nr = sensor_attr->nr;
1464 int point = sensor_attr->index;
1465
1466 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1467}
1468
Guenter Roeckc9620242015-04-04 09:05:57 -07001469static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
1470 const char *buf, size_t count)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001471{
1472 struct it87_data *data = dev_get_drvdata(dev);
1473 struct sensor_device_attribute_2 *sensor_attr =
1474 to_sensor_dev_attr_2(attr);
1475 int nr = sensor_attr->nr;
1476 int point = sensor_attr->index;
1477 long val;
1478
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001479 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001480 return -EINVAL;
1481
1482 mutex_lock(&data->update_lock);
1483 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1484 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1485 data->auto_temp[nr][point]);
1486 mutex_unlock(&data->update_lock);
1487 return count;
1488}
1489
Guenter Roecke1169ba2012-12-19 22:17:01 +01001490static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1491static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1492 0, 1);
1493static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1494 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Guenter Roecke1169ba2012-12-19 22:17:01 +01001496static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1497static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1498 1, 1);
1499static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1500 set_fan_div, 1);
1501
1502static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1503static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1504 2, 1);
1505static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1506 set_fan_div, 2);
1507
1508static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1509static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1510 3, 1);
1511
1512static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1513static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1514 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001516static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1517static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1518 5, 1);
1519
Guenter Roeckc4458db2012-12-19 22:17:02 +01001520static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1521 show_pwm_enable, set_pwm_enable, 0);
1522static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001523static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1524 set_pwm_freq, 0);
Guenter Roeck5c391262013-03-30 15:02:12 -07001525static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001526 show_pwm_temp_map, set_pwm_temp_map, 0);
1527static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1528 show_auto_pwm, set_auto_pwm, 0, 0);
1529static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1530 show_auto_pwm, set_auto_pwm, 0, 1);
1531static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1532 show_auto_pwm, set_auto_pwm, 0, 2);
1533static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1534 show_auto_pwm, NULL, 0, 3);
1535static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1536 show_auto_temp, set_auto_temp, 0, 1);
1537static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1538 show_auto_temp, set_auto_temp, 0, 0);
1539static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1540 show_auto_temp, set_auto_temp, 0, 2);
1541static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1542 show_auto_temp, set_auto_temp, 0, 3);
1543static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1544 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Guenter Roeckc4458db2012-12-19 22:17:02 +01001546static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1547 show_pwm_enable, set_pwm_enable, 1);
1548static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001549static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
Guenter Roeck5c391262013-03-30 15:02:12 -07001550static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001551 show_pwm_temp_map, set_pwm_temp_map, 1);
1552static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1553 show_auto_pwm, set_auto_pwm, 1, 0);
1554static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1555 show_auto_pwm, set_auto_pwm, 1, 1);
1556static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1557 show_auto_pwm, set_auto_pwm, 1, 2);
1558static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1559 show_auto_pwm, NULL, 1, 3);
1560static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1561 show_auto_temp, set_auto_temp, 1, 1);
1562static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1563 show_auto_temp, set_auto_temp, 1, 0);
1564static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1565 show_auto_temp, set_auto_temp, 1, 2);
1566static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1567 show_auto_temp, set_auto_temp, 1, 3);
1568static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1569 show_auto_temp, set_auto_temp, 1, 4);
1570
1571static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1572 show_pwm_enable, set_pwm_enable, 2);
1573static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001574static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
Guenter Roeck5c391262013-03-30 15:02:12 -07001575static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001576 show_pwm_temp_map, set_pwm_temp_map, 2);
1577static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1578 show_auto_pwm, set_auto_pwm, 2, 0);
1579static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1580 show_auto_pwm, set_auto_pwm, 2, 1);
1581static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1582 show_auto_pwm, set_auto_pwm, 2, 2);
1583static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1584 show_auto_pwm, NULL, 2, 3);
1585static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1586 show_auto_temp, set_auto_temp, 2, 1);
1587static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1588 show_auto_temp, set_auto_temp, 2, 0);
1589static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1590 show_auto_temp, set_auto_temp, 2, 2);
1591static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1592 show_auto_temp, set_auto_temp, 2, 3);
1593static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1594 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Guenter Roeck36c4d982015-03-26 18:18:19 -07001596static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1597 show_pwm_enable, set_pwm_enable, 3);
1598static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001599static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
Guenter Roeck5c391262013-03-30 15:02:12 -07001600static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001601 show_pwm_temp_map, set_pwm_temp_map, 3);
1602
1603static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1604 show_pwm_enable, set_pwm_enable, 4);
1605static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001606static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
Guenter Roeck5c391262013-03-30 15:02:12 -07001607static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001608 show_pwm_temp_map, set_pwm_temp_map, 4);
1609
1610static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1611 show_pwm_enable, set_pwm_enable, 5);
1612static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001613static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
Guenter Roeck5c391262013-03-30 15:02:12 -07001614static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001615 show_pwm_temp_map, set_pwm_temp_map, 5);
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001618static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001619 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001622
Jean Delvare68188ba2005-05-16 18:52:38 +02001623 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
Jean Delvare1d66c642005-04-18 21:16:59 -07001625static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Jean Delvare0124dd72007-11-25 16:16:41 +01001627static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001628 char *buf)
Jean Delvare0124dd72007-11-25 16:16:41 +01001629{
Jean Delvare0124dd72007-11-25 16:16:41 +01001630 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001631 int bitnr = to_sensor_dev_attr(attr)->index;
1632
Jean Delvare0124dd72007-11-25 16:16:41 +01001633 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1634}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001635
Guenter Roeckc9620242015-04-04 09:05:57 -07001636static ssize_t clear_intrusion(struct device *dev,
1637 struct device_attribute *attr, const char *buf,
1638 size_t count)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001639{
1640 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001641 int config;
Guenter Roeckc9620242015-04-04 09:05:57 -07001642 long val;
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001643
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001644 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001645 return -EINVAL;
1646
1647 mutex_lock(&data->update_lock);
1648 config = it87_read_value(data, IT87_REG_CONFIG);
1649 if (config < 0) {
1650 count = config;
1651 } else {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001652 config |= BIT(5);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001653 it87_write_value(data, IT87_REG_CONFIG, config);
1654 /* Invalidate cache to force re-read */
1655 data->valid = 0;
1656 }
1657 mutex_unlock(&data->update_lock);
1658
1659 return count;
1660}
1661
Jean Delvare0124dd72007-11-25 16:16:41 +01001662static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1663static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1664static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1665static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1666static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1667static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1668static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1669static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1670static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1671static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1672static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1673static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1674static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001675static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001676static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1677static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1678static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001679static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1680 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001681
Jean Delvared9b327c2010-03-05 22:17:17 +01001682static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001683 char *buf)
Jean Delvared9b327c2010-03-05 22:17:17 +01001684{
Jean Delvared9b327c2010-03-05 22:17:17 +01001685 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001686 int bitnr = to_sensor_dev_attr(attr)->index;
1687
Jean Delvared9b327c2010-03-05 22:17:17 +01001688 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1689}
Guenter Roeckc9620242015-04-04 09:05:57 -07001690
Jean Delvared9b327c2010-03-05 22:17:17 +01001691static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001692 const char *buf, size_t count)
Jean Delvared9b327c2010-03-05 22:17:17 +01001693{
1694 int bitnr = to_sensor_dev_attr(attr)->index;
1695 struct it87_data *data = dev_get_drvdata(dev);
1696 long val;
1697
Guenter Roeckc9620242015-04-04 09:05:57 -07001698 if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
Jean Delvared9b327c2010-03-05 22:17:17 +01001699 return -EINVAL;
1700
1701 mutex_lock(&data->update_lock);
1702 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1703 if (val)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001704 data->beeps |= BIT(bitnr);
Jean Delvared9b327c2010-03-05 22:17:17 +01001705 else
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001706 data->beeps &= ~BIT(bitnr);
Jean Delvared9b327c2010-03-05 22:17:17 +01001707 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1708 mutex_unlock(&data->update_lock);
1709 return count;
1710}
1711
1712static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1713 show_beep, set_beep, 1);
1714static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1715static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1716static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1717static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1718static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1719static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1720static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1721/* fanX_beep writability is set later */
1722static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1723static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1724static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1725static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1726static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001727static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001728static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1729 show_beep, set_beep, 2);
1730static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1731static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1732
Jean Delvare5f2dc792010-03-05 22:17:18 +01001733static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001734 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Jean Delvare90d66192007-10-08 18:24:35 +02001736 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001737
Jean Delvarea7be58a2005-12-18 16:40:14 +01001738 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
Guenter Roeckc9620242015-04-04 09:05:57 -07001740
Jean Delvare5f2dc792010-03-05 22:17:18 +01001741static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001742 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001744 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001745 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001747 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001748 return -EINVAL;
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 data->vrm = val;
1751
1752 return count;
1753}
1754static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Jean Delvare5f2dc792010-03-05 22:17:18 +01001756static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001757 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001760
1761 return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001764
Jean Delvare738e5e02010-08-14 21:08:50 +02001765static ssize_t show_label(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001766 char *buf)
Jean Delvare738e5e02010-08-14 21:08:50 +02001767{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001768 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001769 "+5V",
1770 "5VSB",
1771 "Vbat",
1772 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001773 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001774 "+3.3V",
1775 "3VSB",
1776 "Vbat",
1777 };
1778 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001779 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001780 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001781
Justin Maggardead80802015-08-05 12:53:08 -07001782 if (has_12mv_adc(data) || has_10_9mv_adc(data))
1783 label = labels_it8721[nr];
1784 else
1785 label = labels[nr];
1786
1787 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001788}
1789static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1790static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1791static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001792/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001793static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001794
Guenter Roeck52929712013-03-30 14:00:08 -07001795static umode_t it87_in_is_visible(struct kobject *kobj,
1796 struct attribute *attr, int index)
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001797{
Guenter Roeck52929712013-03-30 14:00:08 -07001798 struct device *dev = container_of(kobj, struct device, kobj);
1799 struct it87_data *data = dev_get_drvdata(dev);
1800 int i = index / 5; /* voltage index */
1801 int a = index % 5; /* attribute index */
1802
Guenter Roeckf838aa22015-04-01 20:09:36 -07001803 if (index >= 40) { /* in8 and higher only have input attributes */
Guenter Roeck52929712013-03-30 14:00:08 -07001804 i = index - 40 + 8;
1805 a = 0;
1806 }
1807
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001808 if (!(data->has_in & BIT(i)))
Guenter Roeck52929712013-03-30 14:00:08 -07001809 return 0;
1810
1811 if (a == 4 && !data->has_beep)
1812 return 0;
1813
1814 return attr->mode;
1815}
1816
1817static struct attribute *it87_attributes_in[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001818 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001819 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001820 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001821 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001822 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
1823
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001824 &sensor_dev_attr_in1_input.dev_attr.attr,
1825 &sensor_dev_attr_in1_min.dev_attr.attr,
1826 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001827 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001828 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
1829
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001830 &sensor_dev_attr_in2_input.dev_attr.attr,
1831 &sensor_dev_attr_in2_min.dev_attr.attr,
1832 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001833 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001834 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
1835
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001836 &sensor_dev_attr_in3_input.dev_attr.attr,
1837 &sensor_dev_attr_in3_min.dev_attr.attr,
1838 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001839 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001840 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
1841
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001842 &sensor_dev_attr_in4_input.dev_attr.attr,
1843 &sensor_dev_attr_in4_min.dev_attr.attr,
1844 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001845 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001846 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
1847
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001848 &sensor_dev_attr_in5_input.dev_attr.attr,
1849 &sensor_dev_attr_in5_min.dev_attr.attr,
1850 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001851 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001852 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
1853
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001854 &sensor_dev_attr_in6_input.dev_attr.attr,
1855 &sensor_dev_attr_in6_min.dev_attr.attr,
1856 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001857 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001858 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
1859
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001860 &sensor_dev_attr_in7_input.dev_attr.attr,
1861 &sensor_dev_attr_in7_min.dev_attr.attr,
1862 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001863 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001864 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
Jean Delvare87808be2006-09-24 21:17:13 +02001865
Guenter Roeck52929712013-03-30 14:00:08 -07001866 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
Guenter Roeck52929712013-03-30 14:00:08 -07001867 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
Guenter Roeckf838aa22015-04-01 20:09:36 -07001868 &sensor_dev_attr_in10_input.dev_attr.attr, /* 41 */
1869 &sensor_dev_attr_in11_input.dev_attr.attr, /* 41 */
1870 &sensor_dev_attr_in12_input.dev_attr.attr, /* 41 */
Guenter Roeck52929712013-03-30 14:00:08 -07001871};
1872
1873static const struct attribute_group it87_group_in = {
1874 .attrs = it87_attributes_in,
1875 .is_visible = it87_in_is_visible,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001876};
1877
Guenter Roeck87533772013-03-30 14:23:20 -07001878static umode_t it87_temp_is_visible(struct kobject *kobj,
1879 struct attribute *attr, int index)
Guenter Roeck4573acb2012-03-26 16:17:41 -07001880{
Guenter Roeck87533772013-03-30 14:23:20 -07001881 struct device *dev = container_of(kobj, struct device, kobj);
1882 struct it87_data *data = dev_get_drvdata(dev);
1883 int i = index / 7; /* temperature index */
1884 int a = index % 7; /* attribute index */
1885
Guenter Roeckcc18da72015-04-01 10:03:01 -07001886 if (index >= 21) {
1887 i = index - 21 + 3;
1888 a = 0;
1889 }
1890
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001891 if (!(data->has_temp & BIT(i)))
Guenter Roeck87533772013-03-30 14:23:20 -07001892 return 0;
1893
1894 if (a == 5 && !has_temp_offset(data))
1895 return 0;
1896
1897 if (a == 6 && !data->has_beep)
1898 return 0;
1899
1900 return attr->mode;
1901}
1902
1903static struct attribute *it87_attributes_temp[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001904 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001905 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001906 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001907 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001908 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001909 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
1910 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
1911
Guenter Roeckcc18da72015-04-01 10:03:01 -07001912 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001913 &sensor_dev_attr_temp2_max.dev_attr.attr,
1914 &sensor_dev_attr_temp2_min.dev_attr.attr,
1915 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001916 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001917 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1918 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1919
Guenter Roeckcc18da72015-04-01 10:03:01 -07001920 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001921 &sensor_dev_attr_temp3_max.dev_attr.attr,
1922 &sensor_dev_attr_temp3_min.dev_attr.attr,
1923 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001924 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001925 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1926 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001927
Guenter Roeckcc18da72015-04-01 10:03:01 -07001928 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
1929 &sensor_dev_attr_temp5_input.dev_attr.attr,
1930 &sensor_dev_attr_temp6_input.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07001931 NULL
Guenter Roeck4573acb2012-03-26 16:17:41 -07001932};
1933
Guenter Roeck87533772013-03-30 14:23:20 -07001934static const struct attribute_group it87_group_temp = {
1935 .attrs = it87_attributes_temp,
1936 .is_visible = it87_temp_is_visible,
Guenter Roeck161d8982012-12-19 22:17:01 +01001937};
1938
Guenter Roeckd3766842013-03-30 15:47:21 -07001939static umode_t it87_is_visible(struct kobject *kobj,
1940 struct attribute *attr, int index)
1941{
1942 struct device *dev = container_of(kobj, struct device, kobj);
1943 struct it87_data *data = dev_get_drvdata(dev);
1944
Guenter Roeck8638d0a2015-03-30 11:13:49 -07001945 if ((index == 2 || index == 3) && !data->has_vid)
Guenter Roeckd3766842013-03-30 15:47:21 -07001946 return 0;
1947
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001948 if (index > 3 && !(data->in_internal & BIT(index - 4)))
Guenter Roeckd3766842013-03-30 15:47:21 -07001949 return 0;
1950
1951 return attr->mode;
1952}
1953
Guenter Roeck4573acb2012-03-26 16:17:41 -07001954static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001955 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001956 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
Guenter Roeck8638d0a2015-03-30 11:13:49 -07001957 &dev_attr_vrm.attr, /* 2 */
1958 &dev_attr_cpu0_vid.attr, /* 3 */
1959 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
Guenter Roeckd3766842013-03-30 15:47:21 -07001960 &sensor_dev_attr_in7_label.dev_attr.attr,
1961 &sensor_dev_attr_in8_label.dev_attr.attr,
1962 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001963 NULL
1964};
1965
1966static const struct attribute_group it87_group = {
1967 .attrs = it87_attributes,
Guenter Roeckd3766842013-03-30 15:47:21 -07001968 .is_visible = it87_is_visible,
Jean Delvare87808be2006-09-24 21:17:13 +02001969};
1970
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001971static umode_t it87_fan_is_visible(struct kobject *kobj,
1972 struct attribute *attr, int index)
1973{
1974 struct device *dev = container_of(kobj, struct device, kobj);
1975 struct it87_data *data = dev_get_drvdata(dev);
1976 int i = index / 5; /* fan index */
1977 int a = index % 5; /* attribute index */
1978
1979 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
1980 i = (index - 15) / 4 + 3;
1981 a = (index - 15) % 4;
1982 }
1983
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001984 if (!(data->has_fan & BIT(i)))
Guenter Roeck9a70ee82013-03-30 14:51:20 -07001985 return 0;
1986
1987 if (a == 3) { /* beep */
1988 if (!data->has_beep)
1989 return 0;
1990 /* first fan beep attribute is writable */
1991 if (i == __ffs(data->has_fan))
1992 return attr->mode | S_IWUSR;
1993 }
1994
1995 if (a == 4 && has_16bit_fans(data)) /* divisor */
1996 return 0;
1997
1998 return attr->mode;
1999}
2000
2001static struct attribute *it87_attributes_fan[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02002002 &sensor_dev_attr_fan1_input.dev_attr.attr,
2003 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01002004 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002005 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
2006 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
2007
Jean Delvare87808be2006-09-24 21:17:13 +02002008 &sensor_dev_attr_fan2_input.dev_attr.attr,
2009 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01002010 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002011 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2012 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
2013
Jean Delvare87808be2006-09-24 21:17:13 +02002014 &sensor_dev_attr_fan3_input.dev_attr.attr,
2015 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002016 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002017 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2018 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
2019
2020 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01002021 &sensor_dev_attr_fan4_min.dev_attr.attr,
2022 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002023 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2024
2025 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01002026 &sensor_dev_attr_fan5_min.dev_attr.attr,
2027 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002028 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2029
2030 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002031 &sensor_dev_attr_fan6_min.dev_attr.attr,
2032 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002033 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002034 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002035};
2036
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002037static const struct attribute_group it87_group_fan = {
2038 .attrs = it87_attributes_fan,
2039 .is_visible = it87_fan_is_visible,
Guenter Roecke1169ba2012-12-19 22:17:01 +01002040};
Jean Delvare723a0aa2010-03-05 22:17:16 +01002041
Guenter Roeck5c391262013-03-30 15:02:12 -07002042static umode_t it87_pwm_is_visible(struct kobject *kobj,
2043 struct attribute *attr, int index)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002044{
2045 struct device *dev = container_of(kobj, struct device, kobj);
2046 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck5c391262013-03-30 15:02:12 -07002047 int i = index / 4; /* pwm index */
2048 int a = index % 4; /* attribute index */
Guenter Roeck60878bc2015-03-26 19:57:42 -07002049
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002050 if (!(data->has_pwm & BIT(i)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002051 return 0;
2052
2053 /* pwmX_auto_channels_temp is only writable for old auto pwm */
2054 if (a == 3 && has_old_autopwm(data))
2055 return attr->mode | S_IWUSR;
2056
2057 /* pwm2_freq is writable if there are two pwm frequency selects */
2058 if (has_pwm_freq2(data) && i == 1 && a == 2)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002059 return attr->mode | S_IWUSR;
2060
2061 return attr->mode;
2062}
2063
Guenter Roeck5c391262013-03-30 15:02:12 -07002064static struct attribute *it87_attributes_pwm[] = {
2065 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2066 &sensor_dev_attr_pwm1.dev_attr.attr,
2067 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2068 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2069
2070 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2071 &sensor_dev_attr_pwm2.dev_attr.attr,
2072 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2073 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2074
2075 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2076 &sensor_dev_attr_pwm3.dev_attr.attr,
2077 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2078 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2079
2080 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2081 &sensor_dev_attr_pwm4.dev_attr.attr,
2082 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2083 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2084
2085 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2086 &sensor_dev_attr_pwm5.dev_attr.attr,
2087 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2088 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2089
2090 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2091 &sensor_dev_attr_pwm6.dev_attr.attr,
2092 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2093 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2094
2095 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002096};
2097
Guenter Roeck5c391262013-03-30 15:02:12 -07002098static const struct attribute_group it87_group_pwm = {
2099 .attrs = it87_attributes_pwm,
2100 .is_visible = it87_pwm_is_visible,
2101};
2102
2103static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2104 struct attribute *attr, int index)
2105{
2106 struct device *dev = container_of(kobj, struct device, kobj);
2107 struct it87_data *data = dev_get_drvdata(dev);
2108 int i = index / 9; /* pwm index */
2109
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002110 if (!(data->has_pwm & BIT(i)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002111 return 0;
2112
2113 return attr->mode;
2114}
2115
2116static struct attribute *it87_attributes_auto_pwm[] = {
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002117 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2118 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2119 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2120 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2121 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2122 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2123 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2124 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2125 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002126
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002127 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
2128 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2129 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2130 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2131 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2132 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2133 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2134 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2135 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002136
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002137 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
2138 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2139 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2140 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2141 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2142 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2143 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2144 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2145 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002146
Guenter Roeck5c391262013-03-30 15:02:12 -07002147 NULL,
2148};
2149
2150static const struct attribute_group it87_group_auto_pwm = {
2151 .attrs = it87_attributes_auto_pwm,
2152 .is_visible = it87_auto_pwm_is_visible,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002153};
2154
Jean Delvare2d8672c2005-07-19 23:56:35 +02002155/* SuperIO detection - will change isa_address if a chip is found */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002156static int __init it87_find(int sioaddr, unsigned short *address,
2157 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002159 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002160 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002161 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002162 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002164 err = superio_enter(sioaddr);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002165 if (err)
2166 return err;
2167
2168 err = -ENODEV;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002169 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002170
2171 switch (chip_type) {
2172 case IT8705F_DEVID:
2173 sio_data->type = it87;
2174 break;
2175 case IT8712F_DEVID:
2176 sio_data->type = it8712;
2177 break;
2178 case IT8716F_DEVID:
2179 case IT8726F_DEVID:
2180 sio_data->type = it8716;
2181 break;
2182 case IT8718F_DEVID:
2183 sio_data->type = it8718;
2184 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002185 case IT8720F_DEVID:
2186 sio_data->type = it8720;
2187 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002188 case IT8721F_DEVID:
2189 sio_data->type = it8721;
2190 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01002191 case IT8728F_DEVID:
2192 sio_data->type = it8728;
2193 break;
Justin Maggardead80802015-08-05 12:53:08 -07002194 case IT8732F_DEVID:
2195 sio_data->type = it8732;
2196 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06002197 case IT8771E_DEVID:
2198 sio_data->type = it8771;
2199 break;
2200 case IT8772E_DEVID:
2201 sio_data->type = it8772;
2202 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002203 case IT8781F_DEVID:
2204 sio_data->type = it8781;
2205 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08002206 case IT8782F_DEVID:
2207 sio_data->type = it8782;
2208 break;
2209 case IT8783E_DEVID:
2210 sio_data->type = it8783;
2211 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002212 case IT8786E_DEVID:
2213 sio_data->type = it8786;
2214 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07002215 case IT8790E_DEVID:
2216 sio_data->type = it8790;
2217 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02002218 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02002219 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002220 sio_data->type = it8603;
2221 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002222 case IT8620E_DEVID:
2223 sio_data->type = it8620;
2224 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002225 case 0xffff: /* No device at all */
2226 goto exit;
2227 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01002228 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002229 goto exit;
2230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002232 superio_select(sioaddr, PME);
2233 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002234 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 goto exit;
2236 }
2237
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002238 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002240 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 goto exit;
2242 }
2243
2244 err = 0;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002245 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07002246 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2247 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002248 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002250 config = &it87_devices[sio_data->type];
2251
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002252 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002253 if (has_in7_internal(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002254 sio_data->internal |= BIT(1);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002255
Jean Delvare738e5e02010-08-14 21:08:50 +02002256 /* in8 (Vbat) is always internal */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002257 sio_data->internal |= BIT(2);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002258
Guenter Roeck73055402015-03-26 09:16:32 -07002259 /* in9 (AVCC3), always internal if supported */
2260 if (has_avcc3(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002261 sio_data->internal |= BIT(3); /* in9 is AVCC */
Guenter Roeck73055402015-03-26 09:16:32 -07002262 else
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002263 sio_data->skip_in |= BIT(9);
Jean Delvare738e5e02010-08-14 21:08:50 +02002264
Guenter Roeck36c4d982015-03-26 18:18:19 -07002265 if (!has_six_pwm(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002266 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002267
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002268 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01002269 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01002270
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002271 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2272 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01002273 /* The IT8705F has a different LD number for GPIO */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002274 superio_select(sioaddr, 5);
2275 sio_data->beep_pin = superio_inb(sioaddr,
2276 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002277 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002278 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002279
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002280 superio_select(sioaddr, GPIO);
Guenter Roeck0531d982012-03-02 11:46:44 -08002281
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002282 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2283 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2284 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2285 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2286 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002287
Guenter Roeck0531d982012-03-02 11:46:44 -08002288 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002289 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
2290 sio_data->skip_fan |= BIT(2);
Guenter Roeckc9620242015-04-04 09:05:57 -07002291 if ((reg25 & BIT(4)) ||
2292 (!(reg2a & BIT(1)) && (regef & BIT(0))))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002293 sio_data->skip_pwm |= BIT(2);
Guenter Roeck0531d982012-03-02 11:46:44 -08002294
2295 /* Check if fan2 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002296 if (reg27 & BIT(7))
2297 sio_data->skip_fan |= BIT(1);
2298 if (reg27 & BIT(3))
2299 sio_data->skip_pwm |= BIT(1);
Guenter Roeck0531d982012-03-02 11:46:44 -08002300
2301 /* VIN5 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002302 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
2303 sio_data->skip_in |= BIT(5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002304
2305 /* VIN6 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002306 if (reg27 & BIT(1))
2307 sio_data->skip_in |= BIT(6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002308
2309 /*
2310 * VIN7
2311 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2312 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002313 if (reg27 & BIT(2)) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002314 /*
2315 * The data sheet is a bit unclear regarding the
2316 * internal voltage divider for VCCH5V. It says
2317 * "This bit enables and switches VIN7 (pin 91) to the
2318 * internal voltage divider for VCCH5V".
2319 * This is different to other chips, where the internal
2320 * voltage divider would connect VIN7 to an internal
2321 * voltage source. Maybe that is the case here as well.
2322 *
2323 * Since we don't know for sure, re-route it if that is
2324 * not the case, and ask the user to report if the
2325 * resulting voltage is sane.
2326 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002327 if (!(reg2c & BIT(1))) {
2328 reg2c |= BIT(1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002329 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2330 reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002331 pr_notice("Routing internal VCCH5V to in7.\n");
2332 }
2333 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2334 pr_notice("Please report if it displays a reasonable voltage.\n");
2335 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002336
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002337 if (reg2c & BIT(0))
2338 sio_data->internal |= BIT(0);
2339 if (reg2c & BIT(1))
2340 sio_data->internal |= BIT(1);
Guenter Roeck0531d982012-03-02 11:46:44 -08002341
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002342 sio_data->beep_pin = superio_inb(sioaddr,
2343 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002344 } else if (sio_data->type == it8603) {
2345 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002346
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002347 superio_select(sioaddr, GPIO);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002348
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002349 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002350
2351 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002352 if (reg27 & BIT(6))
2353 sio_data->skip_pwm |= BIT(2);
2354 if (reg27 & BIT(7))
2355 sio_data->skip_fan |= BIT(2);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002356
2357 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002358 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002359 if (reg29 & BIT(1))
2360 sio_data->skip_pwm |= BIT(1);
2361 if (reg29 & BIT(2))
2362 sio_data->skip_fan |= BIT(1);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002363
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002364 sio_data->skip_in |= BIT(5); /* No VIN5 */
2365 sio_data->skip_in |= BIT(6); /* No VIN6 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002366
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002367 sio_data->beep_pin = superio_inb(sioaddr,
2368 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002369 } else if (sio_data->type == it8620) {
2370 int reg;
2371
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002372 superio_select(sioaddr, GPIO);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002373
Guenter Roeck36c4d982015-03-26 18:18:19 -07002374 /* Check for pwm5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002375 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002376 if (reg & BIT(6))
2377 sio_data->skip_pwm |= BIT(4);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002378
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002379 /* Check for fan4, fan5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002380 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002381 if (!(reg & BIT(5)))
2382 sio_data->skip_fan |= BIT(3);
2383 if (!(reg & BIT(4)))
2384 sio_data->skip_fan |= BIT(4);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002385
2386 /* Check for pwm3, fan3 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002387 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002388 if (reg & BIT(6))
2389 sio_data->skip_pwm |= BIT(2);
2390 if (reg & BIT(7))
2391 sio_data->skip_fan |= BIT(2);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002392
Guenter Roeck36c4d982015-03-26 18:18:19 -07002393 /* Check for pwm4 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002394 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002395 if (!(reg & BIT(2)))
2396 sio_data->skip_pwm |= BIT(3);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002397
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002398 /* Check for pwm2, fan2 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002399 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002400 if (reg & BIT(1))
2401 sio_data->skip_pwm |= BIT(1);
2402 if (reg & BIT(2))
2403 sio_data->skip_fan |= BIT(1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002404 /* Check for pwm6, fan6 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002405 if (!(reg & BIT(7))) {
2406 sio_data->skip_pwm |= BIT(5);
2407 sio_data->skip_fan |= BIT(5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002408 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002409
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002410 sio_data->beep_pin = superio_inb(sioaddr,
2411 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002412 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002413 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002414 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002415
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002416 superio_select(sioaddr, GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002417
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002418 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002419 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002420 /* We need at least 4 VID pins */
2421 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002422 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002423 sio_data->skip_vid = 1;
2424 }
Jean Delvare895ff262009-12-09 20:35:47 +01002425 }
2426
Jean Delvare591ec652009-12-09 20:35:48 +01002427 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002428 if (reg & BIT(6))
2429 sio_data->skip_pwm |= BIT(2);
2430 if (reg & BIT(7))
2431 sio_data->skip_fan |= BIT(2);
Jean Delvare591ec652009-12-09 20:35:48 +01002432
2433 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002434 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002435 if (reg & BIT(1))
2436 sio_data->skip_pwm |= BIT(1);
2437 if (reg & BIT(2))
2438 sio_data->skip_fan |= BIT(1);
Jean Delvare591ec652009-12-09 20:35:48 +01002439
Guenter Roeckc9620242015-04-04 09:05:57 -07002440 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
2441 !(sio_data->skip_vid))
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002442 sio_data->vid_value = superio_inb(sioaddr,
2443 IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002444
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002445 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002446
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002447 uart6 = sio_data->type == it8782 && (reg & BIT(2));
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002448
Jean Delvare436cad22010-07-09 16:22:48 +02002449 /*
2450 * The IT8720F has no VIN7 pin, so VCCH should always be
2451 * routed internally to VIN7 with an internal divider.
2452 * Curiously, there still is a configuration bit to control
2453 * this, which means it can be set incorrectly. And even
2454 * more curiously, many boards out there are improperly
2455 * configured, even though the IT8720F datasheet claims
2456 * that the internal routing of VCCH to VIN7 is the default
2457 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002458 *
2459 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002460 * If UART6 is enabled, re-route VIN7 to the internal divider
2461 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002462 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002463 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
2464 reg |= BIT(1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002465 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002466 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002467 }
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002468 if (reg & BIT(0))
2469 sio_data->internal |= BIT(0);
2470 if (reg & BIT(1))
2471 sio_data->internal |= BIT(1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002472
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002473 /*
2474 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2475 * While VIN7 can be routed to the internal voltage divider,
2476 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002477 *
2478 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2479 * is the temperature source. Since we can not read the
2480 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002481 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002482 if (uart6) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002483 sio_data->skip_in |= BIT(5) | BIT(6);
2484 sio_data->skip_temp |= BIT(2);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002485 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002486
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002487 sio_data->beep_pin = superio_inb(sioaddr,
2488 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002489 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002490 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002491 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002492
Jean Delvare98dd22c2008-10-09 15:33:58 +02002493 /* Disable specific features based on DMI strings */
2494 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2495 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2496 if (board_vendor && board_name) {
Guenter Roeckc9620242015-04-04 09:05:57 -07002497 if (strcmp(board_vendor, "nVIDIA") == 0 &&
2498 strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002499 /*
2500 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2501 * connected to a fan, but to something else. One user
2502 * has reported instant system power-off when changing
2503 * the PWM2 duty cycle, so we disable it.
2504 * I use the board name string as the trigger in case
2505 * the same board is ever used in other systems.
2506 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002507 pr_info("Disabling pwm2 due to hardware constraints\n");
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002508 sio_data->skip_pwm = BIT(1);
Jean Delvare98dd22c2008-10-09 15:33:58 +02002509 }
2510 }
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512exit:
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002513 superio_exit(sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return err;
2515}
2516
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002517/* Called when we have found a new IT87. */
2518static void it87_init_device(struct platform_device *pdev)
2519{
2520 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2521 struct it87_data *data = platform_get_drvdata(pdev);
2522 int tmp, i;
2523 u8 mask;
2524
2525 /*
2526 * For each PWM channel:
2527 * - If it is in automatic mode, setting to manual mode should set
2528 * the fan to full speed by default.
2529 * - If it is in manual mode, we need a mapping to temperature
2530 * channels to use when later setting to automatic mode later.
2531 * Use a 1:1 mapping by default (we are clueless.)
2532 * In both cases, the value can (and should) be changed by the user
2533 * prior to switching to a different mode.
2534 * Note that this is no longer needed for the IT8721F and later, as
2535 * these have separate registers for the temperature mapping and the
2536 * manual duty cycle.
2537 */
Guenter Roeck23100482015-04-02 08:23:45 -07002538 for (i = 0; i < NUM_AUTO_PWM; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002539 data->pwm_temp_map[i] = i;
2540 data->pwm_duty[i] = 0x7f; /* Full speed */
2541 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2542 }
2543
2544 /*
2545 * Some chips seem to have default value 0xff for all limit
2546 * registers. For low voltage limits it makes no sense and triggers
2547 * alarms, so change to 0 instead. For high temperature limits, it
2548 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2549 * but is still confusing, so change to 127 degrees C.
2550 */
Guenter Roeck23100482015-04-02 08:23:45 -07002551 for (i = 0; i < NUM_VIN_LIMIT; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002552 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2553 if (tmp == 0xff)
2554 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2555 }
Guenter Roeck23100482015-04-02 08:23:45 -07002556 for (i = 0; i < NUM_TEMP_LIMIT; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002557 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2558 if (tmp == 0xff)
2559 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2560 }
2561
2562 /*
2563 * Temperature channels are not forcibly enabled, as they can be
2564 * set to two different sensor types and we can't guess which one
2565 * is correct for a given system. These channels can be enabled at
2566 * run-time through the temp{1-3}_type sysfs accessors if needed.
2567 */
2568
2569 /* Check if voltage monitors are reset manually or by some reason */
2570 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2571 if ((tmp & 0xff) == 0) {
2572 /* Enable all voltage monitors */
2573 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2574 }
2575
2576 /* Check if tachometers are reset manually or by some reason */
2577 mask = 0x70 & ~(sio_data->skip_fan << 4);
2578 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2579 if ((data->fan_main_ctrl & mask) == 0) {
2580 /* Enable all fan tachometers */
2581 data->fan_main_ctrl |= mask;
2582 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2583 data->fan_main_ctrl);
2584 }
2585 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2586
2587 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2588
2589 /* Set tachometers to 16-bit mode if needed */
2590 if (has_fan16_config(data)) {
2591 if (~tmp & 0x07 & data->has_fan) {
2592 dev_dbg(&pdev->dev,
2593 "Setting fan1-3 to 16-bit mode\n");
2594 it87_write_value(data, IT87_REG_FAN_16BIT,
2595 tmp | 0x07);
2596 }
2597 }
2598
2599 /* Check for additional fans */
2600 if (has_five_fans(data)) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002601 if (tmp & BIT(4))
2602 data->has_fan |= BIT(3); /* fan4 enabled */
2603 if (tmp & BIT(5))
2604 data->has_fan |= BIT(4); /* fan5 enabled */
2605 if (has_six_fans(data) && (tmp & BIT(2)))
2606 data->has_fan |= BIT(5); /* fan6 enabled */
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002607 }
2608
2609 /* Fan input pins may be used for alternative functions */
2610 data->has_fan &= ~sio_data->skip_fan;
2611
2612 /* Check if pwm5, pwm6 are enabled */
2613 if (has_six_pwm(data)) {
2614 /* The following code may be IT8620E specific */
2615 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2616 if ((tmp & 0xc0) == 0xc0)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002617 sio_data->skip_pwm |= BIT(4);
2618 if (!(tmp & BIT(3)))
2619 sio_data->skip_pwm |= BIT(5);
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002620 }
2621
2622 /* Start monitoring */
2623 it87_write_value(data, IT87_REG_CONFIG,
2624 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2625 | (update_vbat ? 0x41 : 0x01));
2626}
2627
2628/* Return 1 if and only if the PWM interface is safe to use */
2629static int it87_check_pwm(struct device *dev)
2630{
2631 struct it87_data *data = dev_get_drvdata(dev);
2632 /*
2633 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2634 * and polarity set to active low is sign that this is the case so we
2635 * disable pwm control to protect the user.
2636 */
2637 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2638
2639 if ((tmp & 0x87) == 0) {
2640 if (fix_pwm_polarity) {
2641 /*
2642 * The user asks us to attempt a chip reconfiguration.
2643 * This means switching to active high polarity and
2644 * inverting all fan speed values.
2645 */
2646 int i;
2647 u8 pwm[3];
2648
Guenter Roeck23100482015-04-02 08:23:45 -07002649 for (i = 0; i < ARRAY_SIZE(pwm); i++)
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002650 pwm[i] = it87_read_value(data,
2651 IT87_REG_PWM[i]);
2652
2653 /*
2654 * If any fan is in automatic pwm mode, the polarity
2655 * might be correct, as suspicious as it seems, so we
2656 * better don't change anything (but still disable the
2657 * PWM interface).
2658 */
2659 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2660 dev_info(dev,
2661 "Reconfiguring PWM to active high polarity\n");
2662 it87_write_value(data, IT87_REG_FAN_CTL,
2663 tmp | 0x87);
2664 for (i = 0; i < 3; i++)
2665 it87_write_value(data,
2666 IT87_REG_PWM[i],
2667 0x7f & ~pwm[i]);
2668 return 1;
2669 }
2670
2671 dev_info(dev,
2672 "PWM configuration is too broken to be fixed\n");
2673 }
2674
2675 dev_info(dev,
2676 "Detected broken BIOS defaults, disabling PWM interface\n");
2677 return 0;
2678 } else if (fix_pwm_polarity) {
2679 dev_info(dev,
2680 "PWM configuration looks sane, won't touch\n");
2681 }
2682
2683 return 1;
2684}
2685
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002686static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002689 struct resource *res;
2690 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002691 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 int enable_pwm_interface;
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002693 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002695 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002696 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2697 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002698 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2699 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002700 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002701 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Guenter Roeck62a1d052012-03-24 21:54:41 -07002704 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2705 if (!data)
2706 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002708 data->addr = res->start;
2709 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002710 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002711 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002712 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002713 /*
2714 * IT8705F Datasheet 0.4.1, 3h == Version G.
2715 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2716 * These are the first revisions with 16-bit tachometer support.
2717 */
2718 switch (data->type) {
2719 case it87:
2720 if (sio_data->revision >= 0x03) {
2721 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002722 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002723 }
2724 break;
2725 case it8712:
2726 if (sio_data->revision >= 0x08) {
2727 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002728 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2729 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002730 }
2731 break;
2732 default:
2733 break;
2734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736 /* Now, we do the remaining detection. */
Guenter Roeckc9620242015-04-04 09:05:57 -07002737 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) ||
2738 it87_read_value(data, IT87_REG_CHIPID) != 0x90)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002739 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002741 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002743 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002746 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002748 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002749 if (has_12mv_adc(data)) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002750 if (sio_data->internal & BIT(0))
2751 data->in_scaled |= BIT(3); /* in3 is AVCC */
2752 if (sio_data->internal & BIT(1))
2753 data->in_scaled |= BIT(7); /* in7 is VSB */
2754 if (sio_data->internal & BIT(2))
2755 data->in_scaled |= BIT(8); /* in8 is Vbat */
2756 if (sio_data->internal & BIT(3))
2757 data->in_scaled |= BIT(9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002758 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2759 sio_data->type == it8783) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002760 if (sio_data->internal & BIT(0))
2761 data->in_scaled |= BIT(3); /* in3 is VCC5V */
2762 if (sio_data->internal & BIT(1))
2763 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002764 }
2765
Guenter Roeck4573acb2012-03-26 16:17:41 -07002766 data->has_temp = 0x07;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002767 if (sio_data->skip_temp & BIT(2)) {
Guenter Roeckc9620242015-04-04 09:05:57 -07002768 if (sio_data->type == it8782 &&
2769 !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002770 data->has_temp &= ~BIT(2);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002771 }
2772
Guenter Roeckd3766842013-03-30 15:47:21 -07002773 data->in_internal = sio_data->internal;
Guenter Roeck52929712013-03-30 14:00:08 -07002774 data->has_in = 0x3ff & ~sio_data->skip_in;
2775
Guenter Roeckcc18da72015-04-01 10:03:01 -07002776 if (has_six_temp(data)) {
2777 u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
2778
Guenter Roeckf838aa22015-04-01 20:09:36 -07002779 /* Check for additional temperature sensors */
Guenter Roeckcc18da72015-04-01 10:03:01 -07002780 if ((reg & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002781 data->has_temp |= BIT(3);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002782 if (((reg >> 2) & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002783 data->has_temp |= BIT(4);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002784 if (((reg >> 4) & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002785 data->has_temp |= BIT(5);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002786
2787 /* Check for additional voltage sensors */
2788 if ((reg & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002789 data->has_in |= BIT(10);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002790 if (((reg >> 2) & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002791 data->has_in |= BIT(11);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002792 if (((reg >> 4) & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002793 data->has_in |= BIT(12);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002794 }
2795
Guenter Roeck52929712013-03-30 14:00:08 -07002796 data->has_beep = !!sio_data->beep_pin;
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002799 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Guenter Roeckd3766842013-03-30 15:47:21 -07002801 if (!sio_data->skip_vid) {
2802 data->has_vid = true;
2803 data->vrm = vid_which_vrm();
2804 /* VID reading from Super-I/O config space if available */
2805 data->vid = sio_data->vid_value;
2806 }
2807
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002808 /* Prepare for sysfs hooks */
2809 data->groups[0] = &it87_group;
2810 data->groups[1] = &it87_group_in;
2811 data->groups[2] = &it87_group_temp;
2812 data->groups[3] = &it87_group_fan;
Jean Delvare17d648b2006-08-28 14:23:46 +02002813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (enable_pwm_interface) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002815 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
Guenter Roeck5c391262013-03-30 15:02:12 -07002816 data->has_pwm &= ~sio_data->skip_pwm;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002817
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002818 data->groups[4] = &it87_group_pwm;
2819 if (has_old_autopwm(data))
2820 data->groups[5] = &it87_group_auto_pwm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
2822
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002823 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
2824 it87_devices[sio_data->type].name,
2825 data, data->groups);
2826 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
2828
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002829static struct platform_driver it87_driver = {
2830 .driver = {
2831 .name = DRVNAME,
2832 },
2833 .probe = it87_probe,
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002834};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Guenter Roecke84bd952015-03-28 08:24:29 -07002836static int __init it87_device_add(int index, unsigned short address,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002837 const struct it87_sio_data *sio_data)
2838{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07002839 struct platform_device *pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002840 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002841 .start = address + IT87_EC_OFFSET,
2842 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002843 .name = DRVNAME,
2844 .flags = IORESOURCE_IO,
2845 };
2846 int err;
2847
Jean Delvareb9acb642009-01-07 16:37:35 +01002848 err = acpi_check_resource_conflict(&res);
2849 if (err)
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002850 return err;
Jean Delvareb9acb642009-01-07 16:37:35 +01002851
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002852 pdev = platform_device_alloc(DRVNAME, address);
Guenter Roeck5cae84a2015-03-28 07:44:59 -07002853 if (!pdev)
2854 return -ENOMEM;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002855
2856 err = platform_device_add_resources(pdev, &res, 1);
2857 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002858 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002859 goto exit_device_put;
2860 }
2861
2862 err = platform_device_add_data(pdev, sio_data,
2863 sizeof(struct it87_sio_data));
2864 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002865 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002866 goto exit_device_put;
2867 }
2868
2869 err = platform_device_add(pdev);
2870 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002871 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002872 goto exit_device_put;
2873 }
2874
Guenter Roecke84bd952015-03-28 08:24:29 -07002875 it87_pdev[index] = pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002876 return 0;
2877
2878exit_device_put:
2879 platform_device_put(pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002880 return err;
2881}
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883static int __init sm_it87_init(void)
2884{
Guenter Roecke84bd952015-03-28 08:24:29 -07002885 int sioaddr[2] = { REG_2E, REG_4E };
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002886 struct it87_sio_data sio_data;
Guenter Roecke84bd952015-03-28 08:24:29 -07002887 unsigned short isa_address;
2888 bool found = false;
2889 int i, err;
Jean Delvarefde09502005-07-19 23:51:07 +02002890
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002891 err = platform_driver_register(&it87_driver);
2892 if (err)
2893 return err;
2894
Guenter Roecke84bd952015-03-28 08:24:29 -07002895 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
2896 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2897 isa_address = 0;
2898 err = it87_find(sioaddr[i], &isa_address, &sio_data);
2899 if (err || isa_address == 0)
2900 continue;
2901
2902 err = it87_device_add(i, isa_address, &sio_data);
2903 if (err)
2904 goto exit_dev_unregister;
2905 found = true;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002906 }
2907
Guenter Roecke84bd952015-03-28 08:24:29 -07002908 if (!found) {
2909 err = -ENODEV;
2910 goto exit_unregister;
2911 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002912 return 0;
Guenter Roecke84bd952015-03-28 08:24:29 -07002913
2914exit_dev_unregister:
2915 /* NULL check handled by platform_device_unregister */
2916 platform_device_unregister(it87_pdev[0]);
2917exit_unregister:
2918 platform_driver_unregister(&it87_driver);
2919 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
2922static void __exit sm_it87_exit(void)
2923{
Guenter Roecke84bd952015-03-28 08:24:29 -07002924 /* NULL check handled by platform_device_unregister */
2925 platform_device_unregister(it87_pdev[1]);
2926 platform_device_unregister(it87_pdev[0]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002927 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
Jean Delvare7c81c602014-01-29 20:40:08 +01002930MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002931MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932module_param(update_vbat, bool, 0);
2933MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2934module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002935MODULE_PARM_DESC(fix_pwm_polarity,
2936 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937MODULE_LICENSE("GPL");
2938
2939module_init(sm_it87_init);
2940module_exit(sm_it87_exit);