blob: 29632e8492854b074360a448dc6542982182435a [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 *
13 * Supports: IT8705F Super I/O chip w/LPC interface
14 * IT8712F Super I/O chip w/LPC interface
15 * IT8716F Super I/O chip w/LPC interface
16 * IT8718F Super I/O chip w/LPC interface
17 * IT8720F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020018 * IT8721F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010019 * IT8726F Super I/O chip w/LPC interface
Jean Delvare16b5dda2012-01-16 22:51:48 +010020 * IT8728F Super I/O chip w/LPC interface
Jean Delvare44c1bcd2010-10-28 20:31:51 +020021 * IT8758E Super I/O chip w/LPC interface
Guenter Roeck0531d982012-03-02 11:46:44 -080022 * IT8782F Super I/O chip w/LPC interface
23 * IT8783E/F Super I/O chip w/LPC interface
Jean Delvare5f2dc792010-03-05 22:17:18 +010024 * Sis950 A clone of the IT8705F
25 *
26 * Copyright (C) 2001 Chris Gauthron
27 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
28 *
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Joe Perchesa8ca1032011-01-12 21:55:10 +010044#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/module.h>
47#include <linux/init.h>
48#include <linux/slab.h>
49#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020050#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040051#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020052#include <linux/hwmon-sysfs.h>
53#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040054#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010055#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020056#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020057#include <linux/string.h>
58#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010059#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020060#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
corentin.labbeb74f3fd2007-06-13 20:27:36 +020062#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Guenter Roeck0531d982012-03-02 11:46:44 -080064enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8782,
65 it8783 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jean Delvare67b671b2007-12-06 23:13:42 +010067static unsigned short force_id;
68module_param(force_id, ushort, 0);
69MODULE_PARM_DESC(force_id, "Override the detected device ID");
70
corentin.labbeb74f3fd2007-06-13 20:27:36 +020071static struct platform_device *pdev;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define REG 0x2e /* The register to read/write */
74#define DEV 0x07 /* Register: Logical device select */
75#define VAL 0x2f /* The value to read/write */
76#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010077
78/* The device with the IT8718F/IT8720F VID value in it */
79#define GPIO 0x07
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define DEVID 0x20 /* Register: Device ID */
82#define DEVREV 0x22 /* Register: Device Revision */
83
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020084static inline int superio_inb(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 outb(reg, REG);
87 return inb(VAL);
88}
89
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020090static inline void superio_outb(int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +020091{
92 outb(reg, REG);
93 outb(val, VAL);
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int superio_inw(int reg)
97{
98 int val;
99 outb(reg++, REG);
100 val = inb(VAL) << 8;
101 outb(reg, REG);
102 val |= inb(VAL);
103 return val;
104}
105
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200106static inline void superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200109 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200112static inline int superio_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200114 /*
115 * Try to reserve REG and REG + 1 for exclusive access.
116 */
117 if (!request_muxed_region(REG, 2, DRVNAME))
118 return -EBUSY;
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 outb(0x87, REG);
121 outb(0x01, REG);
122 outb(0x55, REG);
123 outb(0x55, REG);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200127static inline void superio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 outb(0x02, REG);
130 outb(0x02, VAL);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200131 release_region(REG, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Jean Delvare87673dd2006-08-28 14:37:19 +0200134/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define IT8712F_DEVID 0x8712
136#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200137#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200138#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100139#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200140#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400141#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100142#define IT8728F_DEVID 0x8728
Guenter Roeck0531d982012-03-02 11:46:44 -0800143#define IT8782F_DEVID 0x8782
144#define IT8783E_DEVID 0x8783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define IT87_ACT_REG 0x30
146#define IT87_BASE_REG 0x60
147
Jean Delvare87673dd2006-08-28 14:37:19 +0200148/* Logical device 7 registers (IT8712F and later) */
Guenter Roeck0531d982012-03-02 11:46:44 -0800149#define IT87_SIO_GPIO1_REG 0x25
Jean Delvare895ff262009-12-09 20:35:47 +0100150#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100151#define IT87_SIO_GPIO5_REG 0x29
Guenter Roeck0531d982012-03-02 11:46:44 -0800152#define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
Jean Delvare87673dd2006-08-28 14:37:19 +0200153#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
Guenter Roeck0531d982012-03-02 11:46:44 -0800154#define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
Jean Delvare87673dd2006-08-28 14:37:19 +0200155#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100156#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030159static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030162static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/* Many IT87 constants specified below */
165
166/* Length of ISA address segment */
167#define IT87_EXTENT 8
168
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500169/* Length of ISA address segment for Environmental Controller */
170#define IT87_EC_EXTENT 2
171
172/* Offset of EC registers from ISA base address */
173#define IT87_EC_OFFSET 5
174
175/* Where are the ISA address/data registers relative to the EC base address */
176#define IT87_ADDR_REG_OFFSET 0
177#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/*----- The IT87 registers -----*/
180
181#define IT87_REG_CONFIG 0x00
182
183#define IT87_REG_ALARM1 0x01
184#define IT87_REG_ALARM2 0x02
185#define IT87_REG_ALARM3 0x03
186
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800187/*
188 * The IT8718F and IT8720F have the VID value in a different register, in
189 * Super-I/O configuration space.
190 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#define IT87_REG_VID 0x0a
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800192/*
193 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
194 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
195 * mode.
196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200198#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
201
Jean Delvarec7f1f712007-09-03 17:11:46 +0200202static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
203static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
204static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
205static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Guenter Roeck161d8982012-12-19 22:17:01 +0100206static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#define IT87_REG_FAN_MAIN_CTRL 0x13
209#define IT87_REG_FAN_CTL 0x14
210#define IT87_REG_PWM(nr) (0x15 + (nr))
Jean Delvare6229cdb2010-12-08 16:27:22 +0100211#define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213#define IT87_REG_VIN(nr) (0x20 + (nr))
214#define IT87_REG_TEMP(nr) (0x29 + (nr))
215
216#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
217#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
218#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
219#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#define IT87_REG_VIN_ENABLE 0x50
222#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700223#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100224#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226#define IT87_REG_CHIPID 0x58
227
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100228#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
229#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
230
Guenter Roeck483db432012-12-19 22:17:02 +0100231struct it87_devices {
232 const char *name;
233 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100234 u8 peci_mask;
235 u8 old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +0100236};
237
238#define FEAT_12MV_ADC (1 << 0)
239#define FEAT_NEWER_AUTOPWM (1 << 1)
240#define FEAT_OLD_AUTOPWM (1 << 2)
241#define FEAT_16BIT_FANS (1 << 3)
242#define FEAT_TEMP_OFFSET (1 << 4)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100243#define FEAT_TEMP_PECI (1 << 5)
Guenter Roeck19529782012-12-19 22:17:02 +0100244#define FEAT_TEMP_OLD_PECI (1 << 6)
Guenter Roeck483db432012-12-19 22:17:02 +0100245
246static const struct it87_devices it87_devices[] = {
247 [it87] = {
248 .name = "it87",
249 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
250 },
251 [it8712] = {
252 .name = "it8712",
253 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
254 },
255 [it8716] = {
256 .name = "it8716",
257 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET,
258 },
259 [it8718] = {
260 .name = "it8718",
Guenter Roeck19529782012-12-19 22:17:02 +0100261 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
262 | FEAT_TEMP_OLD_PECI,
263 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100264 },
265 [it8720] = {
266 .name = "it8720",
Guenter Roeck19529782012-12-19 22:17:02 +0100267 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
268 | FEAT_TEMP_OLD_PECI,
269 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100270 },
271 [it8721] = {
272 .name = "it8721",
273 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck19529782012-12-19 22:17:02 +0100274 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI,
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100275 .peci_mask = 0x05,
Guenter Roeck19529782012-12-19 22:17:02 +0100276 .old_peci_mask = 0x02, /* Actually reports PCH */
Guenter Roeck483db432012-12-19 22:17:02 +0100277 },
278 [it8728] = {
279 .name = "it8728",
280 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100281 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
282 .peci_mask = 0x07,
Guenter Roeck483db432012-12-19 22:17:02 +0100283 },
284 [it8782] = {
285 .name = "it8782",
Guenter Roeck19529782012-12-19 22:17:02 +0100286 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
287 | FEAT_TEMP_OLD_PECI,
288 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100289 },
290 [it8783] = {
291 .name = "it8783",
Guenter Roeck19529782012-12-19 22:17:02 +0100292 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
293 | FEAT_TEMP_OLD_PECI,
294 .old_peci_mask = 0x4,
Guenter Roeck483db432012-12-19 22:17:02 +0100295 },
296};
297
298#define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
299#define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
300#define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
301#define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
302#define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100303#define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
304 ((data)->peci_mask & (1 << nr)))
Guenter Roeck19529782012-12-19 22:17:02 +0100305#define has_temp_old_peci(data, nr) \
306 (((data)->features & FEAT_TEMP_OLD_PECI) && \
307 ((data)->old_peci_mask & (1 << nr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200309struct it87_sio_data {
310 enum chips type;
311 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200312 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200313 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100314 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200315 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100316 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700317 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100318 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100319 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200320 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700321 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200322};
323
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800324/*
325 * For each registered chip, we need to keep some data in memory.
326 * The structure is dynamically allocated.
327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700329 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 enum chips type;
Guenter Roeck483db432012-12-19 22:17:02 +0100331 u16 features;
Guenter Roeck19529782012-12-19 22:17:02 +0100332 u8 peci_mask;
333 u8 old_peci_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200335 unsigned short addr;
336 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100337 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 char valid; /* !=0 if following fields are valid */
339 unsigned long last_updated; /* In jiffies */
340
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200341 u16 in_scaled; /* Internal voltage sensors are scaled */
Guenter Roeck929c6a52012-12-19 22:17:00 +0100342 u8 in[9][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200343 u8 has_fan; /* Bitfield, fans enabled */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100344 u16 fan[5][2]; /* Register values, [nr][0]=fan, [1]=min */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700345 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck161d8982012-12-19 22:17:01 +0100346 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
Guenter Roeck19529782012-12-19 22:17:02 +0100347 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
348 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 u8 fan_div[3]; /* Register encoding, shifted right */
350 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100351 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100353 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100355 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100356
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800357 /*
358 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100359 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
360 * 7, and we want to preserve settings on mode changes, so we have
361 * to track all values separately.
362 * Starting with the IT8721F, the manual PWM duty cycles are stored
363 * in separate registers (8-bit values), so the separate tracking
364 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800365 * simple.
366 */
Jean Delvareb99883d2010-03-05 22:17:15 +0100367 u8 pwm_ctrl[3]; /* Register value */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100368 u8 pwm_duty[3]; /* Manual PWM value set by user */
Jean Delvareb99883d2010-03-05 22:17:15 +0100369 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100370
371 /* Automatic fan speed control registers */
372 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
373 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374};
375
Guenter Roeck0531d982012-03-02 11:46:44 -0800376static int adc_lsb(const struct it87_data *data, int nr)
377{
378 int lsb = has_12mv_adc(data) ? 12 : 16;
379 if (data->in_scaled & (1 << nr))
380 lsb <<= 1;
381 return lsb;
382}
383
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200384static u8 in_to_reg(const struct it87_data *data, int nr, long val)
385{
Guenter Roeck0531d982012-03-02 11:46:44 -0800386 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
Guenter Roeck2a844c12013-01-09 08:09:34 -0800387 return clamp_val(val, 0, 255);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200388}
389
390static int in_from_reg(const struct it87_data *data, int nr, int val)
391{
Guenter Roeck0531d982012-03-02 11:46:44 -0800392 return val * adc_lsb(data, nr);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200393}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200394
395static inline u8 FAN_TO_REG(long rpm, int div)
396{
397 if (rpm == 0)
398 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800399 rpm = clamp_val(rpm, 1, 1000000);
400 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200401}
402
403static inline u16 FAN16_TO_REG(long rpm)
404{
405 if (rpm == 0)
406 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800407 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
Jean Delvare0df6454d2010-10-28 20:31:51 +0200408}
409
410#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
411 1350000 / ((val) * (div)))
412/* The divider is fixed to 2 in 16-bit mode */
413#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
414 1350000 / ((val) * 2))
415
Guenter Roeck2a844c12013-01-09 08:09:34 -0800416#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
417 ((val) + 500) / 1000), -128, 127))
Jean Delvare0df6454d2010-10-28 20:31:51 +0200418#define TEMP_FROM_REG(val) ((val) * 1000)
419
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200420static u8 pwm_to_reg(const struct it87_data *data, long val)
421{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100422 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200423 return val;
424 else
425 return val >> 1;
426}
427
428static int pwm_from_reg(const struct it87_data *data, u8 reg)
429{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100430 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200431 return reg;
432 else
433 return (reg & 0x7f) << 1;
434}
435
Jean Delvare0df6454d2010-10-28 20:31:51 +0200436
437static int DIV_TO_REG(int val)
438{
439 int answer = 0;
440 while (answer < 7 && (val >>= 1))
441 answer++;
442 return answer;
443}
444#define DIV_FROM_REG(val) (1 << (val))
445
446static const unsigned int pwm_freq[8] = {
447 48000000 / 128,
448 24000000 / 128,
449 12000000 / 128,
450 8000000 / 128,
451 6000000 / 128,
452 3000000 / 128,
453 1500000 / 128,
454 750000 / 128,
455};
456
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200457static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500458static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200460static int it87_read_value(struct it87_data *data, u8 reg);
461static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200463static int it87_check_pwm(struct device *dev);
464static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200467static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100468 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200469 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200470 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100471 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200472 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500473 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200474};
475
Jean Delvare20ad93d2005-06-05 11:53:25 +0200476static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100477 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100479 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
480 int nr = sattr->nr;
481 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100484 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Guenter Roeck929c6a52012-12-19 22:17:00 +0100487static ssize_t set_in(struct device *dev, struct device_attribute *attr,
488 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100490 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
491 int nr = sattr->nr;
492 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200493
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200494 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100495 unsigned long val;
496
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100497 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100500 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100501 data->in[nr][index] = in_to_reg(data, nr, val);
502 it87_write_value(data,
503 index == 1 ? IT87_REG_VIN_MIN(nr)
504 : IT87_REG_VIN_MAX(nr),
505 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100506 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return count;
508}
509
Guenter Roeck929c6a52012-12-19 22:17:00 +0100510static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
511static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
512 0, 1);
513static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
514 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Guenter Roeck929c6a52012-12-19 22:17:00 +0100516static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
517static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
518 1, 1);
519static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
520 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Guenter Roeck929c6a52012-12-19 22:17:00 +0100522static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
523static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
524 2, 1);
525static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
526 2, 2);
527
528static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
529static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
530 3, 1);
531static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
532 3, 2);
533
534static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
535static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
536 4, 1);
537static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
538 4, 2);
539
540static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
541static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
542 5, 1);
543static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
544 5, 2);
545
546static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
547static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
548 6, 1);
549static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
550 6, 2);
551
552static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
553static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
554 7, 1);
555static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
556 7, 2);
557
558static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200561static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100562 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100564 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
565 int nr = sattr->nr;
566 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200568
Guenter Roeck60ca3852012-12-19 22:17:00 +0100569 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200571
Guenter Roeck60ca3852012-12-19 22:17:00 +0100572static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
573 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100575 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
576 int nr = sattr->nr;
577 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200578 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100579 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100580 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100581
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100582 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100583 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100585 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100586
587 switch (index) {
588 default:
589 case 1:
590 reg = IT87_REG_TEMP_LOW(nr);
591 break;
592 case 2:
593 reg = IT87_REG_TEMP_HIGH(nr);
594 break;
595 case 3:
596 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
597 if (!(regval & 0x80)) {
598 regval |= 0x80;
599 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
600 }
601 data->valid = 0;
602 reg = IT87_REG_TEMP_OFFSET[nr];
603 break;
604 }
605
Guenter Roeck60ca3852012-12-19 22:17:00 +0100606 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100607 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100608 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return count;
610}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200611
Guenter Roeck60ca3852012-12-19 22:17:00 +0100612static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
613static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
614 0, 1);
615static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
616 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100617static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
618 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100619static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
620static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
621 1, 1);
622static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
623 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100624static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
625 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100626static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
627static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
628 2, 1);
629static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
630 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100631static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
632 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Guenter Roeck2cece012012-12-19 22:17:01 +0100634static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
635 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200637 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
638 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800640 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100641 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100642
Guenter Roeck19529782012-12-19 22:17:02 +0100643 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
644 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100645 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (reg & (1 << nr))
647 return sprintf(buf, "3\n"); /* thermal diode */
648 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200649 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return sprintf(buf, "0\n"); /* disabled */
651}
Guenter Roeck2cece012012-12-19 22:17:01 +0100652
653static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
654 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200656 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
657 int nr = sensor_attr->index;
658
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200659 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100660 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100661 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100662
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100663 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100664 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Jean Delvare8acf07c2010-04-14 16:14:09 +0200666 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
667 reg &= ~(1 << nr);
668 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100669 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
670 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100671 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
672 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
673 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200674 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100675 dev_warn(dev,
676 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200677 val = 4;
678 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100679 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200681 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200682 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200683 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100684 else if (has_temp_peci(data, nr) && val == 6)
685 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100686 else if (has_temp_old_peci(data, nr) && val == 6)
687 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200688 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200690
691 mutex_lock(&data->update_lock);
692 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100693 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200694 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100695 if (has_temp_old_peci(data, nr))
696 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200697 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100698 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return count;
700}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Guenter Roeck2cece012012-12-19 22:17:01 +0100702static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
703 set_temp_type, 0);
704static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
705 set_temp_type, 1);
706static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
707 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100710
711static int pwm_mode(const struct it87_data *data, int nr)
712{
713 int ctrl = data->fan_main_ctrl & (1 << nr);
714
715 if (ctrl == 0) /* Full speed */
716 return 0;
717 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
718 return 2;
719 else /* Manual mode */
720 return 1;
721}
722
Jean Delvare20ad93d2005-06-05 11:53:25 +0200723static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +0100724 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100726 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
727 int nr = sattr->nr;
728 int index = sattr->index;
729 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200731
Guenter Roecke1169ba2012-12-19 22:17:01 +0100732 speed = has_16bit_fans(data) ?
733 FAN16_FROM_REG(data->fan[nr][index]) :
734 FAN_FROM_REG(data->fan[nr][index],
735 DIV_FROM_REG(data->fan_div[nr]));
736 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100738
Jean Delvare20ad93d2005-06-05 11:53:25 +0200739static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
740 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200742 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
743 int nr = sensor_attr->index;
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct it87_data *data = it87_update_device(dev);
746 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
747}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100748static ssize_t show_pwm_enable(struct device *dev,
749 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200751 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
752 int nr = sensor_attr->index;
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100755 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200757static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
758 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200760 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
761 int nr = sensor_attr->index;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200764 return sprintf(buf, "%d\n",
765 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100767static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
768 char *buf)
769{
770 struct it87_data *data = it87_update_device(dev);
771 int index = (data->fan_ctl >> 4) & 0x07;
772
773 return sprintf(buf, "%u\n", pwm_freq[index]);
774}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100775
776static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
777 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100779 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
780 int nr = sattr->nr;
781 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200782
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200783 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100784 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100785 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100787 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100788 return -EINVAL;
789
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100790 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +0100791
792 if (has_16bit_fans(data)) {
793 data->fan[nr][index] = FAN16_TO_REG(val);
794 it87_write_value(data, IT87_REG_FAN_MIN[nr],
795 data->fan[nr][index] & 0xff);
796 it87_write_value(data, IT87_REG_FANX_MIN[nr],
797 data->fan[nr][index] >> 8);
798 } else {
799 reg = it87_read_value(data, IT87_REG_FAN_DIV);
800 switch (nr) {
801 case 0:
802 data->fan_div[nr] = reg & 0x07;
803 break;
804 case 1:
805 data->fan_div[nr] = (reg >> 3) & 0x07;
806 break;
807 case 2:
808 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
809 break;
810 }
811 data->fan[nr][index] =
812 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
813 it87_write_value(data, IT87_REG_FAN_MIN[nr],
814 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -0800815 }
816
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100817 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return count;
819}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100820
Jean Delvare20ad93d2005-06-05 11:53:25 +0200821static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
822 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200824 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
825 int nr = sensor_attr->index;
826
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200827 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100828 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200829 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 u8 old;
831
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100832 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100833 return -EINVAL;
834
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100835 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200836 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200838 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100839 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 switch (nr) {
842 case 0:
843 case 1:
844 data->fan_div[nr] = DIV_TO_REG(val);
845 break;
846 case 2:
847 if (val < 8)
848 data->fan_div[nr] = 1;
849 else
850 data->fan_div[nr] = 3;
851 }
852 val = old & 0x80;
853 val |= (data->fan_div[0] & 0x07);
854 val |= (data->fan_div[1] & 0x07) << 3;
855 if (data->fan_div[2] == 3)
856 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200857 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200859 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100860 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
861 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200862
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100863 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return count;
865}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100866
867/* Returns 0 if OK, -EINVAL otherwise */
868static int check_trip_points(struct device *dev, int nr)
869{
870 const struct it87_data *data = dev_get_drvdata(dev);
871 int i, err = 0;
872
873 if (has_old_autopwm(data)) {
874 for (i = 0; i < 3; i++) {
875 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
876 err = -EINVAL;
877 }
878 for (i = 0; i < 2; i++) {
879 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
880 err = -EINVAL;
881 }
882 }
883
884 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100885 dev_err(dev,
886 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100887 dev_err(dev, "Adjust the trip points and try again\n");
888 }
889 return err;
890}
891
Jean Delvare20ad93d2005-06-05 11:53:25 +0200892static ssize_t set_pwm_enable(struct device *dev,
893 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200895 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
896 int nr = sensor_attr->index;
897
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200898 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100899 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100901 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100902 return -EINVAL;
903
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100904 /* Check trip points before switching to automatic mode */
905 if (val == 2) {
906 if (check_trip_points(dev, nr) < 0)
907 return -EINVAL;
908 }
909
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100910 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (val == 0) {
913 int tmp;
914 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200915 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
916 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* set on/off mode */
918 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100919 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
920 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100921 } else {
922 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +0100923 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +0100924 data->pwm_temp_map[nr] :
925 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +0100926 else /* Automatic mode */
927 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
928 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* set SmartGuardian mode */
930 data->fan_main_ctrl |= (1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100931 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
932 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100935 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return count;
937}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200938static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
939 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200941 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
942 int nr = sensor_attr->index;
943
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200944 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100945 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100947 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return -EINVAL;
949
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100950 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +0100951 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800952 /*
953 * If we are in automatic mode, the PWM duty cycle register
954 * is read-only so we can't write the value.
955 */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100956 if (data->pwm_ctrl[nr] & 0x80) {
957 mutex_unlock(&data->update_lock);
958 return -EBUSY;
959 }
960 data->pwm_duty[nr] = pwm_to_reg(data, val);
961 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
962 data->pwm_duty[nr]);
963 } else {
964 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800965 /*
966 * If we are in manual mode, write the duty cycle immediately;
967 * otherwise, just store it for later use.
968 */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100969 if (!(data->pwm_ctrl[nr] & 0x80)) {
970 data->pwm_ctrl[nr] = data->pwm_duty[nr];
971 it87_write_value(data, IT87_REG_PWM(nr),
972 data->pwm_ctrl[nr]);
973 }
Jean Delvareb99883d2010-03-05 22:17:15 +0100974 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100975 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return count;
977}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100978static ssize_t set_pwm_freq(struct device *dev,
979 struct device_attribute *attr, const char *buf, size_t count)
980{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200981 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100982 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100983 int i;
984
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100985 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100986 return -EINVAL;
987
Jean Delvaref8d0c192007-02-14 21:15:02 +0100988 /* Search for the nearest available frequency */
989 for (i = 0; i < 7; i++) {
990 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
991 break;
992 }
993
994 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200995 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100996 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200997 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100998 mutex_unlock(&data->update_lock);
999
1000 return count;
1001}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001002static ssize_t show_pwm_temp_map(struct device *dev,
1003 struct device_attribute *attr, char *buf)
1004{
1005 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1006 int nr = sensor_attr->index;
1007
1008 struct it87_data *data = it87_update_device(dev);
1009 int map;
1010
1011 if (data->pwm_temp_map[nr] < 3)
1012 map = 1 << data->pwm_temp_map[nr];
1013 else
1014 map = 0; /* Should never happen */
1015 return sprintf(buf, "%d\n", map);
1016}
1017static ssize_t set_pwm_temp_map(struct device *dev,
1018 struct device_attribute *attr, const char *buf, size_t count)
1019{
1020 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1021 int nr = sensor_attr->index;
1022
1023 struct it87_data *data = dev_get_drvdata(dev);
1024 long val;
1025 u8 reg;
1026
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001027 /*
1028 * This check can go away if we ever support automatic fan speed
1029 * control on newer chips.
1030 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001031 if (!has_old_autopwm(data)) {
1032 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1033 return -EINVAL;
1034 }
1035
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001036 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001037 return -EINVAL;
1038
1039 switch (val) {
1040 case (1 << 0):
1041 reg = 0x00;
1042 break;
1043 case (1 << 1):
1044 reg = 0x01;
1045 break;
1046 case (1 << 2):
1047 reg = 0x02;
1048 break;
1049 default:
1050 return -EINVAL;
1051 }
1052
1053 mutex_lock(&data->update_lock);
1054 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001055 /*
1056 * If we are in automatic mode, write the temp mapping immediately;
1057 * otherwise, just store it for later use.
1058 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001059 if (data->pwm_ctrl[nr] & 0x80) {
1060 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1061 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1062 }
1063 mutex_unlock(&data->update_lock);
1064 return count;
1065}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001067static ssize_t show_auto_pwm(struct device *dev,
1068 struct device_attribute *attr, char *buf)
1069{
1070 struct it87_data *data = it87_update_device(dev);
1071 struct sensor_device_attribute_2 *sensor_attr =
1072 to_sensor_dev_attr_2(attr);
1073 int nr = sensor_attr->nr;
1074 int point = sensor_attr->index;
1075
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001076 return sprintf(buf, "%d\n",
1077 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001078}
1079
1080static ssize_t set_auto_pwm(struct device *dev,
1081 struct device_attribute *attr, const char *buf, size_t count)
1082{
1083 struct it87_data *data = dev_get_drvdata(dev);
1084 struct sensor_device_attribute_2 *sensor_attr =
1085 to_sensor_dev_attr_2(attr);
1086 int nr = sensor_attr->nr;
1087 int point = sensor_attr->index;
1088 long val;
1089
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001090 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001091 return -EINVAL;
1092
1093 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001094 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001095 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1096 data->auto_pwm[nr][point]);
1097 mutex_unlock(&data->update_lock);
1098 return count;
1099}
1100
1101static ssize_t show_auto_temp(struct device *dev,
1102 struct device_attribute *attr, char *buf)
1103{
1104 struct it87_data *data = it87_update_device(dev);
1105 struct sensor_device_attribute_2 *sensor_attr =
1106 to_sensor_dev_attr_2(attr);
1107 int nr = sensor_attr->nr;
1108 int point = sensor_attr->index;
1109
1110 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1111}
1112
1113static ssize_t set_auto_temp(struct device *dev,
1114 struct device_attribute *attr, const char *buf, size_t count)
1115{
1116 struct it87_data *data = dev_get_drvdata(dev);
1117 struct sensor_device_attribute_2 *sensor_attr =
1118 to_sensor_dev_attr_2(attr);
1119 int nr = sensor_attr->nr;
1120 int point = sensor_attr->index;
1121 long val;
1122
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001123 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001124 return -EINVAL;
1125
1126 mutex_lock(&data->update_lock);
1127 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1128 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1129 data->auto_temp[nr][point]);
1130 mutex_unlock(&data->update_lock);
1131 return count;
1132}
1133
Guenter Roecke1169ba2012-12-19 22:17:01 +01001134static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1135static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1136 0, 1);
1137static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1138 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Guenter Roecke1169ba2012-12-19 22:17:01 +01001140static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1141static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1142 1, 1);
1143static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1144 set_fan_div, 1);
1145
1146static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1147static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1148 2, 1);
1149static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1150 set_fan_div, 2);
1151
1152static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1153static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1154 3, 1);
1155
1156static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1157static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1158 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Guenter Roeckc4458db2012-12-19 22:17:02 +01001160static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1161 show_pwm_enable, set_pwm_enable, 0);
1162static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1163static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1164static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1165 show_pwm_temp_map, set_pwm_temp_map, 0);
1166static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1167 show_auto_pwm, set_auto_pwm, 0, 0);
1168static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1169 show_auto_pwm, set_auto_pwm, 0, 1);
1170static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1171 show_auto_pwm, set_auto_pwm, 0, 2);
1172static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1173 show_auto_pwm, NULL, 0, 3);
1174static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1175 show_auto_temp, set_auto_temp, 0, 1);
1176static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1177 show_auto_temp, set_auto_temp, 0, 0);
1178static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1179 show_auto_temp, set_auto_temp, 0, 2);
1180static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1181 show_auto_temp, set_auto_temp, 0, 3);
1182static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1183 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Guenter Roeckc4458db2012-12-19 22:17:02 +01001185static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1186 show_pwm_enable, set_pwm_enable, 1);
1187static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1188static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1189static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1190 show_pwm_temp_map, set_pwm_temp_map, 1);
1191static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1192 show_auto_pwm, set_auto_pwm, 1, 0);
1193static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1194 show_auto_pwm, set_auto_pwm, 1, 1);
1195static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1196 show_auto_pwm, set_auto_pwm, 1, 2);
1197static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1198 show_auto_pwm, NULL, 1, 3);
1199static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1200 show_auto_temp, set_auto_temp, 1, 1);
1201static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1202 show_auto_temp, set_auto_temp, 1, 0);
1203static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1204 show_auto_temp, set_auto_temp, 1, 2);
1205static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1206 show_auto_temp, set_auto_temp, 1, 3);
1207static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1208 show_auto_temp, set_auto_temp, 1, 4);
1209
1210static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1211 show_pwm_enable, set_pwm_enable, 2);
1212static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1213static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1214static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1215 show_pwm_temp_map, set_pwm_temp_map, 2);
1216static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1217 show_auto_pwm, set_auto_pwm, 2, 0);
1218static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1219 show_auto_pwm, set_auto_pwm, 2, 1);
1220static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1221 show_auto_pwm, set_auto_pwm, 2, 2);
1222static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1223 show_auto_pwm, NULL, 2, 3);
1224static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1225 show_auto_temp, set_auto_temp, 2, 1);
1226static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1227 show_auto_temp, set_auto_temp, 2, 0);
1228static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1229 show_auto_temp, set_auto_temp, 2, 2);
1230static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1231 show_auto_temp, set_auto_temp, 2, 3);
1232static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1233 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001236static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1237 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001240 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
Jean Delvare1d66c642005-04-18 21:16:59 -07001242static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Jean Delvare0124dd72007-11-25 16:16:41 +01001244static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1245 char *buf)
1246{
1247 int bitnr = to_sensor_dev_attr(attr)->index;
1248 struct it87_data *data = it87_update_device(dev);
1249 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1250}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001251
1252static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1253 *attr, const char *buf, size_t count)
1254{
1255 struct it87_data *data = dev_get_drvdata(dev);
1256 long val;
1257 int config;
1258
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001259 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001260 return -EINVAL;
1261
1262 mutex_lock(&data->update_lock);
1263 config = it87_read_value(data, IT87_REG_CONFIG);
1264 if (config < 0) {
1265 count = config;
1266 } else {
1267 config |= 1 << 5;
1268 it87_write_value(data, IT87_REG_CONFIG, config);
1269 /* Invalidate cache to force re-read */
1270 data->valid = 0;
1271 }
1272 mutex_unlock(&data->update_lock);
1273
1274 return count;
1275}
1276
Jean Delvare0124dd72007-11-25 16:16:41 +01001277static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1278static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1279static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1280static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1281static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1282static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1283static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1284static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1285static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1286static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1287static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1288static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1289static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1290static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1291static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1292static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001293static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1294 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001295
Jean Delvared9b327c2010-03-05 22:17:17 +01001296static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1297 char *buf)
1298{
1299 int bitnr = to_sensor_dev_attr(attr)->index;
1300 struct it87_data *data = it87_update_device(dev);
1301 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1302}
1303static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1304 const char *buf, size_t count)
1305{
1306 int bitnr = to_sensor_dev_attr(attr)->index;
1307 struct it87_data *data = dev_get_drvdata(dev);
1308 long val;
1309
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001310 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001311 || (val != 0 && val != 1))
1312 return -EINVAL;
1313
1314 mutex_lock(&data->update_lock);
1315 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1316 if (val)
1317 data->beeps |= (1 << bitnr);
1318 else
1319 data->beeps &= ~(1 << bitnr);
1320 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1321 mutex_unlock(&data->update_lock);
1322 return count;
1323}
1324
1325static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1326 show_beep, set_beep, 1);
1327static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1328static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1329static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1330static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1331static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1332static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1333static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1334/* fanX_beep writability is set later */
1335static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1336static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1337static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1338static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1339static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1340static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1341 show_beep, set_beep, 2);
1342static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1343static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1344
Jean Delvare5f2dc792010-03-05 22:17:18 +01001345static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1346 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Jean Delvare90d66192007-10-08 18:24:35 +02001348 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001349 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001351static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1352 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001354 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001355 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001357 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001358 return -EINVAL;
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 data->vrm = val;
1361
1362 return count;
1363}
1364static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Jean Delvare5f2dc792010-03-05 22:17:18 +01001366static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1367 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 struct it87_data *data = it87_update_device(dev);
1370 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1371}
1372static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001373
Jean Delvare738e5e02010-08-14 21:08:50 +02001374static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1375 char *buf)
1376{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001377 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001378 "+5V",
1379 "5VSB",
1380 "Vbat",
1381 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001382 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001383 "+3.3V",
1384 "3VSB",
1385 "Vbat",
1386 };
1387 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001388 int nr = to_sensor_dev_attr(attr)->index;
1389
Jean Delvare16b5dda2012-01-16 22:51:48 +01001390 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1391 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001392}
1393static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1394static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1395static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1396
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001397static ssize_t show_name(struct device *dev, struct device_attribute
1398 *devattr, char *buf)
1399{
1400 struct it87_data *data = dev_get_drvdata(dev);
1401 return sprintf(buf, "%s\n", data->name);
1402}
1403static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1404
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001405static struct attribute *it87_attributes_in[9][5] = {
1406{
Jean Delvare87808be2006-09-24 21:17:13 +02001407 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001408 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001409 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001410 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001411 NULL
1412}, {
1413 &sensor_dev_attr_in1_input.dev_attr.attr,
1414 &sensor_dev_attr_in1_min.dev_attr.attr,
1415 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001416 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001417 NULL
1418}, {
1419 &sensor_dev_attr_in2_input.dev_attr.attr,
1420 &sensor_dev_attr_in2_min.dev_attr.attr,
1421 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001422 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001423 NULL
1424}, {
1425 &sensor_dev_attr_in3_input.dev_attr.attr,
1426 &sensor_dev_attr_in3_min.dev_attr.attr,
1427 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001428 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001429 NULL
1430}, {
1431 &sensor_dev_attr_in4_input.dev_attr.attr,
1432 &sensor_dev_attr_in4_min.dev_attr.attr,
1433 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001434 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001435 NULL
1436}, {
1437 &sensor_dev_attr_in5_input.dev_attr.attr,
1438 &sensor_dev_attr_in5_min.dev_attr.attr,
1439 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001440 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001441 NULL
1442}, {
1443 &sensor_dev_attr_in6_input.dev_attr.attr,
1444 &sensor_dev_attr_in6_min.dev_attr.attr,
1445 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001446 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001447 NULL
1448}, {
1449 &sensor_dev_attr_in7_input.dev_attr.attr,
1450 &sensor_dev_attr_in7_min.dev_attr.attr,
1451 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001452 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001453 NULL
1454}, {
1455 &sensor_dev_attr_in8_input.dev_attr.attr,
1456 NULL
1457} };
Jean Delvare87808be2006-09-24 21:17:13 +02001458
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001459static const struct attribute_group it87_group_in[9] = {
1460 { .attrs = it87_attributes_in[0] },
1461 { .attrs = it87_attributes_in[1] },
1462 { .attrs = it87_attributes_in[2] },
1463 { .attrs = it87_attributes_in[3] },
1464 { .attrs = it87_attributes_in[4] },
1465 { .attrs = it87_attributes_in[5] },
1466 { .attrs = it87_attributes_in[6] },
1467 { .attrs = it87_attributes_in[7] },
1468 { .attrs = it87_attributes_in[8] },
1469};
1470
Guenter Roeck4573acb2012-03-26 16:17:41 -07001471static struct attribute *it87_attributes_temp[3][6] = {
1472{
Jean Delvare87808be2006-09-24 21:17:13 +02001473 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001474 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001475 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001476 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001477 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001478 NULL
1479} , {
1480 &sensor_dev_attr_temp2_input.dev_attr.attr,
1481 &sensor_dev_attr_temp2_max.dev_attr.attr,
1482 &sensor_dev_attr_temp2_min.dev_attr.attr,
1483 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001484 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001485 NULL
1486} , {
1487 &sensor_dev_attr_temp3_input.dev_attr.attr,
1488 &sensor_dev_attr_temp3_max.dev_attr.attr,
1489 &sensor_dev_attr_temp3_min.dev_attr.attr,
1490 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001491 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001492 NULL
1493} };
Jean Delvare87808be2006-09-24 21:17:13 +02001494
Guenter Roeck4573acb2012-03-26 16:17:41 -07001495static const struct attribute_group it87_group_temp[3] = {
1496 { .attrs = it87_attributes_temp[0] },
1497 { .attrs = it87_attributes_temp[1] },
1498 { .attrs = it87_attributes_temp[2] },
1499};
1500
Guenter Roeck161d8982012-12-19 22:17:01 +01001501static struct attribute *it87_attributes_temp_offset[] = {
1502 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1503 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1504 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1505};
1506
Guenter Roeck4573acb2012-03-26 16:17:41 -07001507static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001508 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001509 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001510 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001511 NULL
1512};
1513
1514static const struct attribute_group it87_group = {
1515 .attrs = it87_attributes,
1516};
1517
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001518static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001519 &sensor_dev_attr_in0_beep.dev_attr.attr,
1520 &sensor_dev_attr_in1_beep.dev_attr.attr,
1521 &sensor_dev_attr_in2_beep.dev_attr.attr,
1522 &sensor_dev_attr_in3_beep.dev_attr.attr,
1523 &sensor_dev_attr_in4_beep.dev_attr.attr,
1524 &sensor_dev_attr_in5_beep.dev_attr.attr,
1525 &sensor_dev_attr_in6_beep.dev_attr.attr,
1526 &sensor_dev_attr_in7_beep.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001527 NULL
1528};
Jean Delvared9b327c2010-03-05 22:17:17 +01001529
Guenter Roeck4573acb2012-03-26 16:17:41 -07001530static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001531 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1532 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1533 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001534};
1535
Guenter Roecke1169ba2012-12-19 22:17:01 +01001536static struct attribute *it87_attributes_fan[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001537 &sensor_dev_attr_fan1_input.dev_attr.attr,
1538 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001539 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1540 NULL
1541}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001542 &sensor_dev_attr_fan2_input.dev_attr.attr,
1543 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001544 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1545 NULL
1546}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001547 &sensor_dev_attr_fan3_input.dev_attr.attr,
1548 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001549 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001550 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001551}, {
1552 &sensor_dev_attr_fan4_input.dev_attr.attr,
1553 &sensor_dev_attr_fan4_min.dev_attr.attr,
1554 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1555 NULL
1556}, {
1557 &sensor_dev_attr_fan5_input.dev_attr.attr,
1558 &sensor_dev_attr_fan5_min.dev_attr.attr,
1559 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1560 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001561} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001562
Guenter Roecke1169ba2012-12-19 22:17:01 +01001563static const struct attribute_group it87_group_fan[5] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001564 { .attrs = it87_attributes_fan[0] },
1565 { .attrs = it87_attributes_fan[1] },
1566 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001567 { .attrs = it87_attributes_fan[3] },
1568 { .attrs = it87_attributes_fan[4] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001569};
1570
Guenter Roecke1169ba2012-12-19 22:17:01 +01001571static const struct attribute *it87_attributes_fan_div[] = {
1572 &sensor_dev_attr_fan1_div.dev_attr.attr,
1573 &sensor_dev_attr_fan2_div.dev_attr.attr,
1574 &sensor_dev_attr_fan3_div.dev_attr.attr,
1575};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001576
1577static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001578 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001579 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001580 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001581 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001582 NULL
1583}, {
1584 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1585 &sensor_dev_attr_pwm2.dev_attr.attr,
1586 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001587 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001588 NULL
1589}, {
1590 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1591 &sensor_dev_attr_pwm3.dev_attr.attr,
1592 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001593 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001594 NULL
1595} };
Jean Delvare87808be2006-09-24 21:17:13 +02001596
Jean Delvare723a0aa2010-03-05 22:17:16 +01001597static const struct attribute_group it87_group_pwm[3] = {
1598 { .attrs = it87_attributes_pwm[0] },
1599 { .attrs = it87_attributes_pwm[1] },
1600 { .attrs = it87_attributes_pwm[2] },
1601};
1602
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001603static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1604 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1605 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1606 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1607 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1608 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1609 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1610 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1611 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1612 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1613 NULL
1614}, {
1615 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1616 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1617 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1618 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1619 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1620 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1621 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1622 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1623 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1624 NULL
1625}, {
1626 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1627 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1628 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1629 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1630 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1631 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1632 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1633 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1634 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1635 NULL
1636} };
1637
1638static const struct attribute_group it87_group_autopwm[3] = {
1639 { .attrs = it87_attributes_autopwm[0] },
1640 { .attrs = it87_attributes_autopwm[1] },
1641 { .attrs = it87_attributes_autopwm[2] },
1642};
1643
Jean Delvared9b327c2010-03-05 22:17:17 +01001644static struct attribute *it87_attributes_fan_beep[] = {
1645 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1646 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1647 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1648 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1649 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1650};
1651
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001652static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001653 &dev_attr_vrm.attr,
1654 &dev_attr_cpu0_vid.attr,
1655 NULL
1656};
1657
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001658static const struct attribute_group it87_group_vid = {
1659 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001660};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Jean Delvare738e5e02010-08-14 21:08:50 +02001662static struct attribute *it87_attributes_label[] = {
1663 &sensor_dev_attr_in3_label.dev_attr.attr,
1664 &sensor_dev_attr_in7_label.dev_attr.attr,
1665 &sensor_dev_attr_in8_label.dev_attr.attr,
1666 NULL
1667};
1668
1669static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001670 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001671};
1672
Jean Delvare2d8672c2005-07-19 23:56:35 +02001673/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001674static int __init it87_find(unsigned short *address,
1675 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001677 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001678 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001679 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001681 err = superio_enter();
1682 if (err)
1683 return err;
1684
1685 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001686 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001687
1688 switch (chip_type) {
1689 case IT8705F_DEVID:
1690 sio_data->type = it87;
1691 break;
1692 case IT8712F_DEVID:
1693 sio_data->type = it8712;
1694 break;
1695 case IT8716F_DEVID:
1696 case IT8726F_DEVID:
1697 sio_data->type = it8716;
1698 break;
1699 case IT8718F_DEVID:
1700 sio_data->type = it8718;
1701 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001702 case IT8720F_DEVID:
1703 sio_data->type = it8720;
1704 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001705 case IT8721F_DEVID:
1706 sio_data->type = it8721;
1707 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001708 case IT8728F_DEVID:
1709 sio_data->type = it8728;
1710 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001711 case IT8782F_DEVID:
1712 sio_data->type = it8782;
1713 break;
1714 case IT8783E_DEVID:
1715 sio_data->type = it8783;
1716 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001717 case 0xffff: /* No device at all */
1718 goto exit;
1719 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001720 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001721 goto exit;
1722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Jean Delvare87673dd2006-08-28 14:37:19 +02001724 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001726 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 goto exit;
1728 }
1729
1730 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1731 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001732 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 goto exit;
1734 }
1735
1736 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001737 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Joe Perchesa8ca1032011-01-12 21:55:10 +01001738 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001739 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Jean Delvare738e5e02010-08-14 21:08:50 +02001741 /* in8 (Vbat) is always internal */
1742 sio_data->internal = (1 << 2);
1743
Jean Delvare87673dd2006-08-28 14:37:19 +02001744 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001745 if (sio_data->type == it87) {
1746 /* The IT8705F doesn't have VID pins at all */
1747 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001748
1749 /* The IT8705F has a different LD number for GPIO */
1750 superio_select(5);
1751 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08001752 } else if (sio_data->type == it8783) {
1753 int reg25, reg27, reg2A, reg2C, regEF;
Guenter Roeck0531d982012-03-02 11:46:44 -08001754
1755 sio_data->skip_vid = 1; /* No VID */
1756
1757 superio_select(GPIO);
1758
1759 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1760 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1761 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1762 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1763 regEF = superio_inb(IT87_SIO_SPI_REG);
1764
Guenter Roeck0531d982012-03-02 11:46:44 -08001765 /* Check if fan3 is there or not */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001766 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08001767 sio_data->skip_fan |= (1 << 2);
1768 if ((reg25 & (1 << 4))
1769 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1770 sio_data->skip_pwm |= (1 << 2);
1771
1772 /* Check if fan2 is there or not */
1773 if (reg27 & (1 << 7))
1774 sio_data->skip_fan |= (1 << 1);
1775 if (reg27 & (1 << 3))
1776 sio_data->skip_pwm |= (1 << 1);
1777
1778 /* VIN5 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001779 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1780 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001781
1782 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001783 if (reg27 & (1 << 1))
1784 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001785
1786 /*
1787 * VIN7
1788 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1789 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001790 if (reg27 & (1 << 2)) {
1791 /*
1792 * The data sheet is a bit unclear regarding the
1793 * internal voltage divider for VCCH5V. It says
1794 * "This bit enables and switches VIN7 (pin 91) to the
1795 * internal voltage divider for VCCH5V".
1796 * This is different to other chips, where the internal
1797 * voltage divider would connect VIN7 to an internal
1798 * voltage source. Maybe that is the case here as well.
1799 *
1800 * Since we don't know for sure, re-route it if that is
1801 * not the case, and ask the user to report if the
1802 * resulting voltage is sane.
1803 */
1804 if (!(reg2C & (1 << 1))) {
1805 reg2C |= (1 << 1);
1806 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1807 pr_notice("Routing internal VCCH5V to in7.\n");
1808 }
1809 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1810 pr_notice("Please report if it displays a reasonable voltage.\n");
1811 }
Guenter Roeck0531d982012-03-02 11:46:44 -08001812
1813 if (reg2C & (1 << 0))
1814 sio_data->internal |= (1 << 0);
1815 if (reg2C & (1 << 1))
1816 sio_data->internal |= (1 << 1);
1817
1818 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1819
Jean Delvare895ff262009-12-09 20:35:47 +01001820 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001821 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001822 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02001823
1824 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001825
Jean Delvare895ff262009-12-09 20:35:47 +01001826 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001827 if (sio_data->type == it8721 || sio_data->type == it8728 ||
1828 sio_data->type == it8782) {
Jean Delvare16b5dda2012-01-16 22:51:48 +01001829 /*
Guenter Roeck0531d982012-03-02 11:46:44 -08001830 * IT8721F/IT8758E, and IT8782F don't have VID pins
1831 * at all, not sure about the IT8728F.
Jean Delvare16b5dda2012-01-16 22:51:48 +01001832 */
Jean Delvare895ff262009-12-09 20:35:47 +01001833 sio_data->skip_vid = 1;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001834 } else {
1835 /* We need at least 4 VID pins */
1836 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001837 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001838 sio_data->skip_vid = 1;
1839 }
Jean Delvare895ff262009-12-09 20:35:47 +01001840 }
1841
Jean Delvare591ec652009-12-09 20:35:48 +01001842 /* Check if fan3 is there or not */
1843 if (reg & (1 << 6))
1844 sio_data->skip_pwm |= (1 << 2);
1845 if (reg & (1 << 7))
1846 sio_data->skip_fan |= (1 << 2);
1847
1848 /* Check if fan2 is there or not */
1849 reg = superio_inb(IT87_SIO_GPIO5_REG);
1850 if (reg & (1 << 1))
1851 sio_data->skip_pwm |= (1 << 1);
1852 if (reg & (1 << 2))
1853 sio_data->skip_fan |= (1 << 1);
1854
Jean Delvare895ff262009-12-09 20:35:47 +01001855 if ((sio_data->type == it8718 || sio_data->type == it8720)
1856 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001857 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001858
1859 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001860
1861 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1862
Jean Delvare436cad22010-07-09 16:22:48 +02001863 /*
1864 * The IT8720F has no VIN7 pin, so VCCH should always be
1865 * routed internally to VIN7 with an internal divider.
1866 * Curiously, there still is a configuration bit to control
1867 * this, which means it can be set incorrectly. And even
1868 * more curiously, many boards out there are improperly
1869 * configured, even though the IT8720F datasheet claims
1870 * that the internal routing of VCCH to VIN7 is the default
1871 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08001872 *
1873 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001874 * If UART6 is enabled, re-route VIN7 to the internal divider
1875 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02001876 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001877 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02001878 reg |= (1 << 1);
1879 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01001880 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02001881 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001882 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001883 sio_data->internal |= (1 << 0);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001884 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1885 sio_data->type == it8728)
Jean Delvare738e5e02010-08-14 21:08:50 +02001886 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01001887
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001888 /*
1889 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1890 * While VIN7 can be routed to the internal voltage divider,
1891 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07001892 *
1893 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
1894 * is the temperature source. Since we can not read the
1895 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001896 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001897 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001898 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07001899 sio_data->skip_temp |= (1 << 2);
1900 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001901
Jean Delvared9b327c2010-03-05 22:17:17 +01001902 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02001903 }
Jean Delvared9b327c2010-03-05 22:17:17 +01001904 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01001905 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02001906
Jean Delvare98dd22c2008-10-09 15:33:58 +02001907 /* Disable specific features based on DMI strings */
1908 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1909 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1910 if (board_vendor && board_name) {
1911 if (strcmp(board_vendor, "nVIDIA") == 0
1912 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001913 /*
1914 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1915 * connected to a fan, but to something else. One user
1916 * has reported instant system power-off when changing
1917 * the PWM2 duty cycle, so we disable it.
1918 * I use the board name string as the trigger in case
1919 * the same board is ever used in other systems.
1920 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01001921 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02001922 sio_data->skip_pwm = (1 << 1);
1923 }
1924 }
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926exit:
1927 superio_exit();
1928 return err;
1929}
1930
Jean Delvare723a0aa2010-03-05 22:17:16 +01001931static void it87_remove_files(struct device *dev)
1932{
1933 struct it87_data *data = platform_get_drvdata(pdev);
1934 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001935 int i;
1936
1937 sysfs_remove_group(&dev->kobj, &it87_group);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001938 for (i = 0; i < 9; i++) {
1939 if (sio_data->skip_in & (1 << i))
1940 continue;
1941 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
1942 if (it87_attributes_in_beep[i])
1943 sysfs_remove_file(&dev->kobj,
1944 it87_attributes_in_beep[i]);
1945 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07001946 for (i = 0; i < 3; i++) {
1947 if (!(data->has_temp & (1 << i)))
1948 continue;
1949 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01001950 if (has_temp_offset(data))
1951 sysfs_remove_file(&dev->kobj,
1952 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07001953 if (sio_data->beep_pin)
1954 sysfs_remove_file(&dev->kobj,
1955 it87_attributes_temp_beep[i]);
1956 }
Jean Delvare723a0aa2010-03-05 22:17:16 +01001957 for (i = 0; i < 5; i++) {
1958 if (!(data->has_fan & (1 << i)))
1959 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01001960 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01001961 if (sio_data->beep_pin)
1962 sysfs_remove_file(&dev->kobj,
1963 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01001964 if (i < 3 && !has_16bit_fans(data))
1965 sysfs_remove_file(&dev->kobj,
1966 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001967 }
1968 for (i = 0; i < 3; i++) {
1969 if (sio_data->skip_pwm & (1 << 0))
1970 continue;
1971 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001972 if (has_old_autopwm(data))
1973 sysfs_remove_group(&dev->kobj,
1974 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001975 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001976 if (!sio_data->skip_vid)
1977 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02001978 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001979}
1980
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001981static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001984 struct resource *res;
1985 struct device *dev = &pdev->dev;
1986 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001987 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01001989 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001991 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07001992 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
1993 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001994 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1995 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001996 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07001997 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Guenter Roeck62a1d052012-03-24 21:54:41 -07002000 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2001 if (!data)
2002 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002004 data->addr = res->start;
2005 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002006 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002007 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002008 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002009 data->name = it87_devices[sio_data->type].name;
2010 /*
2011 * IT8705F Datasheet 0.4.1, 3h == Version G.
2012 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2013 * These are the first revisions with 16-bit tachometer support.
2014 */
2015 switch (data->type) {
2016 case it87:
2017 if (sio_data->revision >= 0x03) {
2018 data->features &= ~FEAT_OLD_AUTOPWM;
2019 data->features |= FEAT_16BIT_FANS;
2020 }
2021 break;
2022 case it8712:
2023 if (sio_data->revision >= 0x08) {
2024 data->features &= ~FEAT_OLD_AUTOPWM;
2025 data->features |= FEAT_16BIT_FANS;
2026 }
2027 break;
2028 default:
2029 break;
2030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002033 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002034 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2035 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002037 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002039 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002042 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002044 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002045 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002046 if (sio_data->internal & (1 << 0))
2047 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2048 if (sio_data->internal & (1 << 1))
2049 data->in_scaled |= (1 << 7); /* in7 is VSB */
2050 if (sio_data->internal & (1 << 2))
2051 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Guenter Roeck0531d982012-03-02 11:46:44 -08002052 } else if (sio_data->type == it8782 || sio_data->type == it8783) {
2053 if (sio_data->internal & (1 << 0))
2054 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2055 if (sio_data->internal & (1 << 1))
2056 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002057 }
2058
Guenter Roeck4573acb2012-03-26 16:17:41 -07002059 data->has_temp = 0x07;
2060 if (sio_data->skip_temp & (1 << 2)) {
2061 if (sio_data->type == it8782
2062 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2063 data->has_temp &= ~(1 << 2);
2064 }
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002067 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002070 err = sysfs_create_group(&dev->kobj, &it87_group);
2071 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002072 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002073
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002074 for (i = 0; i < 9; i++) {
2075 if (sio_data->skip_in & (1 << i))
2076 continue;
2077 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2078 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002079 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002080 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2081 err = sysfs_create_file(&dev->kobj,
2082 it87_attributes_in_beep[i]);
2083 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002084 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002085 }
2086 }
2087
Guenter Roeck4573acb2012-03-26 16:17:41 -07002088 for (i = 0; i < 3; i++) {
2089 if (!(data->has_temp & (1 << i)))
2090 continue;
2091 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002092 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002093 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002094 if (has_temp_offset(data)) {
2095 err = sysfs_create_file(&dev->kobj,
2096 it87_attributes_temp_offset[i]);
2097 if (err)
2098 goto error;
2099 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002100 if (sio_data->beep_pin) {
2101 err = sysfs_create_file(&dev->kobj,
2102 it87_attributes_temp_beep[i]);
2103 if (err)
2104 goto error;
2105 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002106 }
2107
Jean Delvare9060f8b2006-08-28 14:24:17 +02002108 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002109 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01002110 for (i = 0; i < 5; i++) {
2111 if (!(data->has_fan & (1 << i)))
2112 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002113 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002114 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002115 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002116
Guenter Roecke1169ba2012-12-19 22:17:01 +01002117 if (i < 3 && !has_16bit_fans(data)) {
2118 err = sysfs_create_file(&dev->kobj,
2119 it87_attributes_fan_div[i]);
2120 if (err)
2121 goto error;
2122 }
2123
Jean Delvared9b327c2010-03-05 22:17:17 +01002124 if (sio_data->beep_pin) {
2125 err = sysfs_create_file(&dev->kobj,
2126 it87_attributes_fan_beep[i]);
2127 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002128 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002129 if (!fan_beep_need_rw)
2130 continue;
2131
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002132 /*
2133 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002134 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002135 * for it.
2136 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002137 if (sysfs_chmod_file(&dev->kobj,
2138 it87_attributes_fan_beep[i],
2139 S_IRUGO | S_IWUSR))
2140 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2141 i + 1);
2142 fan_beep_need_rw = 0;
2143 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002144 }
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002147 for (i = 0; i < 3; i++) {
2148 if (sio_data->skip_pwm & (1 << i))
2149 continue;
2150 err = sysfs_create_group(&dev->kobj,
2151 &it87_group_pwm[i]);
2152 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002153 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002154
2155 if (!has_old_autopwm(data))
2156 continue;
2157 err = sysfs_create_group(&dev->kobj,
2158 &it87_group_autopwm[i]);
2159 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002160 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163
Jean Delvare895ff262009-12-09 20:35:47 +01002164 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002165 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002166 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002167 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002168 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2169 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002170 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002171 }
2172
Jean Delvare738e5e02010-08-14 21:08:50 +02002173 /* Export labels for internal sensors */
2174 for (i = 0; i < 3; i++) {
2175 if (!(sio_data->internal & (1 << i)))
2176 continue;
2177 err = sysfs_create_file(&dev->kobj,
2178 it87_attributes_label[i]);
2179 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002180 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002181 }
2182
Tony Jones1beeffe2007-08-20 13:46:20 -07002183 data->hwmon_dev = hwmon_device_register(dev);
2184 if (IS_ERR(data->hwmon_dev)) {
2185 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002186 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
2188
2189 return 0;
2190
Guenter Roeck62a1d052012-03-24 21:54:41 -07002191error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002192 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return err;
2194}
2195
Bill Pemberton281dfd02012-11-19 13:25:51 -05002196static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002198 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Tony Jones1beeffe2007-08-20 13:46:20 -07002200 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002201 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return 0;
2204}
2205
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002206/*
2207 * Must be called with data->update_lock held, except during initialization.
2208 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2209 * would slow down the IT87 access and should not be necessary.
2210 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002211static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002213 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2214 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002217/*
2218 * Must be called with data->update_lock held, except during initialization.
2219 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2220 * would slow down the IT87 access and should not be necessary.
2221 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002222static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002224 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2225 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
2228/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002229static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002231 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002232 /*
2233 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002235 * disable pwm control to protect the user.
2236 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002237 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if ((tmp & 0x87) == 0) {
2239 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002240 /*
2241 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002243 * inverting all fan speed values.
2244 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 int i;
2246 u8 pwm[3];
2247
2248 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002249 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 IT87_REG_PWM(i));
2251
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002252 /*
2253 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 * might be correct, as suspicious as it seems, so we
2255 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002256 * PWM interface).
2257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002259 dev_info(dev,
2260 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002261 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 tmp | 0x87);
2263 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002264 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 IT87_REG_PWM(i),
2266 0x7f & ~pwm[i]);
2267 return 1;
2268 }
2269
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002270 dev_info(dev,
2271 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
2273
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002274 dev_info(dev,
2275 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return 0;
2277 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002278 dev_info(dev,
2279 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281
2282 return 1;
2283}
2284
2285/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002286static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Jean Delvare591ec652009-12-09 20:35:48 +01002288 struct it87_sio_data *sio_data = pdev->dev.platform_data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002289 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002291 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002293 /*
2294 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002295 * - If it is in automatic mode, setting to manual mode should set
2296 * the fan to full speed by default.
2297 * - If it is in manual mode, we need a mapping to temperature
2298 * channels to use when later setting to automatic mode later.
2299 * Use a 1:1 mapping by default (we are clueless.)
2300 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002301 * prior to switching to a different mode.
2302 * Note that this is no longer needed for the IT8721F and later, as
2303 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002304 * manual duty cycle.
2305 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002307 data->pwm_temp_map[i] = i;
2308 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002309 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
2311
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002312 /*
2313 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002314 * registers. For low voltage limits it makes no sense and triggers
2315 * alarms, so change to 0 instead. For high temperature limits, it
2316 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002317 * but is still confusing, so change to 127 degrees C.
2318 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002319 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002320 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002321 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002322 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002323 }
2324 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002325 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002326 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002327 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002328 }
2329
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002330 /*
2331 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002332 * set to two different sensor types and we can't guess which one
2333 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002334 * run-time through the temp{1-3}_type sysfs accessors if needed.
2335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002338 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if ((tmp & 0xff) == 0) {
2340 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002341 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
2343
2344 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002345 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002346 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002347 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002349 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002350 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2351 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002353 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Jean Delvare17d648b2006-08-28 14:23:46 +02002355 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02002356 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002357 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002358 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002359 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002360 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002361 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002362 tmp | 0x07);
2363 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002364 /* IT8705F, IT8782F, and IT8783E/F only support three fans. */
2365 if (data->type != it87 && data->type != it8782 &&
2366 data->type != it8783) {
Andrew Paprocki816d8c62008-08-06 22:41:06 +02002367 if (tmp & (1 << 4))
2368 data->has_fan |= (1 << 3); /* fan4 enabled */
2369 if (tmp & (1 << 5))
2370 data->has_fan |= (1 << 4); /* fan5 enabled */
2371 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002372 }
2373
Jean Delvare591ec652009-12-09 20:35:48 +01002374 /* Fan input pins may be used for alternative functions */
2375 data->has_fan &= ~sio_data->skip_fan;
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002378 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002379 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 | (update_vbat ? 0x41 : 0x01));
2381}
2382
Jean Delvareb99883d2010-03-05 22:17:15 +01002383static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2384{
2385 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002386 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002387 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002388 data->pwm_duty[nr] = it87_read_value(data,
2389 IT87_REG_PWM_DUTY(nr));
2390 } else {
2391 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2392 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2393 else /* Manual mode */
2394 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2395 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002396
2397 if (has_old_autopwm(data)) {
2398 int i;
2399
2400 for (i = 0; i < 5 ; i++)
2401 data->auto_temp[nr][i] = it87_read_value(data,
2402 IT87_REG_AUTO_TEMP(nr, i));
2403 for (i = 0; i < 3 ; i++)
2404 data->auto_pwm[nr][i] = it87_read_value(data,
2405 IT87_REG_AUTO_PWM(nr, i));
2406 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002407}
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409static struct it87_data *it87_update_device(struct device *dev)
2410{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002411 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 int i;
2413
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002414 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2417 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002419 /*
2420 * Cleared after each update, so reenable. Value
2421 * returned by this read will be previous value
2422 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002423 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002424 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
2426 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002427 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002428 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002429 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002430 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002431 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002432 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
Jean Delvare3543a532006-08-28 14:27:25 +02002434 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002435 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Jean Delvarec7f1f712007-09-03 17:11:46 +02002437 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002438 /* Skip disabled fans */
2439 if (!(data->has_fan & (1 << i)))
2440 continue;
2441
Guenter Roecke1169ba2012-12-19 22:17:01 +01002442 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002443 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002444 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002445 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002446 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002447 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002448 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002449 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002450 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002451 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
2454 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002455 if (!(data->has_temp & (1 << i)))
2456 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002457 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002458 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002459 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002460 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002461 data->temp[i][2] =
2462 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002463 if (has_temp_offset(data))
2464 data->temp[i][3] =
2465 it87_read_value(data,
2466 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
2468
Jean Delvare17d648b2006-08-28 14:23:46 +02002469 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002470 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002471 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002472 data->fan_div[0] = i & 0x07;
2473 data->fan_div[1] = (i >> 3) & 0x07;
2474 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002478 it87_read_value(data, IT87_REG_ALARM1) |
2479 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2480 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002481 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002482
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002483 data->fan_main_ctrl = it87_read_value(data,
2484 IT87_REG_FAN_MAIN_CTRL);
2485 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002486 for (i = 0; i < 3; i++)
2487 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002489 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002490 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002491 /*
2492 * The IT8705F does not have VID capability.
2493 * The IT8718F and later don't use IT87_REG_VID for the
2494 * same purpose.
2495 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002496 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002497 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002498 /*
2499 * The older IT8712F revisions had only 5 VID pins,
2500 * but we assume it is always safe to read 6 bits.
2501 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002502 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
2504 data->last_updated = jiffies;
2505 data->valid = 1;
2506 }
2507
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002508 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 return data;
2511}
2512
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002513static int __init it87_device_add(unsigned short address,
2514 const struct it87_sio_data *sio_data)
2515{
2516 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002517 .start = address + IT87_EC_OFFSET,
2518 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002519 .name = DRVNAME,
2520 .flags = IORESOURCE_IO,
2521 };
2522 int err;
2523
Jean Delvareb9acb642009-01-07 16:37:35 +01002524 err = acpi_check_resource_conflict(&res);
2525 if (err)
2526 goto exit;
2527
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002528 pdev = platform_device_alloc(DRVNAME, address);
2529 if (!pdev) {
2530 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002531 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002532 goto exit;
2533 }
2534
2535 err = platform_device_add_resources(pdev, &res, 1);
2536 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002537 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002538 goto exit_device_put;
2539 }
2540
2541 err = platform_device_add_data(pdev, sio_data,
2542 sizeof(struct it87_sio_data));
2543 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002544 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002545 goto exit_device_put;
2546 }
2547
2548 err = platform_device_add(pdev);
2549 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002550 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002551 goto exit_device_put;
2552 }
2553
2554 return 0;
2555
2556exit_device_put:
2557 platform_device_put(pdev);
2558exit:
2559 return err;
2560}
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562static int __init sm_it87_init(void)
2563{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002564 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002565 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002566 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002567
Jean Delvare98dd22c2008-10-09 15:33:58 +02002568 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002569 err = it87_find(&isa_address, &sio_data);
2570 if (err)
2571 return err;
2572 err = platform_driver_register(&it87_driver);
2573 if (err)
2574 return err;
2575
2576 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002577 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002578 platform_driver_unregister(&it87_driver);
2579 return err;
2580 }
2581
2582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
2585static void __exit sm_it87_exit(void)
2586{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002587 platform_device_unregister(pdev);
2588 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589}
2590
2591
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002592MODULE_AUTHOR("Chris Gauthron, Jean Delvare <khali@linux-fr.org>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002593MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594module_param(update_vbat, bool, 0);
2595MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2596module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002597MODULE_PARM_DESC(fix_pwm_polarity,
2598 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599MODULE_LICENSE("GPL");
2600
2601module_init(sm_it87_init);
2602module_exit(sm_it87_exit);