blob: 4e4db72d38e822bf4b9c4fa8a3d214750427a383 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jean Delvare5f2dc792010-03-05 22:17:18 +01002 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring.
4 *
5 * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 * addition to an Environment Controller (Enhanced Hardware Monitor and
8 * Fan Controller)
9 *
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
12 *
Rudolf Marekc145d5c2014-01-29 20:40:08 +010013 * Supports: IT8603E Super I/O chip w/LPC interface
Guenter Roeck3ba9d972015-02-13 20:13:20 -080014 * IT8620E Super I/O chip w/LPC interface
Rudolf Marek574e9bd2014-04-04 18:01:35 +020015 * IT8623E Super I/O chip w/LPC interface
Rudolf Marekc145d5c2014-01-29 20:40:08 +010016 * IT8705F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010017 * IT8712F Super I/O chip w/LPC interface
18 * IT8716F Super I/O chip w/LPC interface
19 * IT8718F Super I/O chip w/LPC interface
20 * IT8720F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020021 * IT8721F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010022 * IT8726F Super I/O chip w/LPC interface
Jean Delvare16b5dda2012-01-16 22:51:48 +010023 * IT8728F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020024 * IT8758E Super I/O chip w/LPC interface
Guenter Roeckb0636702012-09-07 17:34:41 -060025 * IT8771E Super I/O chip w/LPC interface
26 * IT8772E Super I/O chip w/LPC interface
Guenter Roeck7bc32d22015-01-17 14:10:24 -080027 * IT8781F Super I/O chip w/LPC interface
Guenter Roeck0531d982012-03-02 11:46:44 -080028 * IT8782F Super I/O chip w/LPC interface
29 * IT8783E/F Super I/O chip w/LPC interface
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +010030 * IT8786E Super I/O chip w/LPC interface
Guenter Roeck4ee07152015-03-25 23:26:28 -070031 * IT8790E Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010032 * Sis950 A clone of the IT8705F
33 *
34 * Copyright (C) 2001 Chris Gauthron
Jean Delvare7c81c602014-01-29 20:40:08 +010035 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
Jean Delvare5f2dc792010-03-05 22:17:18 +010036 *
37 * This program is free software; you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation; either version 2 of the License, or
40 * (at your option) any later version.
41 *
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
46 *
47 * You should have received a copy of the GNU General Public License
48 * along with this program; if not, write to the Free Software
49 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
50 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Joe Perchesa8ca1032011-01-12 21:55:10 +010052#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
53
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
Guenter Roeckb0636702012-09-07 17:34:41 -060072enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8771,
Guenter Roeck3ba9d972015-02-13 20:13:20 -080073 it8772, it8781, it8782, it8783, it8786, it8790, it8603, it8620 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Jean Delvare67b671b2007-12-06 23:13:42 +010075static unsigned short force_id;
76module_param(force_id, ushort, 0);
77MODULE_PARM_DESC(force_id, "Override the detected device ID");
78
corentin.labbeb74f3fd2007-06-13 20:27:36 +020079static struct platform_device *pdev;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define REG 0x2e /* The register to read/write */
82#define DEV 0x07 /* Register: Logical device select */
83#define VAL 0x2f /* The value to read/write */
84#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010085
86/* The device with the IT8718F/IT8720F VID value in it */
87#define GPIO 0x07
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define DEVID 0x20 /* Register: Device ID */
90#define DEVREV 0x22 /* Register: Device Revision */
91
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020092static inline int superio_inb(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 outb(reg, REG);
95 return inb(VAL);
96}
97
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020098static inline void superio_outb(int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +020099{
100 outb(reg, REG);
101 outb(val, VAL);
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static int superio_inw(int reg)
105{
106 int val;
107 outb(reg++, REG);
108 val = inb(VAL) << 8;
109 outb(reg, REG);
110 val |= inb(VAL);
111 return val;
112}
113
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200114static inline void superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200117 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200120static inline int superio_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200122 /*
123 * Try to reserve REG and REG + 1 for exclusive access.
124 */
125 if (!request_muxed_region(REG, 2, DRVNAME))
126 return -EBUSY;
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 outb(0x87, REG);
129 outb(0x01, REG);
130 outb(0x55, REG);
131 outb(0x55, REG);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200135static inline void superio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 outb(0x02, REG);
138 outb(0x02, VAL);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200139 release_region(REG, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Jean Delvare87673dd2006-08-28 14:37:19 +0200142/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#define IT8712F_DEVID 0x8712
144#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200145#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200146#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100147#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200148#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400149#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100150#define IT8728F_DEVID 0x8728
Guenter Roeckb0636702012-09-07 17:34:41 -0600151#define IT8771E_DEVID 0x8771
152#define IT8772E_DEVID 0x8772
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800153#define IT8781F_DEVID 0x8781
Guenter Roeck0531d982012-03-02 11:46:44 -0800154#define IT8782F_DEVID 0x8782
155#define IT8783E_DEVID 0x8783
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100156#define IT8786E_DEVID 0x8786
Guenter Roeck4ee07152015-03-25 23:26:28 -0700157#define IT8790E_DEVID 0x8790
Rudolf Marek7183ae82014-04-04 18:01:35 +0200158#define IT8603E_DEVID 0x8603
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800159#define IT8620E_DEVID 0x8620
Rudolf Marek574e9bd2014-04-04 18:01:35 +0200160#define IT8623E_DEVID 0x8623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define IT87_ACT_REG 0x30
162#define IT87_BASE_REG 0x60
163
Jean Delvare87673dd2006-08-28 14:37:19 +0200164/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800165#define IT87_SIO_GPIO1_REG 0x25
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800166#define IT87_SIO_GPIO2_REG 0x26
Jean Delvare895ff262009-12-09 20:35:47 +0100167#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100168#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800169#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200170#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800171#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200172#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100173#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030176static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030179static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* Many IT87 constants specified below */
182
183/* Length of ISA address segment */
184#define IT87_EXTENT 8
185
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500186/* Length of ISA address segment for Environmental Controller */
187#define IT87_EC_EXTENT 2
188
189/* Offset of EC registers from ISA base address */
190#define IT87_EC_OFFSET 5
191
192/* Where are the ISA address/data registers relative to the EC base address */
193#define IT87_ADDR_REG_OFFSET 0
194#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196/*----- The IT87 registers -----*/
197
198#define IT87_REG_CONFIG 0x00
199
200#define IT87_REG_ALARM1 0x01
201#define IT87_REG_ALARM2 0x02
202#define IT87_REG_ALARM3 0x03
203
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800204/*
205 * The IT8718F and IT8720F have the VID value in a different register, in
206 * Super-I/O configuration space.
207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800209/*
210 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
211 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
212 * mode.
213 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200215#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
218
Jean Delvarec7f1f712007-09-03 17:11:46 +0200219static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
220static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
221static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
222static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100223static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#define IT87_REG_FAN_MAIN_CTRL 0x13
226#define IT87_REG_FAN_CTL 0x14
227#define IT87_REG_PWM(nr) (0x15 + (nr))
Jean Delvare6229cdb2010-12-08 16:27:22 +0100228#define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230#define IT87_REG_VIN(nr) (0x20 + (nr))
231#define IT87_REG_TEMP(nr) (0x29 + (nr))
232
233#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
234#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
235#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
236#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#define IT87_REG_VIN_ENABLE 0x50
239#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700240#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100241#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243#define IT87_REG_CHIPID 0x58
244
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100245#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
246#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
247
Guenter Roeck483db432012-12-19 22:17:02 +0100248struct it87_devices {
249 const char *name;
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700250 const char * const suffix;
Guenter Roeck483db432012-12-19 22:17:02 +0100251 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100252 u8 peci_mask;
253 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100254};
255
256#define FEAT_12MV_ADC (1 << 0)
257#define FEAT_NEWER_AUTOPWM (1 << 1)
258#define FEAT_OLD_AUTOPWM (1 << 2)
259#define FEAT_16BIT_FANS (1 << 3)
260#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100261#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100262#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800263#define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
264#define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800265#define FEAT_VID (1 << 9) /* Set if chip supports VID */
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700266#define FEAT_IN7_INTERNAL (1 << 10) /* Set if in7 is internal */
Guenter Roeck483db432012-12-19 22:17:02 +0100267
268static const struct it87_devices it87_devices[] = {
269 [it87] = {
270 .name = "it87",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700271 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100272 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
273 },
274 [it8712] = {
275 .name = "it8712",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700276 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800277 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
278 /* may need to overwrite */
Guenter Roeck483db432012-12-19 22:17:02 +0100279 },
280 [it8716] = {
281 .name = "it8716",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700282 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800283 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800284 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck483db432012-12-19 22:17:02 +0100285 },
286 [it8718] = {
287 .name = "it8718",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700288 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800289 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800290 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100291 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100292 },
293 [it8720] = {
294 .name = "it8720",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700295 .suffix = "F",
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800296 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800297 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
Guenter Roeck19529782012-12-19 22:17:02 +0100298 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100299 },
300 [it8721] = {
301 .name = "it8721",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700302 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100303 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800304 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700305 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100306 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100307 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100308 },
309 [it8728] = {
310 .name = "it8728",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700311 .suffix = "F",
Guenter Roeck483db432012-12-19 22:17:02 +0100312 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700313 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
314 | FEAT_IN7_INTERNAL,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100315 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100316 },
Guenter Roeckb0636702012-09-07 17:34:41 -0600317 [it8771] = {
318 .name = "it8771",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700319 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600320 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700321 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800322 /* PECI: guesswork */
323 /* 12mV ADC (OHM) */
324 /* 16 bit fans (OHM) */
325 /* three fans, always 16 bit (guesswork) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600326 .peci_mask = 0x07,
327 },
328 [it8772] = {
329 .name = "it8772",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700330 .suffix = "E",
Guenter Roeckb0636702012-09-07 17:34:41 -0600331 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700332 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800333 /* PECI (coreboot) */
334 /* 12mV ADC (HWSensors4, OHM) */
335 /* 16 bit fans (HWSensors4, OHM) */
336 /* three fans, always 16 bit (datasheet) */
Guenter Roeckb0636702012-09-07 17:34:41 -0600337 .peci_mask = 0x07,
338 },
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800339 [it8781] = {
340 .name = "it8781",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700341 .suffix = "F",
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800342 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800343 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck7bc32d22015-01-17 14:10:24 -0800344 .old_peci_mask = 0x4,
345 },
Guenter Roeck483db432012-12-19 22:17:02 +0100346 [it8782] = {
347 .name = "it8782",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700348 .suffix = "F",
Guenter Roeck19529782012-12-19 22:17:02 +0100349 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800350 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100351 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100352 },
353 [it8783] = {
354 .name = "it8783",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700355 .suffix = "E/F",
Guenter Roeck19529782012-12-19 22:17:02 +0100356 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800357 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
Guenter Roeck19529782012-12-19 22:17:02 +0100358 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100359 },
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100360 [it8786] = {
361 .name = "it8786",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700362 .suffix = "E",
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100363 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700364 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +0100365 .peci_mask = 0x07,
366 },
Guenter Roeck4ee07152015-03-25 23:26:28 -0700367 [it8790] = {
368 .name = "it8790",
369 .suffix = "E",
370 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
371 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
372 .peci_mask = 0x07,
373 },
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100374 [it8603] = {
375 .name = "it8603",
Guenter Roeckfaf392f2015-03-26 08:36:18 -0700376 .suffix = "E",
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100377 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700378 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100379 .peci_mask = 0x07,
380 },
Guenter Roeck3ba9d972015-02-13 20:13:20 -0800381 [it8620] = {
382 .name = "it8620",
383 .suffix = "E",
384 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
385 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
386 | FEAT_IN7_INTERNAL,
387 .peci_mask = 0x07,
388 },
Guenter Roeck483db432012-12-19 22:17:02 +0100389};
390
391#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
392#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
393#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
394#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
395#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100396#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
397 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100398#define has_temp_old_peci(data, nr) \
399 (((data)->features & FEAT_TEMP_OLD_PECI) && \
400 ((data)->old_peci_mask & (1 << nr)))
Guenter Roeck9faf28c2015-02-12 07:11:38 -0800401#define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
402#define has_five_fans(data) ((data)->features & FEAT_FIVE_FANS)
Guenter Roeck32dd7c42015-02-12 07:40:23 -0800403#define has_vid(data) ((data)->features & FEAT_VID)
Guenter Roeck7f5726c2015-03-26 08:49:18 -0700404#define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200406struct it87_sio_data {
407 enum chips type;
408 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200409 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200410 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100411 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200412 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100413 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700414 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100415 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100416 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200417 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700418 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200419};
420
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800421/*
422 * For each registered chip, we need to keep some data in memory.
423 * The structure is dynamically allocated.
424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700426 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100428 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100429 u8 peci_mask;
430 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200432 unsigned short addr;
433 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100434 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 char valid; /* !=0 if following fields are valid */
436 unsigned long last_updated; /* In jiffies */
437
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200438 u16 in_scaled; /* Internal voltage sensors are scaled */
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100439 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200440 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100441 u16 fan[5][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700442 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck161d8982012-12-19 22:17:01 +0100443 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100444 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
445 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 u8 fan_div[3]; /* Register encoding, shifted right */
447 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100448 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100450 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100452 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100453
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800454 /*
455 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100456 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
457 * 7, and we want to preserve settings on mode changes, so we have
458 * to track all values separately.
459 * Starting with the IT8721F, the manual PWM duty cycles are stored
460 * in separate registers (8-bit values), so the separate tracking
461 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800462 * simple.
463 */
Jean Delvareb99883d2010-03-05 22:17:15 +0100464 u8 pwm_ctrl[3]; /* Register value */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100465 u8 pwm_duty[3]; /* Manual PWM value set by user */
Jean Delvareb99883d2010-03-05 22:17:15 +0100466 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100467
468 /* Automatic fan speed control registers */
469 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
470 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471};
472
Guenter Roeck0531d982012-03-02 11:46:44 -0800473static int adc_lsb(const struct it87_data *data, int nr)
474{
475 int lsb = has_12mv_adc(data) ? 12 : 16;
476 if (data->in_scaled & (1 << nr))
477 lsb <<= 1;
478 return lsb;
479}
480
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200481static u8 in_to_reg(const struct it87_data *data, int nr, long val)
482{
Guenter Roeck0531d982012-03-02 11:46:44 -0800483 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800484 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200485}
486
487static int in_from_reg(const struct it87_data *data, int nr, int val)
488{
Guenter Roeck0531d982012-03-02 11:46:44 -0800489 return val * adc_lsb(data, nr);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200490}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200491
492static inline u8 FAN_TO_REG(long rpm, int div)
493{
494 if (rpm == 0)
495 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800496 rpm = clamp_val(rpm, 1, 1000000);
497 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200498}
499
500static inline u16 FAN16_TO_REG(long rpm)
501{
502 if (rpm == 0)
503 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800504 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200505}
506
507#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
508 1350000 / ((val) * (div)))
509/* The divider is fixed to 2 in 16-bit mode */
510#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
511 1350000 / ((val) * 2))
512
Guenter Roeck2a844c12013-01-09 08:09:34 -0800513#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
514 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200515#define TEMP_FROM_REG(val) ((val) * 1000)
516
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200517static u8 pwm_to_reg(const struct it87_data *data, long val)
518{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100519 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200520 return val;
521 else
522 return val >> 1;
523}
524
525static int pwm_from_reg(const struct it87_data *data, u8 reg)
526{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100527 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200528 return reg;
529 else
530 return (reg & 0x7f) << 1;
531}
532
Jean Delvare0df6454d2010-10-28 20:31:51 +0200533
534static int DIV_TO_REG(int val)
535{
536 int answer = 0;
537 while (answer < 7 && (val >>= 1))
538 answer++;
539 return answer;
540}
541#define DIV_FROM_REG(val) (1 << (val))
542
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700543/*
544 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
545 * depending on the chip type, to calculate the actual PWM frequency.
546 *
547 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
548 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
549 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
550 * sometimes just one. It is unknown if this is a datasheet error or real,
551 * so this is ignored for now.
552 */
Jean Delvare0df6454d2010-10-28 20:31:51 +0200553static const unsigned int pwm_freq[8] = {
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700554 48000000,
555 24000000,
556 12000000,
557 8000000,
558 6000000,
559 3000000,
560 1500000,
561 750000,
Jean Delvare0df6454d2010-10-28 20:31:51 +0200562};
563
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200564static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500565static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200567static int it87_read_value(struct it87_data *data, u8 reg);
568static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200570static int it87_check_pwm(struct device *dev);
571static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200574static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100575 .driver = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200576 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100577 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200578 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500579 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200580};
581
Jean Delvare20ad93d2005-06-05 11:53:25 +0200582static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100583 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100585 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
586 int nr = sattr->nr;
587 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100590 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Guenter Roeck929c6a52012-12-19 22:17:00 +0100593static ssize_t set_in(struct device *dev, struct device_attribute *attr,
594 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100596 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
597 int nr = sattr->nr;
598 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200599
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200600 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100601 unsigned long val;
602
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100603 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100604 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100606 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100607 data->in[nr][index] = in_to_reg(data, nr, val);
608 it87_write_value(data,
609 index == 1 ? IT87_REG_VIN_MIN(nr)
610 : IT87_REG_VIN_MAX(nr),
611 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100612 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return count;
614}
615
Guenter Roeck929c6a52012-12-19 22:17:00 +0100616static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
617static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
618 0, 1);
619static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
620 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Guenter Roeck929c6a52012-12-19 22:17:00 +0100622static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
623static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
624 1, 1);
625static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
626 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Guenter Roeck929c6a52012-12-19 22:17:00 +0100628static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
629static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
630 2, 1);
631static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
632 2, 2);
633
634static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
635static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
636 3, 1);
637static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
638 3, 2);
639
640static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
641static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
642 4, 1);
643static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
644 4, 2);
645
646static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
647static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
648 5, 1);
649static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
650 5, 2);
651
652static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
653static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
654 6, 1);
655static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
656 6, 2);
657
658static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
659static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
660 7, 1);
661static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
662 7, 2);
663
664static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100665static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200668static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100669 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100671 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
672 int nr = sattr->nr;
673 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200675
Guenter Roeck60ca3852012-12-19 22:17:00 +0100676 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200678
Guenter Roeck60ca3852012-12-19 22:17:00 +0100679static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
680 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100682 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
683 int nr = sattr->nr;
684 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200685 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100686 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100687 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100688
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100689 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100690 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100692 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100693
694 switch (index) {
695 default:
696 case 1:
697 reg = IT87_REG_TEMP_LOW(nr);
698 break;
699 case 2:
700 reg = IT87_REG_TEMP_HIGH(nr);
701 break;
702 case 3:
703 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
704 if (!(regval & 0x80)) {
705 regval |= 0x80;
706 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
707 }
708 data->valid = 0;
709 reg = IT87_REG_TEMP_OFFSET[nr];
710 break;
711 }
712
Guenter Roeck60ca3852012-12-19 22:17:00 +0100713 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100714 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100715 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return count;
717}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200718
Guenter Roeck60ca3852012-12-19 22:17:00 +0100719static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
720static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
721 0, 1);
722static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
723 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100724static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
725 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100726static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
727static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
728 1, 1);
729static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
730 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100731static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
732 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100733static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
734static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
735 2, 1);
736static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
737 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100738static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
739 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Guenter Roeck2cece012012-12-19 22:17:01 +0100741static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
742 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200744 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
745 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800747 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100748 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100749
Guenter Roeck19529782012-12-19 22:17:02 +0100750 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
751 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100752 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (reg & (1 << nr))
754 return sprintf(buf, "3\n"); /* thermal diode */
755 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200756 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return sprintf(buf, "0\n"); /* disabled */
758}
Guenter Roeck2cece012012-12-19 22:17:01 +0100759
760static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
761 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200763 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
764 int nr = sensor_attr->index;
765
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200766 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100767 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100768 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100769
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100770 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100771 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Jean Delvare8acf07c2010-04-14 16:14:09 +0200773 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
774 reg &= ~(1 << nr);
775 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100776 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
777 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100778 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
779 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
780 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200781 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100782 dev_warn(dev,
783 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200784 val = 4;
785 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100786 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200788 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200789 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200790 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100791 else if (has_temp_peci(data, nr) && val == 6)
792 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100793 else if (has_temp_old_peci(data, nr) && val == 6)
794 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200795 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200797
798 mutex_lock(&data->update_lock);
799 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100800 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200801 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100802 if (has_temp_old_peci(data, nr))
803 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200804 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100805 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return count;
807}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Guenter Roeck2cece012012-12-19 22:17:01 +0100809static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
810 set_temp_type, 0);
811static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
812 set_temp_type, 1);
813static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
814 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100817
818static int pwm_mode(const struct it87_data *data, int nr)
819{
820 int ctrl = data->fan_main_ctrl & (1 << nr);
821
Rudolf Marekc145d5c2014-01-29 20:40:08 +0100822 if (ctrl == 0 && data->type != it8603) /* Full speed */
Jean Delvareb99883d2010-03-05 22:17:15 +0100823 return 0;
824 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
825 return 2;
826 else /* Manual mode */
827 return 1;
828}
829
Jean Delvare20ad93d2005-06-05 11:53:25 +0200830static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +0100831 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100833 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
834 int nr = sattr->nr;
835 int index = sattr->index;
836 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200838
Guenter Roecke1169ba2012-12-19 22:17:01 +0100839 speed = has_16bit_fans(data) ?
840 FAN16_FROM_REG(data->fan[nr][index]) :
841 FAN_FROM_REG(data->fan[nr][index],
842 DIV_FROM_REG(data->fan_div[nr]));
843 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100845
Jean Delvare20ad93d2005-06-05 11:53:25 +0200846static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
847 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200849 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
850 int nr = sensor_attr->index;
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct it87_data *data = it87_update_device(dev);
853 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
854}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100855static ssize_t show_pwm_enable(struct device *dev,
856 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200858 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
859 int nr = sensor_attr->index;
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100862 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200864static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
865 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200867 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
868 int nr = sensor_attr->index;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200871 return sprintf(buf, "%d\n",
872 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100874static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
875 char *buf)
876{
877 struct it87_data *data = it87_update_device(dev);
878 int index = (data->fan_ctl >> 4) & 0x07;
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700879 unsigned int freq;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100880
Guenter Roeckf56c9c02015-03-26 20:01:24 -0700881 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
882
883 return sprintf(buf, "%u\n", freq);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100884}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100885
886static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
887 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100889 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
890 int nr = sattr->nr;
891 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200892
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200893 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100894 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100895 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100897 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100898 return -EINVAL;
899
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100900 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +0100901
902 if (has_16bit_fans(data)) {
903 data->fan[nr][index] = FAN16_TO_REG(val);
904 it87_write_value(data, IT87_REG_FAN_MIN[nr],
905 data->fan[nr][index] & 0xff);
906 it87_write_value(data, IT87_REG_FANX_MIN[nr],
907 data->fan[nr][index] >> 8);
908 } else {
909 reg = it87_read_value(data, IT87_REG_FAN_DIV);
910 switch (nr) {
911 case 0:
912 data->fan_div[nr] = reg & 0x07;
913 break;
914 case 1:
915 data->fan_div[nr] = (reg >> 3) & 0x07;
916 break;
917 case 2:
918 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
919 break;
920 }
921 data->fan[nr][index] =
922 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
923 it87_write_value(data, IT87_REG_FAN_MIN[nr],
924 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -0800925 }
926
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100927 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return count;
929}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100930
Jean Delvare20ad93d2005-06-05 11:53:25 +0200931static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
932 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200934 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
935 int nr = sensor_attr->index;
936
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200937 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100938 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200939 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 u8 old;
941
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100942 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100943 return -EINVAL;
944
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100945 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200946 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200948 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100949 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 switch (nr) {
952 case 0:
953 case 1:
954 data->fan_div[nr] = DIV_TO_REG(val);
955 break;
956 case 2:
957 if (val < 8)
958 data->fan_div[nr] = 1;
959 else
960 data->fan_div[nr] = 3;
961 }
962 val = old & 0x80;
963 val |= (data->fan_div[0] & 0x07);
964 val |= (data->fan_div[1] & 0x07) << 3;
965 if (data->fan_div[2] == 3)
966 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200967 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200969 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100970 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
971 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200972
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100973 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return count;
975}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100976
977/* Returns 0 if OK, -EINVAL otherwise */
978static int check_trip_points(struct device *dev, int nr)
979{
980 const struct it87_data *data = dev_get_drvdata(dev);
981 int i, err = 0;
982
983 if (has_old_autopwm(data)) {
984 for (i = 0; i < 3; i++) {
985 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
986 err = -EINVAL;
987 }
988 for (i = 0; i < 2; i++) {
989 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
990 err = -EINVAL;
991 }
992 }
993
994 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100995 dev_err(dev,
996 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100997 dev_err(dev, "Adjust the trip points and try again\n");
998 }
999 return err;
1000}
1001
Jean Delvare20ad93d2005-06-05 11:53:25 +02001002static ssize_t set_pwm_enable(struct device *dev,
1003 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001005 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1006 int nr = sensor_attr->index;
1007
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001008 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001009 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001011 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +01001012 return -EINVAL;
1013
Jean Delvarecccfc9c2010-03-05 22:17:21 +01001014 /* Check trip points before switching to automatic mode */
1015 if (val == 2) {
1016 if (check_trip_points(dev, nr) < 0)
1017 return -EINVAL;
1018 }
1019
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001020 /* IT8603E does not have on/off mode */
1021 if (val == 0 && data->type == it8603)
1022 return -EINVAL;
1023
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001024 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (val == 0) {
1027 int tmp;
1028 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001029 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1030 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /* set on/off mode */
1032 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +01001033 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1034 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +01001035 } else {
1036 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001037 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +01001038 data->pwm_temp_map[nr] :
1039 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +01001040 else /* Automatic mode */
1041 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1042 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001043
1044 if (data->type != it8603) {
1045 /* set SmartGuardian mode */
1046 data->fan_main_ctrl |= (1 << nr);
1047 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1048 data->fan_main_ctrl);
1049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001052 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return count;
1054}
Jean Delvare20ad93d2005-06-05 11:53:25 +02001055static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1056 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Jean Delvare20ad93d2005-06-05 11:53:25 +02001058 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1059 int nr = sensor_attr->index;
1060
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001061 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001062 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001064 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return -EINVAL;
1066
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001067 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001068 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001069 /*
1070 * If we are in automatic mode, the PWM duty cycle register
1071 * is read-only so we can't write the value.
1072 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001073 if (data->pwm_ctrl[nr] & 0x80) {
1074 mutex_unlock(&data->update_lock);
1075 return -EBUSY;
1076 }
1077 data->pwm_duty[nr] = pwm_to_reg(data, val);
1078 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
1079 data->pwm_duty[nr]);
1080 } else {
1081 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001082 /*
1083 * If we are in manual mode, write the duty cycle immediately;
1084 * otherwise, just store it for later use.
1085 */
Jean Delvare6229cdb2010-12-08 16:27:22 +01001086 if (!(data->pwm_ctrl[nr] & 0x80)) {
1087 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1088 it87_write_value(data, IT87_REG_PWM(nr),
1089 data->pwm_ctrl[nr]);
1090 }
Jean Delvareb99883d2010-03-05 22:17:15 +01001091 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001092 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return count;
1094}
Jean Delvaref8d0c192007-02-14 21:15:02 +01001095static ssize_t set_pwm_freq(struct device *dev,
1096 struct device_attribute *attr, const char *buf, size_t count)
1097{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001098 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001099 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001100 int i;
1101
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001102 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001103 return -EINVAL;
1104
Guenter Roeckf56c9c02015-03-26 20:01:24 -07001105 val = clamp_val(val, 0, 1000000);
1106 val *= has_newer_autopwm(data) ? 256 : 128;
1107
Jean Delvaref8d0c192007-02-14 21:15:02 +01001108 /* Search for the nearest available frequency */
1109 for (i = 0; i < 7; i++) {
1110 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1111 break;
1112 }
1113
1114 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001115 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +01001116 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001117 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +01001118 mutex_unlock(&data->update_lock);
1119
1120 return count;
1121}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001122static ssize_t show_pwm_temp_map(struct device *dev,
1123 struct device_attribute *attr, char *buf)
1124{
1125 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1126 int nr = sensor_attr->index;
1127
1128 struct it87_data *data = it87_update_device(dev);
1129 int map;
1130
1131 if (data->pwm_temp_map[nr] < 3)
1132 map = 1 << data->pwm_temp_map[nr];
1133 else
1134 map = 0; /* Should never happen */
1135 return sprintf(buf, "%d\n", map);
1136}
1137static ssize_t set_pwm_temp_map(struct device *dev,
1138 struct device_attribute *attr, const char *buf, size_t count)
1139{
1140 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1141 int nr = sensor_attr->index;
1142
1143 struct it87_data *data = dev_get_drvdata(dev);
1144 long val;
1145 u8 reg;
1146
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001147 /*
1148 * This check can go away if we ever support automatic fan speed
1149 * control on newer chips.
1150 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001151 if (!has_old_autopwm(data)) {
1152 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1153 return -EINVAL;
1154 }
1155
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001156 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001157 return -EINVAL;
1158
1159 switch (val) {
1160 case (1 << 0):
1161 reg = 0x00;
1162 break;
1163 case (1 << 1):
1164 reg = 0x01;
1165 break;
1166 case (1 << 2):
1167 reg = 0x02;
1168 break;
1169 default:
1170 return -EINVAL;
1171 }
1172
1173 mutex_lock(&data->update_lock);
1174 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001175 /*
1176 * If we are in automatic mode, write the temp mapping immediately;
1177 * otherwise, just store it for later use.
1178 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001179 if (data->pwm_ctrl[nr] & 0x80) {
1180 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1181 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1182 }
1183 mutex_unlock(&data->update_lock);
1184 return count;
1185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001187static ssize_t show_auto_pwm(struct device *dev,
1188 struct device_attribute *attr, char *buf)
1189{
1190 struct it87_data *data = it87_update_device(dev);
1191 struct sensor_device_attribute_2 *sensor_attr =
1192 to_sensor_dev_attr_2(attr);
1193 int nr = sensor_attr->nr;
1194 int point = sensor_attr->index;
1195
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001196 return sprintf(buf, "%d\n",
1197 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001198}
1199
1200static ssize_t set_auto_pwm(struct device *dev,
1201 struct device_attribute *attr, const char *buf, size_t count)
1202{
1203 struct it87_data *data = dev_get_drvdata(dev);
1204 struct sensor_device_attribute_2 *sensor_attr =
1205 to_sensor_dev_attr_2(attr);
1206 int nr = sensor_attr->nr;
1207 int point = sensor_attr->index;
1208 long val;
1209
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001210 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001211 return -EINVAL;
1212
1213 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001214 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001215 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1216 data->auto_pwm[nr][point]);
1217 mutex_unlock(&data->update_lock);
1218 return count;
1219}
1220
1221static ssize_t show_auto_temp(struct device *dev,
1222 struct device_attribute *attr, char *buf)
1223{
1224 struct it87_data *data = it87_update_device(dev);
1225 struct sensor_device_attribute_2 *sensor_attr =
1226 to_sensor_dev_attr_2(attr);
1227 int nr = sensor_attr->nr;
1228 int point = sensor_attr->index;
1229
1230 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1231}
1232
1233static ssize_t set_auto_temp(struct device *dev,
1234 struct device_attribute *attr, const char *buf, size_t count)
1235{
1236 struct it87_data *data = dev_get_drvdata(dev);
1237 struct sensor_device_attribute_2 *sensor_attr =
1238 to_sensor_dev_attr_2(attr);
1239 int nr = sensor_attr->nr;
1240 int point = sensor_attr->index;
1241 long val;
1242
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001243 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001244 return -EINVAL;
1245
1246 mutex_lock(&data->update_lock);
1247 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1248 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1249 data->auto_temp[nr][point]);
1250 mutex_unlock(&data->update_lock);
1251 return count;
1252}
1253
Guenter Roecke1169ba2012-12-19 22:17:01 +01001254static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1255static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1256 0, 1);
1257static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1258 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Guenter Roecke1169ba2012-12-19 22:17:01 +01001260static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1261static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1262 1, 1);
1263static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1264 set_fan_div, 1);
1265
1266static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1267static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1268 2, 1);
1269static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1270 set_fan_div, 2);
1271
1272static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1273static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1274 3, 1);
1275
1276static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1277static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1278 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Guenter Roeckc4458db2012-12-19 22:17:02 +01001280static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1281 show_pwm_enable, set_pwm_enable, 0);
1282static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1283static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1284static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1285 show_pwm_temp_map, set_pwm_temp_map, 0);
1286static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1287 show_auto_pwm, set_auto_pwm, 0, 0);
1288static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1289 show_auto_pwm, set_auto_pwm, 0, 1);
1290static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1291 show_auto_pwm, set_auto_pwm, 0, 2);
1292static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1293 show_auto_pwm, NULL, 0, 3);
1294static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1295 show_auto_temp, set_auto_temp, 0, 1);
1296static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1297 show_auto_temp, set_auto_temp, 0, 0);
1298static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1299 show_auto_temp, set_auto_temp, 0, 2);
1300static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1301 show_auto_temp, set_auto_temp, 0, 3);
1302static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1303 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Guenter Roeckc4458db2012-12-19 22:17:02 +01001305static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1306 show_pwm_enable, set_pwm_enable, 1);
1307static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1308static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1309static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1310 show_pwm_temp_map, set_pwm_temp_map, 1);
1311static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1312 show_auto_pwm, set_auto_pwm, 1, 0);
1313static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1314 show_auto_pwm, set_auto_pwm, 1, 1);
1315static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1316 show_auto_pwm, set_auto_pwm, 1, 2);
1317static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1318 show_auto_pwm, NULL, 1, 3);
1319static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1320 show_auto_temp, set_auto_temp, 1, 1);
1321static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1322 show_auto_temp, set_auto_temp, 1, 0);
1323static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1324 show_auto_temp, set_auto_temp, 1, 2);
1325static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1326 show_auto_temp, set_auto_temp, 1, 3);
1327static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1328 show_auto_temp, set_auto_temp, 1, 4);
1329
1330static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1331 show_pwm_enable, set_pwm_enable, 2);
1332static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1333static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1334static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1335 show_pwm_temp_map, set_pwm_temp_map, 2);
1336static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1337 show_auto_pwm, set_auto_pwm, 2, 0);
1338static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1339 show_auto_pwm, set_auto_pwm, 2, 1);
1340static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1341 show_auto_pwm, set_auto_pwm, 2, 2);
1342static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1343 show_auto_pwm, NULL, 2, 3);
1344static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1345 show_auto_temp, set_auto_temp, 2, 1);
1346static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1347 show_auto_temp, set_auto_temp, 2, 0);
1348static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1349 show_auto_temp, set_auto_temp, 2, 2);
1350static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1351 show_auto_temp, set_auto_temp, 2, 3);
1352static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1353 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001356static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1357 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
1359 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001360 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
Jean Delvare1d66c642005-04-18 21:16:59 -07001362static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Jean Delvare0124dd72007-11-25 16:16:41 +01001364static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1365 char *buf)
1366{
1367 int bitnr = to_sensor_dev_attr(attr)->index;
1368 struct it87_data *data = it87_update_device(dev);
1369 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1370}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001371
1372static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1373 *attr, const char *buf, size_t count)
1374{
1375 struct it87_data *data = dev_get_drvdata(dev);
1376 long val;
1377 int config;
1378
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001379 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001380 return -EINVAL;
1381
1382 mutex_lock(&data->update_lock);
1383 config = it87_read_value(data, IT87_REG_CONFIG);
1384 if (config < 0) {
1385 count = config;
1386 } else {
1387 config |= 1 << 5;
1388 it87_write_value(data, IT87_REG_CONFIG, config);
1389 /* Invalidate cache to force re-read */
1390 data->valid = 0;
1391 }
1392 mutex_unlock(&data->update_lock);
1393
1394 return count;
1395}
1396
Jean Delvare0124dd72007-11-25 16:16:41 +01001397static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1398static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1399static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1400static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1401static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1402static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1403static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1404static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1405static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1406static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1407static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1408static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1409static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1410static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1411static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1412static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001413static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1414 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001415
Jean Delvared9b327c2010-03-05 22:17:17 +01001416static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1417 char *buf)
1418{
1419 int bitnr = to_sensor_dev_attr(attr)->index;
1420 struct it87_data *data = it87_update_device(dev);
1421 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1422}
1423static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1424 const char *buf, size_t count)
1425{
1426 int bitnr = to_sensor_dev_attr(attr)->index;
1427 struct it87_data *data = dev_get_drvdata(dev);
1428 long val;
1429
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001430 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001431 || (val != 0 && val != 1))
1432 return -EINVAL;
1433
1434 mutex_lock(&data->update_lock);
1435 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1436 if (val)
1437 data->beeps |= (1 << bitnr);
1438 else
1439 data->beeps &= ~(1 << bitnr);
1440 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1441 mutex_unlock(&data->update_lock);
1442 return count;
1443}
1444
1445static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1446 show_beep, set_beep, 1);
1447static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1448static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1449static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1450static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1451static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1452static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1453static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1454/* fanX_beep writability is set later */
1455static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1456static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1457static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1458static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1459static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1460static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1461 show_beep, set_beep, 2);
1462static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1463static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1464
Jean Delvare5f2dc792010-03-05 22:17:18 +01001465static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1466 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Jean Delvare90d66192007-10-08 18:24:35 +02001468 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001469 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001471static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1472 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001474 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001475 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001477 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001478 return -EINVAL;
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 data->vrm = val;
1481
1482 return count;
1483}
1484static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Jean Delvare5f2dc792010-03-05 22:17:18 +01001486static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1487 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 struct it87_data *data = it87_update_device(dev);
1490 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1491}
1492static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001493
Jean Delvare738e5e02010-08-14 21:08:50 +02001494static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1495 char *buf)
1496{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001497 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001498 "+5V",
1499 "5VSB",
1500 "Vbat",
1501 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001502 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001503 "+3.3V",
1504 "3VSB",
1505 "Vbat",
1506 };
1507 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001508 int nr = to_sensor_dev_attr(attr)->index;
1509
Jean Delvare16b5dda2012-01-16 22:51:48 +01001510 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1511 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001512}
1513static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1514static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1515static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
Rudolf Marek7183ae82014-04-04 18:01:35 +02001516/* special AVCC3 IT8603E in9 */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001517static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
Jean Delvare738e5e02010-08-14 21:08:50 +02001518
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001519static ssize_t show_name(struct device *dev, struct device_attribute
1520 *devattr, char *buf)
1521{
1522 struct it87_data *data = dev_get_drvdata(dev);
1523 return sprintf(buf, "%s\n", data->name);
1524}
1525static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1526
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001527static struct attribute *it87_attributes_in[10][5] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001528{
Jean Delvare87808be2006-09-24 21:17:13 +02001529 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001530 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001531 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001532 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001533 NULL
1534}, {
1535 &sensor_dev_attr_in1_input.dev_attr.attr,
1536 &sensor_dev_attr_in1_min.dev_attr.attr,
1537 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001538 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001539 NULL
1540}, {
1541 &sensor_dev_attr_in2_input.dev_attr.attr,
1542 &sensor_dev_attr_in2_min.dev_attr.attr,
1543 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001544 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001545 NULL
1546}, {
1547 &sensor_dev_attr_in3_input.dev_attr.attr,
1548 &sensor_dev_attr_in3_min.dev_attr.attr,
1549 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001550 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001551 NULL
1552}, {
1553 &sensor_dev_attr_in4_input.dev_attr.attr,
1554 &sensor_dev_attr_in4_min.dev_attr.attr,
1555 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001556 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001557 NULL
1558}, {
1559 &sensor_dev_attr_in5_input.dev_attr.attr,
1560 &sensor_dev_attr_in5_min.dev_attr.attr,
1561 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001562 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001563 NULL
1564}, {
1565 &sensor_dev_attr_in6_input.dev_attr.attr,
1566 &sensor_dev_attr_in6_min.dev_attr.attr,
1567 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001568 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001569 NULL
1570}, {
1571 &sensor_dev_attr_in7_input.dev_attr.attr,
1572 &sensor_dev_attr_in7_min.dev_attr.attr,
1573 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001574 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001575 NULL
1576}, {
1577 &sensor_dev_attr_in8_input.dev_attr.attr,
1578 NULL
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001579}, {
1580 &sensor_dev_attr_in9_input.dev_attr.attr,
1581 NULL
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001582} };
Jean Delvare87808be2006-09-24 21:17:13 +02001583
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001584static const struct attribute_group it87_group_in[10] = {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001585 { .attrs = it87_attributes_in[0] },
1586 { .attrs = it87_attributes_in[1] },
1587 { .attrs = it87_attributes_in[2] },
1588 { .attrs = it87_attributes_in[3] },
1589 { .attrs = it87_attributes_in[4] },
1590 { .attrs = it87_attributes_in[5] },
1591 { .attrs = it87_attributes_in[6] },
1592 { .attrs = it87_attributes_in[7] },
1593 { .attrs = it87_attributes_in[8] },
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001594 { .attrs = it87_attributes_in[9] },
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001595};
1596
Guenter Roeck4573acb2012-03-26 16:17:41 -07001597static struct attribute *it87_attributes_temp[3][6] = {
1598{
Jean Delvare87808be2006-09-24 21:17:13 +02001599 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001600 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001601 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001602 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001603 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001604 NULL
1605} , {
1606 &sensor_dev_attr_temp2_input.dev_attr.attr,
1607 &sensor_dev_attr_temp2_max.dev_attr.attr,
1608 &sensor_dev_attr_temp2_min.dev_attr.attr,
1609 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001610 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001611 NULL
1612} , {
1613 &sensor_dev_attr_temp3_input.dev_attr.attr,
1614 &sensor_dev_attr_temp3_max.dev_attr.attr,
1615 &sensor_dev_attr_temp3_min.dev_attr.attr,
1616 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001617 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001618 NULL
1619} };
Jean Delvare87808be2006-09-24 21:17:13 +02001620
Guenter Roeck4573acb2012-03-26 16:17:41 -07001621static const struct attribute_group it87_group_temp[3] = {
1622 { .attrs = it87_attributes_temp[0] },
1623 { .attrs = it87_attributes_temp[1] },
1624 { .attrs = it87_attributes_temp[2] },
1625};
1626
Guenter Roeck161d8982012-12-19 22:17:01 +01001627static struct attribute *it87_attributes_temp_offset[] = {
1628 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1629 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1630 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1631};
1632
Guenter Roeck4573acb2012-03-26 16:17:41 -07001633static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001634 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001635 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001636 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001637 NULL
1638};
1639
1640static const struct attribute_group it87_group = {
1641 .attrs = it87_attributes,
1642};
1643
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001644static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001645 &sensor_dev_attr_in0_beep.dev_attr.attr,
1646 &sensor_dev_attr_in1_beep.dev_attr.attr,
1647 &sensor_dev_attr_in2_beep.dev_attr.attr,
1648 &sensor_dev_attr_in3_beep.dev_attr.attr,
1649 &sensor_dev_attr_in4_beep.dev_attr.attr,
1650 &sensor_dev_attr_in5_beep.dev_attr.attr,
1651 &sensor_dev_attr_in6_beep.dev_attr.attr,
1652 &sensor_dev_attr_in7_beep.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001653 NULL,
1654 NULL,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001655};
Jean Delvared9b327c2010-03-05 22:17:17 +01001656
Guenter Roeck4573acb2012-03-26 16:17:41 -07001657static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001658 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1659 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1660 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001661};
1662
Guenter Roecke1169ba2012-12-19 22:17:01 +01001663static struct attribute *it87_attributes_fan[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001664 &sensor_dev_attr_fan1_input.dev_attr.attr,
1665 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001666 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1667 NULL
1668}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001669 &sensor_dev_attr_fan2_input.dev_attr.attr,
1670 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001671 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1672 NULL
1673}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001674 &sensor_dev_attr_fan3_input.dev_attr.attr,
1675 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001676 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001677 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001678}, {
1679 &sensor_dev_attr_fan4_input.dev_attr.attr,
1680 &sensor_dev_attr_fan4_min.dev_attr.attr,
1681 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1682 NULL
1683}, {
1684 &sensor_dev_attr_fan5_input.dev_attr.attr,
1685 &sensor_dev_attr_fan5_min.dev_attr.attr,
1686 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1687 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001688} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001689
Guenter Roecke1169ba2012-12-19 22:17:01 +01001690static const struct attribute_group it87_group_fan[5] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001691 { .attrs = it87_attributes_fan[0] },
1692 { .attrs = it87_attributes_fan[1] },
1693 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001694 { .attrs = it87_attributes_fan[3] },
1695 { .attrs = it87_attributes_fan[4] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001696};
1697
Guenter Roecke1169ba2012-12-19 22:17:01 +01001698static const struct attribute *it87_attributes_fan_div[] = {
1699 &sensor_dev_attr_fan1_div.dev_attr.attr,
1700 &sensor_dev_attr_fan2_div.dev_attr.attr,
1701 &sensor_dev_attr_fan3_div.dev_attr.attr,
1702};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001703
1704static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001705 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001706 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001707 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001708 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001709 NULL
1710}, {
1711 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1712 &sensor_dev_attr_pwm2.dev_attr.attr,
1713 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001714 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001715 NULL
1716}, {
1717 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1718 &sensor_dev_attr_pwm3.dev_attr.attr,
1719 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001720 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001721 NULL
1722} };
Jean Delvare87808be2006-09-24 21:17:13 +02001723
Jean Delvare723a0aa2010-03-05 22:17:16 +01001724static const struct attribute_group it87_group_pwm[3] = {
1725 { .attrs = it87_attributes_pwm[0] },
1726 { .attrs = it87_attributes_pwm[1] },
1727 { .attrs = it87_attributes_pwm[2] },
1728};
1729
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001730static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1731 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1732 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1733 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1734 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1735 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1736 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1737 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1738 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1739 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1740 NULL
1741}, {
1742 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1743 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1744 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1745 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1746 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1747 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1748 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1749 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1750 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1751 NULL
1752}, {
1753 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1754 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1755 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1756 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1757 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1758 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1759 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1760 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1761 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1762 NULL
1763} };
1764
1765static const struct attribute_group it87_group_autopwm[3] = {
1766 { .attrs = it87_attributes_autopwm[0] },
1767 { .attrs = it87_attributes_autopwm[1] },
1768 { .attrs = it87_attributes_autopwm[2] },
1769};
1770
Jean Delvared9b327c2010-03-05 22:17:17 +01001771static struct attribute *it87_attributes_fan_beep[] = {
1772 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1773 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1774 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1775 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1776 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1777};
1778
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001779static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001780 &dev_attr_vrm.attr,
1781 &dev_attr_cpu0_vid.attr,
1782 NULL
1783};
1784
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001785static const struct attribute_group it87_group_vid = {
1786 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001787};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Jean Delvare738e5e02010-08-14 21:08:50 +02001789static struct attribute *it87_attributes_label[] = {
1790 &sensor_dev_attr_in3_label.dev_attr.attr,
1791 &sensor_dev_attr_in7_label.dev_attr.attr,
1792 &sensor_dev_attr_in8_label.dev_attr.attr,
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001793 &sensor_dev_attr_in9_label.dev_attr.attr,
Jean Delvare738e5e02010-08-14 21:08:50 +02001794 NULL
1795};
1796
1797static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001798 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001799};
1800
Jean Delvare2d8672c2005-07-19 23:56:35 +02001801/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001802static int __init it87_find(unsigned short *address,
1803 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001805 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001806 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001807 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001809 err = superio_enter();
1810 if (err)
1811 return err;
1812
1813 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001814 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001815
1816 switch (chip_type) {
1817 case IT8705F_DEVID:
1818 sio_data->type = it87;
1819 break;
1820 case IT8712F_DEVID:
1821 sio_data->type = it8712;
1822 break;
1823 case IT8716F_DEVID:
1824 case IT8726F_DEVID:
1825 sio_data->type = it8716;
1826 break;
1827 case IT8718F_DEVID:
1828 sio_data->type = it8718;
1829 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001830 case IT8720F_DEVID:
1831 sio_data->type = it8720;
1832 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001833 case IT8721F_DEVID:
1834 sio_data->type = it8721;
1835 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001836 case IT8728F_DEVID:
1837 sio_data->type = it8728;
1838 break;
Guenter Roeckb0636702012-09-07 17:34:41 -06001839 case IT8771E_DEVID:
1840 sio_data->type = it8771;
1841 break;
1842 case IT8772E_DEVID:
1843 sio_data->type = it8772;
1844 break;
Guenter Roeck7bc32d22015-01-17 14:10:24 -08001845 case IT8781F_DEVID:
1846 sio_data->type = it8781;
1847 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001848 case IT8782F_DEVID:
1849 sio_data->type = it8782;
1850 break;
1851 case IT8783E_DEVID:
1852 sio_data->type = it8783;
1853 break;
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001854 case IT8786E_DEVID:
1855 sio_data->type = it8786;
1856 break;
Guenter Roeck4ee07152015-03-25 23:26:28 -07001857 case IT8790E_DEVID:
1858 sio_data->type = it8790;
1859 break;
Rudolf Marek7183ae82014-04-04 18:01:35 +02001860 case IT8603E_DEVID:
Rudolf Marek574e9bd2014-04-04 18:01:35 +02001861 case IT8623E_DEVID:
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001862 sio_data->type = it8603;
1863 break;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08001864 case IT8620E_DEVID:
1865 sio_data->type = it8620;
1866 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001867 case 0xffff: /* No device at all */
1868 goto exit;
1869 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001870 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001871 goto exit;
1872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Jean Delvare87673dd2006-08-28 14:37:19 +02001874 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001876 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto exit;
1878 }
1879
1880 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1881 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001882 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 goto exit;
1884 }
1885
1886 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001887 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Guenter Roeckfaf392f2015-03-26 08:36:18 -07001888 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
1889 it87_devices[sio_data->type].suffix,
Thomas Lorblanchesa0c14242015-02-13 13:19:06 +01001890 *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Guenter Roeck7f5726c2015-03-26 08:49:18 -07001892 /* in7 (VSB or VCCH5V) is always internal on some chips */
1893 if (it87_devices[sio_data->type].features & FEAT_IN7_INTERNAL)
1894 sio_data->internal |= (1 << 1);
1895
Jean Delvare738e5e02010-08-14 21:08:50 +02001896 /* in8 (Vbat) is always internal */
Guenter Roeck7f5726c2015-03-26 08:49:18 -07001897 sio_data->internal |= (1 << 2);
1898
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001899 /* Only the IT8603E has in9 */
1900 if (sio_data->type != it8603)
1901 sio_data->skip_in |= (1 << 9);
Jean Delvare738e5e02010-08-14 21:08:50 +02001902
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001903 if (!(it87_devices[sio_data->type].features & FEAT_VID))
Jean Delvare895ff262009-12-09 20:35:47 +01001904 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001905
Guenter Roeck32dd7c42015-02-12 07:40:23 -08001906 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1907 if (sio_data->type == it87) {
Jean Delvared9b327c2010-03-05 22:17:17 +01001908 /* The IT8705F has a different LD number for GPIO */
1909 superio_select(5);
1910 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08001911 } else if (sio_data->type == it8783) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001912 int reg25, reg27, reg2a, reg2c, regef;
Guenter Roeck0531d982012-03-02 11:46:44 -08001913
Guenter Roeck0531d982012-03-02 11:46:44 -08001914 superio_select(GPIO);
1915
1916 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1917 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001918 reg2a = superio_inb(IT87_SIO_PINX1_REG);
1919 reg2c = superio_inb(IT87_SIO_PINX2_REG);
1920 regef = superio_inb(IT87_SIO_SPI_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001921
Guenter Roeck0531d982012-03-02 11:46:44 -08001922 /* Check if fan3 is there or not */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001923 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08001924 sio_data->skip_fan |= (1 << 2);
1925 if ((reg25 & (1 << 4))
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001926 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
Guenter Roeck0531d982012-03-02 11:46:44 -08001927 sio_data->skip_pwm |= (1 << 2);
1928
1929 /* Check if fan2 is there or not */
1930 if (reg27 & (1 << 7))
1931 sio_data->skip_fan |= (1 << 1);
1932 if (reg27 & (1 << 3))
1933 sio_data->skip_pwm |= (1 << 1);
1934
1935 /* VIN5 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001936 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001937 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001938
1939 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001940 if (reg27 & (1 << 1))
1941 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001942
1943 /*
1944 * VIN7
1945 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1946 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001947 if (reg27 & (1 << 2)) {
1948 /*
1949 * The data sheet is a bit unclear regarding the
1950 * internal voltage divider for VCCH5V. It says
1951 * "This bit enables and switches VIN7 (pin 91) to the
1952 * internal voltage divider for VCCH5V".
1953 * This is different to other chips, where the internal
1954 * voltage divider would connect VIN7 to an internal
1955 * voltage source. Maybe that is the case here as well.
1956 *
1957 * Since we don't know for sure, re-route it if that is
1958 * not the case, and ask the user to report if the
1959 * resulting voltage is sane.
1960 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001961 if (!(reg2c & (1 << 1))) {
1962 reg2c |= (1 << 1);
1963 superio_outb(IT87_SIO_PINX2_REG, reg2c);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001964 pr_notice("Routing internal VCCH5V to in7.\n");
1965 }
1966 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1967 pr_notice("Please report if it displays a reasonable voltage.\n");
1968 }
Guenter Roeck0531d982012-03-02 11:46:44 -08001969
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001970 if (reg2c & (1 << 0))
Guenter Roeck0531d982012-03-02 11:46:44 -08001971 sio_data->internal |= (1 << 0);
Guenter Roeck088ce2a2013-03-13 16:40:39 -07001972 if (reg2c & (1 << 1))
Guenter Roeck0531d982012-03-02 11:46:44 -08001973 sio_data->internal |= (1 << 1);
1974
1975 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001976 } else if (sio_data->type == it8603) {
1977 int reg27, reg29;
Guenter Roeck0531d982012-03-02 11:46:44 -08001978
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001979 superio_select(GPIO);
1980
1981 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1982
1983 /* Check if fan3 is there or not */
1984 if (reg27 & (1 << 6))
1985 sio_data->skip_pwm |= (1 << 2);
1986 if (reg27 & (1 << 7))
1987 sio_data->skip_fan |= (1 << 2);
1988
1989 /* Check if fan2 is there or not */
1990 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
1991 if (reg29 & (1 << 1))
1992 sio_data->skip_pwm |= (1 << 1);
1993 if (reg29 & (1 << 2))
1994 sio_data->skip_fan |= (1 << 1);
1995
1996 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1997 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1998
Rudolf Marekc145d5c2014-01-29 20:40:08 +01001999 sio_data->internal |= (1 << 3); /* in9 is AVCC */
2000
2001 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck3ba9d972015-02-13 20:13:20 -08002002 } else if (sio_data->type == it8620) {
2003 int reg;
2004
2005 superio_select(GPIO);
2006
2007 /* Check for fan4, fan5 */
2008 reg = superio_inb(IT87_SIO_GPIO2_REG);
2009 if (!(reg & (1 << 5)))
2010 sio_data->skip_fan |= (1 << 3);
2011 if (!(reg & (1 << 4)))
2012 sio_data->skip_fan |= (1 << 4);
2013
2014 /* Check for pwm3, fan3 */
2015 reg = superio_inb(IT87_SIO_GPIO3_REG);
2016 if (reg & (1 << 6))
2017 sio_data->skip_pwm |= (1 << 2);
2018 if (reg & (1 << 7))
2019 sio_data->skip_fan |= (1 << 2);
2020
2021 /* Check for pwm2, fan2 */
2022 reg = superio_inb(IT87_SIO_GPIO5_REG);
2023 if (reg & (1 << 1))
2024 sio_data->skip_pwm |= (1 << 1);
2025 if (reg & (1 << 2))
2026 sio_data->skip_fan |= (1 << 1);
2027
2028 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01002029 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02002030 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002031 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02002032
2033 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002034
Jean Delvare895ff262009-12-09 20:35:47 +01002035 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck32dd7c42015-02-12 07:40:23 -08002036 if (!sio_data->skip_vid) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002037 /* We need at least 4 VID pins */
2038 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002039 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002040 sio_data->skip_vid = 1;
2041 }
Jean Delvare895ff262009-12-09 20:35:47 +01002042 }
2043
Jean Delvare591ec652009-12-09 20:35:48 +01002044 /* Check if fan3 is there or not */
2045 if (reg & (1 << 6))
2046 sio_data->skip_pwm |= (1 << 2);
2047 if (reg & (1 << 7))
2048 sio_data->skip_fan |= (1 << 2);
2049
2050 /* Check if fan2 is there or not */
2051 reg = superio_inb(IT87_SIO_GPIO5_REG);
2052 if (reg & (1 << 1))
2053 sio_data->skip_pwm |= (1 << 1);
2054 if (reg & (1 << 2))
2055 sio_data->skip_fan |= (1 << 1);
2056
Jean Delvare895ff262009-12-09 20:35:47 +01002057 if ((sio_data->type == it8718 || sio_data->type == it8720)
2058 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002059 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02002060
2061 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002062
2063 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2064
Jean Delvare436cad22010-07-09 16:22:48 +02002065 /*
2066 * The IT8720F has no VIN7 pin, so VCCH should always be
2067 * routed internally to VIN7 with an internal divider.
2068 * Curiously, there still is a configuration bit to control
2069 * this, which means it can be set incorrectly. And even
2070 * more curiously, many boards out there are improperly
2071 * configured, even though the IT8720F datasheet claims
2072 * that the internal routing of VCCH to VIN7 is the default
2073 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08002074 *
2075 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002076 * If UART6 is enabled, re-route VIN7 to the internal divider
2077 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02002078 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002079 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02002080 reg |= (1 << 1);
2081 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01002082 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02002083 }
Jean Delvare87673dd2006-08-28 14:37:19 +02002084 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02002085 sio_data->internal |= (1 << 0);
Guenter Roeck7f5726c2015-03-26 08:49:18 -07002086 if (reg & (1 << 1))
Jean Delvare738e5e02010-08-14 21:08:50 +02002087 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01002088
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002089 /*
2090 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2091 * While VIN7 can be routed to the internal voltage divider,
2092 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07002093 *
2094 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2095 * is the temperature source. Since we can not read the
2096 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002097 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07002098 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002099 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002100 sio_data->skip_temp |= (1 << 2);
2101 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002102
Jean Delvared9b327c2010-03-05 22:17:17 +01002103 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02002104 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002105 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01002106 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02002107
Jean Delvare98dd22c2008-10-09 15:33:58 +02002108 /* Disable specific features based on DMI strings */
2109 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2110 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2111 if (board_vendor && board_name) {
2112 if (strcmp(board_vendor, "nVIDIA") == 0
2113 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002114 /*
2115 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2116 * connected to a fan, but to something else. One user
2117 * has reported instant system power-off when changing
2118 * the PWM2 duty cycle, so we disable it.
2119 * I use the board name string as the trigger in case
2120 * the same board is ever used in other systems.
2121 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01002122 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02002123 sio_data->skip_pwm = (1 << 1);
2124 }
2125 }
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127exit:
2128 superio_exit();
2129 return err;
2130}
2131
Jean Delvare723a0aa2010-03-05 22:17:16 +01002132static void it87_remove_files(struct device *dev)
2133{
2134 struct it87_data *data = platform_get_drvdata(pdev);
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002135 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002136 int i;
2137
2138 sysfs_remove_group(&dev->kobj, &it87_group);
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002139 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002140 if (sio_data->skip_in & (1 << i))
2141 continue;
2142 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2143 if (it87_attributes_in_beep[i])
2144 sysfs_remove_file(&dev->kobj,
2145 it87_attributes_in_beep[i]);
2146 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002147 for (i = 0; i < 3; i++) {
2148 if (!(data->has_temp & (1 << i)))
2149 continue;
2150 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01002151 if (has_temp_offset(data))
2152 sysfs_remove_file(&dev->kobj,
2153 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07002154 if (sio_data->beep_pin)
2155 sysfs_remove_file(&dev->kobj,
2156 it87_attributes_temp_beep[i]);
2157 }
Jean Delvare723a0aa2010-03-05 22:17:16 +01002158 for (i = 0; i < 5; i++) {
2159 if (!(data->has_fan & (1 << i)))
2160 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002161 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002162 if (sio_data->beep_pin)
2163 sysfs_remove_file(&dev->kobj,
2164 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002165 if (i < 3 && !has_16bit_fans(data))
2166 sysfs_remove_file(&dev->kobj,
2167 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002168 }
2169 for (i = 0; i < 3; i++) {
Guenter Roeck1696d1d2015-03-27 06:03:41 -07002170 if (sio_data->skip_pwm & (1 << i))
Jean Delvare723a0aa2010-03-05 22:17:16 +01002171 continue;
2172 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002173 if (has_old_autopwm(data))
2174 sysfs_remove_group(&dev->kobj,
2175 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002176 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002177 if (!sio_data->skip_vid)
2178 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02002179 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002180}
2181
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002182static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002185 struct resource *res;
2186 struct device *dev = &pdev->dev;
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002187 struct it87_sio_data *sio_data = dev_get_platdata(dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002188 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01002190 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002192 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002193 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2194 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002195 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2196 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002197 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07002198 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01002199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Guenter Roeck62a1d052012-03-24 21:54:41 -07002201 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2202 if (!data)
2203 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002205 data->addr = res->start;
2206 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002207 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002208 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002209 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002210 data->name = it87_devices[sio_data->type].name;
2211 /*
2212 * IT8705F Datasheet 0.4.1, 3h == Version G.
2213 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2214 * These are the first revisions with 16-bit tachometer support.
2215 */
2216 switch (data->type) {
2217 case it87:
2218 if (sio_data->revision >= 0x03) {
2219 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002220 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002221 }
2222 break;
2223 case it8712:
2224 if (sio_data->revision >= 0x08) {
2225 data->features &= ~FEAT_OLD_AUTOPWM;
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002226 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2227 FEAT_FIVE_FANS;
Guenter Roeck483db432012-12-19 22:17:02 +01002228 }
2229 break;
2230 default:
2231 break;
2232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002235 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002236 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2237 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002239 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002241 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002244 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002246 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002247 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002248 if (sio_data->internal & (1 << 0))
2249 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2250 if (sio_data->internal & (1 << 1))
2251 data->in_scaled |= (1 << 7); /* in7 is VSB */
2252 if (sio_data->internal & (1 << 2))
2253 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002254 if (sio_data->internal & (1 << 3))
2255 data->in_scaled |= (1 << 9); /* in9 is AVCC */
Guenter Roeck7bc32d22015-01-17 14:10:24 -08002256 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2257 sio_data->type == it8783) {
Guenter Roeck0531d982012-03-02 11:46:44 -08002258 if (sio_data->internal & (1 << 0))
2259 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2260 if (sio_data->internal & (1 << 1))
2261 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002262 }
2263
Guenter Roeck4573acb2012-03-26 16:17:41 -07002264 data->has_temp = 0x07;
2265 if (sio_data->skip_temp & (1 << 2)) {
2266 if (sio_data->type == it8782
2267 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2268 data->has_temp &= ~(1 << 2);
2269 }
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002272 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002275 err = sysfs_create_group(&dev->kobj, &it87_group);
2276 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002277 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002278
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002279 for (i = 0; i < 10; i++) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002280 if (sio_data->skip_in & (1 << i))
2281 continue;
2282 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2283 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002284 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002285 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2286 err = sysfs_create_file(&dev->kobj,
2287 it87_attributes_in_beep[i]);
2288 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002289 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002290 }
2291 }
2292
Guenter Roeck4573acb2012-03-26 16:17:41 -07002293 for (i = 0; i < 3; i++) {
2294 if (!(data->has_temp & (1 << i)))
2295 continue;
2296 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002297 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002298 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002299 if (has_temp_offset(data)) {
2300 err = sysfs_create_file(&dev->kobj,
2301 it87_attributes_temp_offset[i]);
2302 if (err)
2303 goto error;
2304 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002305 if (sio_data->beep_pin) {
2306 err = sysfs_create_file(&dev->kobj,
2307 it87_attributes_temp_beep[i]);
2308 if (err)
2309 goto error;
2310 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002311 }
2312
Jean Delvare9060f8b2006-08-28 14:24:17 +02002313 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002314 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01002315 for (i = 0; i < 5; i++) {
2316 if (!(data->has_fan & (1 << i)))
2317 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002318 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002319 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002320 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002321
Guenter Roecke1169ba2012-12-19 22:17:01 +01002322 if (i < 3 && !has_16bit_fans(data)) {
2323 err = sysfs_create_file(&dev->kobj,
2324 it87_attributes_fan_div[i]);
2325 if (err)
2326 goto error;
2327 }
2328
Jean Delvared9b327c2010-03-05 22:17:17 +01002329 if (sio_data->beep_pin) {
2330 err = sysfs_create_file(&dev->kobj,
2331 it87_attributes_fan_beep[i]);
2332 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002333 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002334 if (!fan_beep_need_rw)
2335 continue;
2336
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002337 /*
2338 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002339 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002340 * for it.
2341 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002342 if (sysfs_chmod_file(&dev->kobj,
2343 it87_attributes_fan_beep[i],
2344 S_IRUGO | S_IWUSR))
2345 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2346 i + 1);
2347 fan_beep_need_rw = 0;
2348 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002349 }
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002352 for (i = 0; i < 3; i++) {
2353 if (sio_data->skip_pwm & (1 << i))
2354 continue;
2355 err = sysfs_create_group(&dev->kobj,
2356 &it87_group_pwm[i]);
2357 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002358 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002359
2360 if (!has_old_autopwm(data))
2361 continue;
2362 err = sysfs_create_group(&dev->kobj,
2363 &it87_group_autopwm[i]);
2364 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002365 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368
Jean Delvare895ff262009-12-09 20:35:47 +01002369 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002370 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002371 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002372 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002373 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2374 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002375 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002376 }
2377
Jean Delvare738e5e02010-08-14 21:08:50 +02002378 /* Export labels for internal sensors */
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002379 for (i = 0; i < 4; i++) {
Jean Delvare738e5e02010-08-14 21:08:50 +02002380 if (!(sio_data->internal & (1 << i)))
2381 continue;
2382 err = sysfs_create_file(&dev->kobj,
2383 it87_attributes_label[i]);
2384 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002385 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002386 }
2387
Tony Jones1beeffe2007-08-20 13:46:20 -07002388 data->hwmon_dev = hwmon_device_register(dev);
2389 if (IS_ERR(data->hwmon_dev)) {
2390 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002391 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393
2394 return 0;
2395
Guenter Roeck62a1d052012-03-24 21:54:41 -07002396error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002397 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return err;
2399}
2400
Bill Pemberton281dfd02012-11-19 13:25:51 -05002401static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002403 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Tony Jones1beeffe2007-08-20 13:46:20 -07002405 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002406 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return 0;
2409}
2410
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002411/*
2412 * Must be called with data->update_lock held, except during initialization.
2413 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2414 * would slow down the IT87 access and should not be necessary.
2415 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002416static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002418 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2419 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002422/*
2423 * Must be called with data->update_lock held, except during initialization.
2424 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2425 * would slow down the IT87 access and should not be necessary.
2426 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002427static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002429 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2430 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
2433/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002434static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002436 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002437 /*
2438 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002440 * disable pwm control to protect the user.
2441 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002442 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if ((tmp & 0x87) == 0) {
2444 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002445 /*
2446 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002448 * inverting all fan speed values.
2449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 int i;
2451 u8 pwm[3];
2452
2453 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002454 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 IT87_REG_PWM(i));
2456
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002457 /*
2458 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 * might be correct, as suspicious as it seems, so we
2460 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002461 * PWM interface).
2462 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002464 dev_info(dev,
2465 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002466 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 tmp | 0x87);
2468 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002469 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 IT87_REG_PWM(i),
2471 0x7f & ~pwm[i]);
2472 return 1;
2473 }
2474
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002475 dev_info(dev,
2476 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002479 dev_info(dev,
2480 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return 0;
2482 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002483 dev_info(dev,
2484 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
2486
2487 return 1;
2488}
2489
2490/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002491static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +09002493 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002494 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002496 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002498 /*
2499 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002500 * - If it is in automatic mode, setting to manual mode should set
2501 * the fan to full speed by default.
2502 * - If it is in manual mode, we need a mapping to temperature
2503 * channels to use when later setting to automatic mode later.
2504 * Use a 1:1 mapping by default (we are clueless.)
2505 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002506 * prior to switching to a different mode.
2507 * Note that this is no longer needed for the IT8721F and later, as
2508 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002509 * manual duty cycle.
2510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002512 data->pwm_temp_map[i] = i;
2513 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002514 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 }
2516
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002517 /*
2518 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002519 * registers. For low voltage limits it makes no sense and triggers
2520 * alarms, so change to 0 instead. For high temperature limits, it
2521 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002522 * but is still confusing, so change to 127 degrees C.
2523 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002524 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002525 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002526 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002527 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002528 }
2529 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002530 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002531 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002532 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002533 }
2534
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002535 /*
2536 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002537 * set to two different sensor types and we can't guess which one
2538 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002539 * run-time through the temp{1-3}_type sysfs accessors if needed.
2540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002543 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if ((tmp & 0xff) == 0) {
2545 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002546 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
2548
2549 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002550 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002551 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002552 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002554 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002555 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2556 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002558 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002560 /* Set tachometers to 16-bit mode if needed */
2561 if (has_fan16_config(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002562 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002563 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002564 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002565 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002566 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002567 tmp | 0x07);
2568 }
Guenter Roeck9faf28c2015-02-12 07:11:38 -08002569 }
2570
2571 /* Check for additional fans */
2572 if (has_five_fans(data)) {
2573 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2574 if (tmp & (1 << 4))
2575 data->has_fan |= (1 << 3); /* fan4 enabled */
2576 if (tmp & (1 << 5))
2577 data->has_fan |= (1 << 4); /* fan5 enabled */
Jean Delvare17d648b2006-08-28 14:23:46 +02002578 }
2579
Jean Delvare591ec652009-12-09 20:35:48 +01002580 /* Fan input pins may be used for alternative functions */
2581 data->has_fan &= ~sio_data->skip_fan;
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002584 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002585 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 | (update_vbat ? 0x41 : 0x01));
2587}
2588
Jean Delvareb99883d2010-03-05 22:17:15 +01002589static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2590{
2591 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002592 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002593 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002594 data->pwm_duty[nr] = it87_read_value(data,
2595 IT87_REG_PWM_DUTY(nr));
2596 } else {
2597 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2598 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2599 else /* Manual mode */
2600 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2601 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002602
2603 if (has_old_autopwm(data)) {
2604 int i;
2605
2606 for (i = 0; i < 5 ; i++)
2607 data->auto_temp[nr][i] = it87_read_value(data,
2608 IT87_REG_AUTO_TEMP(nr, i));
2609 for (i = 0; i < 3 ; i++)
2610 data->auto_pwm[nr][i] = it87_read_value(data,
2611 IT87_REG_AUTO_PWM(nr, i));
2612 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002613}
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615static struct it87_data *it87_update_device(struct device *dev)
2616{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002617 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 int i;
2619
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002620 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2623 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002625 /*
2626 * Cleared after each update, so reenable. Value
2627 * returned by this read will be previous value
2628 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002629 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002630 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002633 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002634 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002635 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002636 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002637 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002638 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
Jean Delvare3543a532006-08-28 14:27:25 +02002640 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002641 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Rudolf Marekc145d5c2014-01-29 20:40:08 +01002642 if (data->type == it8603)
2643 data->in[9][0] = it87_read_value(data, 0x2f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Jean Delvarec7f1f712007-09-03 17:11:46 +02002645 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002646 /* Skip disabled fans */
2647 if (!(data->has_fan & (1 << i)))
2648 continue;
2649
Guenter Roecke1169ba2012-12-19 22:17:01 +01002650 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002651 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002652 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002653 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002654 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002655 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002656 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002657 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002658 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002659 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002663 if (!(data->has_temp & (1 << i)))
2664 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002665 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002666 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002667 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002668 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002669 data->temp[i][2] =
2670 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002671 if (has_temp_offset(data))
2672 data->temp[i][3] =
2673 it87_read_value(data,
2674 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
2676
Jean Delvare17d648b2006-08-28 14:23:46 +02002677 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002678 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002679 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002680 data->fan_div[0] = i & 0x07;
2681 data->fan_div[1] = (i >> 3) & 0x07;
2682 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002686 it87_read_value(data, IT87_REG_ALARM1) |
2687 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2688 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002689 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002690
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002691 data->fan_main_ctrl = it87_read_value(data,
2692 IT87_REG_FAN_MAIN_CTRL);
2693 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002694 for (i = 0; i < 3; i++)
2695 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002697 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002698 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002699 /*
2700 * The IT8705F does not have VID capability.
2701 * The IT8718F and later don't use IT87_REG_VID for the
2702 * same purpose.
2703 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002704 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002705 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002706 /*
2707 * The older IT8712F revisions had only 5 VID pins,
2708 * but we assume it is always safe to read 6 bits.
2709 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002710 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
2712 data->last_updated = jiffies;
2713 data->valid = 1;
2714 }
2715
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002716 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 return data;
2719}
2720
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002721static int __init it87_device_add(unsigned short address,
2722 const struct it87_sio_data *sio_data)
2723{
2724 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002725 .start = address + IT87_EC_OFFSET,
2726 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002727 .name = DRVNAME,
2728 .flags = IORESOURCE_IO,
2729 };
2730 int err;
2731
Jean Delvareb9acb642009-01-07 16:37:35 +01002732 err = acpi_check_resource_conflict(&res);
2733 if (err)
2734 goto exit;
2735
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002736 pdev = platform_device_alloc(DRVNAME, address);
2737 if (!pdev) {
2738 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002739 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002740 goto exit;
2741 }
2742
2743 err = platform_device_add_resources(pdev, &res, 1);
2744 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002745 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002746 goto exit_device_put;
2747 }
2748
2749 err = platform_device_add_data(pdev, sio_data,
2750 sizeof(struct it87_sio_data));
2751 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002752 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002753 goto exit_device_put;
2754 }
2755
2756 err = platform_device_add(pdev);
2757 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002758 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002759 goto exit_device_put;
2760 }
2761
2762 return 0;
2763
2764exit_device_put:
2765 platform_device_put(pdev);
2766exit:
2767 return err;
2768}
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770static int __init sm_it87_init(void)
2771{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002772 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002773 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002774 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002775
Jean Delvare98dd22c2008-10-09 15:33:58 +02002776 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002777 err = it87_find(&isa_address, &sio_data);
2778 if (err)
2779 return err;
2780 err = platform_driver_register(&it87_driver);
2781 if (err)
2782 return err;
2783
2784 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002785 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002786 platform_driver_unregister(&it87_driver);
2787 return err;
2788 }
2789
2790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791}
2792
2793static void __exit sm_it87_exit(void)
2794{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002795 platform_device_unregister(pdev);
2796 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797}
2798
2799
Jean Delvare7c81c602014-01-29 20:40:08 +01002800MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002801MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802module_param(update_vbat, bool, 0);
2803MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2804module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002805MODULE_PARM_DESC(fix_pwm_polarity,
2806 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807MODULE_LICENSE("GPL");
2808
2809module_init(sm_it87_init);
2810module_exit(sm_it87_exit);