blob: e3fbcf49afdb656fd1bb7dea75c5706660441e46 [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
Guenter Roeck2cbb9c32015-04-05 11:53:29 -0700255static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
256
257#define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
258#define IT87_REG_AUTO_PWM(nr, i) (IT87_REG_AUTO_BASE[nr] + 5 + (i))
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100259
Guenter Roeckcc18da72015-04-01 10:03:01 -0700260#define IT87_REG_TEMP456_ENABLE 0x77
261
Guenter Roeck23100482015-04-02 08:23:45 -0700262#define NUM_VIN ARRAY_SIZE(IT87_REG_VIN)
263#define NUM_VIN_LIMIT 8
264#define NUM_TEMP 6
265#define NUM_TEMP_OFFSET ARRAY_SIZE(IT87_REG_TEMP_OFFSET)
266#define NUM_TEMP_LIMIT 3
267#define NUM_FAN ARRAY_SIZE(IT87_REG_FAN)
268#define NUM_FAN_DIV 3
269#define NUM_PWM ARRAY_SIZE(IT87_REG_PWM)
270#define NUM_AUTO_PWM ARRAY_SIZE(IT87_REG_PWM)
271
Guenter Roeck483db432012-12-19 22:17:02 +0100272struct it87_devices {
273 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700274 const char * const suffix;
Guenter Roeckcc18da72015-04-01 10:03:01 -0700275 u32 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100276 u8 peci_mask;
277 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100278};
279
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700280#define FEAT_12MV_ADC BIT(0)
281#define FEAT_NEWER_AUTOPWM BIT(1)
282#define FEAT_OLD_AUTOPWM BIT(2)
283#define FEAT_16BIT_FANS BIT(3)
284#define FEAT_TEMP_OFFSET BIT(4)
285#define FEAT_TEMP_PECI BIT(5)
286#define FEAT_TEMP_OLD_PECI BIT(6)
287#define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
288#define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
289#define FEAT_VID BIT(9) /* Set if chip supports VID */
290#define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
291#define FEAT_SIX_FANS BIT(11) /* Supports six fans */
292#define FEAT_10_9MV_ADC BIT(12)
293#define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
294#define FEAT_SIX_PWM BIT(14) /* Chip supports 6 pwm chn */
295#define FEAT_PWM_FREQ2 BIT(15) /* Separate pwm freq 2 */
296#define FEAT_SIX_TEMP BIT(16) /* Up to 6 temp sensors */
Guenter Roeck483db432012-12-19 22:17:02 +0100297
298static const struct it87_devices it87_devices[] = {
299 [it87] = {
300 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700301 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100302 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
303 },
304 [it8712] = {
305 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700306 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800307 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
308 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100309 },
310 [it8716] = {
311 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700312 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800313 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700314 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
Guenter Roeck483db432012-12-19 22:17:02 +0100315 },
316 [it8718] = {
317 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700318 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800319 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700320 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
321 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100322 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100323 },
324 [it8720] = {
325 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700326 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800327 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700328 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
329 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100330 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100331 },
332 [it8721] = {
333 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700334 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100335 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800336 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck60878bc2015-03-26 19:57:42 -0700337 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
338 | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100339 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100340 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100341 },
342 [it8728] = {
343 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700344 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100345 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700346 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700347 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100348 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100349 },
Justin Maggardead80802015-08-05 12:53:08 -0700350 [it8732] = {
351 .name = "it8732",
352 .suffix = "F",
353 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
354 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
355 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
356 .peci_mask = 0x07,
357 .old_peci_mask = 0x02, /* Actually reports PCH */
358 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600359 [it8771] = {
360 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700361 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600362 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700363 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
364 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800365 /* PECI: guesswork */
366 /* 12mV ADC (OHM) */
367 /* 16 bit fans (OHM) */
368 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600369 .peci_mask = 0x07,
370 },
371 [it8772] = {
372 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700373 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600374 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700375 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
376 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800377 /* PECI (coreboot) */
378 /* 12mV ADC (HWSensors4, OHM) */
379 /* 16 bit fans (HWSensors4, OHM) */
380 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600381 .peci_mask = 0x07,
382 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800383 [it8781] = {
384 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700385 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800386 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700387 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800388 .old_peci_mask = 0x4,
389 },
Guenter Roeck483db432012-12-19 22:17:02 +0100390 [it8782] = {
391 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700392 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100393 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700394 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100395 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100396 },
397 [it8783] = {
398 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700399 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100400 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700401 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100402 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100403 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100404 [it8786] = {
405 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700406 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100407 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700408 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
409 | FEAT_PWM_FREQ2,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100410 .peci_mask = 0x07,
411 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700412 [it8790] = {
413 .name = "it8790",
414 .suffix = "E",
415 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700416 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
417 | FEAT_PWM_FREQ2,
Guenter Roeck4ee07152015-03-25 23:26:28 -0700418 .peci_mask = 0x07,
419 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100420 [it8603] = {
421 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700422 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100423 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck73055402015-03-26 09:16:32 -0700424 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
Guenter Roeck60878bc2015-03-26 19:57:42 -0700425 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100426 .peci_mask = 0x07,
427 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800428 [it8620] = {
429 .name = "it8620",
430 .suffix = "E",
431 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700432 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeckcc18da72015-04-01 10:03:01 -0700433 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
434 | FEAT_SIX_TEMP,
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800435 .peci_mask = 0x07,
436 },
Guenter Roeck483db432012-12-19 22:17:02 +0100437};
438
439#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
440#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
Justin Maggardead80802015-08-05 12:53:08 -0700441#define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
Guenter Roeck483db432012-12-19 22:17:02 +0100442#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
443#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
444#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100445#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700446 ((data)->peci_mask & BIT(nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100447#define has_temp_old_peci(data, nr) \
448 (((data)->features & FEAT_TEMP_OLD_PECI) && \
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700449 ((data)->old_peci_mask & BIT(nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800450#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700451#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
452 FEAT_SIX_FANS))
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800453#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700454#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700455#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Guenter Roeck73055402015-03-26 09:16:32 -0700456#define has_avcc3(data) ((data)->features & FEAT_AVCC3)
Guenter Roeck36c4d982015-03-26 18:18:19 -0700457#define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
Guenter Roeck60878bc2015-03-26 19:57:42 -0700458#define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
Guenter Roeckcc18da72015-04-01 10:03:01 -0700459#define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200461struct it87_sio_data {
462 enum chips type;
463 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200464 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200465 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100466 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200467 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100468 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700469 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100470 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100471 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200472 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700473 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200474};
475
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800476/*
477 * For each registered chip, we need to keep some data in memory.
478 * The structure is dynamically allocated.
479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480struct it87_data {
Guenter Roeck8638d0a2015-03-30 11:13:49 -0700481 const struct attribute_group *groups[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100483 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100484 u8 peci_mask;
485 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200487 unsigned short addr;
488 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100489 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 char valid; /* !=0 if following fields are valid */
491 unsigned long last_updated; /* In jiffies */
492
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200493 u16 in_scaled; /* Internal voltage sensors are scaled */
Guenter Roeckd3766842013-03-30 15:47:21 -0700494 u16 in_internal; /* Bitfield, internal sensors (for labels) */
Guenter Roeck52929712013-03-30 14:00:08 -0700495 u16 has_in; /* Bitfield, voltage sensors enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700496 u8 in[NUM_VIN][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200497 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700498 u16 fan[NUM_FAN][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700499 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700500 s8 temp[NUM_TEMP][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100501 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
502 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Guenter Roeck23100482015-04-02 08:23:45 -0700503 u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
Guenter Roeckd3766842013-03-30 15:47:21 -0700504 bool has_vid; /* True if VID supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100506 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 u32 alarms; /* Register encoding, combined */
Guenter Roeck52929712013-03-30 14:00:08 -0700508 bool has_beep; /* true if beep supported */
Jean Delvared9b327c2010-03-05 22:17:17 +0100509 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100511 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100512
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800513 /*
514 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100515 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
516 * 7, and we want to preserve settings on mode changes, so we have
517 * to track all values separately.
518 * Starting with the IT8721F, the manual PWM duty cycles are stored
519 * in separate registers (8-bit values), so the separate tracking
520 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800521 * simple.
522 */
Guenter Roeck5c391262013-03-30 15:02:12 -0700523 u8 has_pwm; /* Bitfield, pwm control enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700524 u8 pwm_ctrl[NUM_PWM]; /* Register value */
525 u8 pwm_duty[NUM_PWM]; /* Manual PWM value set by user */
526 u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100527
528 /* Automatic fan speed control registers */
Guenter Roeck23100482015-04-02 08:23:45 -0700529 u8 auto_pwm[NUM_AUTO_PWM][4]; /* [nr][3] is hard-coded */
530 s8 auto_temp[NUM_AUTO_PWM][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531};
532
Guenter Roeck0531d982012-03-02 11:46:44 -0800533static int adc_lsb(const struct it87_data *data, int nr)
534{
Justin Maggardead80802015-08-05 12:53:08 -0700535 int lsb;
536
537 if (has_12mv_adc(data))
538 lsb = 120;
539 else if (has_10_9mv_adc(data))
540 lsb = 109;
541 else
542 lsb = 160;
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700543 if (data->in_scaled & BIT(nr))
Guenter Roeck0531d982012-03-02 11:46:44 -0800544 lsb <<= 1;
545 return lsb;
546}
547
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200548static u8 in_to_reg(const struct it87_data *data, int nr, long val)
549{
Justin Maggardead80802015-08-05 12:53:08 -0700550 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800551 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200552}
553
554static int in_from_reg(const struct it87_data *data, int nr, int val)
555{
Justin Maggardead80802015-08-05 12:53:08 -0700556 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200557}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200558
559static inline u8 FAN_TO_REG(long rpm, int div)
560{
561 if (rpm == 0)
562 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800563 rpm = clamp_val(rpm, 1, 1000000);
564 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200565}
566
567static inline u16 FAN16_TO_REG(long rpm)
568{
569 if (rpm == 0)
570 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800571 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200572}
573
574#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
575 1350000 / ((val) * (div)))
576/* The divider is fixed to 2 in 16-bit mode */
577#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
578 1350000 / ((val) * 2))
579
Guenter Roeck2a844c12013-01-09 08:09:34 -0800580#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
581 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200582#define TEMP_FROM_REG(val) ((val) * 1000)
583
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200584static u8 pwm_to_reg(const struct it87_data *data, long val)
585{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100586 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200587 return val;
588 else
589 return val >> 1;
590}
591
592static int pwm_from_reg(const struct it87_data *data, u8 reg)
593{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100594 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200595 return reg;
596 else
597 return (reg & 0x7f) << 1;
598}
599
Jean Delvare0df6454d2010-10-28 20:31:51 +0200600static int DIV_TO_REG(int val)
601{
602 int answer = 0;
Guenter Roeckc9620242015-04-04 09:05:57 -0700603
Jean Delvare0df6454d2010-10-28 20:31:51 +0200604 while (answer < 7 && (val >>= 1))
605 answer++;
606 return answer;
607}
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700608
609#define DIV_FROM_REG(val) BIT(val)
Jean Delvare0df6454d2010-10-28 20:31:51 +0200610
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700611/*
612 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
613 * depending on the chip type, to calculate the actual PWM frequency.
614 *
615 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
616 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
617 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
618 * sometimes just one. It is unknown if this is a datasheet error or real,
619 * so this is ignored for now.
620 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200621static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700622 48000000,
623 24000000,
624 12000000,
625 8000000,
626 6000000,
627 3000000,
628 1500000,
629 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200630};
631
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700632/*
633 * Must be called with data->update_lock held, except during initialization.
634 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
635 * would slow down the IT87 access and should not be necessary.
636 */
637static int it87_read_value(struct it87_data *data, u8 reg)
638{
639 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
640 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
641}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700643/*
644 * Must be called with data->update_lock held, except during initialization.
645 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
646 * would slow down the IT87 access and should not be necessary.
647 */
648static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
649{
650 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
651 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700654static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
655{
656 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
657 if (has_newer_autopwm(data)) {
658 data->pwm_temp_map[nr] = (data->pwm_ctrl[nr] & 0x03) +
659 nr < 3 ? 0 : 3;
660 data->pwm_duty[nr] = it87_read_value(data,
661 IT87_REG_PWM_DUTY[nr]);
662 } else {
663 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
664 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
665 else /* Manual mode */
666 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700669 if (has_old_autopwm(data)) {
670 int i;
671
672 for (i = 0; i < 5 ; i++)
673 data->auto_temp[nr][i] = it87_read_value(data,
674 IT87_REG_AUTO_TEMP(nr, i));
675 for (i = 0; i < 3 ; i++)
676 data->auto_pwm[nr][i] = it87_read_value(data,
677 IT87_REG_AUTO_PWM(nr, i));
Guenter Roeck2cbb9c32015-04-05 11:53:29 -0700678 } else if (has_newer_autopwm(data)) {
679 int i;
680
681 /*
682 * 0: temperature hysteresis (base + 5)
683 * 1: fan off temperature (base + 0)
684 * 2: fan start temperature (base + 1)
685 * 3: fan max temperature (base + 2)
686 */
687 data->auto_temp[nr][0] =
688 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 5));
689
690 for (i = 0; i < 3 ; i++)
691 data->auto_temp[nr][i + 1] =
692 it87_read_value(data,
693 IT87_REG_AUTO_TEMP(nr, i));
694 /*
695 * 0: start pwm value (base + 3)
696 * 1: pwm slope (base + 4, 1/8th pwm)
697 */
698 data->auto_pwm[nr][0] =
699 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 3));
700 data->auto_pwm[nr][1] =
701 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 4));
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700702 }
703}
704
705static struct it87_data *it87_update_device(struct device *dev)
706{
707 struct it87_data *data = dev_get_drvdata(dev);
708 int i;
709
710 mutex_lock(&data->update_lock);
711
Guenter Roeckc9620242015-04-04 09:05:57 -0700712 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
713 !data->valid) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700714 if (update_vbat) {
715 /*
716 * Cleared after each update, so reenable. Value
717 * returned by this read will be previous value
718 */
719 it87_write_value(data, IT87_REG_CONFIG,
720 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
721 }
Guenter Roeck23100482015-04-02 08:23:45 -0700722 for (i = 0; i < NUM_VIN; i++) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700723 if (!(data->has_in & BIT(i)))
Guenter Roeck559313c2015-04-01 10:15:38 -0700724 continue;
725
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700726 data->in[i][0] =
Guenter Roeck559313c2015-04-01 10:15:38 -0700727 it87_read_value(data, IT87_REG_VIN[i]);
728
729 /* VBAT and AVCC don't have limit registers */
Guenter Roeck23100482015-04-02 08:23:45 -0700730 if (i >= NUM_VIN_LIMIT)
Guenter Roeck559313c2015-04-01 10:15:38 -0700731 continue;
732
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700733 data->in[i][1] =
734 it87_read_value(data, IT87_REG_VIN_MIN(i));
735 data->in[i][2] =
736 it87_read_value(data, IT87_REG_VIN_MAX(i));
737 }
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700738
Guenter Roeck23100482015-04-02 08:23:45 -0700739 for (i = 0; i < NUM_FAN; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700740 /* Skip disabled fans */
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700741 if (!(data->has_fan & BIT(i)))
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700742 continue;
743
744 data->fan[i][1] =
745 it87_read_value(data, IT87_REG_FAN_MIN[i]);
746 data->fan[i][0] = it87_read_value(data,
747 IT87_REG_FAN[i]);
748 /* Add high byte if in 16-bit mode */
749 if (has_16bit_fans(data)) {
750 data->fan[i][0] |= it87_read_value(data,
751 IT87_REG_FANX[i]) << 8;
752 data->fan[i][1] |= it87_read_value(data,
753 IT87_REG_FANX_MIN[i]) << 8;
754 }
755 }
Guenter Roeck23100482015-04-02 08:23:45 -0700756 for (i = 0; i < NUM_TEMP; i++) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700757 if (!(data->has_temp & BIT(i)))
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700758 continue;
759 data->temp[i][0] =
760 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeckcc18da72015-04-01 10:03:01 -0700761
Guenter Roeck23100482015-04-02 08:23:45 -0700762 if (has_temp_offset(data) && i < NUM_TEMP_OFFSET)
763 data->temp[i][3] =
764 it87_read_value(data,
765 IT87_REG_TEMP_OFFSET[i]);
766
767 if (i >= NUM_TEMP_LIMIT)
Guenter Roeckcc18da72015-04-01 10:03:01 -0700768 continue;
769
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700770 data->temp[i][1] =
771 it87_read_value(data, IT87_REG_TEMP_LOW(i));
772 data->temp[i][2] =
773 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700774 }
775
776 /* Newer chips don't have clock dividers */
777 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
778 i = it87_read_value(data, IT87_REG_FAN_DIV);
779 data->fan_div[0] = i & 0x07;
780 data->fan_div[1] = (i >> 3) & 0x07;
781 data->fan_div[2] = (i & 0x40) ? 3 : 1;
782 }
783
784 data->alarms =
785 it87_read_value(data, IT87_REG_ALARM1) |
786 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
787 (it87_read_value(data, IT87_REG_ALARM3) << 16);
788 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
789
790 data->fan_main_ctrl = it87_read_value(data,
791 IT87_REG_FAN_MAIN_CTRL);
792 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Guenter Roeck23100482015-04-02 08:23:45 -0700793 for (i = 0; i < NUM_PWM; i++)
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700794 it87_update_pwm_ctrl(data, i);
795
796 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
797 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
798 /*
799 * The IT8705F does not have VID capability.
800 * The IT8718F and later don't use IT87_REG_VID for the
801 * same purpose.
802 */
803 if (data->type == it8712 || data->type == it8716) {
804 data->vid = it87_read_value(data, IT87_REG_VID);
805 /*
806 * The older IT8712F revisions had only 5 VID pins,
807 * but we assume it is always safe to read 6 bits.
808 */
809 data->vid &= 0x3f;
810 }
811 data->last_updated = jiffies;
812 data->valid = 1;
813 }
814
815 mutex_unlock(&data->update_lock);
816
817 return data;
818}
Jean Delvarefde09502005-07-19 23:51:07 +0200819
Jean Delvare20ad93d2005-06-05 11:53:25 +0200820static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100821 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100823 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -0700825 int index = sattr->index;
826 int nr = sattr->nr;
827
Guenter Roeck929c6a52012-12-19 22:17:00 +0100828 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Guenter Roeck929c6a52012-12-19 22:17:00 +0100831static ssize_t set_in(struct device *dev, struct device_attribute *attr,
832 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100834 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200835 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -0700836 int index = sattr->index;
837 int nr = sattr->nr;
Jean Delvaref5f64502010-03-05 22:17:19 +0100838 unsigned long val;
839
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100840 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100841 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100843 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100844 data->in[nr][index] = in_to_reg(data, nr, val);
845 it87_write_value(data,
846 index == 1 ? IT87_REG_VIN_MIN(nr)
847 : IT87_REG_VIN_MAX(nr),
848 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100849 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return count;
851}
852
Guenter Roeck929c6a52012-12-19 22:17:00 +0100853static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
854static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
855 0, 1);
856static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
857 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Guenter Roeck929c6a52012-12-19 22:17:00 +0100859static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
860static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
861 1, 1);
862static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
863 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Guenter Roeck929c6a52012-12-19 22:17:00 +0100865static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
866static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
867 2, 1);
868static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
869 2, 2);
870
871static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
872static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
873 3, 1);
874static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
875 3, 2);
876
877static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
878static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
879 4, 1);
880static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
881 4, 2);
882
883static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
884static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
885 5, 1);
886static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
887 5, 2);
888
889static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
890static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
891 6, 1);
892static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
893 6, 2);
894
895static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
896static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
897 7, 1);
898static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
899 7, 2);
900
901static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100902static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Guenter Roeckf838aa22015-04-01 20:09:36 -0700903static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
904static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
905static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Guenter Roeckcc18da72015-04-01 10:03:01 -0700907/* Up to 6 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200908static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100909 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100911 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
912 int nr = sattr->nr;
913 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200915
Guenter Roeck60ca3852012-12-19 22:17:00 +0100916 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200918
Guenter Roeck60ca3852012-12-19 22:17:00 +0100919static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
920 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100922 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
923 int nr = sattr->nr;
924 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200925 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100926 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100927 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100928
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100929 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100930 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100932 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100933
934 switch (index) {
935 default:
936 case 1:
937 reg = IT87_REG_TEMP_LOW(nr);
938 break;
939 case 2:
940 reg = IT87_REG_TEMP_HIGH(nr);
941 break;
942 case 3:
943 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
944 if (!(regval & 0x80)) {
945 regval |= 0x80;
946 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
947 }
948 data->valid = 0;
949 reg = IT87_REG_TEMP_OFFSET[nr];
950 break;
951 }
952
Guenter Roeck60ca3852012-12-19 22:17:00 +0100953 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100954 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100955 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return count;
957}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200958
Guenter Roeck60ca3852012-12-19 22:17:00 +0100959static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
960static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
961 0, 1);
962static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
963 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100964static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
965 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100966static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
967static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
968 1, 1);
969static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
970 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100971static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
972 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100973static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
974static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
975 2, 1);
976static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
977 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100978static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
979 set_temp, 2, 3);
Guenter Roeckcc18da72015-04-01 10:03:01 -0700980static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
981static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
982static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Guenter Roeck2cece012012-12-19 22:17:01 +0100984static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
985 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200987 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
988 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800990 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100991 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100992
Guenter Roeckc9620242015-04-04 09:05:57 -0700993 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1)) ||
994 (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100995 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (reg & (1 << nr))
997 return sprintf(buf, "3\n"); /* thermal diode */
998 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200999 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return sprintf(buf, "0\n"); /* disabled */
1001}
Guenter Roeck2cece012012-12-19 22:17:01 +01001002
1003static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
1004 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001006 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1007 int nr = sensor_attr->index;
1008
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001009 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001010 long val;
Guenter Roeck19529782012-12-19 22:17:02 +01001011 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +01001012
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001013 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001014 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Jean Delvare8acf07c2010-04-14 16:14:09 +02001016 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1017 reg &= ~(1 << nr);
1018 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001019 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1020 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +01001021 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
1022 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1023 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +02001024 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001025 dev_warn(dev,
1026 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +02001027 val = 4;
1028 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001029 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +02001031 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +02001032 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +02001033 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001034 else if (has_temp_peci(data, nr) && val == 6)
1035 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +01001036 else if (has_temp_old_peci(data, nr) && val == 6)
1037 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +02001038 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +02001040
1041 mutex_lock(&data->update_lock);
1042 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +01001043 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001044 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +01001045 if (has_temp_old_peci(data, nr))
1046 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +02001047 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001048 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return count;
1050}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Guenter Roeck2cece012012-12-19 22:17:01 +01001052static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1053 set_temp_type, 0);
1054static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1055 set_temp_type, 1);
1056static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1057 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001059/* 6 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +01001060
1061static int pwm_mode(const struct it87_data *data, int nr)
1062{
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001063 if (data->type != it8603 && nr < 3 && !(data->fan_main_ctrl & BIT(nr)))
1064 return 0; /* Full speed */
1065 if (data->pwm_ctrl[nr] & 0x80)
1066 return 2; /* Automatic mode */
1067 if ((data->type == it8603 || nr >= 3) &&
1068 data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1069 return 0; /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +01001070
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001071 return 1; /* Manual mode */
Jean Delvareb99883d2010-03-05 22:17:15 +01001072}
1073
Jean Delvare20ad93d2005-06-05 11:53:25 +02001074static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +01001075 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001077 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1078 int nr = sattr->nr;
1079 int index = sattr->index;
1080 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001082
Guenter Roecke1169ba2012-12-19 22:17:01 +01001083 speed = has_16bit_fans(data) ?
1084 FAN16_FROM_REG(data->fan[nr][index]) :
1085 FAN_FROM_REG(data->fan[nr][index],
1086 DIV_FROM_REG(data->fan_div[nr]));
1087 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001089
Jean Delvare20ad93d2005-06-05 11:53:25 +02001090static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001091 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001093 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckc9620242015-04-04 09:05:57 -07001094 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001095 int nr = sensor_attr->index;
1096
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001097 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
Guenter Roeckc9620242015-04-04 09:05:57 -07001099
Jean Delvare5f2dc792010-03-05 22:17:18 +01001100static ssize_t show_pwm_enable(struct device *dev,
Guenter Roeckc9620242015-04-04 09:05:57 -07001101 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001103 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckc9620242015-04-04 09:05:57 -07001104 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001105 int nr = sensor_attr->index;
1106
Jean Delvareb99883d2010-03-05 22:17:15 +01001107 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
Guenter Roeckc9620242015-04-04 09:05:57 -07001109
Jean Delvare20ad93d2005-06-05 11:53:25 +02001110static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001111 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001113 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckc9620242015-04-04 09:05:57 -07001114 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001115 int nr = sensor_attr->index;
1116
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001117 return sprintf(buf, "%d\n",
1118 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
Guenter Roeckc9620242015-04-04 09:05:57 -07001120
Jean Delvaref8d0c192007-02-14 21:15:02 +01001121static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001122 char *buf)
Jean Delvaref8d0c192007-02-14 21:15:02 +01001123{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001124 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001125 struct it87_data *data = it87_update_device(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001126 int nr = sensor_attr->index;
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001127 unsigned int freq;
Guenter Roeck60878bc2015-03-26 19:57:42 -07001128 int index;
1129
1130 if (has_pwm_freq2(data) && nr == 1)
1131 index = (data->extra >> 4) & 0x07;
1132 else
1133 index = (data->fan_ctl >> 4) & 0x07;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001134
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001135 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1136
1137 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001138}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001139
1140static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1141 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001143 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1144 int nr = sattr->nr;
1145 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +02001146
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001147 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001148 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +01001149 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001151 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001152 return -EINVAL;
1153
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001154 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +01001155
1156 if (has_16bit_fans(data)) {
1157 data->fan[nr][index] = FAN16_TO_REG(val);
1158 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1159 data->fan[nr][index] & 0xff);
1160 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1161 data->fan[nr][index] >> 8);
1162 } else {
1163 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1164 switch (nr) {
1165 case 0:
1166 data->fan_div[nr] = reg & 0x07;
1167 break;
1168 case 1:
1169 data->fan_div[nr] = (reg >> 3) & 0x07;
1170 break;
1171 case 2:
1172 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1173 break;
1174 }
1175 data->fan[nr][index] =
1176 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1177 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1178 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -08001179 }
1180
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001181 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return count;
1183}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001184
Jean Delvare20ad93d2005-06-05 11:53:25 +02001185static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001186 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001188 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001189 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001190 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001191 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001192 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 u8 old;
1194
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001195 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001196 return -EINVAL;
1197
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001198 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001199 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001201 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001202 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 switch (nr) {
1205 case 0:
1206 case 1:
1207 data->fan_div[nr] = DIV_TO_REG(val);
1208 break;
1209 case 2:
1210 if (val < 8)
1211 data->fan_div[nr] = 1;
1212 else
1213 data->fan_div[nr] = 3;
1214 }
1215 val = old & 0x80;
1216 val |= (data->fan_div[0] & 0x07);
1217 val |= (data->fan_div[1] & 0x07) << 3;
1218 if (data->fan_div[2] == 3)
1219 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001220 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001222 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001223 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1224 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001225
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001226 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return count;
1228}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001229
1230/* Returns 0 if OK, -EINVAL otherwise */
1231static int check_trip_points(struct device *dev, int nr)
1232{
1233 const struct it87_data *data = dev_get_drvdata(dev);
1234 int i, err = 0;
1235
1236 if (has_old_autopwm(data)) {
1237 for (i = 0; i < 3; i++) {
1238 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1239 err = -EINVAL;
1240 }
1241 for (i = 0; i < 2; i++) {
1242 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1243 err = -EINVAL;
1244 }
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001245 } else if (has_newer_autopwm(data)) {
1246 for (i = 1; i < 3; i++) {
1247 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1248 err = -EINVAL;
1249 }
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001250 }
1251
1252 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001253 dev_err(dev,
1254 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001255 dev_err(dev, "Adjust the trip points and try again\n");
1256 }
1257 return err;
1258}
1259
Guenter Roeckc9620242015-04-04 09:05:57 -07001260static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1261 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001263 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001264 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001265 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001266 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001268 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001269 return -EINVAL;
1270
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001271 /* Check trip points before switching to automatic mode */
1272 if (val == 2) {
1273 if (check_trip_points(dev, nr) < 0)
1274 return -EINVAL;
1275 }
1276
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001277 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 if (val == 0) {
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001280 if (nr < 3 && data->type != it8603) {
1281 int tmp;
1282 /* make sure the fan is on when in on/off mode */
1283 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1284 it87_write_value(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1285 /* set on/off mode */
1286 data->fan_main_ctrl &= ~BIT(nr);
1287 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1288 data->fan_main_ctrl);
1289 } else {
1290 /* No on/off mode, set maximum pwm value */
1291 data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1292 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1293 data->pwm_duty[nr]);
1294 /* and set manual mode */
1295 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1296 data->pwm_temp_map[nr] :
1297 data->pwm_duty[nr];
1298 it87_write_value(data, IT87_REG_PWM[nr],
1299 data->pwm_ctrl[nr]);
1300 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001301 } else {
1302 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001303 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001304 data->pwm_temp_map[nr] :
1305 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001306 else /* Automatic mode */
1307 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001308 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001309
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001310 if (data->type != it8603 && nr < 3) {
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001311 /* set SmartGuardian mode */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001312 data->fan_main_ctrl |= BIT(nr);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001313 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1314 data->fan_main_ctrl);
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001318 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return count;
1320}
Guenter Roeckc9620242015-04-04 09:05:57 -07001321
Jean Delvare20ad93d2005-06-05 11:53:25 +02001322static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001323 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001325 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001326 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001327 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001328 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001330 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return -EINVAL;
1332
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001333 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001334 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001335 /*
1336 * If we are in automatic mode, the PWM duty cycle register
1337 * is read-only so we can't write the value.
1338 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001339 if (data->pwm_ctrl[nr] & 0x80) {
1340 mutex_unlock(&data->update_lock);
1341 return -EBUSY;
1342 }
1343 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001344 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001345 data->pwm_duty[nr]);
1346 } else {
1347 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001348 /*
1349 * If we are in manual mode, write the duty cycle immediately;
1350 * otherwise, just store it for later use.
1351 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001352 if (!(data->pwm_ctrl[nr] & 0x80)) {
1353 data->pwm_ctrl[nr] = data->pwm_duty[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001354 it87_write_value(data, IT87_REG_PWM[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001355 data->pwm_ctrl[nr]);
1356 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001357 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001358 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return count;
1360}
Guenter Roeckc9620242015-04-04 09:05:57 -07001361
1362static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1363 const char *buf, size_t count)
Jean Delvaref8d0c192007-02-14 21:15:02 +01001364{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001365 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001366 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001367 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001368 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001369 int i;
1370
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001371 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001372 return -EINVAL;
1373
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001374 val = clamp_val(val, 0, 1000000);
1375 val *= has_newer_autopwm(data) ? 256 : 128;
1376
Jean Delvaref8d0c192007-02-14 21:15:02 +01001377 /* Search for the nearest available frequency */
1378 for (i = 0; i < 7; i++) {
Guenter Roeckc9620242015-04-04 09:05:57 -07001379 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
Jean Delvaref8d0c192007-02-14 21:15:02 +01001380 break;
1381 }
1382
1383 mutex_lock(&data->update_lock);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001384 if (nr == 0) {
1385 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1386 data->fan_ctl |= i << 4;
1387 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1388 } else {
1389 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1390 data->extra |= i << 4;
1391 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1392 }
Jean Delvaref8d0c192007-02-14 21:15:02 +01001393 mutex_unlock(&data->update_lock);
1394
1395 return count;
1396}
Guenter Roeckc9620242015-04-04 09:05:57 -07001397
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001398static ssize_t show_pwm_temp_map(struct device *dev,
Guenter Roeckc9620242015-04-04 09:05:57 -07001399 struct device_attribute *attr, char *buf)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001400{
1401 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001402 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001403 int nr = sensor_attr->index;
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001404 int map;
1405
1406 if (data->pwm_temp_map[nr] < 3)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001407 map = BIT(data->pwm_temp_map[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001408 else
1409 map = 0; /* Should never happen */
1410 return sprintf(buf, "%d\n", map);
1411}
Guenter Roeckc9620242015-04-04 09:05:57 -07001412
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001413static ssize_t set_pwm_temp_map(struct device *dev,
Guenter Roeckc9620242015-04-04 09:05:57 -07001414 struct device_attribute *attr, const char *buf,
1415 size_t count)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001416{
1417 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001418 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001419 int nr = sensor_attr->index;
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001420 long val;
1421 u8 reg;
1422
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001423 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001424 return -EINVAL;
1425
1426 switch (val) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001427 case BIT(0):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001428 reg = 0x00;
1429 break;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001430 case BIT(1):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001431 reg = 0x01;
1432 break;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001433 case BIT(2):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001434 reg = 0x02;
1435 break;
1436 default:
1437 return -EINVAL;
1438 }
1439
1440 mutex_lock(&data->update_lock);
1441 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001442 /*
1443 * If we are in automatic mode, write the temp mapping immediately;
1444 * otherwise, just store it for later use.
1445 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001446 if (data->pwm_ctrl[nr] & 0x80) {
1447 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001448 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001449 }
1450 mutex_unlock(&data->update_lock);
1451 return count;
1452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Guenter Roeckc9620242015-04-04 09:05:57 -07001454static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1455 char *buf)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001456{
1457 struct it87_data *data = it87_update_device(dev);
1458 struct sensor_device_attribute_2 *sensor_attr =
1459 to_sensor_dev_attr_2(attr);
1460 int nr = sensor_attr->nr;
1461 int point = sensor_attr->index;
1462
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001463 return sprintf(buf, "%d\n",
1464 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001465}
1466
Guenter Roeckc9620242015-04-04 09:05:57 -07001467static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
1468 const char *buf, size_t count)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001469{
1470 struct it87_data *data = dev_get_drvdata(dev);
1471 struct sensor_device_attribute_2 *sensor_attr =
1472 to_sensor_dev_attr_2(attr);
1473 int nr = sensor_attr->nr;
1474 int point = sensor_attr->index;
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001475 int regaddr;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001476 long val;
1477
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001478 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001479 return -EINVAL;
1480
1481 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001482 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001483 if (has_newer_autopwm(data))
1484 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
1485 else
1486 regaddr = IT87_REG_AUTO_PWM(nr, point);
1487 it87_write_value(data, regaddr, data->auto_pwm[nr][point]);
1488 mutex_unlock(&data->update_lock);
1489 return count;
1490}
1491
1492static ssize_t show_auto_pwm_slope(struct device *dev,
1493 struct device_attribute *attr, char *buf)
1494{
1495 struct it87_data *data = it87_update_device(dev);
1496 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1497 int nr = sensor_attr->index;
1498
1499 return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
1500}
1501
1502static ssize_t set_auto_pwm_slope(struct device *dev,
1503 struct device_attribute *attr,
1504 const char *buf, size_t count)
1505{
1506 struct it87_data *data = dev_get_drvdata(dev);
1507 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1508 int nr = sensor_attr->index;
1509 unsigned long val;
1510
1511 if (kstrtoul(buf, 10, &val) < 0 || val > 127)
1512 return -EINVAL;
1513
1514 mutex_lock(&data->update_lock);
1515 data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
1516 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 4),
1517 data->auto_pwm[nr][1]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001518 mutex_unlock(&data->update_lock);
1519 return count;
1520}
1521
Guenter Roeckc9620242015-04-04 09:05:57 -07001522static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
1523 char *buf)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001524{
1525 struct it87_data *data = it87_update_device(dev);
1526 struct sensor_device_attribute_2 *sensor_attr =
1527 to_sensor_dev_attr_2(attr);
1528 int nr = sensor_attr->nr;
1529 int point = sensor_attr->index;
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001530 int reg;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001531
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001532 if (has_old_autopwm(data) || point)
1533 reg = data->auto_temp[nr][point];
1534 else
1535 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
1536
1537 return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001538}
1539
Guenter Roeckc9620242015-04-04 09:05:57 -07001540static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
1541 const char *buf, size_t count)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001542{
1543 struct it87_data *data = dev_get_drvdata(dev);
1544 struct sensor_device_attribute_2 *sensor_attr =
1545 to_sensor_dev_attr_2(attr);
1546 int nr = sensor_attr->nr;
1547 int point = sensor_attr->index;
1548 long val;
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001549 int reg;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001550
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001551 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001552 return -EINVAL;
1553
1554 mutex_lock(&data->update_lock);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001555 if (has_newer_autopwm(data) && !point) {
1556 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
1557 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
1558 data->auto_temp[nr][0] = reg;
1559 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
1560 } else {
1561 reg = TEMP_TO_REG(val);
1562 data->auto_temp[nr][point] = reg;
1563 if (has_newer_autopwm(data))
1564 point--;
1565 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point), reg);
1566 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001567 mutex_unlock(&data->update_lock);
1568 return count;
1569}
1570
Guenter Roecke1169ba2012-12-19 22:17:01 +01001571static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1572static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1573 0, 1);
1574static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1575 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Guenter Roecke1169ba2012-12-19 22:17:01 +01001577static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1578static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1579 1, 1);
1580static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1581 set_fan_div, 1);
1582
1583static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1584static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1585 2, 1);
1586static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1587 set_fan_div, 2);
1588
1589static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1590static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1591 3, 1);
1592
1593static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1594static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1595 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001597static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1598static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1599 5, 1);
1600
Guenter Roeckc4458db2012-12-19 22:17:02 +01001601static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1602 show_pwm_enable, set_pwm_enable, 0);
1603static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001604static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1605 set_pwm_freq, 0);
Guenter Roeck5c391262013-03-30 15:02:12 -07001606static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001607 show_pwm_temp_map, set_pwm_temp_map, 0);
1608static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1609 show_auto_pwm, set_auto_pwm, 0, 0);
1610static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1611 show_auto_pwm, set_auto_pwm, 0, 1);
1612static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1613 show_auto_pwm, set_auto_pwm, 0, 2);
1614static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1615 show_auto_pwm, NULL, 0, 3);
1616static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1617 show_auto_temp, set_auto_temp, 0, 1);
1618static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1619 show_auto_temp, set_auto_temp, 0, 0);
1620static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1621 show_auto_temp, set_auto_temp, 0, 2);
1622static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1623 show_auto_temp, set_auto_temp, 0, 3);
1624static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1625 show_auto_temp, set_auto_temp, 0, 4);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001626static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, S_IRUGO | S_IWUSR,
1627 show_auto_pwm, set_auto_pwm, 0, 0);
1628static SENSOR_DEVICE_ATTR(pwm1_auto_slope, S_IRUGO | S_IWUSR,
1629 show_auto_pwm_slope, set_auto_pwm_slope, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Guenter Roeckc4458db2012-12-19 22:17:02 +01001631static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1632 show_pwm_enable, set_pwm_enable, 1);
1633static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001634static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
Guenter Roeck5c391262013-03-30 15:02:12 -07001635static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001636 show_pwm_temp_map, set_pwm_temp_map, 1);
1637static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1638 show_auto_pwm, set_auto_pwm, 1, 0);
1639static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1640 show_auto_pwm, set_auto_pwm, 1, 1);
1641static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1642 show_auto_pwm, set_auto_pwm, 1, 2);
1643static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1644 show_auto_pwm, NULL, 1, 3);
1645static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1646 show_auto_temp, set_auto_temp, 1, 1);
1647static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1648 show_auto_temp, set_auto_temp, 1, 0);
1649static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1650 show_auto_temp, set_auto_temp, 1, 2);
1651static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1652 show_auto_temp, set_auto_temp, 1, 3);
1653static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1654 show_auto_temp, set_auto_temp, 1, 4);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001655static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, S_IRUGO | S_IWUSR,
1656 show_auto_pwm, set_auto_pwm, 1, 0);
1657static SENSOR_DEVICE_ATTR(pwm2_auto_slope, S_IRUGO | S_IWUSR,
1658 show_auto_pwm_slope, set_auto_pwm_slope, 1);
Guenter Roeckc4458db2012-12-19 22:17:02 +01001659
1660static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1661 show_pwm_enable, set_pwm_enable, 2);
1662static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001663static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
Guenter Roeck5c391262013-03-30 15:02:12 -07001664static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001665 show_pwm_temp_map, set_pwm_temp_map, 2);
1666static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1667 show_auto_pwm, set_auto_pwm, 2, 0);
1668static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1669 show_auto_pwm, set_auto_pwm, 2, 1);
1670static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1671 show_auto_pwm, set_auto_pwm, 2, 2);
1672static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1673 show_auto_pwm, NULL, 2, 3);
1674static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1675 show_auto_temp, set_auto_temp, 2, 1);
1676static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1677 show_auto_temp, set_auto_temp, 2, 0);
1678static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1679 show_auto_temp, set_auto_temp, 2, 2);
1680static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1681 show_auto_temp, set_auto_temp, 2, 3);
1682static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1683 show_auto_temp, set_auto_temp, 2, 4);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001684static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, S_IRUGO | S_IWUSR,
1685 show_auto_pwm, set_auto_pwm, 2, 0);
1686static SENSOR_DEVICE_ATTR(pwm3_auto_slope, S_IRUGO | S_IWUSR,
1687 show_auto_pwm_slope, set_auto_pwm_slope, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Guenter Roeck36c4d982015-03-26 18:18:19 -07001689static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1690 show_pwm_enable, set_pwm_enable, 3);
1691static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001692static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
Guenter Roeck5c391262013-03-30 15:02:12 -07001693static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001694 show_pwm_temp_map, set_pwm_temp_map, 3);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001695static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, S_IRUGO | S_IWUSR,
1696 show_auto_temp, set_auto_temp, 2, 1);
1697static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1698 show_auto_temp, set_auto_temp, 2, 0);
1699static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, S_IRUGO | S_IWUSR,
1700 show_auto_temp, set_auto_temp, 2, 2);
1701static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, S_IRUGO | S_IWUSR,
1702 show_auto_temp, set_auto_temp, 2, 3);
1703static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, S_IRUGO | S_IWUSR,
1704 show_auto_pwm, set_auto_pwm, 3, 0);
1705static SENSOR_DEVICE_ATTR(pwm4_auto_slope, S_IRUGO | S_IWUSR,
1706 show_auto_pwm_slope, set_auto_pwm_slope, 3);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001707
1708static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1709 show_pwm_enable, set_pwm_enable, 4);
1710static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001711static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
Guenter Roeck5c391262013-03-30 15:02:12 -07001712static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001713 show_pwm_temp_map, set_pwm_temp_map, 4);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001714static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, S_IRUGO | S_IWUSR,
1715 show_auto_temp, set_auto_temp, 2, 1);
1716static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1717 show_auto_temp, set_auto_temp, 2, 0);
1718static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, S_IRUGO | S_IWUSR,
1719 show_auto_temp, set_auto_temp, 2, 2);
1720static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, S_IRUGO | S_IWUSR,
1721 show_auto_temp, set_auto_temp, 2, 3);
1722static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, S_IRUGO | S_IWUSR,
1723 show_auto_pwm, set_auto_pwm, 4, 0);
1724static SENSOR_DEVICE_ATTR(pwm5_auto_slope, S_IRUGO | S_IWUSR,
1725 show_auto_pwm_slope, set_auto_pwm_slope, 4);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001726
1727static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1728 show_pwm_enable, set_pwm_enable, 5);
1729static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001730static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
Guenter Roeck5c391262013-03-30 15:02:12 -07001731static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001732 show_pwm_temp_map, set_pwm_temp_map, 5);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001733static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, S_IRUGO | S_IWUSR,
1734 show_auto_temp, set_auto_temp, 2, 1);
1735static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1736 show_auto_temp, set_auto_temp, 2, 0);
1737static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, S_IRUGO | S_IWUSR,
1738 show_auto_temp, set_auto_temp, 2, 2);
1739static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, S_IRUGO | S_IWUSR,
1740 show_auto_temp, set_auto_temp, 2, 3);
1741static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, S_IRUGO | S_IWUSR,
1742 show_auto_pwm, set_auto_pwm, 5, 0);
1743static SENSOR_DEVICE_ATTR(pwm6_auto_slope, S_IRUGO | S_IWUSR,
1744 show_auto_pwm_slope, set_auto_pwm_slope, 5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001747static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001748 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001751
Jean Delvare68188ba2005-05-16 18:52:38 +02001752 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
Jean Delvare1d66c642005-04-18 21:16:59 -07001754static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Jean Delvare0124dd72007-11-25 16:16:41 +01001756static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001757 char *buf)
Jean Delvare0124dd72007-11-25 16:16:41 +01001758{
Jean Delvare0124dd72007-11-25 16:16:41 +01001759 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001760 int bitnr = to_sensor_dev_attr(attr)->index;
1761
Jean Delvare0124dd72007-11-25 16:16:41 +01001762 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1763}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001764
Guenter Roeckc9620242015-04-04 09:05:57 -07001765static ssize_t clear_intrusion(struct device *dev,
1766 struct device_attribute *attr, const char *buf,
1767 size_t count)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001768{
1769 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001770 int config;
Guenter Roeckc9620242015-04-04 09:05:57 -07001771 long val;
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001772
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001773 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001774 return -EINVAL;
1775
1776 mutex_lock(&data->update_lock);
1777 config = it87_read_value(data, IT87_REG_CONFIG);
1778 if (config < 0) {
1779 count = config;
1780 } else {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001781 config |= BIT(5);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001782 it87_write_value(data, IT87_REG_CONFIG, config);
1783 /* Invalidate cache to force re-read */
1784 data->valid = 0;
1785 }
1786 mutex_unlock(&data->update_lock);
1787
1788 return count;
1789}
1790
Jean Delvare0124dd72007-11-25 16:16:41 +01001791static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1792static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1793static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1794static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1795static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1796static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1797static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1798static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1799static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1800static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1801static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1802static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1803static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001804static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001805static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1806static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1807static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001808static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1809 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001810
Jean Delvared9b327c2010-03-05 22:17:17 +01001811static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001812 char *buf)
Jean Delvared9b327c2010-03-05 22:17:17 +01001813{
Jean Delvared9b327c2010-03-05 22:17:17 +01001814 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001815 int bitnr = to_sensor_dev_attr(attr)->index;
1816
Jean Delvared9b327c2010-03-05 22:17:17 +01001817 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1818}
Guenter Roeckc9620242015-04-04 09:05:57 -07001819
Jean Delvared9b327c2010-03-05 22:17:17 +01001820static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001821 const char *buf, size_t count)
Jean Delvared9b327c2010-03-05 22:17:17 +01001822{
1823 int bitnr = to_sensor_dev_attr(attr)->index;
1824 struct it87_data *data = dev_get_drvdata(dev);
1825 long val;
1826
Guenter Roeckc9620242015-04-04 09:05:57 -07001827 if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
Jean Delvared9b327c2010-03-05 22:17:17 +01001828 return -EINVAL;
1829
1830 mutex_lock(&data->update_lock);
1831 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1832 if (val)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001833 data->beeps |= BIT(bitnr);
Jean Delvared9b327c2010-03-05 22:17:17 +01001834 else
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001835 data->beeps &= ~BIT(bitnr);
Jean Delvared9b327c2010-03-05 22:17:17 +01001836 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1837 mutex_unlock(&data->update_lock);
1838 return count;
1839}
1840
1841static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1842 show_beep, set_beep, 1);
1843static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1844static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1845static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1846static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1847static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1848static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1849static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1850/* fanX_beep writability is set later */
1851static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1852static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1853static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1854static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1855static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001856static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001857static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1858 show_beep, set_beep, 2);
1859static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1860static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1861
Jean Delvare5f2dc792010-03-05 22:17:18 +01001862static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001863 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Jean Delvare90d66192007-10-08 18:24:35 +02001865 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001866
Jean Delvarea7be58a2005-12-18 16:40:14 +01001867 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
Guenter Roeckc9620242015-04-04 09:05:57 -07001869
Jean Delvare5f2dc792010-03-05 22:17:18 +01001870static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001871 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001873 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001874 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001876 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001877 return -EINVAL;
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 data->vrm = val;
1880
1881 return count;
1882}
1883static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Jean Delvare5f2dc792010-03-05 22:17:18 +01001885static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001886 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
1888 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001889
1890 return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001893
Jean Delvare738e5e02010-08-14 21:08:50 +02001894static ssize_t show_label(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001895 char *buf)
Jean Delvare738e5e02010-08-14 21:08:50 +02001896{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001897 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001898 "+5V",
1899 "5VSB",
1900 "Vbat",
1901 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001902 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001903 "+3.3V",
1904 "3VSB",
1905 "Vbat",
1906 };
1907 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001908 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001909 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001910
Justin Maggardead80802015-08-05 12:53:08 -07001911 if (has_12mv_adc(data) || has_10_9mv_adc(data))
1912 label = labels_it8721[nr];
1913 else
1914 label = labels[nr];
1915
1916 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001917}
1918static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1919static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1920static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001921/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001922static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001923
Guenter Roeck52929712013-03-30 14:00:08 -07001924static umode_t it87_in_is_visible(struct kobject *kobj,
1925 struct attribute *attr, int index)
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001926{
Guenter Roeck52929712013-03-30 14:00:08 -07001927 struct device *dev = container_of(kobj, struct device, kobj);
1928 struct it87_data *data = dev_get_drvdata(dev);
1929 int i = index / 5; /* voltage index */
1930 int a = index % 5; /* attribute index */
1931
Guenter Roeckf838aa22015-04-01 20:09:36 -07001932 if (index >= 40) { /* in8 and higher only have input attributes */
Guenter Roeck52929712013-03-30 14:00:08 -07001933 i = index - 40 + 8;
1934 a = 0;
1935 }
1936
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001937 if (!(data->has_in & BIT(i)))
Guenter Roeck52929712013-03-30 14:00:08 -07001938 return 0;
1939
1940 if (a == 4 && !data->has_beep)
1941 return 0;
1942
1943 return attr->mode;
1944}
1945
1946static struct attribute *it87_attributes_in[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001947 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001948 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001949 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001950 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001951 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
1952
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001953 &sensor_dev_attr_in1_input.dev_attr.attr,
1954 &sensor_dev_attr_in1_min.dev_attr.attr,
1955 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001956 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001957 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
1958
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001959 &sensor_dev_attr_in2_input.dev_attr.attr,
1960 &sensor_dev_attr_in2_min.dev_attr.attr,
1961 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001962 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001963 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
1964
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001965 &sensor_dev_attr_in3_input.dev_attr.attr,
1966 &sensor_dev_attr_in3_min.dev_attr.attr,
1967 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001968 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001969 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
1970
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001971 &sensor_dev_attr_in4_input.dev_attr.attr,
1972 &sensor_dev_attr_in4_min.dev_attr.attr,
1973 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001974 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001975 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
1976
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001977 &sensor_dev_attr_in5_input.dev_attr.attr,
1978 &sensor_dev_attr_in5_min.dev_attr.attr,
1979 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001980 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001981 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
1982
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001983 &sensor_dev_attr_in6_input.dev_attr.attr,
1984 &sensor_dev_attr_in6_min.dev_attr.attr,
1985 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001986 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001987 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
1988
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001989 &sensor_dev_attr_in7_input.dev_attr.attr,
1990 &sensor_dev_attr_in7_min.dev_attr.attr,
1991 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001992 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001993 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
Jean Delvare87808be2006-09-24 21:17:13 +02001994
Guenter Roeck52929712013-03-30 14:00:08 -07001995 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
Guenter Roeck52929712013-03-30 14:00:08 -07001996 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
Guenter Roeckf838aa22015-04-01 20:09:36 -07001997 &sensor_dev_attr_in10_input.dev_attr.attr, /* 41 */
1998 &sensor_dev_attr_in11_input.dev_attr.attr, /* 41 */
1999 &sensor_dev_attr_in12_input.dev_attr.attr, /* 41 */
Guenter Roeck52929712013-03-30 14:00:08 -07002000};
2001
2002static const struct attribute_group it87_group_in = {
2003 .attrs = it87_attributes_in,
2004 .is_visible = it87_in_is_visible,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002005};
2006
Guenter Roeck87533772013-03-30 14:23:20 -07002007static umode_t it87_temp_is_visible(struct kobject *kobj,
2008 struct attribute *attr, int index)
Guenter Roeck4573acb2012-03-26 16:17:41 -07002009{
Guenter Roeck87533772013-03-30 14:23:20 -07002010 struct device *dev = container_of(kobj, struct device, kobj);
2011 struct it87_data *data = dev_get_drvdata(dev);
2012 int i = index / 7; /* temperature index */
2013 int a = index % 7; /* attribute index */
2014
Guenter Roeckcc18da72015-04-01 10:03:01 -07002015 if (index >= 21) {
2016 i = index - 21 + 3;
2017 a = 0;
2018 }
2019
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002020 if (!(data->has_temp & BIT(i)))
Guenter Roeck87533772013-03-30 14:23:20 -07002021 return 0;
2022
2023 if (a == 5 && !has_temp_offset(data))
2024 return 0;
2025
2026 if (a == 6 && !data->has_beep)
2027 return 0;
2028
2029 return attr->mode;
2030}
2031
2032static struct attribute *it87_attributes_temp[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02002033 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002034 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002035 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002036 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002037 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07002038 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
2039 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
2040
Guenter Roeckcc18da72015-04-01 10:03:01 -07002041 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002042 &sensor_dev_attr_temp2_max.dev_attr.attr,
2043 &sensor_dev_attr_temp2_min.dev_attr.attr,
2044 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002045 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07002046 &sensor_dev_attr_temp2_offset.dev_attr.attr,
2047 &sensor_dev_attr_temp2_beep.dev_attr.attr,
2048
Guenter Roeckcc18da72015-04-01 10:03:01 -07002049 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002050 &sensor_dev_attr_temp3_max.dev_attr.attr,
2051 &sensor_dev_attr_temp3_min.dev_attr.attr,
2052 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002053 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07002054 &sensor_dev_attr_temp3_offset.dev_attr.attr,
2055 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002056
Guenter Roeckcc18da72015-04-01 10:03:01 -07002057 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
2058 &sensor_dev_attr_temp5_input.dev_attr.attr,
2059 &sensor_dev_attr_temp6_input.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07002060 NULL
Guenter Roeck4573acb2012-03-26 16:17:41 -07002061};
2062
Guenter Roeck87533772013-03-30 14:23:20 -07002063static const struct attribute_group it87_group_temp = {
2064 .attrs = it87_attributes_temp,
2065 .is_visible = it87_temp_is_visible,
Guenter Roeck161d8982012-12-19 22:17:01 +01002066};
2067
Guenter Roeckd3766842013-03-30 15:47:21 -07002068static umode_t it87_is_visible(struct kobject *kobj,
2069 struct attribute *attr, int index)
2070{
2071 struct device *dev = container_of(kobj, struct device, kobj);
2072 struct it87_data *data = dev_get_drvdata(dev);
2073
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002074 if ((index == 2 || index == 3) && !data->has_vid)
Guenter Roeckd3766842013-03-30 15:47:21 -07002075 return 0;
2076
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002077 if (index > 3 && !(data->in_internal & BIT(index - 4)))
Guenter Roeckd3766842013-03-30 15:47:21 -07002078 return 0;
2079
2080 return attr->mode;
2081}
2082
Guenter Roeck4573acb2012-03-26 16:17:41 -07002083static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02002084 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02002085 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002086 &dev_attr_vrm.attr, /* 2 */
2087 &dev_attr_cpu0_vid.attr, /* 3 */
2088 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
Guenter Roeckd3766842013-03-30 15:47:21 -07002089 &sensor_dev_attr_in7_label.dev_attr.attr,
2090 &sensor_dev_attr_in8_label.dev_attr.attr,
2091 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002092 NULL
2093};
2094
2095static const struct attribute_group it87_group = {
2096 .attrs = it87_attributes,
Guenter Roeckd3766842013-03-30 15:47:21 -07002097 .is_visible = it87_is_visible,
Jean Delvare87808be2006-09-24 21:17:13 +02002098};
2099
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002100static umode_t it87_fan_is_visible(struct kobject *kobj,
2101 struct attribute *attr, int index)
2102{
2103 struct device *dev = container_of(kobj, struct device, kobj);
2104 struct it87_data *data = dev_get_drvdata(dev);
2105 int i = index / 5; /* fan index */
2106 int a = index % 5; /* attribute index */
2107
2108 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
2109 i = (index - 15) / 4 + 3;
2110 a = (index - 15) % 4;
2111 }
2112
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002113 if (!(data->has_fan & BIT(i)))
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002114 return 0;
2115
2116 if (a == 3) { /* beep */
2117 if (!data->has_beep)
2118 return 0;
2119 /* first fan beep attribute is writable */
2120 if (i == __ffs(data->has_fan))
2121 return attr->mode | S_IWUSR;
2122 }
2123
2124 if (a == 4 && has_16bit_fans(data)) /* divisor */
2125 return 0;
2126
2127 return attr->mode;
2128}
2129
2130static struct attribute *it87_attributes_fan[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02002131 &sensor_dev_attr_fan1_input.dev_attr.attr,
2132 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01002133 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002134 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
2135 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
2136
Jean Delvare87808be2006-09-24 21:17:13 +02002137 &sensor_dev_attr_fan2_input.dev_attr.attr,
2138 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01002139 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002140 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2141 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
2142
Jean Delvare87808be2006-09-24 21:17:13 +02002143 &sensor_dev_attr_fan3_input.dev_attr.attr,
2144 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002145 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002146 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2147 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
2148
2149 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01002150 &sensor_dev_attr_fan4_min.dev_attr.attr,
2151 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002152 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2153
2154 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01002155 &sensor_dev_attr_fan5_min.dev_attr.attr,
2156 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002157 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2158
2159 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002160 &sensor_dev_attr_fan6_min.dev_attr.attr,
2161 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002162 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002163 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002164};
2165
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002166static const struct attribute_group it87_group_fan = {
2167 .attrs = it87_attributes_fan,
2168 .is_visible = it87_fan_is_visible,
Guenter Roecke1169ba2012-12-19 22:17:01 +01002169};
Jean Delvare723a0aa2010-03-05 22:17:16 +01002170
Guenter Roeck5c391262013-03-30 15:02:12 -07002171static umode_t it87_pwm_is_visible(struct kobject *kobj,
2172 struct attribute *attr, int index)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002173{
2174 struct device *dev = container_of(kobj, struct device, kobj);
2175 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck5c391262013-03-30 15:02:12 -07002176 int i = index / 4; /* pwm index */
2177 int a = index % 4; /* attribute index */
Guenter Roeck60878bc2015-03-26 19:57:42 -07002178
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002179 if (!(data->has_pwm & BIT(i)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002180 return 0;
2181
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002182 /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2183 if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002184 return attr->mode | S_IWUSR;
2185
2186 /* pwm2_freq is writable if there are two pwm frequency selects */
2187 if (has_pwm_freq2(data) && i == 1 && a == 2)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002188 return attr->mode | S_IWUSR;
2189
2190 return attr->mode;
2191}
2192
Guenter Roeck5c391262013-03-30 15:02:12 -07002193static struct attribute *it87_attributes_pwm[] = {
2194 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2195 &sensor_dev_attr_pwm1.dev_attr.attr,
2196 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2197 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2198
2199 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2200 &sensor_dev_attr_pwm2.dev_attr.attr,
2201 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2202 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2203
2204 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2205 &sensor_dev_attr_pwm3.dev_attr.attr,
2206 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2207 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2208
2209 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2210 &sensor_dev_attr_pwm4.dev_attr.attr,
2211 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2212 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2213
2214 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2215 &sensor_dev_attr_pwm5.dev_attr.attr,
2216 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2217 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2218
2219 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2220 &sensor_dev_attr_pwm6.dev_attr.attr,
2221 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2222 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2223
2224 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002225};
2226
Guenter Roeck5c391262013-03-30 15:02:12 -07002227static const struct attribute_group it87_group_pwm = {
2228 .attrs = it87_attributes_pwm,
2229 .is_visible = it87_pwm_is_visible,
2230};
2231
2232static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2233 struct attribute *attr, int index)
2234{
2235 struct device *dev = container_of(kobj, struct device, kobj);
2236 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002237 int i = index / 11; /* pwm index */
2238 int a = index % 11; /* attribute index */
2239
2240 if (index >= 33) { /* pwm 4..6 */
2241 i = (index - 33) / 6 + 3;
2242 a = (index - 33) % 6 + 4;
2243 }
Guenter Roeck5c391262013-03-30 15:02:12 -07002244
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002245 if (!(data->has_pwm & BIT(i)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002246 return 0;
2247
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002248 if (has_newer_autopwm(data)) {
2249 if (a < 4) /* no auto point pwm */
2250 return 0;
2251 if (a == 8) /* no auto_point4 */
2252 return 0;
2253 }
2254 if (has_old_autopwm(data)) {
2255 if (a >= 9) /* no pwm_auto_start, pwm_auto_slope */
2256 return 0;
2257 }
2258
Guenter Roeck5c391262013-03-30 15:02:12 -07002259 return attr->mode;
2260}
2261
2262static struct attribute *it87_attributes_auto_pwm[] = {
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002263 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2264 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2265 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2266 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2267 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2268 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2269 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2270 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2271 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002272 &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2273 &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002274
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002275 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, /* 11 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002276 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2277 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2278 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2279 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2280 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2281 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2282 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2283 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002284 &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2285 &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002286
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002287 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, /* 22 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002288 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2289 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2290 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2291 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2292 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2293 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2294 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2295 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002296 &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2297 &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2298
2299 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, /* 33 */
2300 &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2301 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2302 &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2303 &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2304 &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2305
2306 &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2307 &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2308 &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2309 &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2310 &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2311 &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2312
2313 &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2314 &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2315 &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2316 &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2317 &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2318 &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002319
Guenter Roeck5c391262013-03-30 15:02:12 -07002320 NULL,
2321};
2322
2323static const struct attribute_group it87_group_auto_pwm = {
2324 .attrs = it87_attributes_auto_pwm,
2325 .is_visible = it87_auto_pwm_is_visible,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002326};
2327
Jean Delvare2d8672c2005-07-19 23:56:35 +02002328/* SuperIO detection - will change isa_address if a chip is found */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002329static int __init it87_find(int sioaddr, unsigned short *address,
2330 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002332 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002333 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002334 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002335 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002337 err = superio_enter(sioaddr);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002338 if (err)
2339 return err;
2340
2341 err = -ENODEV;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002342 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002343
2344 switch (chip_type) {
2345 case IT8705F_DEVID:
2346 sio_data->type = it87;
2347 break;
2348 case IT8712F_DEVID:
2349 sio_data->type = it8712;
2350 break;
2351 case IT8716F_DEVID:
2352 case IT8726F_DEVID:
2353 sio_data->type = it8716;
2354 break;
2355 case IT8718F_DEVID:
2356 sio_data->type = it8718;
2357 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002358 case IT8720F_DEVID:
2359 sio_data->type = it8720;
2360 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002361 case IT8721F_DEVID:
2362 sio_data->type = it8721;
2363 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01002364 case IT8728F_DEVID:
2365 sio_data->type = it8728;
2366 break;
Justin Maggardead80802015-08-05 12:53:08 -07002367 case IT8732F_DEVID:
2368 sio_data->type = it8732;
2369 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06002370 case IT8771E_DEVID:
2371 sio_data->type = it8771;
2372 break;
2373 case IT8772E_DEVID:
2374 sio_data->type = it8772;
2375 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002376 case IT8781F_DEVID:
2377 sio_data->type = it8781;
2378 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08002379 case IT8782F_DEVID:
2380 sio_data->type = it8782;
2381 break;
2382 case IT8783E_DEVID:
2383 sio_data->type = it8783;
2384 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002385 case IT8786E_DEVID:
2386 sio_data->type = it8786;
2387 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07002388 case IT8790E_DEVID:
2389 sio_data->type = it8790;
2390 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02002391 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02002392 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002393 sio_data->type = it8603;
2394 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002395 case IT8620E_DEVID:
2396 sio_data->type = it8620;
2397 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002398 case 0xffff: /* No device at all */
2399 goto exit;
2400 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01002401 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002402 goto exit;
2403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002405 superio_select(sioaddr, PME);
2406 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002407 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 goto exit;
2409 }
2410
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002411 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002413 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 goto exit;
2415 }
2416
2417 err = 0;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002418 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07002419 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2420 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002421 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002423 config = &it87_devices[sio_data->type];
2424
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002425 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002426 if (has_in7_internal(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002427 sio_data->internal |= BIT(1);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002428
Jean Delvare738e5e02010-08-14 21:08:50 +02002429 /* in8 (Vbat) is always internal */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002430 sio_data->internal |= BIT(2);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002431
Guenter Roeck73055402015-03-26 09:16:32 -07002432 /* in9 (AVCC3), always internal if supported */
2433 if (has_avcc3(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002434 sio_data->internal |= BIT(3); /* in9 is AVCC */
Guenter Roeck73055402015-03-26 09:16:32 -07002435 else
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002436 sio_data->skip_in |= BIT(9);
Jean Delvare738e5e02010-08-14 21:08:50 +02002437
Guenter Roeck36c4d982015-03-26 18:18:19 -07002438 if (!has_six_pwm(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002439 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002440
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002441 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01002442 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01002443
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002444 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2445 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01002446 /* The IT8705F has a different LD number for GPIO */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002447 superio_select(sioaddr, 5);
2448 sio_data->beep_pin = superio_inb(sioaddr,
2449 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002450 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002451 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002452
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002453 superio_select(sioaddr, GPIO);
Guenter Roeck0531d982012-03-02 11:46:44 -08002454
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002455 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2456 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2457 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2458 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2459 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002460
Guenter Roeck0531d982012-03-02 11:46:44 -08002461 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002462 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
2463 sio_data->skip_fan |= BIT(2);
Guenter Roeckc9620242015-04-04 09:05:57 -07002464 if ((reg25 & BIT(4)) ||
2465 (!(reg2a & BIT(1)) && (regef & BIT(0))))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002466 sio_data->skip_pwm |= BIT(2);
Guenter Roeck0531d982012-03-02 11:46:44 -08002467
2468 /* Check if fan2 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002469 if (reg27 & BIT(7))
2470 sio_data->skip_fan |= BIT(1);
2471 if (reg27 & BIT(3))
2472 sio_data->skip_pwm |= BIT(1);
Guenter Roeck0531d982012-03-02 11:46:44 -08002473
2474 /* VIN5 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002475 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
2476 sio_data->skip_in |= BIT(5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002477
2478 /* VIN6 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002479 if (reg27 & BIT(1))
2480 sio_data->skip_in |= BIT(6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002481
2482 /*
2483 * VIN7
2484 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2485 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002486 if (reg27 & BIT(2)) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002487 /*
2488 * The data sheet is a bit unclear regarding the
2489 * internal voltage divider for VCCH5V. It says
2490 * "This bit enables and switches VIN7 (pin 91) to the
2491 * internal voltage divider for VCCH5V".
2492 * This is different to other chips, where the internal
2493 * voltage divider would connect VIN7 to an internal
2494 * voltage source. Maybe that is the case here as well.
2495 *
2496 * Since we don't know for sure, re-route it if that is
2497 * not the case, and ask the user to report if the
2498 * resulting voltage is sane.
2499 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002500 if (!(reg2c & BIT(1))) {
2501 reg2c |= BIT(1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002502 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2503 reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002504 pr_notice("Routing internal VCCH5V to in7.\n");
2505 }
2506 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2507 pr_notice("Please report if it displays a reasonable voltage.\n");
2508 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002509
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002510 if (reg2c & BIT(0))
2511 sio_data->internal |= BIT(0);
2512 if (reg2c & BIT(1))
2513 sio_data->internal |= BIT(1);
Guenter Roeck0531d982012-03-02 11:46:44 -08002514
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002515 sio_data->beep_pin = superio_inb(sioaddr,
2516 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002517 } else if (sio_data->type == it8603) {
2518 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002519
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002520 superio_select(sioaddr, GPIO);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002521
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002522 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002523
2524 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002525 if (reg27 & BIT(6))
2526 sio_data->skip_pwm |= BIT(2);
2527 if (reg27 & BIT(7))
2528 sio_data->skip_fan |= BIT(2);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002529
2530 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002531 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002532 if (reg29 & BIT(1))
2533 sio_data->skip_pwm |= BIT(1);
2534 if (reg29 & BIT(2))
2535 sio_data->skip_fan |= BIT(1);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002536
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002537 sio_data->skip_in |= BIT(5); /* No VIN5 */
2538 sio_data->skip_in |= BIT(6); /* No VIN6 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002539
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002540 sio_data->beep_pin = superio_inb(sioaddr,
2541 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002542 } else if (sio_data->type == it8620) {
2543 int reg;
2544
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002545 superio_select(sioaddr, GPIO);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002546
Guenter Roeck36c4d982015-03-26 18:18:19 -07002547 /* Check for pwm5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002548 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002549 if (reg & BIT(6))
2550 sio_data->skip_pwm |= BIT(4);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002551
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002552 /* Check for fan4, fan5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002553 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002554 if (!(reg & BIT(5)))
2555 sio_data->skip_fan |= BIT(3);
2556 if (!(reg & BIT(4)))
2557 sio_data->skip_fan |= BIT(4);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002558
2559 /* Check for pwm3, fan3 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002560 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002561 if (reg & BIT(6))
2562 sio_data->skip_pwm |= BIT(2);
2563 if (reg & BIT(7))
2564 sio_data->skip_fan |= BIT(2);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002565
Guenter Roeck36c4d982015-03-26 18:18:19 -07002566 /* Check for pwm4 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002567 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002568 if (!(reg & BIT(2)))
2569 sio_data->skip_pwm |= BIT(3);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002570
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002571 /* Check for pwm2, fan2 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002572 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002573 if (reg & BIT(1))
2574 sio_data->skip_pwm |= BIT(1);
2575 if (reg & BIT(2))
2576 sio_data->skip_fan |= BIT(1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002577 /* Check for pwm6, fan6 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002578 if (!(reg & BIT(7))) {
2579 sio_data->skip_pwm |= BIT(5);
2580 sio_data->skip_fan |= BIT(5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002581 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002582
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002583 sio_data->beep_pin = superio_inb(sioaddr,
2584 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002585 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002586 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002587 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002588
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002589 superio_select(sioaddr, GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002590
Guenter Roecka0df9262015-04-05 16:51:36 -07002591 /* Check for fan4, fan5 */
2592 if (has_five_fans(config)) {
2593 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2594 switch (sio_data->type) {
2595 case it8718:
2596 if (reg & BIT(5))
2597 sio_data->skip_fan |= BIT(3);
2598 if (reg & BIT(4))
2599 sio_data->skip_fan |= BIT(4);
2600 break;
2601 case it8720:
2602 case it8721:
2603 case it8728:
2604 if (!(reg & BIT(5)))
2605 sio_data->skip_fan |= BIT(3);
2606 if (!(reg & BIT(4)))
2607 sio_data->skip_fan |= BIT(4);
2608 break;
2609 default:
2610 break;
2611 }
2612 }
2613
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002614 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002615 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002616 /* We need at least 4 VID pins */
2617 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002618 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002619 sio_data->skip_vid = 1;
2620 }
Jean Delvare895ff262009-12-09 20:35:47 +01002621 }
2622
Jean Delvare591ec652009-12-09 20:35:48 +01002623 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002624 if (reg & BIT(6))
2625 sio_data->skip_pwm |= BIT(2);
2626 if (reg & BIT(7))
2627 sio_data->skip_fan |= BIT(2);
Jean Delvare591ec652009-12-09 20:35:48 +01002628
2629 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002630 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002631 if (reg & BIT(1))
2632 sio_data->skip_pwm |= BIT(1);
2633 if (reg & BIT(2))
2634 sio_data->skip_fan |= BIT(1);
Jean Delvare591ec652009-12-09 20:35:48 +01002635
Guenter Roeckc9620242015-04-04 09:05:57 -07002636 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
2637 !(sio_data->skip_vid))
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002638 sio_data->vid_value = superio_inb(sioaddr,
2639 IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002640
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002641 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002642
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002643 uart6 = sio_data->type == it8782 && (reg & BIT(2));
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002644
Jean Delvare436cad22010-07-09 16:22:48 +02002645 /*
2646 * The IT8720F has no VIN7 pin, so VCCH should always be
2647 * routed internally to VIN7 with an internal divider.
2648 * Curiously, there still is a configuration bit to control
2649 * this, which means it can be set incorrectly. And even
2650 * more curiously, many boards out there are improperly
2651 * configured, even though the IT8720F datasheet claims
2652 * that the internal routing of VCCH to VIN7 is the default
2653 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002654 *
2655 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002656 * If UART6 is enabled, re-route VIN7 to the internal divider
2657 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002658 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002659 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
2660 reg |= BIT(1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002661 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002662 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002663 }
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002664 if (reg & BIT(0))
2665 sio_data->internal |= BIT(0);
2666 if (reg & BIT(1))
2667 sio_data->internal |= BIT(1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002668
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002669 /*
2670 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2671 * While VIN7 can be routed to the internal voltage divider,
2672 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002673 *
2674 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2675 * is the temperature source. Since we can not read the
2676 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002677 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002678 if (uart6) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002679 sio_data->skip_in |= BIT(5) | BIT(6);
2680 sio_data->skip_temp |= BIT(2);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002681 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002682
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002683 sio_data->beep_pin = superio_inb(sioaddr,
2684 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002685 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002686 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002687 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002688
Jean Delvare98dd22c2008-10-09 15:33:58 +02002689 /* Disable specific features based on DMI strings */
2690 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2691 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2692 if (board_vendor && board_name) {
Guenter Roeckc9620242015-04-04 09:05:57 -07002693 if (strcmp(board_vendor, "nVIDIA") == 0 &&
2694 strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002695 /*
2696 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2697 * connected to a fan, but to something else. One user
2698 * has reported instant system power-off when changing
2699 * the PWM2 duty cycle, so we disable it.
2700 * I use the board name string as the trigger in case
2701 * the same board is ever used in other systems.
2702 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002703 pr_info("Disabling pwm2 due to hardware constraints\n");
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002704 sio_data->skip_pwm = BIT(1);
Jean Delvare98dd22c2008-10-09 15:33:58 +02002705 }
2706 }
2707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708exit:
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002709 superio_exit(sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return err;
2711}
2712
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002713/* Called when we have found a new IT87. */
2714static void it87_init_device(struct platform_device *pdev)
2715{
2716 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2717 struct it87_data *data = platform_get_drvdata(pdev);
2718 int tmp, i;
2719 u8 mask;
2720
2721 /*
2722 * For each PWM channel:
2723 * - If it is in automatic mode, setting to manual mode should set
2724 * the fan to full speed by default.
2725 * - If it is in manual mode, we need a mapping to temperature
2726 * channels to use when later setting to automatic mode later.
2727 * Use a 1:1 mapping by default (we are clueless.)
2728 * In both cases, the value can (and should) be changed by the user
2729 * prior to switching to a different mode.
2730 * Note that this is no longer needed for the IT8721F and later, as
2731 * these have separate registers for the temperature mapping and the
2732 * manual duty cycle.
2733 */
Guenter Roeck23100482015-04-02 08:23:45 -07002734 for (i = 0; i < NUM_AUTO_PWM; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002735 data->pwm_temp_map[i] = i;
2736 data->pwm_duty[i] = 0x7f; /* Full speed */
2737 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2738 }
2739
2740 /*
2741 * Some chips seem to have default value 0xff for all limit
2742 * registers. For low voltage limits it makes no sense and triggers
2743 * alarms, so change to 0 instead. For high temperature limits, it
2744 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2745 * but is still confusing, so change to 127 degrees C.
2746 */
Guenter Roeck23100482015-04-02 08:23:45 -07002747 for (i = 0; i < NUM_VIN_LIMIT; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002748 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2749 if (tmp == 0xff)
2750 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2751 }
Guenter Roeck23100482015-04-02 08:23:45 -07002752 for (i = 0; i < NUM_TEMP_LIMIT; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002753 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2754 if (tmp == 0xff)
2755 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2756 }
2757
2758 /*
2759 * Temperature channels are not forcibly enabled, as they can be
2760 * set to two different sensor types and we can't guess which one
2761 * is correct for a given system. These channels can be enabled at
2762 * run-time through the temp{1-3}_type sysfs accessors if needed.
2763 */
2764
2765 /* Check if voltage monitors are reset manually or by some reason */
2766 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2767 if ((tmp & 0xff) == 0) {
2768 /* Enable all voltage monitors */
2769 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2770 }
2771
2772 /* Check if tachometers are reset manually or by some reason */
2773 mask = 0x70 & ~(sio_data->skip_fan << 4);
2774 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2775 if ((data->fan_main_ctrl & mask) == 0) {
2776 /* Enable all fan tachometers */
2777 data->fan_main_ctrl |= mask;
2778 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2779 data->fan_main_ctrl);
2780 }
2781 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2782
2783 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2784
2785 /* Set tachometers to 16-bit mode if needed */
2786 if (has_fan16_config(data)) {
2787 if (~tmp & 0x07 & data->has_fan) {
2788 dev_dbg(&pdev->dev,
2789 "Setting fan1-3 to 16-bit mode\n");
2790 it87_write_value(data, IT87_REG_FAN_16BIT,
2791 tmp | 0x07);
2792 }
2793 }
2794
2795 /* Check for additional fans */
2796 if (has_five_fans(data)) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002797 if (tmp & BIT(4))
2798 data->has_fan |= BIT(3); /* fan4 enabled */
2799 if (tmp & BIT(5))
2800 data->has_fan |= BIT(4); /* fan5 enabled */
2801 if (has_six_fans(data) && (tmp & BIT(2)))
2802 data->has_fan |= BIT(5); /* fan6 enabled */
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002803 }
2804
2805 /* Fan input pins may be used for alternative functions */
2806 data->has_fan &= ~sio_data->skip_fan;
2807
2808 /* Check if pwm5, pwm6 are enabled */
2809 if (has_six_pwm(data)) {
2810 /* The following code may be IT8620E specific */
2811 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2812 if ((tmp & 0xc0) == 0xc0)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002813 sio_data->skip_pwm |= BIT(4);
2814 if (!(tmp & BIT(3)))
2815 sio_data->skip_pwm |= BIT(5);
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002816 }
2817
2818 /* Start monitoring */
2819 it87_write_value(data, IT87_REG_CONFIG,
2820 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2821 | (update_vbat ? 0x41 : 0x01));
2822}
2823
2824/* Return 1 if and only if the PWM interface is safe to use */
2825static int it87_check_pwm(struct device *dev)
2826{
2827 struct it87_data *data = dev_get_drvdata(dev);
2828 /*
2829 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2830 * and polarity set to active low is sign that this is the case so we
2831 * disable pwm control to protect the user.
2832 */
2833 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2834
2835 if ((tmp & 0x87) == 0) {
2836 if (fix_pwm_polarity) {
2837 /*
2838 * The user asks us to attempt a chip reconfiguration.
2839 * This means switching to active high polarity and
2840 * inverting all fan speed values.
2841 */
2842 int i;
2843 u8 pwm[3];
2844
Guenter Roeck23100482015-04-02 08:23:45 -07002845 for (i = 0; i < ARRAY_SIZE(pwm); i++)
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002846 pwm[i] = it87_read_value(data,
2847 IT87_REG_PWM[i]);
2848
2849 /*
2850 * If any fan is in automatic pwm mode, the polarity
2851 * might be correct, as suspicious as it seems, so we
2852 * better don't change anything (but still disable the
2853 * PWM interface).
2854 */
2855 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2856 dev_info(dev,
2857 "Reconfiguring PWM to active high polarity\n");
2858 it87_write_value(data, IT87_REG_FAN_CTL,
2859 tmp | 0x87);
2860 for (i = 0; i < 3; i++)
2861 it87_write_value(data,
2862 IT87_REG_PWM[i],
2863 0x7f & ~pwm[i]);
2864 return 1;
2865 }
2866
2867 dev_info(dev,
2868 "PWM configuration is too broken to be fixed\n");
2869 }
2870
2871 dev_info(dev,
2872 "Detected broken BIOS defaults, disabling PWM interface\n");
2873 return 0;
2874 } else if (fix_pwm_polarity) {
2875 dev_info(dev,
2876 "PWM configuration looks sane, won't touch\n");
2877 }
2878
2879 return 1;
2880}
2881
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002882static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002885 struct resource *res;
2886 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002887 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 int enable_pwm_interface;
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002889 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002891 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002892 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2893 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002894 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2895 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002896 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002897 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Guenter Roeck62a1d052012-03-24 21:54:41 -07002900 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2901 if (!data)
2902 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002904 data->addr = res->start;
2905 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002906 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002907 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002908 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002909 /*
2910 * IT8705F Datasheet 0.4.1, 3h == Version G.
2911 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2912 * These are the first revisions with 16-bit tachometer support.
2913 */
2914 switch (data->type) {
2915 case it87:
2916 if (sio_data->revision >= 0x03) {
2917 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002918 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002919 }
2920 break;
2921 case it8712:
2922 if (sio_data->revision >= 0x08) {
2923 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002924 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2925 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002926 }
2927 break;
2928 default:
2929 break;
2930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932 /* Now, we do the remaining detection. */
Guenter Roeckc9620242015-04-04 09:05:57 -07002933 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) ||
2934 it87_read_value(data, IT87_REG_CHIPID) != 0x90)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002935 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002937 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002939 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002942 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002944 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002945 if (has_12mv_adc(data)) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002946 if (sio_data->internal & BIT(0))
2947 data->in_scaled |= BIT(3); /* in3 is AVCC */
2948 if (sio_data->internal & BIT(1))
2949 data->in_scaled |= BIT(7); /* in7 is VSB */
2950 if (sio_data->internal & BIT(2))
2951 data->in_scaled |= BIT(8); /* in8 is Vbat */
2952 if (sio_data->internal & BIT(3))
2953 data->in_scaled |= BIT(9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002954 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2955 sio_data->type == it8783) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002956 if (sio_data->internal & BIT(0))
2957 data->in_scaled |= BIT(3); /* in3 is VCC5V */
2958 if (sio_data->internal & BIT(1))
2959 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002960 }
2961
Guenter Roeck4573acb2012-03-26 16:17:41 -07002962 data->has_temp = 0x07;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002963 if (sio_data->skip_temp & BIT(2)) {
Guenter Roeckc9620242015-04-04 09:05:57 -07002964 if (sio_data->type == it8782 &&
2965 !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002966 data->has_temp &= ~BIT(2);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002967 }
2968
Guenter Roeckd3766842013-03-30 15:47:21 -07002969 data->in_internal = sio_data->internal;
Guenter Roeck52929712013-03-30 14:00:08 -07002970 data->has_in = 0x3ff & ~sio_data->skip_in;
2971
Guenter Roeckcc18da72015-04-01 10:03:01 -07002972 if (has_six_temp(data)) {
2973 u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
2974
Guenter Roeckf838aa22015-04-01 20:09:36 -07002975 /* Check for additional temperature sensors */
Guenter Roeckcc18da72015-04-01 10:03:01 -07002976 if ((reg & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002977 data->has_temp |= BIT(3);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002978 if (((reg >> 2) & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002979 data->has_temp |= BIT(4);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002980 if (((reg >> 4) & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002981 data->has_temp |= BIT(5);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002982
2983 /* Check for additional voltage sensors */
2984 if ((reg & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002985 data->has_in |= BIT(10);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002986 if (((reg >> 2) & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002987 data->has_in |= BIT(11);
Guenter Roeckf838aa22015-04-01 20:09:36 -07002988 if (((reg >> 4) & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002989 data->has_in |= BIT(12);
Guenter Roeckcc18da72015-04-01 10:03:01 -07002990 }
2991
Guenter Roeck52929712013-03-30 14:00:08 -07002992 data->has_beep = !!sio_data->beep_pin;
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002995 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Guenter Roeckd3766842013-03-30 15:47:21 -07002997 if (!sio_data->skip_vid) {
2998 data->has_vid = true;
2999 data->vrm = vid_which_vrm();
3000 /* VID reading from Super-I/O config space if available */
3001 data->vid = sio_data->vid_value;
3002 }
3003
Guenter Roeck8638d0a2015-03-30 11:13:49 -07003004 /* Prepare for sysfs hooks */
3005 data->groups[0] = &it87_group;
3006 data->groups[1] = &it87_group_in;
3007 data->groups[2] = &it87_group_temp;
3008 data->groups[3] = &it87_group_fan;
Jean Delvare17d648b2006-08-28 14:23:46 +02003009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (enable_pwm_interface) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07003011 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
Guenter Roeck5c391262013-03-30 15:02:12 -07003012 data->has_pwm &= ~sio_data->skip_pwm;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01003013
Guenter Roeck8638d0a2015-03-30 11:13:49 -07003014 data->groups[4] = &it87_group_pwm;
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07003015 if (has_old_autopwm(data) || has_newer_autopwm(data))
Guenter Roeck8638d0a2015-03-30 11:13:49 -07003016 data->groups[5] = &it87_group_auto_pwm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 }
3018
Guenter Roeck8638d0a2015-03-30 11:13:49 -07003019 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
3020 it87_devices[sio_data->type].name,
3021 data, data->groups);
3022 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
3024
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07003025static struct platform_driver it87_driver = {
3026 .driver = {
3027 .name = DRVNAME,
3028 },
3029 .probe = it87_probe,
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07003030};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Guenter Roecke84bd952015-03-28 08:24:29 -07003032static int __init it87_device_add(int index, unsigned short address,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003033 const struct it87_sio_data *sio_data)
3034{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07003035 struct platform_device *pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003036 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05003037 .start = address + IT87_EC_OFFSET,
3038 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003039 .name = DRVNAME,
3040 .flags = IORESOURCE_IO,
3041 };
3042 int err;
3043
Jean Delvareb9acb642009-01-07 16:37:35 +01003044 err = acpi_check_resource_conflict(&res);
3045 if (err)
Guenter Roeck5cae84a2015-03-28 07:44:59 -07003046 return err;
Jean Delvareb9acb642009-01-07 16:37:35 +01003047
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003048 pdev = platform_device_alloc(DRVNAME, address);
Guenter Roeck5cae84a2015-03-28 07:44:59 -07003049 if (!pdev)
3050 return -ENOMEM;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003051
3052 err = platform_device_add_resources(pdev, &res, 1);
3053 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01003054 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003055 goto exit_device_put;
3056 }
3057
3058 err = platform_device_add_data(pdev, sio_data,
3059 sizeof(struct it87_sio_data));
3060 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01003061 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003062 goto exit_device_put;
3063 }
3064
3065 err = platform_device_add(pdev);
3066 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01003067 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003068 goto exit_device_put;
3069 }
3070
Guenter Roecke84bd952015-03-28 08:24:29 -07003071 it87_pdev[index] = pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003072 return 0;
3073
3074exit_device_put:
3075 platform_device_put(pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003076 return err;
3077}
3078
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079static int __init sm_it87_init(void)
3080{
Guenter Roecke84bd952015-03-28 08:24:29 -07003081 int sioaddr[2] = { REG_2E, REG_4E };
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003082 struct it87_sio_data sio_data;
Guenter Roecke84bd952015-03-28 08:24:29 -07003083 unsigned short isa_address;
3084 bool found = false;
3085 int i, err;
Jean Delvarefde09502005-07-19 23:51:07 +02003086
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003087 err = platform_driver_register(&it87_driver);
3088 if (err)
3089 return err;
3090
Guenter Roecke84bd952015-03-28 08:24:29 -07003091 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
3092 memset(&sio_data, 0, sizeof(struct it87_sio_data));
3093 isa_address = 0;
3094 err = it87_find(sioaddr[i], &isa_address, &sio_data);
3095 if (err || isa_address == 0)
3096 continue;
3097
3098 err = it87_device_add(i, isa_address, &sio_data);
3099 if (err)
3100 goto exit_dev_unregister;
3101 found = true;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003102 }
3103
Guenter Roecke84bd952015-03-28 08:24:29 -07003104 if (!found) {
3105 err = -ENODEV;
3106 goto exit_unregister;
3107 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003108 return 0;
Guenter Roecke84bd952015-03-28 08:24:29 -07003109
3110exit_dev_unregister:
3111 /* NULL check handled by platform_device_unregister */
3112 platform_device_unregister(it87_pdev[0]);
3113exit_unregister:
3114 platform_driver_unregister(&it87_driver);
3115 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116}
3117
3118static void __exit sm_it87_exit(void)
3119{
Guenter Roecke84bd952015-03-28 08:24:29 -07003120 /* NULL check handled by platform_device_unregister */
3121 platform_device_unregister(it87_pdev[1]);
3122 platform_device_unregister(it87_pdev[0]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003123 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
Jean Delvare7c81c602014-01-29 20:40:08 +01003126MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02003127MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128module_param(update_vbat, bool, 0);
3129MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
3130module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01003131MODULE_PARM_DESC(fix_pwm_polarity,
3132 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133MODULE_LICENSE("GPL");
3134
3135module_init(sm_it87_init);
3136module_exit(sm_it87_exit);