blob: 4d9090f894010a964bff213f6c25bc7114acba90 [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
Guenter Roeck8af1aba2017-02-08 13:52:57 -080015 * IT8622E Super I/O chip w/LPC interface
Rudolf Marek574e9bd2014-04-04 18:01:35 +020016 * IT8623E Super I/O chip w/LPC interface
Guenter Roeck71a9c232016-01-18 00:35:58 -080017 * IT8628E Super I/O chip w/LPC interface
Rudolf Marekc145d5c2014-01-29 20:40:08 +010018 * IT8705F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010019 * IT8712F Super I/O chip w/LPC interface
20 * IT8716F Super I/O chip w/LPC interface
21 * IT8718F Super I/O chip w/LPC interface
22 * IT8720F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020023 * IT8721F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010024 * IT8726F Super I/O chip w/LPC interface
Jean Delvare16b5dda2012-01-16 22:51:48 +010025 * IT8728F Super I/O chip w/LPC interface
Justin Maggardead80802015-08-05 12:53:08 -070026 * IT8732F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020027 * IT8758E Super I/O chip w/LPC interface
Guenter Roeckb0636702012-09-07 17:34:41 -060028 * IT8771E Super I/O chip w/LPC interface
29 * IT8772E Super I/O chip w/LPC interface
Guenter Roeck7bc32d22015-01-17 14:10:24 -080030 * IT8781F Super I/O chip w/LPC interface
Guenter Roeck0531d982012-03-02 11:46:44 -080031 * IT8782F Super I/O chip w/LPC interface
32 * IT8783E/F Super I/O chip w/LPC interface
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +010033 * IT8786E Super I/O chip w/LPC interface
Guenter Roeck4ee07152015-03-25 23:26:28 -070034 * IT8790E Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010035 * Sis950 A clone of the IT8705F
36 *
37 * Copyright (C) 2001 Chris Gauthron
Jean Delvare7c81c60f2014-01-29 20:40:08 +010038 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
Jean Delvare5f2dc792010-03-05 22:17:18 +010039 *
40 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation; either version 2 of the License, or
43 * (at your option) any later version.
44 *
45 * This program is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU General Public License for more details.
Jean Delvare5f2dc792010-03-05 22:17:18 +010049 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Joe Perchesa8ca1032011-01-12 21:55:10 +010051#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
52
Guenter Roeck48b2ae72015-04-02 08:06:12 -070053#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/module.h>
55#include <linux/init.h>
56#include <linux/slab.h>
57#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020058#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040059#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020060#include <linux/hwmon-sysfs.h>
61#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040062#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010063#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020064#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020065#include <linux/string.h>
66#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010067#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020068#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
corentin.labbeb74f3fd2007-06-13 20:27:36 +020070#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Justin Maggardead80802015-08-05 12:53:08 -070072enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
73 it8771, it8772, it8781, it8782, it8783, it8786, it8790, it8603,
Guenter Roeck8af1aba2017-02-08 13:52:57 -080074 it8620, it8622, it8628 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Jean Delvare67b671b2007-12-06 23:13:42 +010076static unsigned short force_id;
77module_param(force_id, ushort, 0);
78MODULE_PARM_DESC(force_id, "Override the detected device ID");
79
Guenter Roecke84bd952015-03-28 08:24:29 -070080static struct platform_device *it87_pdev[2];
corentin.labbeb74f3fd2007-06-13 20:27:36 +020081
Guenter Roeck3c2e3512015-03-28 08:03:10 -070082#define REG_2E 0x2e /* The register to read/write */
Guenter Roecke84bd952015-03-28 08:24:29 -070083#define REG_4E 0x4e /* Secondary register to read/write */
Guenter Roeck3c2e3512015-03-28 08:03:10 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define DEV 0x07 /* Register: Logical device select */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010087
88/* The device with the IT8718F/IT8720F VID value in it */
89#define GPIO 0x07
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define DEVID 0x20 /* Register: Device ID */
92#define DEVREV 0x22 /* Register: Device Revision */
93
Guenter Roeck3c2e3512015-03-28 08:03:10 -070094static inline int superio_inb(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Guenter Roeck3c2e3512015-03-28 08:03:10 -070096 outb(reg, ioreg);
97 return inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700100static inline void superio_outb(int ioreg, int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +0200101{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700102 outb(reg, ioreg);
103 outb(val, ioreg + 1);
Jean Delvare436cad22010-07-09 16:22:48 +0200104}
105
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700106static int superio_inw(int ioreg, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 int val;
Guenter Roeckc9620242015-04-04 09:05:57 -0700109
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700110 outb(reg++, ioreg);
111 val = inb(ioreg + 1) << 8;
112 outb(reg, ioreg);
113 val |= inb(ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return val;
115}
116
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700117static inline void superio_select(int ioreg, int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700119 outb(DEV, ioreg);
120 outb(ldn, ioreg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700123static inline int superio_enter(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200125 /*
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700126 * Try to reserve ioreg and ioreg + 1 for exclusive access.
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200127 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700128 if (!request_muxed_region(ioreg, 2, DRVNAME))
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200129 return -EBUSY;
130
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700131 outb(0x87, ioreg);
132 outb(0x01, ioreg);
133 outb(0x55, ioreg);
Guenter Roecke84bd952015-03-28 08:24:29 -0700134 outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700138static inline void superio_exit(int ioreg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Guenter Roeck3c2e3512015-03-28 08:03:10 -0700140 outb(0x02, ioreg);
141 outb(0x02, ioreg + 1);
142 release_region(ioreg, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Jean Delvare87673dd2006-08-28 14:37:19 +0200145/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define IT8712F_DEVID 0x8712
147#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200148#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200149#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100150#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200151#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400152#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100153#define IT8728F_DEVID 0x8728
Justin Maggardead80802015-08-05 12:53:08 -0700154#define IT8732F_DEVID 0x8732
Guenter Roeckb0636702012-09-07 17:34:41 -0600155#define IT8771E_DEVID 0x8771
156#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800157#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800158#define IT8782F_DEVID 0x8782
159#define IT8783E_DEVID 0x8783
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100160#define IT8786E_DEVID 0x8786
Guenter Roeck4ee07152015-03-25 23:26:28 -0700161#define IT8790E_DEVID 0x8790
Rudolf Marek7183ae82014-04-04 18:01:35 +0200162#define IT8603E_DEVID 0x8603
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800163#define IT8620E_DEVID 0x8620
Guenter Roeck8af1aba2017-02-08 13:52:57 -0800164#define IT8622E_DEVID 0x8622
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200165#define IT8623E_DEVID 0x8623
Guenter Roeck71a9c232016-01-18 00:35:58 -0800166#define IT8628E_DEVID 0x8628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define IT87_ACT_REG 0x30
168#define IT87_BASE_REG 0x60
169
Jean Delvare87673dd2006-08-28 14:37:19 +0200170/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800171#define IT87_SIO_GPIO1_REG 0x25
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800172#define IT87_SIO_GPIO2_REG 0x26
Jean Delvare895ff262009-12-09 20:35:47 +0100173#define IT87_SIO_GPIO3_REG 0x27
Guenter Roeck36c4d982015-03-26 18:18:19 -0700174#define IT87_SIO_GPIO4_REG 0x28
Jean Delvare591ec652009-12-09 20:35:48 +0100175#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800176#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200177#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800178#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200179#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100180#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030183static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030186static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/* Many IT87 constants specified below */
189
190/* Length of ISA address segment */
191#define IT87_EXTENT 8
192
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500193/* Length of ISA address segment for Environmental Controller */
194#define IT87_EC_EXTENT 2
195
196/* Offset of EC registers from ISA base address */
197#define IT87_EC_OFFSET 5
198
199/* Where are the ISA address/data registers relative to the EC base address */
200#define IT87_ADDR_REG_OFFSET 0
201#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203/*----- The IT87 registers -----*/
204
205#define IT87_REG_CONFIG 0x00
206
207#define IT87_REG_ALARM1 0x01
208#define IT87_REG_ALARM2 0x02
209#define IT87_REG_ALARM3 0x03
210
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800211/*
212 * The IT8718F and IT8720F have the VID value in a different register, in
213 * Super-I/O configuration space.
214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800216/*
217 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
218 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
219 * mode.
220 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200222#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Guenter Roeckf838aa22015-04-01 20:09:36 -0700224/*
225 * Monitors:
226 * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
227 * - up to 6 temp (1 to 6)
228 * - up to 6 fan (1 to 6)
229 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700231static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
232static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
233static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
234static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
235static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237#define IT87_REG_FAN_MAIN_CTRL 0x13
238#define IT87_REG_FAN_CTL 0x14
Guenter Roeck36c4d982015-03-26 18:18:19 -0700239static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
240static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Guenter Roeck559313c2015-04-01 10:15:38 -0700242static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
Guenter Roeckf838aa22015-04-01 20:09:36 -0700243 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Guenter Roeck559313c2015-04-01 10:15:38 -0700245#define IT87_REG_TEMP(nr) (0x29 + (nr))
Guenter Roeck73055402015-03-26 09:16:32 -0700246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
248#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
249#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
250#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#define IT87_REG_VIN_ENABLE 0x50
253#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700254#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100255#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257#define IT87_REG_CHIPID 0x58
258
Guenter Roeck2cbb9c32015-04-05 11:53:29 -0700259static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
260
261#define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
262#define IT87_REG_AUTO_PWM(nr, i) (IT87_REG_AUTO_BASE[nr] + 5 + (i))
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100263
Guenter Roeckcc18da72015-04-01 10:03:01 -0700264#define IT87_REG_TEMP456_ENABLE 0x77
265
Guenter Roeck23100482015-04-02 08:23:45 -0700266#define NUM_VIN ARRAY_SIZE(IT87_REG_VIN)
267#define NUM_VIN_LIMIT 8
268#define NUM_TEMP 6
269#define NUM_TEMP_OFFSET ARRAY_SIZE(IT87_REG_TEMP_OFFSET)
270#define NUM_TEMP_LIMIT 3
271#define NUM_FAN ARRAY_SIZE(IT87_REG_FAN)
272#define NUM_FAN_DIV 3
273#define NUM_PWM ARRAY_SIZE(IT87_REG_PWM)
274#define NUM_AUTO_PWM ARRAY_SIZE(IT87_REG_PWM)
275
Guenter Roeck483db432012-12-19 22:17:02 +0100276struct it87_devices {
277 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700278 const char * const suffix;
Guenter Roeckcc18da72015-04-01 10:03:01 -0700279 u32 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100280 u8 peci_mask;
281 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100282};
283
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700284#define FEAT_12MV_ADC BIT(0)
285#define FEAT_NEWER_AUTOPWM BIT(1)
286#define FEAT_OLD_AUTOPWM BIT(2)
287#define FEAT_16BIT_FANS BIT(3)
288#define FEAT_TEMP_OFFSET BIT(4)
289#define FEAT_TEMP_PECI BIT(5)
290#define FEAT_TEMP_OLD_PECI BIT(6)
291#define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
292#define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
293#define FEAT_VID BIT(9) /* Set if chip supports VID */
294#define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
295#define FEAT_SIX_FANS BIT(11) /* Supports six fans */
296#define FEAT_10_9MV_ADC BIT(12)
297#define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
298#define FEAT_SIX_PWM BIT(14) /* Chip supports 6 pwm chn */
299#define FEAT_PWM_FREQ2 BIT(15) /* Separate pwm freq 2 */
300#define FEAT_SIX_TEMP BIT(16) /* Up to 6 temp sensors */
Guenter Roecka9eebd42017-02-08 13:51:16 -0800301#define FEAT_VIN3_5V BIT(17) /* VIN3 connected to +5V */
Guenter Roeck483db432012-12-19 22:17:02 +0100302
303static const struct it87_devices it87_devices[] = {
304 [it87] = {
305 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700306 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100307 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
308 },
309 [it8712] = {
310 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700311 .suffix = "F",
Guenter Roeck32dd7c402015-02-12 07:40:23 -0800312 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
313 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100314 },
315 [it8716] = {
316 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700317 .suffix = "F",
Guenter Roeck32dd7c402015-02-12 07:40:23 -0800318 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700319 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
Guenter Roeck483db432012-12-19 22:17:02 +0100320 },
321 [it8718] = {
322 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700323 .suffix = "F",
Guenter Roeck32dd7c402015-02-12 07:40:23 -0800324 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700325 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
326 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100327 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100328 },
329 [it8720] = {
330 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700331 .suffix = "F",
Guenter Roeck32dd7c402015-02-12 07:40:23 -0800332 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck60878bc2015-03-26 19:57:42 -0700333 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
334 | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100335 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100336 },
337 [it8721] = {
338 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700339 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100340 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800341 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck60878bc2015-03-26 19:57:42 -0700342 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
343 | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100344 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100345 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100346 },
347 [it8728] = {
348 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700349 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100350 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700351 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700352 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100353 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100354 },
Justin Maggardead80802015-08-05 12:53:08 -0700355 [it8732] = {
356 .name = "it8732",
357 .suffix = "F",
358 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
359 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
360 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
361 .peci_mask = 0x07,
362 .old_peci_mask = 0x02, /* Actually reports PCH */
363 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600364 [it8771] = {
365 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700366 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600367 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700368 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
369 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800370 /* PECI: guesswork */
371 /* 12mV ADC (OHM) */
372 /* 16 bit fans (OHM) */
373 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600374 .peci_mask = 0x07,
375 },
376 [it8772] = {
377 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700378 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600379 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700380 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
381 | FEAT_PWM_FREQ2,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800382 /* PECI (coreboot) */
383 /* 12mV ADC (HWSensors4, OHM) */
384 /* 16 bit fans (HWSensors4, OHM) */
385 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600386 .peci_mask = 0x07,
387 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800388 [it8781] = {
389 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700390 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800391 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700392 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800393 .old_peci_mask = 0x4,
394 },
Guenter Roeck483db432012-12-19 22:17:02 +0100395 [it8782] = {
396 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700397 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100398 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700399 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100400 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100401 },
402 [it8783] = {
403 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700404 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100405 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck60878bc2015-03-26 19:57:42 -0700406 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
Guenter Roeck19529782012-12-19 22:17:02 +0100407 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100408 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100409 [it8786] = {
410 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700411 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100412 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700413 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
414 | FEAT_PWM_FREQ2,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100415 .peci_mask = 0x07,
416 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700417 [it8790] = {
418 .name = "it8790",
419 .suffix = "E",
420 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck60878bc2015-03-26 19:57:42 -0700421 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
422 | FEAT_PWM_FREQ2,
Guenter Roeck4ee07152015-03-25 23:26:28 -0700423 .peci_mask = 0x07,
424 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100425 [it8603] = {
426 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700427 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100428 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck73055402015-03-26 09:16:32 -0700429 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
Guenter Roeck60878bc2015-03-26 19:57:42 -0700430 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100431 .peci_mask = 0x07,
432 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800433 [it8620] = {
434 .name = "it8620",
435 .suffix = "E",
436 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700437 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
Guenter Roeckcc18da72015-04-01 10:03:01 -0700438 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
Guenter Roecka9eebd42017-02-08 13:51:16 -0800439 | FEAT_SIX_TEMP | FEAT_VIN3_5V,
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800440 .peci_mask = 0x07,
441 },
Guenter Roeck8af1aba2017-02-08 13:52:57 -0800442 [it8622] = {
443 .name = "it8622",
444 .suffix = "E",
445 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
446 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
447 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_AVCC3
448 | FEAT_VIN3_5V,
449 .peci_mask = 0x07,
450 },
Guenter Roeck71a9c232016-01-18 00:35:58 -0800451 [it8628] = {
452 .name = "it8628",
453 .suffix = "E",
454 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
455 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
456 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
Guenter Roecka9eebd42017-02-08 13:51:16 -0800457 | FEAT_SIX_TEMP | FEAT_VIN3_5V,
Guenter Roeck71a9c232016-01-18 00:35:58 -0800458 .peci_mask = 0x07,
459 },
Guenter Roeck483db432012-12-19 22:17:02 +0100460};
461
462#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
463#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
Justin Maggardead80802015-08-05 12:53:08 -0700464#define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
Guenter Roeck483db432012-12-19 22:17:02 +0100465#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
466#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
467#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100468#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700469 ((data)->peci_mask & BIT(nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100470#define has_temp_old_peci(data, nr) \
471 (((data)->features & FEAT_TEMP_OLD_PECI) && \
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700472 ((data)->old_peci_mask & BIT(nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800473#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700474#define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
475 FEAT_SIX_FANS))
Guenter Roeck32dd7c402015-02-12 07:40:23 -0800476#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700477#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Guenter Roeckfa3f70d2015-03-25 23:15:32 -0700478#define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
Guenter Roeck73055402015-03-26 09:16:32 -0700479#define has_avcc3(data) ((data)->features & FEAT_AVCC3)
Guenter Roeck36c4d982015-03-26 18:18:19 -0700480#define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
Guenter Roeck60878bc2015-03-26 19:57:42 -0700481#define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
Guenter Roeckcc18da72015-04-01 10:03:01 -0700482#define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
Guenter Roecka9eebd42017-02-08 13:51:16 -0800483#define has_vin3_5v(data) ((data)->features & FEAT_VIN3_5V)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200485struct it87_sio_data {
486 enum chips type;
487 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200488 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200489 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100490 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200491 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100492 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700493 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100494 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100495 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200496 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700497 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200498};
499
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800500/*
501 * For each registered chip, we need to keep some data in memory.
502 * The structure is dynamically allocated.
503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504struct it87_data {
Guenter Roeck8638d0a2015-03-30 11:13:49 -0700505 const struct attribute_group *groups[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 enum chips type;
Guenter Roeckaa8b1872016-08-09 22:09:19 -0700507 u32 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100508 u8 peci_mask;
509 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200511 unsigned short addr;
512 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100513 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 char valid; /* !=0 if following fields are valid */
515 unsigned long last_updated; /* In jiffies */
516
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200517 u16 in_scaled; /* Internal voltage sensors are scaled */
Guenter Roeckd3766842013-03-30 15:47:21 -0700518 u16 in_internal; /* Bitfield, internal sensors (for labels) */
Guenter Roeck52929712013-03-30 14:00:08 -0700519 u16 has_in; /* Bitfield, voltage sensors enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700520 u8 in[NUM_VIN][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200521 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700522 u16 fan[NUM_FAN][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700523 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700524 s8 temp[NUM_TEMP][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100525 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
526 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Guenter Roeck23100482015-04-02 08:23:45 -0700527 u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
Guenter Roeckd3766842013-03-30 15:47:21 -0700528 bool has_vid; /* True if VID supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100530 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 u32 alarms; /* Register encoding, combined */
Guenter Roeck52929712013-03-30 14:00:08 -0700532 bool has_beep; /* true if beep supported */
Jean Delvared9b327c2010-03-05 22:17:17 +0100533 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100535 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100536
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800537 /*
538 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100539 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
540 * 7, and we want to preserve settings on mode changes, so we have
541 * to track all values separately.
542 * Starting with the IT8721F, the manual PWM duty cycles are stored
543 * in separate registers (8-bit values), so the separate tracking
544 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800545 * simple.
546 */
Guenter Roeck5c391262013-03-30 15:02:12 -0700547 u8 has_pwm; /* Bitfield, pwm control enabled */
Guenter Roeck23100482015-04-02 08:23:45 -0700548 u8 pwm_ctrl[NUM_PWM]; /* Register value */
549 u8 pwm_duty[NUM_PWM]; /* Manual PWM value set by user */
550 u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100551
552 /* Automatic fan speed control registers */
Guenter Roeck23100482015-04-02 08:23:45 -0700553 u8 auto_pwm[NUM_AUTO_PWM][4]; /* [nr][3] is hard-coded */
554 s8 auto_temp[NUM_AUTO_PWM][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555};
556
Guenter Roeck0531d982012-03-02 11:46:44 -0800557static int adc_lsb(const struct it87_data *data, int nr)
558{
Justin Maggardead80802015-08-05 12:53:08 -0700559 int lsb;
560
561 if (has_12mv_adc(data))
562 lsb = 120;
563 else if (has_10_9mv_adc(data))
564 lsb = 109;
565 else
566 lsb = 160;
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700567 if (data->in_scaled & BIT(nr))
Guenter Roeck0531d982012-03-02 11:46:44 -0800568 lsb <<= 1;
569 return lsb;
570}
571
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200572static u8 in_to_reg(const struct it87_data *data, int nr, long val)
573{
Justin Maggardead80802015-08-05 12:53:08 -0700574 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800575 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200576}
577
578static int in_from_reg(const struct it87_data *data, int nr, int val)
579{
Justin Maggardead80802015-08-05 12:53:08 -0700580 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200581}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200582
583static inline u8 FAN_TO_REG(long rpm, int div)
584{
585 if (rpm == 0)
586 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800587 rpm = clamp_val(rpm, 1, 1000000);
588 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200589}
590
591static inline u16 FAN16_TO_REG(long rpm)
592{
593 if (rpm == 0)
594 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800595 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200596}
597
598#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
599 1350000 / ((val) * (div)))
600/* The divider is fixed to 2 in 16-bit mode */
601#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
602 1350000 / ((val) * 2))
603
Guenter Roeck2a844c12013-01-09 08:09:34 -0800604#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
605 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200606#define TEMP_FROM_REG(val) ((val) * 1000)
607
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200608static u8 pwm_to_reg(const struct it87_data *data, long val)
609{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100610 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200611 return val;
612 else
613 return val >> 1;
614}
615
616static int pwm_from_reg(const struct it87_data *data, u8 reg)
617{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100618 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200619 return reg;
620 else
621 return (reg & 0x7f) << 1;
622}
623
Jean Delvare0df6454d2010-10-28 20:31:51 +0200624static int DIV_TO_REG(int val)
625{
626 int answer = 0;
Guenter Roeckc9620242015-04-04 09:05:57 -0700627
Jean Delvare0df6454d2010-10-28 20:31:51 +0200628 while (answer < 7 && (val >>= 1))
629 answer++;
630 return answer;
631}
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700632
633#define DIV_FROM_REG(val) BIT(val)
Jean Delvare0df6454d2010-10-28 20:31:51 +0200634
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700635/*
636 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
637 * depending on the chip type, to calculate the actual PWM frequency.
638 *
639 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
640 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
641 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
642 * sometimes just one. It is unknown if this is a datasheet error or real,
643 * so this is ignored for now.
644 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200645static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700646 48000000,
647 24000000,
648 12000000,
649 8000000,
650 6000000,
651 3000000,
652 1500000,
653 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200654};
655
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700656/*
657 * Must be called with data->update_lock held, except during initialization.
658 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
659 * would slow down the IT87 access and should not be necessary.
660 */
661static int it87_read_value(struct it87_data *data, u8 reg)
662{
663 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
664 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
665}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700667/*
668 * Must be called with data->update_lock held, except during initialization.
669 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
670 * would slow down the IT87 access and should not be necessary.
671 */
672static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
673{
674 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
675 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
676}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700678static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
679{
680 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
681 if (has_newer_autopwm(data)) {
Guenter Roeck0624d862015-04-06 21:04:18 -0700682 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700683 data->pwm_duty[nr] = it87_read_value(data,
684 IT87_REG_PWM_DUTY[nr]);
685 } else {
686 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
687 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
688 else /* Manual mode */
689 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700692 if (has_old_autopwm(data)) {
693 int i;
694
695 for (i = 0; i < 5 ; i++)
696 data->auto_temp[nr][i] = it87_read_value(data,
697 IT87_REG_AUTO_TEMP(nr, i));
698 for (i = 0; i < 3 ; i++)
699 data->auto_pwm[nr][i] = it87_read_value(data,
700 IT87_REG_AUTO_PWM(nr, i));
Guenter Roeck2cbb9c32015-04-05 11:53:29 -0700701 } else if (has_newer_autopwm(data)) {
702 int i;
703
704 /*
705 * 0: temperature hysteresis (base + 5)
706 * 1: fan off temperature (base + 0)
707 * 2: fan start temperature (base + 1)
708 * 3: fan max temperature (base + 2)
709 */
710 data->auto_temp[nr][0] =
711 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 5));
712
713 for (i = 0; i < 3 ; i++)
714 data->auto_temp[nr][i + 1] =
715 it87_read_value(data,
716 IT87_REG_AUTO_TEMP(nr, i));
717 /*
718 * 0: start pwm value (base + 3)
719 * 1: pwm slope (base + 4, 1/8th pwm)
720 */
721 data->auto_pwm[nr][0] =
722 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 3));
723 data->auto_pwm[nr][1] =
724 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 4));
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700725 }
726}
727
728static struct it87_data *it87_update_device(struct device *dev)
729{
730 struct it87_data *data = dev_get_drvdata(dev);
731 int i;
732
733 mutex_lock(&data->update_lock);
734
Guenter Roeckc9620242015-04-04 09:05:57 -0700735 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
736 !data->valid) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700737 if (update_vbat) {
738 /*
739 * Cleared after each update, so reenable. Value
740 * returned by this read will be previous value
741 */
742 it87_write_value(data, IT87_REG_CONFIG,
743 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
744 }
Guenter Roeck23100482015-04-02 08:23:45 -0700745 for (i = 0; i < NUM_VIN; i++) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700746 if (!(data->has_in & BIT(i)))
Guenter Roeck559313c2015-04-01 10:15:38 -0700747 continue;
748
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700749 data->in[i][0] =
Guenter Roeck559313c2015-04-01 10:15:38 -0700750 it87_read_value(data, IT87_REG_VIN[i]);
751
752 /* VBAT and AVCC don't have limit registers */
Guenter Roeck23100482015-04-02 08:23:45 -0700753 if (i >= NUM_VIN_LIMIT)
Guenter Roeck559313c2015-04-01 10:15:38 -0700754 continue;
755
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700756 data->in[i][1] =
757 it87_read_value(data, IT87_REG_VIN_MIN(i));
758 data->in[i][2] =
759 it87_read_value(data, IT87_REG_VIN_MAX(i));
760 }
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700761
Guenter Roeck23100482015-04-02 08:23:45 -0700762 for (i = 0; i < NUM_FAN; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700763 /* Skip disabled fans */
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700764 if (!(data->has_fan & BIT(i)))
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700765 continue;
766
767 data->fan[i][1] =
768 it87_read_value(data, IT87_REG_FAN_MIN[i]);
769 data->fan[i][0] = it87_read_value(data,
770 IT87_REG_FAN[i]);
771 /* Add high byte if in 16-bit mode */
772 if (has_16bit_fans(data)) {
773 data->fan[i][0] |= it87_read_value(data,
774 IT87_REG_FANX[i]) << 8;
775 data->fan[i][1] |= it87_read_value(data,
776 IT87_REG_FANX_MIN[i]) << 8;
777 }
778 }
Guenter Roeck23100482015-04-02 08:23:45 -0700779 for (i = 0; i < NUM_TEMP; i++) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -0700780 if (!(data->has_temp & BIT(i)))
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700781 continue;
782 data->temp[i][0] =
783 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeckcc18da72015-04-01 10:03:01 -0700784
Guenter Roeck23100482015-04-02 08:23:45 -0700785 if (has_temp_offset(data) && i < NUM_TEMP_OFFSET)
786 data->temp[i][3] =
787 it87_read_value(data,
788 IT87_REG_TEMP_OFFSET[i]);
789
790 if (i >= NUM_TEMP_LIMIT)
Guenter Roeckcc18da72015-04-01 10:03:01 -0700791 continue;
792
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700793 data->temp[i][1] =
794 it87_read_value(data, IT87_REG_TEMP_LOW(i));
795 data->temp[i][2] =
796 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700797 }
798
799 /* Newer chips don't have clock dividers */
800 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
801 i = it87_read_value(data, IT87_REG_FAN_DIV);
802 data->fan_div[0] = i & 0x07;
803 data->fan_div[1] = (i >> 3) & 0x07;
804 data->fan_div[2] = (i & 0x40) ? 3 : 1;
805 }
806
807 data->alarms =
808 it87_read_value(data, IT87_REG_ALARM1) |
809 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
810 (it87_read_value(data, IT87_REG_ALARM3) << 16);
811 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
812
813 data->fan_main_ctrl = it87_read_value(data,
814 IT87_REG_FAN_MAIN_CTRL);
815 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Guenter Roeck0624d862015-04-06 21:04:18 -0700816 for (i = 0; i < NUM_PWM; i++) {
817 if (!(data->has_pwm & BIT(i)))
818 continue;
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700819 it87_update_pwm_ctrl(data, i);
Guenter Roeck0624d862015-04-06 21:04:18 -0700820 }
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -0700821
822 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
823 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
824 /*
825 * The IT8705F does not have VID capability.
826 * The IT8718F and later don't use IT87_REG_VID for the
827 * same purpose.
828 */
829 if (data->type == it8712 || data->type == it8716) {
830 data->vid = it87_read_value(data, IT87_REG_VID);
831 /*
832 * The older IT8712F revisions had only 5 VID pins,
833 * but we assume it is always safe to read 6 bits.
834 */
835 data->vid &= 0x3f;
836 }
837 data->last_updated = jiffies;
838 data->valid = 1;
839 }
840
841 mutex_unlock(&data->update_lock);
842
843 return data;
844}
Jean Delvarefde09502005-07-19 23:51:07 +0200845
Jean Delvare20ad93d2005-06-05 11:53:25 +0200846static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100847 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100849 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -0700851 int index = sattr->index;
852 int nr = sattr->nr;
853
Guenter Roeck929c6a52012-12-19 22:17:00 +0100854 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Guenter Roeck929c6a52012-12-19 22:17:00 +0100857static ssize_t set_in(struct device *dev, struct device_attribute *attr,
858 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100860 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200861 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -0700862 int index = sattr->index;
863 int nr = sattr->nr;
Jean Delvaref5f64502010-03-05 22:17:19 +0100864 unsigned long val;
865
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100866 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100867 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100869 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100870 data->in[nr][index] = in_to_reg(data, nr, val);
871 it87_write_value(data,
872 index == 1 ? IT87_REG_VIN_MIN(nr)
873 : IT87_REG_VIN_MAX(nr),
874 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100875 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return count;
877}
878
Guenter Roeck929c6a52012-12-19 22:17:00 +0100879static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
880static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
881 0, 1);
882static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
883 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Guenter Roeck929c6a52012-12-19 22:17:00 +0100885static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
886static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
887 1, 1);
888static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
889 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Guenter Roeck929c6a52012-12-19 22:17:00 +0100891static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
892static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
893 2, 1);
894static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
895 2, 2);
896
897static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
898static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
899 3, 1);
900static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
901 3, 2);
902
903static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
904static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
905 4, 1);
906static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
907 4, 2);
908
909static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
910static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
911 5, 1);
912static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
913 5, 2);
914
915static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
916static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
917 6, 1);
918static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
919 6, 2);
920
921static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
922static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
923 7, 1);
924static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
925 7, 2);
926
927static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100928static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Guenter Roeckf838aa22015-04-01 20:09:36 -0700929static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
930static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
931static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Guenter Roeckcc18da72015-04-01 10:03:01 -0700933/* Up to 6 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200934static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100935 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100937 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
938 int nr = sattr->nr;
939 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200941
Guenter Roeck60ca3852012-12-19 22:17:00 +0100942 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200944
Guenter Roeck60ca3852012-12-19 22:17:00 +0100945static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
946 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100948 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
949 int nr = sattr->nr;
950 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200951 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100952 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100953 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100954
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100955 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100956 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100958 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100959
960 switch (index) {
961 default:
962 case 1:
963 reg = IT87_REG_TEMP_LOW(nr);
964 break;
965 case 2:
966 reg = IT87_REG_TEMP_HIGH(nr);
967 break;
968 case 3:
969 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
970 if (!(regval & 0x80)) {
971 regval |= 0x80;
972 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
973 }
974 data->valid = 0;
975 reg = IT87_REG_TEMP_OFFSET[nr];
976 break;
977 }
978
Guenter Roeck60ca3852012-12-19 22:17:00 +0100979 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100980 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100981 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return count;
983}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200984
Guenter Roeck60ca3852012-12-19 22:17:00 +0100985static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
986static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
987 0, 1);
988static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
989 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100990static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
991 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100992static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
993static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
994 1, 1);
995static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
996 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100997static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
998 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100999static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
1000static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1001 2, 1);
1002static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1003 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +01001004static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
1005 set_temp, 2, 3);
Guenter Roeckcc18da72015-04-01 10:03:01 -07001006static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
1007static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
1008static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Guenter Roeck2cece012012-12-19 22:17:01 +01001010static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
1011 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001013 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1014 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001016 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +01001017 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +01001018
Guenter Roeckc9620242015-04-04 09:05:57 -07001019 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1)) ||
1020 (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001021 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (reg & (1 << nr))
1023 return sprintf(buf, "3\n"); /* thermal diode */
1024 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +02001025 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return sprintf(buf, "0\n"); /* disabled */
1027}
Guenter Roeck2cece012012-12-19 22:17:01 +01001028
1029static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
1030 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001032 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1033 int nr = sensor_attr->index;
1034
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001035 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001036 long val;
Guenter Roeck19529782012-12-19 22:17:02 +01001037 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +01001038
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001039 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001040 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Jean Delvare8acf07c2010-04-14 16:14:09 +02001042 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1043 reg &= ~(1 << nr);
1044 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001045 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1046 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +01001047 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
1048 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1049 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +02001050 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001051 dev_warn(dev,
1052 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +02001053 val = 4;
1054 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001055 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +02001057 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +02001058 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +02001059 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01001060 else if (has_temp_peci(data, nr) && val == 6)
1061 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +01001062 else if (has_temp_old_peci(data, nr) && val == 6)
1063 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +02001064 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +02001066
1067 mutex_lock(&data->update_lock);
1068 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +01001069 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001070 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +01001071 if (has_temp_old_peci(data, nr))
1072 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +02001073 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001074 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return count;
1076}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Guenter Roeck2cece012012-12-19 22:17:01 +01001078static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1079 set_temp_type, 0);
1080static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1081 set_temp_type, 1);
1082static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1083 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001085/* 6 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +01001086
1087static int pwm_mode(const struct it87_data *data, int nr)
1088{
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001089 if (data->type != it8603 && nr < 3 && !(data->fan_main_ctrl & BIT(nr)))
1090 return 0; /* Full speed */
1091 if (data->pwm_ctrl[nr] & 0x80)
1092 return 2; /* Automatic mode */
1093 if ((data->type == it8603 || nr >= 3) &&
1094 data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1095 return 0; /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +01001096
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001097 return 1; /* Manual mode */
Jean Delvareb99883d2010-03-05 22:17:15 +01001098}
1099
Jean Delvare20ad93d2005-06-05 11:53:25 +02001100static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +01001101 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001103 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1104 int nr = sattr->nr;
1105 int index = sattr->index;
1106 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001108
Guenter Roecke1169ba2012-12-19 22:17:01 +01001109 speed = has_16bit_fans(data) ?
1110 FAN16_FROM_REG(data->fan[nr][index]) :
1111 FAN_FROM_REG(data->fan[nr][index],
1112 DIV_FROM_REG(data->fan_div[nr]));
1113 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001115
Jean Delvare20ad93d2005-06-05 11:53:25 +02001116static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001117 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001119 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckc9620242015-04-04 09:05:57 -07001120 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001121 int nr = sensor_attr->index;
1122
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001123 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
Guenter Roeckc9620242015-04-04 09:05:57 -07001125
Jean Delvare5f2dc792010-03-05 22:17:18 +01001126static ssize_t show_pwm_enable(struct device *dev,
Guenter Roeckc9620242015-04-04 09:05:57 -07001127 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001129 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckc9620242015-04-04 09:05:57 -07001130 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001131 int nr = sensor_attr->index;
1132
Jean Delvareb99883d2010-03-05 22:17:15 +01001133 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
Guenter Roeckc9620242015-04-04 09:05:57 -07001135
Jean Delvare20ad93d2005-06-05 11:53:25 +02001136static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001137 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001139 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeckc9620242015-04-04 09:05:57 -07001140 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +02001141 int nr = sensor_attr->index;
1142
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001143 return sprintf(buf, "%d\n",
1144 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
Guenter Roeckc9620242015-04-04 09:05:57 -07001146
Jean Delvaref8d0c192007-02-14 21:15:02 +01001147static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001148 char *buf)
Jean Delvaref8d0c192007-02-14 21:15:02 +01001149{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001150 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001151 struct it87_data *data = it87_update_device(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001152 int nr = sensor_attr->index;
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001153 unsigned int freq;
Guenter Roeck60878bc2015-03-26 19:57:42 -07001154 int index;
1155
1156 if (has_pwm_freq2(data) && nr == 1)
1157 index = (data->extra >> 4) & 0x07;
1158 else
1159 index = (data->fan_ctl >> 4) & 0x07;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001160
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001161 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1162
1163 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001164}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001165
1166static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1167 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Guenter Roecke1169ba2012-12-19 22:17:01 +01001169 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1170 int nr = sattr->nr;
1171 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +02001172
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001173 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001174 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +01001175 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001177 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001178 return -EINVAL;
1179
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001180 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +01001181
1182 if (has_16bit_fans(data)) {
1183 data->fan[nr][index] = FAN16_TO_REG(val);
1184 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1185 data->fan[nr][index] & 0xff);
1186 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1187 data->fan[nr][index] >> 8);
1188 } else {
1189 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1190 switch (nr) {
1191 case 0:
1192 data->fan_div[nr] = reg & 0x07;
1193 break;
1194 case 1:
1195 data->fan_div[nr] = (reg >> 3) & 0x07;
1196 break;
1197 case 2:
1198 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1199 break;
1200 }
1201 data->fan[nr][index] =
1202 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1203 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1204 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -08001205 }
1206
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001207 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return count;
1209}
Guenter Roecke1169ba2012-12-19 22:17:01 +01001210
Jean Delvare20ad93d2005-06-05 11:53:25 +02001211static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001212 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001214 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001215 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001216 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001217 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001218 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 u8 old;
1220
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001221 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001222 return -EINVAL;
1223
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001224 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001225 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001227 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001228 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 switch (nr) {
1231 case 0:
1232 case 1:
1233 data->fan_div[nr] = DIV_TO_REG(val);
1234 break;
1235 case 2:
1236 if (val < 8)
1237 data->fan_div[nr] = 1;
1238 else
1239 data->fan_div[nr] = 3;
1240 }
1241 val = old & 0x80;
1242 val |= (data->fan_div[0] & 0x07);
1243 val |= (data->fan_div[1] & 0x07) << 3;
1244 if (data->fan_div[2] == 3)
1245 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001246 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001248 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +01001249 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1250 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +02001251
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001252 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return count;
1254}
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001255
1256/* Returns 0 if OK, -EINVAL otherwise */
1257static int check_trip_points(struct device *dev, int nr)
1258{
1259 const struct it87_data *data = dev_get_drvdata(dev);
1260 int i, err = 0;
1261
1262 if (has_old_autopwm(data)) {
1263 for (i = 0; i < 3; i++) {
1264 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1265 err = -EINVAL;
1266 }
1267 for (i = 0; i < 2; i++) {
1268 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1269 err = -EINVAL;
1270 }
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001271 } else if (has_newer_autopwm(data)) {
1272 for (i = 1; i < 3; i++) {
1273 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1274 err = -EINVAL;
1275 }
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001276 }
1277
1278 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01001279 dev_err(dev,
1280 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001281 dev_err(dev, "Adjust the trip points and try again\n");
1282 }
1283 return err;
1284}
1285
Guenter Roeckc9620242015-04-04 09:05:57 -07001286static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1287 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001289 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001290 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001291 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001292 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001294 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001295 return -EINVAL;
1296
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001297 /* Check trip points before switching to automatic mode */
1298 if (val == 2) {
1299 if (check_trip_points(dev, nr) < 0)
1300 return -EINVAL;
1301 }
1302
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001303 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 if (val == 0) {
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001306 if (nr < 3 && data->type != it8603) {
1307 int tmp;
1308 /* make sure the fan is on when in on/off mode */
1309 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1310 it87_write_value(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1311 /* set on/off mode */
1312 data->fan_main_ctrl &= ~BIT(nr);
1313 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1314 data->fan_main_ctrl);
1315 } else {
1316 /* No on/off mode, set maximum pwm value */
1317 data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1318 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1319 data->pwm_duty[nr]);
1320 /* and set manual mode */
1321 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1322 data->pwm_temp_map[nr] :
1323 data->pwm_duty[nr];
1324 it87_write_value(data, IT87_REG_PWM[nr],
1325 data->pwm_ctrl[nr]);
1326 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001327 } else {
1328 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001329 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001330 data->pwm_temp_map[nr] :
1331 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001332 else /* Automatic mode */
1333 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001334 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001335
Guenter Roeckf1bbe612015-04-05 13:16:54 -07001336 if (data->type != it8603 && nr < 3) {
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001337 /* set SmartGuardian mode */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001338 data->fan_main_ctrl |= BIT(nr);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001339 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1340 data->fan_main_ctrl);
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001344 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return count;
1346}
Guenter Roeckc9620242015-04-04 09:05:57 -07001347
Jean Delvare20ad93d2005-06-05 11:53:25 +02001348static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001349 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001351 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001352 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001353 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001354 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001356 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -EINVAL;
1358
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001359 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001360 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001361 /*
1362 * If we are in automatic mode, the PWM duty cycle register
1363 * is read-only so we can't write the value.
1364 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001365 if (data->pwm_ctrl[nr] & 0x80) {
1366 mutex_unlock(&data->update_lock);
1367 return -EBUSY;
1368 }
1369 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001370 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001371 data->pwm_duty[nr]);
1372 } else {
1373 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001374 /*
1375 * If we are in manual mode, write the duty cycle immediately;
1376 * otherwise, just store it for later use.
1377 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001378 if (!(data->pwm_ctrl[nr] & 0x80)) {
1379 data->pwm_ctrl[nr] = data->pwm_duty[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001380 it87_write_value(data, IT87_REG_PWM[nr],
Jean Delvare6229cdb2010-12-08 16:27:22 +01001381 data->pwm_ctrl[nr]);
1382 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001383 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001384 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return count;
1386}
Guenter Roeckc9620242015-04-04 09:05:57 -07001387
1388static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1389 const char *buf, size_t count)
Jean Delvaref8d0c192007-02-14 21:15:02 +01001390{
Guenter Roeck60878bc2015-03-26 19:57:42 -07001391 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001392 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001393 int nr = sensor_attr->index;
Jean Delvaref5f64502010-03-05 22:17:19 +01001394 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001395 int i;
1396
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001397 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001398 return -EINVAL;
1399
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001400 val = clamp_val(val, 0, 1000000);
1401 val *= has_newer_autopwm(data) ? 256 : 128;
1402
Jean Delvaref8d0c192007-02-14 21:15:02 +01001403 /* Search for the nearest available frequency */
1404 for (i = 0; i < 7; i++) {
Guenter Roeckc9620242015-04-04 09:05:57 -07001405 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
Jean Delvaref8d0c192007-02-14 21:15:02 +01001406 break;
1407 }
1408
1409 mutex_lock(&data->update_lock);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001410 if (nr == 0) {
1411 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1412 data->fan_ctl |= i << 4;
1413 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1414 } else {
1415 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1416 data->extra |= i << 4;
1417 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1418 }
Jean Delvaref8d0c192007-02-14 21:15:02 +01001419 mutex_unlock(&data->update_lock);
1420
1421 return count;
1422}
Guenter Roeckc9620242015-04-04 09:05:57 -07001423
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001424static ssize_t show_pwm_temp_map(struct device *dev,
Guenter Roeckc9620242015-04-04 09:05:57 -07001425 struct device_attribute *attr, char *buf)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001426{
1427 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001428 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001429 int nr = sensor_attr->index;
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001430 int map;
1431
Guenter Roeck0624d862015-04-06 21:04:18 -07001432 map = data->pwm_temp_map[nr];
1433 if (map >= 3)
1434 map = 0; /* Should never happen */
1435 if (nr >= 3) /* pwm channels 3..6 map to temp4..6 */
1436 map += 3;
1437
1438 return sprintf(buf, "%d\n", (int)BIT(map));
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001439}
Guenter Roeckc9620242015-04-04 09:05:57 -07001440
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001441static ssize_t set_pwm_temp_map(struct device *dev,
Guenter Roeckc9620242015-04-04 09:05:57 -07001442 struct device_attribute *attr, const char *buf,
1443 size_t count)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001444{
1445 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001446 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001447 int nr = sensor_attr->index;
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001448 long val;
1449 u8 reg;
1450
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001451 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001452 return -EINVAL;
1453
Guenter Roeck0624d862015-04-06 21:04:18 -07001454 if (nr >= 3)
1455 val -= 3;
1456
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001457 switch (val) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001458 case BIT(0):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001459 reg = 0x00;
1460 break;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001461 case BIT(1):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001462 reg = 0x01;
1463 break;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001464 case BIT(2):
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001465 reg = 0x02;
1466 break;
1467 default:
1468 return -EINVAL;
1469 }
1470
1471 mutex_lock(&data->update_lock);
1472 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001473 /*
1474 * If we are in automatic mode, write the temp mapping immediately;
1475 * otherwise, just store it for later use.
1476 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001477 if (data->pwm_ctrl[nr] & 0x80) {
1478 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
Guenter Roeck36c4d982015-03-26 18:18:19 -07001479 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001480 }
1481 mutex_unlock(&data->update_lock);
1482 return count;
1483}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Guenter Roeckc9620242015-04-04 09:05:57 -07001485static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1486 char *buf)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001487{
1488 struct it87_data *data = it87_update_device(dev);
1489 struct sensor_device_attribute_2 *sensor_attr =
1490 to_sensor_dev_attr_2(attr);
1491 int nr = sensor_attr->nr;
1492 int point = sensor_attr->index;
1493
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001494 return sprintf(buf, "%d\n",
1495 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001496}
1497
Guenter Roeckc9620242015-04-04 09:05:57 -07001498static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
1499 const char *buf, size_t count)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001500{
1501 struct it87_data *data = dev_get_drvdata(dev);
1502 struct sensor_device_attribute_2 *sensor_attr =
1503 to_sensor_dev_attr_2(attr);
1504 int nr = sensor_attr->nr;
1505 int point = sensor_attr->index;
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001506 int regaddr;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001507 long val;
1508
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001509 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001510 return -EINVAL;
1511
1512 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001513 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001514 if (has_newer_autopwm(data))
1515 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
1516 else
1517 regaddr = IT87_REG_AUTO_PWM(nr, point);
1518 it87_write_value(data, regaddr, data->auto_pwm[nr][point]);
1519 mutex_unlock(&data->update_lock);
1520 return count;
1521}
1522
1523static ssize_t show_auto_pwm_slope(struct device *dev,
1524 struct device_attribute *attr, char *buf)
1525{
1526 struct it87_data *data = it87_update_device(dev);
1527 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1528 int nr = sensor_attr->index;
1529
1530 return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
1531}
1532
1533static ssize_t set_auto_pwm_slope(struct device *dev,
1534 struct device_attribute *attr,
1535 const char *buf, size_t count)
1536{
1537 struct it87_data *data = dev_get_drvdata(dev);
1538 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1539 int nr = sensor_attr->index;
1540 unsigned long val;
1541
1542 if (kstrtoul(buf, 10, &val) < 0 || val > 127)
1543 return -EINVAL;
1544
1545 mutex_lock(&data->update_lock);
1546 data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
1547 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 4),
1548 data->auto_pwm[nr][1]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001549 mutex_unlock(&data->update_lock);
1550 return count;
1551}
1552
Guenter Roeckc9620242015-04-04 09:05:57 -07001553static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
1554 char *buf)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001555{
1556 struct it87_data *data = it87_update_device(dev);
1557 struct sensor_device_attribute_2 *sensor_attr =
1558 to_sensor_dev_attr_2(attr);
1559 int nr = sensor_attr->nr;
1560 int point = sensor_attr->index;
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001561 int reg;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001562
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001563 if (has_old_autopwm(data) || point)
1564 reg = data->auto_temp[nr][point];
1565 else
1566 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
1567
1568 return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001569}
1570
Guenter Roeckc9620242015-04-04 09:05:57 -07001571static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
1572 const char *buf, size_t count)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001573{
1574 struct it87_data *data = dev_get_drvdata(dev);
1575 struct sensor_device_attribute_2 *sensor_attr =
1576 to_sensor_dev_attr_2(attr);
1577 int nr = sensor_attr->nr;
1578 int point = sensor_attr->index;
1579 long val;
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001580 int reg;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001581
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001582 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001583 return -EINVAL;
1584
1585 mutex_lock(&data->update_lock);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001586 if (has_newer_autopwm(data) && !point) {
1587 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
1588 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
1589 data->auto_temp[nr][0] = reg;
1590 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
1591 } else {
1592 reg = TEMP_TO_REG(val);
1593 data->auto_temp[nr][point] = reg;
1594 if (has_newer_autopwm(data))
1595 point--;
1596 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point), reg);
1597 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001598 mutex_unlock(&data->update_lock);
1599 return count;
1600}
1601
Guenter Roecke1169ba2012-12-19 22:17:01 +01001602static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1603static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1604 0, 1);
1605static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1606 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Guenter Roecke1169ba2012-12-19 22:17:01 +01001608static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1609static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1610 1, 1);
1611static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1612 set_fan_div, 1);
1613
1614static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1615static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1616 2, 1);
1617static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1618 set_fan_div, 2);
1619
1620static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1621static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1622 3, 1);
1623
1624static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1625static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1626 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001628static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1629static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1630 5, 1);
1631
Guenter Roeckc4458db2012-12-19 22:17:02 +01001632static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1633 show_pwm_enable, set_pwm_enable, 0);
1634static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001635static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1636 set_pwm_freq, 0);
Guenter Roeck5c391262013-03-30 15:02:12 -07001637static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001638 show_pwm_temp_map, set_pwm_temp_map, 0);
1639static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1640 show_auto_pwm, set_auto_pwm, 0, 0);
1641static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1642 show_auto_pwm, set_auto_pwm, 0, 1);
1643static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1644 show_auto_pwm, set_auto_pwm, 0, 2);
1645static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1646 show_auto_pwm, NULL, 0, 3);
1647static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1648 show_auto_temp, set_auto_temp, 0, 1);
1649static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1650 show_auto_temp, set_auto_temp, 0, 0);
1651static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1652 show_auto_temp, set_auto_temp, 0, 2);
1653static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1654 show_auto_temp, set_auto_temp, 0, 3);
1655static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1656 show_auto_temp, set_auto_temp, 0, 4);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001657static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, S_IRUGO | S_IWUSR,
1658 show_auto_pwm, set_auto_pwm, 0, 0);
1659static SENSOR_DEVICE_ATTR(pwm1_auto_slope, S_IRUGO | S_IWUSR,
1660 show_auto_pwm_slope, set_auto_pwm_slope, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Guenter Roeckc4458db2012-12-19 22:17:02 +01001662static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1663 show_pwm_enable, set_pwm_enable, 1);
1664static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001665static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
Guenter Roeck5c391262013-03-30 15:02:12 -07001666static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001667 show_pwm_temp_map, set_pwm_temp_map, 1);
1668static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1669 show_auto_pwm, set_auto_pwm, 1, 0);
1670static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1671 show_auto_pwm, set_auto_pwm, 1, 1);
1672static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1673 show_auto_pwm, set_auto_pwm, 1, 2);
1674static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1675 show_auto_pwm, NULL, 1, 3);
1676static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1677 show_auto_temp, set_auto_temp, 1, 1);
1678static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1679 show_auto_temp, set_auto_temp, 1, 0);
1680static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1681 show_auto_temp, set_auto_temp, 1, 2);
1682static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1683 show_auto_temp, set_auto_temp, 1, 3);
1684static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1685 show_auto_temp, set_auto_temp, 1, 4);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001686static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, S_IRUGO | S_IWUSR,
1687 show_auto_pwm, set_auto_pwm, 1, 0);
1688static SENSOR_DEVICE_ATTR(pwm2_auto_slope, S_IRUGO | S_IWUSR,
1689 show_auto_pwm_slope, set_auto_pwm_slope, 1);
Guenter Roeckc4458db2012-12-19 22:17:02 +01001690
1691static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1692 show_pwm_enable, set_pwm_enable, 2);
1693static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001694static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
Guenter Roeck5c391262013-03-30 15:02:12 -07001695static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
Guenter Roeckc4458db2012-12-19 22:17:02 +01001696 show_pwm_temp_map, set_pwm_temp_map, 2);
1697static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1698 show_auto_pwm, set_auto_pwm, 2, 0);
1699static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1700 show_auto_pwm, set_auto_pwm, 2, 1);
1701static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1702 show_auto_pwm, set_auto_pwm, 2, 2);
1703static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1704 show_auto_pwm, NULL, 2, 3);
1705static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1706 show_auto_temp, set_auto_temp, 2, 1);
1707static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1708 show_auto_temp, set_auto_temp, 2, 0);
1709static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1710 show_auto_temp, set_auto_temp, 2, 2);
1711static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1712 show_auto_temp, set_auto_temp, 2, 3);
1713static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1714 show_auto_temp, set_auto_temp, 2, 4);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001715static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, S_IRUGO | S_IWUSR,
1716 show_auto_pwm, set_auto_pwm, 2, 0);
1717static SENSOR_DEVICE_ATTR(pwm3_auto_slope, S_IRUGO | S_IWUSR,
1718 show_auto_pwm_slope, set_auto_pwm_slope, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Guenter Roeck36c4d982015-03-26 18:18:19 -07001720static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1721 show_pwm_enable, set_pwm_enable, 3);
1722static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001723static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
Guenter Roeck5c391262013-03-30 15:02:12 -07001724static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001725 show_pwm_temp_map, set_pwm_temp_map, 3);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001726static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, S_IRUGO | S_IWUSR,
1727 show_auto_temp, set_auto_temp, 2, 1);
1728static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1729 show_auto_temp, set_auto_temp, 2, 0);
1730static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, S_IRUGO | S_IWUSR,
1731 show_auto_temp, set_auto_temp, 2, 2);
1732static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, S_IRUGO | S_IWUSR,
1733 show_auto_temp, set_auto_temp, 2, 3);
1734static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, S_IRUGO | S_IWUSR,
1735 show_auto_pwm, set_auto_pwm, 3, 0);
1736static SENSOR_DEVICE_ATTR(pwm4_auto_slope, S_IRUGO | S_IWUSR,
1737 show_auto_pwm_slope, set_auto_pwm_slope, 3);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001738
1739static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1740 show_pwm_enable, set_pwm_enable, 4);
1741static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001742static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
Guenter Roeck5c391262013-03-30 15:02:12 -07001743static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001744 show_pwm_temp_map, set_pwm_temp_map, 4);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001745static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, S_IRUGO | S_IWUSR,
1746 show_auto_temp, set_auto_temp, 2, 1);
1747static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1748 show_auto_temp, set_auto_temp, 2, 0);
1749static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, S_IRUGO | S_IWUSR,
1750 show_auto_temp, set_auto_temp, 2, 2);
1751static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, S_IRUGO | S_IWUSR,
1752 show_auto_temp, set_auto_temp, 2, 3);
1753static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, S_IRUGO | S_IWUSR,
1754 show_auto_pwm, set_auto_pwm, 4, 0);
1755static SENSOR_DEVICE_ATTR(pwm5_auto_slope, S_IRUGO | S_IWUSR,
1756 show_auto_pwm_slope, set_auto_pwm_slope, 4);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001757
1758static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1759 show_pwm_enable, set_pwm_enable, 5);
1760static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
Guenter Roeck60878bc2015-03-26 19:57:42 -07001761static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
Guenter Roeck5c391262013-03-30 15:02:12 -07001762static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
Guenter Roeck36c4d982015-03-26 18:18:19 -07001763 show_pwm_temp_map, set_pwm_temp_map, 5);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07001764static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, S_IRUGO | S_IWUSR,
1765 show_auto_temp, set_auto_temp, 2, 1);
1766static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1767 show_auto_temp, set_auto_temp, 2, 0);
1768static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, S_IRUGO | S_IWUSR,
1769 show_auto_temp, set_auto_temp, 2, 2);
1770static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, S_IRUGO | S_IWUSR,
1771 show_auto_temp, set_auto_temp, 2, 3);
1772static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, S_IRUGO | S_IWUSR,
1773 show_auto_pwm, set_auto_pwm, 5, 0);
1774static SENSOR_DEVICE_ATTR(pwm6_auto_slope, S_IRUGO | S_IWUSR,
1775 show_auto_pwm_slope, set_auto_pwm_slope, 5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777/* Alarms */
Julia Lawallddc64ae2016-12-22 13:05:21 +01001778static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001779 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001782
Jean Delvare68188ba2005-05-16 18:52:38 +02001783 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
Julia Lawallddc64ae2016-12-22 13:05:21 +01001785static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Jean Delvare0124dd72007-11-25 16:16:41 +01001787static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001788 char *buf)
Jean Delvare0124dd72007-11-25 16:16:41 +01001789{
Jean Delvare0124dd72007-11-25 16:16:41 +01001790 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001791 int bitnr = to_sensor_dev_attr(attr)->index;
1792
Jean Delvare0124dd72007-11-25 16:16:41 +01001793 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1794}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001795
Guenter Roeckc9620242015-04-04 09:05:57 -07001796static ssize_t clear_intrusion(struct device *dev,
1797 struct device_attribute *attr, const char *buf,
1798 size_t count)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001799{
1800 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001801 int config;
Guenter Roeckc9620242015-04-04 09:05:57 -07001802 long val;
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001803
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001804 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001805 return -EINVAL;
1806
1807 mutex_lock(&data->update_lock);
1808 config = it87_read_value(data, IT87_REG_CONFIG);
1809 if (config < 0) {
1810 count = config;
1811 } else {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001812 config |= BIT(5);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001813 it87_write_value(data, IT87_REG_CONFIG, config);
1814 /* Invalidate cache to force re-read */
1815 data->valid = 0;
1816 }
1817 mutex_unlock(&data->update_lock);
1818
1819 return count;
1820}
1821
Jean Delvare0124dd72007-11-25 16:16:41 +01001822static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1823static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1824static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1825static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1826static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1827static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1828static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1829static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1830static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1831static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1832static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1833static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1834static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001835static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
Jean Delvare0124dd72007-11-25 16:16:41 +01001836static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1837static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1838static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001839static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1840 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001841
Jean Delvared9b327c2010-03-05 22:17:17 +01001842static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001843 char *buf)
Jean Delvared9b327c2010-03-05 22:17:17 +01001844{
Jean Delvared9b327c2010-03-05 22:17:17 +01001845 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001846 int bitnr = to_sensor_dev_attr(attr)->index;
1847
Jean Delvared9b327c2010-03-05 22:17:17 +01001848 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1849}
Guenter Roeckc9620242015-04-04 09:05:57 -07001850
Jean Delvared9b327c2010-03-05 22:17:17 +01001851static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001852 const char *buf, size_t count)
Jean Delvared9b327c2010-03-05 22:17:17 +01001853{
1854 int bitnr = to_sensor_dev_attr(attr)->index;
1855 struct it87_data *data = dev_get_drvdata(dev);
1856 long val;
1857
Guenter Roeckc9620242015-04-04 09:05:57 -07001858 if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
Jean Delvared9b327c2010-03-05 22:17:17 +01001859 return -EINVAL;
1860
1861 mutex_lock(&data->update_lock);
1862 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1863 if (val)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001864 data->beeps |= BIT(bitnr);
Jean Delvared9b327c2010-03-05 22:17:17 +01001865 else
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001866 data->beeps &= ~BIT(bitnr);
Jean Delvared9b327c2010-03-05 22:17:17 +01001867 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1868 mutex_unlock(&data->update_lock);
1869 return count;
1870}
1871
1872static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1873 show_beep, set_beep, 1);
1874static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1875static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1876static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1877static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1878static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1879static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1880static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1881/* fanX_beep writability is set later */
1882static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1883static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1884static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1885static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1886static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07001887static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
Jean Delvared9b327c2010-03-05 22:17:17 +01001888static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1889 show_beep, set_beep, 2);
1890static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1891static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1892
Julia Lawallddc64ae2016-12-22 13:05:21 +01001893static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
1894 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Jean Delvare90d66192007-10-08 18:24:35 +02001896 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001897
Jean Delvarea7be58a2005-12-18 16:40:14 +01001898 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
Guenter Roeckc9620242015-04-04 09:05:57 -07001900
Julia Lawallddc64ae2016-12-22 13:05:21 +01001901static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
1902 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001904 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001905 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001907 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001908 return -EINVAL;
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 data->vrm = val;
1911
1912 return count;
1913}
Julia Lawallddc64ae2016-12-22 13:05:21 +01001914static DEVICE_ATTR_RW(vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Julia Lawallddc64ae2016-12-22 13:05:21 +01001916static ssize_t cpu0_vid_show(struct device *dev,
1917 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 struct it87_data *data = it87_update_device(dev);
Guenter Roeckc9620242015-04-04 09:05:57 -07001920
1921 return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
Julia Lawallddc64ae2016-12-22 13:05:21 +01001923static DEVICE_ATTR_RO(cpu0_vid);
Jean Delvare87808be2006-09-24 21:17:13 +02001924
Jean Delvare738e5e02010-08-14 21:08:50 +02001925static ssize_t show_label(struct device *dev, struct device_attribute *attr,
Guenter Roeckc9620242015-04-04 09:05:57 -07001926 char *buf)
Jean Delvare738e5e02010-08-14 21:08:50 +02001927{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001928 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001929 "+5V",
1930 "5VSB",
1931 "Vbat",
1932 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001933 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001934 "+3.3V",
1935 "3VSB",
1936 "Vbat",
1937 };
1938 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001939 int nr = to_sensor_dev_attr(attr)->index;
Justin Maggardead80802015-08-05 12:53:08 -07001940 const char *label;
Jean Delvare738e5e02010-08-14 21:08:50 +02001941
Guenter Roecka9eebd42017-02-08 13:51:16 -08001942 if (has_vin3_5v(data) && nr == 0)
1943 label = labels[0];
1944 else if (has_12mv_adc(data) || has_10_9mv_adc(data))
Justin Maggardead80802015-08-05 12:53:08 -07001945 label = labels_it8721[nr];
1946 else
1947 label = labels[nr];
1948
1949 return sprintf(buf, "%s\n", label);
Jean Delvare738e5e02010-08-14 21:08:50 +02001950}
1951static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1952static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1953static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Guenter Roeck73055402015-03-26 09:16:32 -07001954/* AVCC3 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001955static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001956
Guenter Roeck52929712013-03-30 14:00:08 -07001957static umode_t it87_in_is_visible(struct kobject *kobj,
1958 struct attribute *attr, int index)
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001959{
Guenter Roeck52929712013-03-30 14:00:08 -07001960 struct device *dev = container_of(kobj, struct device, kobj);
1961 struct it87_data *data = dev_get_drvdata(dev);
1962 int i = index / 5; /* voltage index */
1963 int a = index % 5; /* attribute index */
1964
Guenter Roeckf838aa22015-04-01 20:09:36 -07001965 if (index >= 40) { /* in8 and higher only have input attributes */
Guenter Roeck52929712013-03-30 14:00:08 -07001966 i = index - 40 + 8;
1967 a = 0;
1968 }
1969
Guenter Roeck48b2ae72015-04-02 08:06:12 -07001970 if (!(data->has_in & BIT(i)))
Guenter Roeck52929712013-03-30 14:00:08 -07001971 return 0;
1972
1973 if (a == 4 && !data->has_beep)
1974 return 0;
1975
1976 return attr->mode;
1977}
1978
1979static struct attribute *it87_attributes_in[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001980 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001981 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001982 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001983 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001984 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
1985
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001986 &sensor_dev_attr_in1_input.dev_attr.attr,
1987 &sensor_dev_attr_in1_min.dev_attr.attr,
1988 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001989 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001990 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
1991
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001992 &sensor_dev_attr_in2_input.dev_attr.attr,
1993 &sensor_dev_attr_in2_min.dev_attr.attr,
1994 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001995 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07001996 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
1997
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001998 &sensor_dev_attr_in3_input.dev_attr.attr,
1999 &sensor_dev_attr_in3_min.dev_attr.attr,
2000 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002001 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07002002 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
2003
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002004 &sensor_dev_attr_in4_input.dev_attr.attr,
2005 &sensor_dev_attr_in4_min.dev_attr.attr,
2006 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002007 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07002008 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
2009
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002010 &sensor_dev_attr_in5_input.dev_attr.attr,
2011 &sensor_dev_attr_in5_min.dev_attr.attr,
2012 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002013 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07002014 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
2015
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002016 &sensor_dev_attr_in6_input.dev_attr.attr,
2017 &sensor_dev_attr_in6_min.dev_attr.attr,
2018 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002019 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07002020 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
2021
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002022 &sensor_dev_attr_in7_input.dev_attr.attr,
2023 &sensor_dev_attr_in7_min.dev_attr.attr,
2024 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002025 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck52929712013-03-30 14:00:08 -07002026 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
Jean Delvare87808be2006-09-24 21:17:13 +02002027
Guenter Roeck52929712013-03-30 14:00:08 -07002028 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
Jean Delvared5f3f6c2016-08-29 13:33:29 +02002029 &sensor_dev_attr_in9_input.dev_attr.attr,
2030 &sensor_dev_attr_in10_input.dev_attr.attr,
2031 &sensor_dev_attr_in11_input.dev_attr.attr,
2032 &sensor_dev_attr_in12_input.dev_attr.attr,
Jean Delvare3c329262016-08-29 13:18:23 +02002033 NULL
Guenter Roeck52929712013-03-30 14:00:08 -07002034};
2035
2036static const struct attribute_group it87_group_in = {
2037 .attrs = it87_attributes_in,
2038 .is_visible = it87_in_is_visible,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002039};
2040
Guenter Roeck87533772013-03-30 14:23:20 -07002041static umode_t it87_temp_is_visible(struct kobject *kobj,
2042 struct attribute *attr, int index)
Guenter Roeck4573acb2012-03-26 16:17:41 -07002043{
Guenter Roeck87533772013-03-30 14:23:20 -07002044 struct device *dev = container_of(kobj, struct device, kobj);
2045 struct it87_data *data = dev_get_drvdata(dev);
2046 int i = index / 7; /* temperature index */
2047 int a = index % 7; /* attribute index */
2048
Guenter Roeckcc18da72015-04-01 10:03:01 -07002049 if (index >= 21) {
2050 i = index - 21 + 3;
2051 a = 0;
2052 }
2053
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002054 if (!(data->has_temp & BIT(i)))
Guenter Roeck87533772013-03-30 14:23:20 -07002055 return 0;
2056
2057 if (a == 5 && !has_temp_offset(data))
2058 return 0;
2059
2060 if (a == 6 && !data->has_beep)
2061 return 0;
2062
2063 return attr->mode;
2064}
2065
2066static struct attribute *it87_attributes_temp[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02002067 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002068 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002069 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002070 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002071 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07002072 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
2073 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
2074
Guenter Roeckcc18da72015-04-01 10:03:01 -07002075 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002076 &sensor_dev_attr_temp2_max.dev_attr.attr,
2077 &sensor_dev_attr_temp2_min.dev_attr.attr,
2078 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002079 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07002080 &sensor_dev_attr_temp2_offset.dev_attr.attr,
2081 &sensor_dev_attr_temp2_beep.dev_attr.attr,
2082
Guenter Roeckcc18da72015-04-01 10:03:01 -07002083 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002084 &sensor_dev_attr_temp3_max.dev_attr.attr,
2085 &sensor_dev_attr_temp3_min.dev_attr.attr,
2086 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002087 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07002088 &sensor_dev_attr_temp3_offset.dev_attr.attr,
2089 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002090
Guenter Roeckcc18da72015-04-01 10:03:01 -07002091 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
2092 &sensor_dev_attr_temp5_input.dev_attr.attr,
2093 &sensor_dev_attr_temp6_input.dev_attr.attr,
Guenter Roeck87533772013-03-30 14:23:20 -07002094 NULL
Guenter Roeck4573acb2012-03-26 16:17:41 -07002095};
2096
Guenter Roeck87533772013-03-30 14:23:20 -07002097static const struct attribute_group it87_group_temp = {
2098 .attrs = it87_attributes_temp,
2099 .is_visible = it87_temp_is_visible,
Guenter Roeck161d8982012-12-19 22:17:01 +01002100};
2101
Guenter Roeckd3766842013-03-30 15:47:21 -07002102static umode_t it87_is_visible(struct kobject *kobj,
2103 struct attribute *attr, int index)
2104{
2105 struct device *dev = container_of(kobj, struct device, kobj);
2106 struct it87_data *data = dev_get_drvdata(dev);
2107
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002108 if ((index == 2 || index == 3) && !data->has_vid)
Guenter Roeckd3766842013-03-30 15:47:21 -07002109 return 0;
2110
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002111 if (index > 3 && !(data->in_internal & BIT(index - 4)))
Guenter Roeckd3766842013-03-30 15:47:21 -07002112 return 0;
2113
2114 return attr->mode;
2115}
2116
Guenter Roeck4573acb2012-03-26 16:17:41 -07002117static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02002118 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02002119 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002120 &dev_attr_vrm.attr, /* 2 */
2121 &dev_attr_cpu0_vid.attr, /* 3 */
2122 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
Guenter Roeckd3766842013-03-30 15:47:21 -07002123 &sensor_dev_attr_in7_label.dev_attr.attr,
2124 &sensor_dev_attr_in8_label.dev_attr.attr,
2125 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02002126 NULL
2127};
2128
2129static const struct attribute_group it87_group = {
2130 .attrs = it87_attributes,
Guenter Roeckd3766842013-03-30 15:47:21 -07002131 .is_visible = it87_is_visible,
Jean Delvare87808be2006-09-24 21:17:13 +02002132};
2133
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002134static umode_t it87_fan_is_visible(struct kobject *kobj,
2135 struct attribute *attr, int index)
2136{
2137 struct device *dev = container_of(kobj, struct device, kobj);
2138 struct it87_data *data = dev_get_drvdata(dev);
2139 int i = index / 5; /* fan index */
2140 int a = index % 5; /* attribute index */
2141
2142 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
2143 i = (index - 15) / 4 + 3;
2144 a = (index - 15) % 4;
2145 }
2146
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002147 if (!(data->has_fan & BIT(i)))
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002148 return 0;
2149
2150 if (a == 3) { /* beep */
2151 if (!data->has_beep)
2152 return 0;
2153 /* first fan beep attribute is writable */
2154 if (i == __ffs(data->has_fan))
2155 return attr->mode | S_IWUSR;
2156 }
2157
2158 if (a == 4 && has_16bit_fans(data)) /* divisor */
2159 return 0;
2160
2161 return attr->mode;
2162}
2163
2164static struct attribute *it87_attributes_fan[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02002165 &sensor_dev_attr_fan1_input.dev_attr.attr,
2166 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01002167 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002168 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
2169 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
2170
Jean Delvare87808be2006-09-24 21:17:13 +02002171 &sensor_dev_attr_fan2_input.dev_attr.attr,
2172 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01002173 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002174 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2175 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
2176
Jean Delvare87808be2006-09-24 21:17:13 +02002177 &sensor_dev_attr_fan3_input.dev_attr.attr,
2178 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01002179 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002180 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2181 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
2182
2183 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01002184 &sensor_dev_attr_fan4_min.dev_attr.attr,
2185 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002186 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2187
2188 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
Guenter Roecke1169ba2012-12-19 22:17:01 +01002189 &sensor_dev_attr_fan5_min.dev_attr.attr,
2190 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002191 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2192
2193 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002194 &sensor_dev_attr_fan6_min.dev_attr.attr,
2195 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002196 &sensor_dev_attr_fan6_beep.dev_attr.attr,
Guenter Roeckfa3f70d2015-03-25 23:15:32 -07002197 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002198};
2199
Guenter Roeck9a70ee82013-03-30 14:51:20 -07002200static const struct attribute_group it87_group_fan = {
2201 .attrs = it87_attributes_fan,
2202 .is_visible = it87_fan_is_visible,
Guenter Roecke1169ba2012-12-19 22:17:01 +01002203};
Jean Delvare723a0aa2010-03-05 22:17:16 +01002204
Guenter Roeck5c391262013-03-30 15:02:12 -07002205static umode_t it87_pwm_is_visible(struct kobject *kobj,
2206 struct attribute *attr, int index)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002207{
2208 struct device *dev = container_of(kobj, struct device, kobj);
2209 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck5c391262013-03-30 15:02:12 -07002210 int i = index / 4; /* pwm index */
2211 int a = index % 4; /* attribute index */
Guenter Roeck60878bc2015-03-26 19:57:42 -07002212
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002213 if (!(data->has_pwm & BIT(i)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002214 return 0;
2215
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002216 /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2217 if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002218 return attr->mode | S_IWUSR;
2219
2220 /* pwm2_freq is writable if there are two pwm frequency selects */
2221 if (has_pwm_freq2(data) && i == 1 && a == 2)
Guenter Roeck60878bc2015-03-26 19:57:42 -07002222 return attr->mode | S_IWUSR;
2223
2224 return attr->mode;
2225}
2226
Guenter Roeck5c391262013-03-30 15:02:12 -07002227static struct attribute *it87_attributes_pwm[] = {
2228 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2229 &sensor_dev_attr_pwm1.dev_attr.attr,
2230 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2231 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2232
2233 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2234 &sensor_dev_attr_pwm2.dev_attr.attr,
2235 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2236 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2237
2238 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2239 &sensor_dev_attr_pwm3.dev_attr.attr,
2240 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2241 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2242
2243 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2244 &sensor_dev_attr_pwm4.dev_attr.attr,
2245 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2246 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2247
2248 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2249 &sensor_dev_attr_pwm5.dev_attr.attr,
2250 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2251 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2252
2253 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2254 &sensor_dev_attr_pwm6.dev_attr.attr,
2255 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2256 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2257
2258 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01002259};
2260
Guenter Roeck5c391262013-03-30 15:02:12 -07002261static const struct attribute_group it87_group_pwm = {
2262 .attrs = it87_attributes_pwm,
2263 .is_visible = it87_pwm_is_visible,
2264};
2265
2266static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2267 struct attribute *attr, int index)
2268{
2269 struct device *dev = container_of(kobj, struct device, kobj);
2270 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002271 int i = index / 11; /* pwm index */
2272 int a = index % 11; /* attribute index */
2273
2274 if (index >= 33) { /* pwm 4..6 */
2275 i = (index - 33) / 6 + 3;
2276 a = (index - 33) % 6 + 4;
2277 }
Guenter Roeck5c391262013-03-30 15:02:12 -07002278
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002279 if (!(data->has_pwm & BIT(i)))
Guenter Roeck5c391262013-03-30 15:02:12 -07002280 return 0;
2281
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002282 if (has_newer_autopwm(data)) {
2283 if (a < 4) /* no auto point pwm */
2284 return 0;
2285 if (a == 8) /* no auto_point4 */
2286 return 0;
2287 }
2288 if (has_old_autopwm(data)) {
2289 if (a >= 9) /* no pwm_auto_start, pwm_auto_slope */
2290 return 0;
2291 }
2292
Guenter Roeck5c391262013-03-30 15:02:12 -07002293 return attr->mode;
2294}
2295
2296static struct attribute *it87_attributes_auto_pwm[] = {
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002297 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2298 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2299 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2300 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2301 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2302 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2303 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2304 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2305 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002306 &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2307 &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002308
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002309 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, /* 11 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002310 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2311 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2312 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2313 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2314 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2315 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2316 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2317 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002318 &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2319 &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
Guenter Roeck5c391262013-03-30 15:02:12 -07002320
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002321 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, /* 22 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002322 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2323 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2324 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2325 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2326 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2327 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2328 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2329 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07002330 &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2331 &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2332
2333 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, /* 33 */
2334 &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2335 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2336 &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2337 &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2338 &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2339
2340 &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2341 &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2342 &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2343 &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2344 &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2345 &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2346
2347 &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2348 &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2349 &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2350 &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2351 &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2352 &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002353
Guenter Roeck5c391262013-03-30 15:02:12 -07002354 NULL,
2355};
2356
2357static const struct attribute_group it87_group_auto_pwm = {
2358 .attrs = it87_attributes_auto_pwm,
2359 .is_visible = it87_auto_pwm_is_visible,
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002360};
2361
Jean Delvare2d8672c2005-07-19 23:56:35 +02002362/* SuperIO detection - will change isa_address if a chip is found */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002363static int __init it87_find(int sioaddr, unsigned short *address,
2364 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002366 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002367 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002368 const char *board_vendor, *board_name;
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002369 const struct it87_devices *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002371 err = superio_enter(sioaddr);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02002372 if (err)
2373 return err;
2374
2375 err = -ENODEV;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002376 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002377
2378 switch (chip_type) {
2379 case IT8705F_DEVID:
2380 sio_data->type = it87;
2381 break;
2382 case IT8712F_DEVID:
2383 sio_data->type = it8712;
2384 break;
2385 case IT8716F_DEVID:
2386 case IT8726F_DEVID:
2387 sio_data->type = it8716;
2388 break;
2389 case IT8718F_DEVID:
2390 sio_data->type = it8718;
2391 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002392 case IT8720F_DEVID:
2393 sio_data->type = it8720;
2394 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002395 case IT8721F_DEVID:
2396 sio_data->type = it8721;
2397 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01002398 case IT8728F_DEVID:
2399 sio_data->type = it8728;
2400 break;
Justin Maggardead80802015-08-05 12:53:08 -07002401 case IT8732F_DEVID:
2402 sio_data->type = it8732;
2403 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06002404 case IT8771E_DEVID:
2405 sio_data->type = it8771;
2406 break;
2407 case IT8772E_DEVID:
2408 sio_data->type = it8772;
2409 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002410 case IT8781F_DEVID:
2411 sio_data->type = it8781;
2412 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08002413 case IT8782F_DEVID:
2414 sio_data->type = it8782;
2415 break;
2416 case IT8783E_DEVID:
2417 sio_data->type = it8783;
2418 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002419 case IT8786E_DEVID:
2420 sio_data->type = it8786;
2421 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07002422 case IT8790E_DEVID:
2423 sio_data->type = it8790;
2424 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02002425 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02002426 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002427 sio_data->type = it8603;
2428 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002429 case IT8620E_DEVID:
2430 sio_data->type = it8620;
2431 break;
Guenter Roeck8af1aba2017-02-08 13:52:57 -08002432 case IT8622E_DEVID:
2433 sio_data->type = it8622;
2434 break;
Guenter Roeck71a9c232016-01-18 00:35:58 -08002435 case IT8628E_DEVID:
2436 sio_data->type = it8628;
2437 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002438 case 0xffff: /* No device at all */
2439 goto exit;
2440 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01002441 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002442 goto exit;
2443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002445 superio_select(sioaddr, PME);
2446 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002447 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 goto exit;
2449 }
2450
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002451 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002453 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto exit;
2455 }
2456
2457 err = 0;
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002458 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07002459 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2460 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01002461 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002463 config = &it87_devices[sio_data->type];
2464
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002465 /* in7 (VSB or VCCH5V) is always internal on some chips */
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002466 if (has_in7_internal(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002467 sio_data->internal |= BIT(1);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002468
Jean Delvare738e5e02010-08-14 21:08:50 +02002469 /* in8 (Vbat) is always internal */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002470 sio_data->internal |= BIT(2);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002471
Guenter Roeck73055402015-03-26 09:16:32 -07002472 /* in9 (AVCC3), always internal if supported */
2473 if (has_avcc3(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002474 sio_data->internal |= BIT(3); /* in9 is AVCC */
Guenter Roeck73055402015-03-26 09:16:32 -07002475 else
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002476 sio_data->skip_in |= BIT(9);
Jean Delvare738e5e02010-08-14 21:08:50 +02002477
Guenter Roeck36c4d982015-03-26 18:18:19 -07002478 if (!has_six_pwm(config))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002479 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002480
Guenter Roeckf83a9cb2015-03-31 09:31:34 -07002481 if (!has_vid(config))
Jean Delvare895ff262009-12-09 20:35:47 +01002482 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01002483
Guenter Roeck32dd7c402015-02-12 07:40:23 -08002484 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2485 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01002486 /* The IT8705F has a different LD number for GPIO */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002487 superio_select(sioaddr, 5);
2488 sio_data->beep_pin = superio_inb(sioaddr,
2489 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08002490 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07002491 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08002492
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002493 superio_select(sioaddr, GPIO);
Guenter Roeck0531d982012-03-02 11:46:44 -08002494
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002495 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2496 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2497 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2498 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2499 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08002500
Guenter Roeck0531d982012-03-02 11:46:44 -08002501 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002502 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
2503 sio_data->skip_fan |= BIT(2);
Guenter Roeckc9620242015-04-04 09:05:57 -07002504 if ((reg25 & BIT(4)) ||
2505 (!(reg2a & BIT(1)) && (regef & BIT(0))))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002506 sio_data->skip_pwm |= BIT(2);
Guenter Roeck0531d982012-03-02 11:46:44 -08002507
2508 /* Check if fan2 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002509 if (reg27 & BIT(7))
2510 sio_data->skip_fan |= BIT(1);
2511 if (reg27 & BIT(3))
2512 sio_data->skip_pwm |= BIT(1);
Guenter Roeck0531d982012-03-02 11:46:44 -08002513
2514 /* VIN5 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002515 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
2516 sio_data->skip_in |= BIT(5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002517
2518 /* VIN6 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002519 if (reg27 & BIT(1))
2520 sio_data->skip_in |= BIT(6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08002521
2522 /*
2523 * VIN7
2524 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2525 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002526 if (reg27 & BIT(2)) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002527 /*
2528 * The data sheet is a bit unclear regarding the
2529 * internal voltage divider for VCCH5V. It says
2530 * "This bit enables and switches VIN7 (pin 91) to the
2531 * internal voltage divider for VCCH5V".
2532 * This is different to other chips, where the internal
2533 * voltage divider would connect VIN7 to an internal
2534 * voltage source. Maybe that is the case here as well.
2535 *
2536 * Since we don't know for sure, re-route it if that is
2537 * not the case, and ask the user to report if the
2538 * resulting voltage is sane.
2539 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002540 if (!(reg2c & BIT(1))) {
2541 reg2c |= BIT(1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002542 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2543 reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002544 pr_notice("Routing internal VCCH5V to in7.\n");
2545 }
2546 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2547 pr_notice("Please report if it displays a reasonable voltage.\n");
2548 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002549
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002550 if (reg2c & BIT(0))
2551 sio_data->internal |= BIT(0);
2552 if (reg2c & BIT(1))
2553 sio_data->internal |= BIT(1);
Guenter Roeck0531d982012-03-02 11:46:44 -08002554
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002555 sio_data->beep_pin = superio_inb(sioaddr,
2556 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002557 } else if (sio_data->type == it8603) {
2558 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08002559
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002560 superio_select(sioaddr, GPIO);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002561
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002562 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002563
2564 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002565 if (reg27 & BIT(6))
2566 sio_data->skip_pwm |= BIT(2);
2567 if (reg27 & BIT(7))
2568 sio_data->skip_fan |= BIT(2);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002569
2570 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002571 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002572 if (reg29 & BIT(1))
2573 sio_data->skip_pwm |= BIT(1);
2574 if (reg29 & BIT(2))
2575 sio_data->skip_fan |= BIT(1);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002576
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002577 sio_data->skip_in |= BIT(5); /* No VIN5 */
2578 sio_data->skip_in |= BIT(6); /* No VIN6 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002579
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002580 sio_data->beep_pin = superio_inb(sioaddr,
2581 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck71a9c232016-01-18 00:35:58 -08002582 } else if (sio_data->type == it8620 || sio_data->type == it8628) {
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002583 int reg;
2584
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002585 superio_select(sioaddr, GPIO);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002586
Guenter Roeck36c4d982015-03-26 18:18:19 -07002587 /* Check for pwm5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002588 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002589 if (reg & BIT(6))
2590 sio_data->skip_pwm |= BIT(4);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002591
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002592 /* Check for fan4, fan5 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002593 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002594 if (!(reg & BIT(5)))
2595 sio_data->skip_fan |= BIT(3);
2596 if (!(reg & BIT(4)))
2597 sio_data->skip_fan |= BIT(4);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002598
2599 /* Check for pwm3, fan3 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002600 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002601 if (reg & BIT(6))
2602 sio_data->skip_pwm |= BIT(2);
2603 if (reg & BIT(7))
2604 sio_data->skip_fan |= BIT(2);
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002605
Guenter Roeck36c4d982015-03-26 18:18:19 -07002606 /* Check for pwm4 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002607 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002608 if (!(reg & BIT(2)))
2609 sio_data->skip_pwm |= BIT(3);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002610
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002611 /* Check for pwm2, fan2 */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002612 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002613 if (reg & BIT(1))
2614 sio_data->skip_pwm |= BIT(1);
2615 if (reg & BIT(2))
2616 sio_data->skip_fan |= BIT(1);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002617 /* Check for pwm6, fan6 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002618 if (!(reg & BIT(7))) {
2619 sio_data->skip_pwm |= BIT(5);
2620 sio_data->skip_fan |= BIT(5);
Guenter Roeck36c4d982015-03-26 18:18:19 -07002621 }
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002622
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002623 sio_data->beep_pin = superio_inb(sioaddr,
2624 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002625 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002626 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002627 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002628
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002629 superio_select(sioaddr, GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002630
Guenter Roecka0df9262015-04-05 16:51:36 -07002631 /* Check for fan4, fan5 */
2632 if (has_five_fans(config)) {
2633 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2634 switch (sio_data->type) {
2635 case it8718:
2636 if (reg & BIT(5))
2637 sio_data->skip_fan |= BIT(3);
2638 if (reg & BIT(4))
2639 sio_data->skip_fan |= BIT(4);
2640 break;
2641 case it8720:
2642 case it8721:
2643 case it8728:
2644 if (!(reg & BIT(5)))
2645 sio_data->skip_fan |= BIT(3);
2646 if (!(reg & BIT(4)))
2647 sio_data->skip_fan |= BIT(4);
2648 break;
2649 default:
2650 break;
2651 }
2652 }
2653
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002654 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c402015-02-12 07:40:23 -08002655 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002656 /* We need at least 4 VID pins */
2657 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002658 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002659 sio_data->skip_vid = 1;
2660 }
Jean Delvare895ff262009-12-09 20:35:47 +01002661 }
2662
Jean Delvare591ec652009-12-09 20:35:48 +01002663 /* Check if fan3 is there or not */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002664 if (reg & BIT(6))
2665 sio_data->skip_pwm |= BIT(2);
2666 if (reg & BIT(7))
2667 sio_data->skip_fan |= BIT(2);
Jean Delvare591ec652009-12-09 20:35:48 +01002668
2669 /* Check if fan2 is there or not */
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002670 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002671 if (reg & BIT(1))
2672 sio_data->skip_pwm |= BIT(1);
2673 if (reg & BIT(2))
2674 sio_data->skip_fan |= BIT(1);
Jean Delvare591ec652009-12-09 20:35:48 +01002675
Guenter Roeckc9620242015-04-04 09:05:57 -07002676 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
2677 !(sio_data->skip_vid))
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002678 sio_data->vid_value = superio_inb(sioaddr,
2679 IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002680
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002681 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002682
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002683 uart6 = sio_data->type == it8782 && (reg & BIT(2));
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002684
Jean Delvare436cad22010-07-09 16:22:48 +02002685 /*
2686 * The IT8720F has no VIN7 pin, so VCCH should always be
2687 * routed internally to VIN7 with an internal divider.
2688 * Curiously, there still is a configuration bit to control
2689 * this, which means it can be set incorrectly. And even
2690 * more curiously, many boards out there are improperly
2691 * configured, even though the IT8720F datasheet claims
2692 * that the internal routing of VCCH to VIN7 is the default
2693 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002694 *
2695 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002696 * If UART6 is enabled, re-route VIN7 to the internal divider
2697 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002698 */
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002699 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
2700 reg |= BIT(1);
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002701 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002702 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002703 }
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002704 if (reg & BIT(0))
2705 sio_data->internal |= BIT(0);
2706 if (reg & BIT(1))
2707 sio_data->internal |= BIT(1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002708
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002709 /*
2710 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2711 * While VIN7 can be routed to the internal voltage divider,
2712 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002713 *
2714 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2715 * is the temperature source. Since we can not read the
2716 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002717 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002718 if (uart6) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002719 sio_data->skip_in |= BIT(5) | BIT(6);
2720 sio_data->skip_temp |= BIT(2);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002721 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002722
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002723 sio_data->beep_pin = superio_inb(sioaddr,
2724 IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002725 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002726 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002727 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002728
Jean Delvare98dd22c2008-10-09 15:33:58 +02002729 /* Disable specific features based on DMI strings */
2730 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2731 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2732 if (board_vendor && board_name) {
Guenter Roeckc9620242015-04-04 09:05:57 -07002733 if (strcmp(board_vendor, "nVIDIA") == 0 &&
2734 strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002735 /*
2736 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2737 * connected to a fan, but to something else. One user
2738 * has reported instant system power-off when changing
2739 * the PWM2 duty cycle, so we disable it.
2740 * I use the board name string as the trigger in case
2741 * the same board is ever used in other systems.
2742 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002743 pr_info("Disabling pwm2 due to hardware constraints\n");
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002744 sio_data->skip_pwm = BIT(1);
Jean Delvare98dd22c2008-10-09 15:33:58 +02002745 }
2746 }
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748exit:
Guenter Roeck3c2e3512015-03-28 08:03:10 -07002749 superio_exit(sioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return err;
2751}
2752
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002753/* Called when we have found a new IT87. */
2754static void it87_init_device(struct platform_device *pdev)
2755{
2756 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2757 struct it87_data *data = platform_get_drvdata(pdev);
2758 int tmp, i;
2759 u8 mask;
2760
2761 /*
2762 * For each PWM channel:
2763 * - If it is in automatic mode, setting to manual mode should set
2764 * the fan to full speed by default.
2765 * - If it is in manual mode, we need a mapping to temperature
2766 * channels to use when later setting to automatic mode later.
2767 * Use a 1:1 mapping by default (we are clueless.)
2768 * In both cases, the value can (and should) be changed by the user
2769 * prior to switching to a different mode.
2770 * Note that this is no longer needed for the IT8721F and later, as
2771 * these have separate registers for the temperature mapping and the
2772 * manual duty cycle.
2773 */
Guenter Roeck23100482015-04-02 08:23:45 -07002774 for (i = 0; i < NUM_AUTO_PWM; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002775 data->pwm_temp_map[i] = i;
2776 data->pwm_duty[i] = 0x7f; /* Full speed */
2777 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2778 }
2779
2780 /*
2781 * Some chips seem to have default value 0xff for all limit
2782 * registers. For low voltage limits it makes no sense and triggers
2783 * alarms, so change to 0 instead. For high temperature limits, it
2784 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2785 * but is still confusing, so change to 127 degrees C.
2786 */
Guenter Roeck23100482015-04-02 08:23:45 -07002787 for (i = 0; i < NUM_VIN_LIMIT; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002788 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2789 if (tmp == 0xff)
2790 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2791 }
Guenter Roeck23100482015-04-02 08:23:45 -07002792 for (i = 0; i < NUM_TEMP_LIMIT; i++) {
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002793 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2794 if (tmp == 0xff)
2795 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2796 }
2797
2798 /*
2799 * Temperature channels are not forcibly enabled, as they can be
2800 * set to two different sensor types and we can't guess which one
2801 * is correct for a given system. These channels can be enabled at
2802 * run-time through the temp{1-3}_type sysfs accessors if needed.
2803 */
2804
2805 /* Check if voltage monitors are reset manually or by some reason */
2806 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2807 if ((tmp & 0xff) == 0) {
2808 /* Enable all voltage monitors */
2809 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2810 }
2811
2812 /* Check if tachometers are reset manually or by some reason */
2813 mask = 0x70 & ~(sio_data->skip_fan << 4);
2814 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2815 if ((data->fan_main_ctrl & mask) == 0) {
2816 /* Enable all fan tachometers */
2817 data->fan_main_ctrl |= mask;
2818 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2819 data->fan_main_ctrl);
2820 }
2821 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2822
2823 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2824
2825 /* Set tachometers to 16-bit mode if needed */
2826 if (has_fan16_config(data)) {
2827 if (~tmp & 0x07 & data->has_fan) {
2828 dev_dbg(&pdev->dev,
2829 "Setting fan1-3 to 16-bit mode\n");
2830 it87_write_value(data, IT87_REG_FAN_16BIT,
2831 tmp | 0x07);
2832 }
2833 }
2834
2835 /* Check for additional fans */
2836 if (has_five_fans(data)) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002837 if (tmp & BIT(4))
2838 data->has_fan |= BIT(3); /* fan4 enabled */
2839 if (tmp & BIT(5))
2840 data->has_fan |= BIT(4); /* fan5 enabled */
2841 if (has_six_fans(data) && (tmp & BIT(2)))
2842 data->has_fan |= BIT(5); /* fan6 enabled */
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002843 }
2844
2845 /* Fan input pins may be used for alternative functions */
2846 data->has_fan &= ~sio_data->skip_fan;
2847
2848 /* Check if pwm5, pwm6 are enabled */
2849 if (has_six_pwm(data)) {
2850 /* The following code may be IT8620E specific */
2851 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2852 if ((tmp & 0xc0) == 0xc0)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002853 sio_data->skip_pwm |= BIT(4);
2854 if (!(tmp & BIT(3)))
2855 sio_data->skip_pwm |= BIT(5);
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002856 }
2857
2858 /* Start monitoring */
2859 it87_write_value(data, IT87_REG_CONFIG,
2860 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2861 | (update_vbat ? 0x41 : 0x01));
2862}
2863
2864/* Return 1 if and only if the PWM interface is safe to use */
2865static int it87_check_pwm(struct device *dev)
2866{
2867 struct it87_data *data = dev_get_drvdata(dev);
2868 /*
2869 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2870 * and polarity set to active low is sign that this is the case so we
2871 * disable pwm control to protect the user.
2872 */
2873 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2874
2875 if ((tmp & 0x87) == 0) {
2876 if (fix_pwm_polarity) {
2877 /*
2878 * The user asks us to attempt a chip reconfiguration.
2879 * This means switching to active high polarity and
2880 * inverting all fan speed values.
2881 */
2882 int i;
2883 u8 pwm[3];
2884
Guenter Roeck23100482015-04-02 08:23:45 -07002885 for (i = 0; i < ARRAY_SIZE(pwm); i++)
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07002886 pwm[i] = it87_read_value(data,
2887 IT87_REG_PWM[i]);
2888
2889 /*
2890 * If any fan is in automatic pwm mode, the polarity
2891 * might be correct, as suspicious as it seems, so we
2892 * better don't change anything (but still disable the
2893 * PWM interface).
2894 */
2895 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2896 dev_info(dev,
2897 "Reconfiguring PWM to active high polarity\n");
2898 it87_write_value(data, IT87_REG_FAN_CTL,
2899 tmp | 0x87);
2900 for (i = 0; i < 3; i++)
2901 it87_write_value(data,
2902 IT87_REG_PWM[i],
2903 0x7f & ~pwm[i]);
2904 return 1;
2905 }
2906
2907 dev_info(dev,
2908 "PWM configuration is too broken to be fixed\n");
2909 }
2910
2911 dev_info(dev,
2912 "Detected broken BIOS defaults, disabling PWM interface\n");
2913 return 0;
2914 } else if (fix_pwm_polarity) {
2915 dev_info(dev,
2916 "PWM configuration looks sane, won't touch\n");
2917 }
2918
2919 return 1;
2920}
2921
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002922static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002925 struct resource *res;
2926 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002927 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 int enable_pwm_interface;
Guenter Roeck8638d0a2015-03-30 11:13:49 -07002929 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002931 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002932 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2933 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002934 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2935 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002936 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002937 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Guenter Roeck62a1d052012-03-24 21:54:41 -07002940 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2941 if (!data)
2942 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002944 data->addr = res->start;
2945 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002946 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002947 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002948 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002949 /*
2950 * IT8705F Datasheet 0.4.1, 3h == Version G.
2951 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2952 * These are the first revisions with 16-bit tachometer support.
2953 */
2954 switch (data->type) {
2955 case it87:
2956 if (sio_data->revision >= 0x03) {
2957 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002958 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002959 }
2960 break;
2961 case it8712:
2962 if (sio_data->revision >= 0x08) {
2963 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002964 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2965 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002966 }
2967 break;
2968 default:
2969 break;
2970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 /* Now, we do the remaining detection. */
Guenter Roeckc9620242015-04-04 09:05:57 -07002973 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) ||
2974 it87_read_value(data, IT87_REG_CHIPID) != 0x90)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002975 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002977 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002979 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002982 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002984 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002985 if (has_12mv_adc(data)) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002986 if (sio_data->internal & BIT(0))
2987 data->in_scaled |= BIT(3); /* in3 is AVCC */
2988 if (sio_data->internal & BIT(1))
2989 data->in_scaled |= BIT(7); /* in7 is VSB */
2990 if (sio_data->internal & BIT(2))
2991 data->in_scaled |= BIT(8); /* in8 is Vbat */
2992 if (sio_data->internal & BIT(3))
2993 data->in_scaled |= BIT(9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002994 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2995 sio_data->type == it8783) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07002996 if (sio_data->internal & BIT(0))
2997 data->in_scaled |= BIT(3); /* in3 is VCC5V */
2998 if (sio_data->internal & BIT(1))
2999 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02003000 }
3001
Guenter Roeck4573acb2012-03-26 16:17:41 -07003002 data->has_temp = 0x07;
Guenter Roeck48b2ae72015-04-02 08:06:12 -07003003 if (sio_data->skip_temp & BIT(2)) {
Guenter Roeckc9620242015-04-04 09:05:57 -07003004 if (sio_data->type == it8782 &&
3005 !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
Guenter Roeck48b2ae72015-04-02 08:06:12 -07003006 data->has_temp &= ~BIT(2);
Guenter Roeck4573acb2012-03-26 16:17:41 -07003007 }
3008
Guenter Roeckd3766842013-03-30 15:47:21 -07003009 data->in_internal = sio_data->internal;
Guenter Roeck52929712013-03-30 14:00:08 -07003010 data->has_in = 0x3ff & ~sio_data->skip_in;
3011
Guenter Roeckcc18da72015-04-01 10:03:01 -07003012 if (has_six_temp(data)) {
3013 u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
3014
Guenter Roeckf838aa22015-04-01 20:09:36 -07003015 /* Check for additional temperature sensors */
Guenter Roeckcc18da72015-04-01 10:03:01 -07003016 if ((reg & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07003017 data->has_temp |= BIT(3);
Guenter Roeckcc18da72015-04-01 10:03:01 -07003018 if (((reg >> 2) & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07003019 data->has_temp |= BIT(4);
Guenter Roeckcc18da72015-04-01 10:03:01 -07003020 if (((reg >> 4) & 0x03) >= 0x02)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07003021 data->has_temp |= BIT(5);
Guenter Roeckf838aa22015-04-01 20:09:36 -07003022
3023 /* Check for additional voltage sensors */
3024 if ((reg & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07003025 data->has_in |= BIT(10);
Guenter Roeckf838aa22015-04-01 20:09:36 -07003026 if (((reg >> 2) & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07003027 data->has_in |= BIT(11);
Guenter Roeckf838aa22015-04-01 20:09:36 -07003028 if (((reg >> 4) & 0x03) == 0x01)
Guenter Roeck48b2ae72015-04-02 08:06:12 -07003029 data->has_in |= BIT(12);
Guenter Roeckcc18da72015-04-01 10:03:01 -07003030 }
3031
Guenter Roeck52929712013-03-30 14:00:08 -07003032 data->has_beep = !!sio_data->beep_pin;
3033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003035 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Guenter Roeckd3766842013-03-30 15:47:21 -07003037 if (!sio_data->skip_vid) {
3038 data->has_vid = true;
3039 data->vrm = vid_which_vrm();
3040 /* VID reading from Super-I/O config space if available */
3041 data->vid = sio_data->vid_value;
3042 }
3043
Guenter Roeck8638d0a2015-03-30 11:13:49 -07003044 /* Prepare for sysfs hooks */
3045 data->groups[0] = &it87_group;
3046 data->groups[1] = &it87_group_in;
3047 data->groups[2] = &it87_group_temp;
3048 data->groups[3] = &it87_group_fan;
Jean Delvare17d648b2006-08-28 14:23:46 +02003049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 if (enable_pwm_interface) {
Guenter Roeck48b2ae72015-04-02 08:06:12 -07003051 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
Guenter Roeck5c391262013-03-30 15:02:12 -07003052 data->has_pwm &= ~sio_data->skip_pwm;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01003053
Guenter Roeck8638d0a2015-03-30 11:13:49 -07003054 data->groups[4] = &it87_group_pwm;
Guenter Roeck2cbb9c32015-04-05 11:53:29 -07003055 if (has_old_autopwm(data) || has_newer_autopwm(data))
Guenter Roeck8638d0a2015-03-30 11:13:49 -07003056 data->groups[5] = &it87_group_auto_pwm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
3058
Guenter Roeck8638d0a2015-03-30 11:13:49 -07003059 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
3060 it87_devices[sio_data->type].name,
3061 data, data->groups);
3062 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07003065static struct platform_driver it87_driver = {
3066 .driver = {
3067 .name = DRVNAME,
3068 },
3069 .probe = it87_probe,
Guenter Roeckc1e7a4c2015-03-28 09:27:27 -07003070};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Guenter Roecke84bd952015-03-28 08:24:29 -07003072static int __init it87_device_add(int index, unsigned short address,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003073 const struct it87_sio_data *sio_data)
3074{
Guenter Roeck8e50e3c2015-03-28 07:49:14 -07003075 struct platform_device *pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003076 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05003077 .start = address + IT87_EC_OFFSET,
3078 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003079 .name = DRVNAME,
3080 .flags = IORESOURCE_IO,
3081 };
3082 int err;
3083
Jean Delvareb9acb642009-01-07 16:37:35 +01003084 err = acpi_check_resource_conflict(&res);
3085 if (err)
Guenter Roeck5cae84a52015-03-28 07:44:59 -07003086 return err;
Jean Delvareb9acb642009-01-07 16:37:35 +01003087
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003088 pdev = platform_device_alloc(DRVNAME, address);
Guenter Roeck5cae84a52015-03-28 07:44:59 -07003089 if (!pdev)
3090 return -ENOMEM;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003091
3092 err = platform_device_add_resources(pdev, &res, 1);
3093 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01003094 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003095 goto exit_device_put;
3096 }
3097
3098 err = platform_device_add_data(pdev, sio_data,
3099 sizeof(struct it87_sio_data));
3100 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01003101 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003102 goto exit_device_put;
3103 }
3104
3105 err = platform_device_add(pdev);
3106 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01003107 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003108 goto exit_device_put;
3109 }
3110
Guenter Roecke84bd952015-03-28 08:24:29 -07003111 it87_pdev[index] = pdev;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003112 return 0;
3113
3114exit_device_put:
3115 platform_device_put(pdev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003116 return err;
3117}
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119static int __init sm_it87_init(void)
3120{
Guenter Roecke84bd952015-03-28 08:24:29 -07003121 int sioaddr[2] = { REG_2E, REG_4E };
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003122 struct it87_sio_data sio_data;
Guenter Roecke84bd952015-03-28 08:24:29 -07003123 unsigned short isa_address;
3124 bool found = false;
3125 int i, err;
Jean Delvarefde09502005-07-19 23:51:07 +02003126
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003127 err = platform_driver_register(&it87_driver);
3128 if (err)
3129 return err;
3130
Guenter Roecke84bd952015-03-28 08:24:29 -07003131 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
3132 memset(&sio_data, 0, sizeof(struct it87_sio_data));
3133 isa_address = 0;
3134 err = it87_find(sioaddr[i], &isa_address, &sio_data);
3135 if (err || isa_address == 0)
3136 continue;
3137
3138 err = it87_device_add(i, isa_address, &sio_data);
3139 if (err)
3140 goto exit_dev_unregister;
3141 found = true;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003142 }
3143
Guenter Roecke84bd952015-03-28 08:24:29 -07003144 if (!found) {
3145 err = -ENODEV;
3146 goto exit_unregister;
3147 }
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003148 return 0;
Guenter Roecke84bd952015-03-28 08:24:29 -07003149
3150exit_dev_unregister:
3151 /* NULL check handled by platform_device_unregister */
3152 platform_device_unregister(it87_pdev[0]);
3153exit_unregister:
3154 platform_driver_unregister(&it87_driver);
3155 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
3158static void __exit sm_it87_exit(void)
3159{
Guenter Roecke84bd952015-03-28 08:24:29 -07003160 /* NULL check handled by platform_device_unregister */
3161 platform_device_unregister(it87_pdev[1]);
3162 platform_device_unregister(it87_pdev[0]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02003163 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
Jean Delvare7c81c60f2014-01-29 20:40:08 +01003166MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02003167MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168module_param(update_vbat, bool, 0);
3169MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
3170module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01003171MODULE_PARM_DESC(fix_pwm_polarity,
3172 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173MODULE_LICENSE("GPL");
3174
3175module_init(sm_it87_init);
3176module_exit(sm_it87_exit);