blob: 117d66fcded6ae9fb9d6d28ac0d05aff67e480cb [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));
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200387 return SENSORS_LIMIT(val, 0, 255);
388}
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;
399 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
400 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
401 254);
402}
403
404static inline u16 FAN16_TO_REG(long rpm)
405{
406 if (rpm == 0)
407 return 0xffff;
408 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
409}
410
411#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
412 1350000 / ((val) * (div)))
413/* The divider is fixed to 2 in 16-bit mode */
414#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
415 1350000 / ((val) * 2))
416
417#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
418 ((val) + 500) / 1000), -128, 127))
419#define TEMP_FROM_REG(val) ((val) * 1000)
420
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200421static u8 pwm_to_reg(const struct it87_data *data, long val)
422{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100423 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200424 return val;
425 else
426 return val >> 1;
427}
428
429static int pwm_from_reg(const struct it87_data *data, u8 reg)
430{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100431 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200432 return reg;
433 else
434 return (reg & 0x7f) << 1;
435}
436
Jean Delvare0df6454d2010-10-28 20:31:51 +0200437
438static int DIV_TO_REG(int val)
439{
440 int answer = 0;
441 while (answer < 7 && (val >>= 1))
442 answer++;
443 return answer;
444}
445#define DIV_FROM_REG(val) (1 << (val))
446
447static const unsigned int pwm_freq[8] = {
448 48000000 / 128,
449 24000000 / 128,
450 12000000 / 128,
451 8000000 / 128,
452 6000000 / 128,
453 3000000 / 128,
454 1500000 / 128,
455 750000 / 128,
456};
457
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200458static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500459static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200461static int it87_read_value(struct it87_data *data, u8 reg);
462static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200464static int it87_check_pwm(struct device *dev);
465static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200468static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100469 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200470 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200471 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100472 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200473 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500474 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200475};
476
Jean Delvare20ad93d2005-06-05 11:53:25 +0200477static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100478 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100480 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
481 int nr = sattr->nr;
482 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100485 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Guenter Roeck929c6a52012-12-19 22:17:00 +0100488static ssize_t set_in(struct device *dev, struct device_attribute *attr,
489 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100491 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
492 int nr = sattr->nr;
493 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200494
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200495 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100496 unsigned long val;
497
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100498 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100499 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100501 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100502 data->in[nr][index] = in_to_reg(data, nr, val);
503 it87_write_value(data,
504 index == 1 ? IT87_REG_VIN_MIN(nr)
505 : IT87_REG_VIN_MAX(nr),
506 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100507 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return count;
509}
510
Guenter Roeck929c6a52012-12-19 22:17:00 +0100511static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
512static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
513 0, 1);
514static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
515 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Guenter Roeck929c6a52012-12-19 22:17:00 +0100517static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
518static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
519 1, 1);
520static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
521 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Guenter Roeck929c6a52012-12-19 22:17:00 +0100523static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
524static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
525 2, 1);
526static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
527 2, 2);
528
529static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
530static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
531 3, 1);
532static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
533 3, 2);
534
535static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
536static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
537 4, 1);
538static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
539 4, 2);
540
541static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
542static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
543 5, 1);
544static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
545 5, 2);
546
547static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
548static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
549 6, 1);
550static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
551 6, 2);
552
553static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
554static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
555 7, 1);
556static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
557 7, 2);
558
559static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200562static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100563 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100565 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
566 int nr = sattr->nr;
567 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200569
Guenter Roeck60ca3852012-12-19 22:17:00 +0100570 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200572
Guenter Roeck60ca3852012-12-19 22:17:00 +0100573static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
574 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100576 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
577 int nr = sattr->nr;
578 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200579 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100580 long val;
Guenter Roeck161d8982012-12-19 22:17:01 +0100581 u8 reg, regval;
Jean Delvaref5f64502010-03-05 22:17:19 +0100582
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100583 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100584 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100586 mutex_lock(&data->update_lock);
Guenter Roeck161d8982012-12-19 22:17:01 +0100587
588 switch (index) {
589 default:
590 case 1:
591 reg = IT87_REG_TEMP_LOW(nr);
592 break;
593 case 2:
594 reg = IT87_REG_TEMP_HIGH(nr);
595 break;
596 case 3:
597 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
598 if (!(regval & 0x80)) {
599 regval |= 0x80;
600 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
601 }
602 data->valid = 0;
603 reg = IT87_REG_TEMP_OFFSET[nr];
604 break;
605 }
606
Guenter Roeck60ca3852012-12-19 22:17:00 +0100607 data->temp[nr][index] = TEMP_TO_REG(val);
Guenter Roeck161d8982012-12-19 22:17:01 +0100608 it87_write_value(data, reg, data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100609 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return count;
611}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200612
Guenter Roeck60ca3852012-12-19 22:17:00 +0100613static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
614static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
615 0, 1);
616static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
617 0, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100618static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
619 set_temp, 0, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100620static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
621static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
622 1, 1);
623static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
624 1, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100625static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
626 set_temp, 1, 3);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100627static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
628static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
629 2, 1);
630static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
631 2, 2);
Guenter Roeck161d8982012-12-19 22:17:01 +0100632static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
633 set_temp, 2, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Guenter Roeck2cece012012-12-19 22:17:01 +0100635static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
636 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200638 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
639 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800641 u8 reg = data->sensor; /* In case value is updated while used */
Guenter Roeck19529782012-12-19 22:17:02 +0100642 u8 extra = data->extra;
Jean Delvare5f2dc792010-03-05 22:17:18 +0100643
Guenter Roeck19529782012-12-19 22:17:02 +0100644 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
645 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100646 return sprintf(buf, "6\n"); /* Intel PECI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (reg & (1 << nr))
648 return sprintf(buf, "3\n"); /* thermal diode */
649 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200650 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return sprintf(buf, "0\n"); /* disabled */
652}
Guenter Roeck2cece012012-12-19 22:17:01 +0100653
654static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
655 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200657 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
658 int nr = sensor_attr->index;
659
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200660 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100661 long val;
Guenter Roeck19529782012-12-19 22:17:02 +0100662 u8 reg, extra;
Jean Delvaref5f64502010-03-05 22:17:19 +0100663
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100664 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100665 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Jean Delvare8acf07c2010-04-14 16:14:09 +0200667 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
668 reg &= ~(1 << nr);
669 reg &= ~(8 << nr);
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100670 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
671 reg &= 0x3f;
Guenter Roeck19529782012-12-19 22:17:02 +0100672 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
673 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
674 extra &= 0x7f;
Jean Delvare4ed10772008-10-17 17:51:16 +0200675 if (val == 2) { /* backwards compatibility */
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100676 dev_warn(dev,
677 "Sensor type 2 is deprecated, please use 4 instead\n");
Jean Delvare4ed10772008-10-17 17:51:16 +0200678 val = 4;
679 }
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100680 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200682 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200683 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200684 reg |= 8 << nr;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +0100685 else if (has_temp_peci(data, nr) && val == 6)
686 reg |= (nr + 1) << 6;
Guenter Roeck19529782012-12-19 22:17:02 +0100687 else if (has_temp_old_peci(data, nr) && val == 6)
688 extra |= 0x80;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200689 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200691
692 mutex_lock(&data->update_lock);
693 data->sensor = reg;
Guenter Roeck19529782012-12-19 22:17:02 +0100694 data->extra = extra;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200695 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Guenter Roeck19529782012-12-19 22:17:02 +0100696 if (has_temp_old_peci(data, nr))
697 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200698 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100699 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return count;
701}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Guenter Roeck2cece012012-12-19 22:17:01 +0100703static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
704 set_temp_type, 0);
705static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
706 set_temp_type, 1);
707static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
708 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100711
712static int pwm_mode(const struct it87_data *data, int nr)
713{
714 int ctrl = data->fan_main_ctrl & (1 << nr);
715
716 if (ctrl == 0) /* Full speed */
717 return 0;
718 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
719 return 2;
720 else /* Manual mode */
721 return 1;
722}
723
Jean Delvare20ad93d2005-06-05 11:53:25 +0200724static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
Guenter Roecke1169ba2012-12-19 22:17:01 +0100725 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100727 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
728 int nr = sattr->nr;
729 int index = sattr->index;
730 int speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200732
Guenter Roecke1169ba2012-12-19 22:17:01 +0100733 speed = has_16bit_fans(data) ?
734 FAN16_FROM_REG(data->fan[nr][index]) :
735 FAN_FROM_REG(data->fan[nr][index],
736 DIV_FROM_REG(data->fan_div[nr]));
737 return sprintf(buf, "%d\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100739
Jean Delvare20ad93d2005-06-05 11:53:25 +0200740static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
741 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200743 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
744 int nr = sensor_attr->index;
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct it87_data *data = it87_update_device(dev);
747 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
748}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100749static ssize_t show_pwm_enable(struct device *dev,
750 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200752 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
753 int nr = sensor_attr->index;
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100756 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200758static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
759 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200761 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
762 int nr = sensor_attr->index;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200765 return sprintf(buf, "%d\n",
766 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100768static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
769 char *buf)
770{
771 struct it87_data *data = it87_update_device(dev);
772 int index = (data->fan_ctl >> 4) & 0x07;
773
774 return sprintf(buf, "%u\n", pwm_freq[index]);
775}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100776
777static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
778 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Guenter Roecke1169ba2012-12-19 22:17:01 +0100780 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
781 int nr = sattr->nr;
782 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200783
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200784 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100785 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100786 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100788 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100789 return -EINVAL;
790
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100791 mutex_lock(&data->update_lock);
Guenter Roecke1169ba2012-12-19 22:17:01 +0100792
793 if (has_16bit_fans(data)) {
794 data->fan[nr][index] = FAN16_TO_REG(val);
795 it87_write_value(data, IT87_REG_FAN_MIN[nr],
796 data->fan[nr][index] & 0xff);
797 it87_write_value(data, IT87_REG_FANX_MIN[nr],
798 data->fan[nr][index] >> 8);
799 } else {
800 reg = it87_read_value(data, IT87_REG_FAN_DIV);
801 switch (nr) {
802 case 0:
803 data->fan_div[nr] = reg & 0x07;
804 break;
805 case 1:
806 data->fan_div[nr] = (reg >> 3) & 0x07;
807 break;
808 case 2:
809 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
810 break;
811 }
812 data->fan[nr][index] =
813 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
814 it87_write_value(data, IT87_REG_FAN_MIN[nr],
815 data->fan[nr][index]);
Jean Delvare07eab462005-11-23 15:44:31 -0800816 }
817
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100818 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return count;
820}
Guenter Roecke1169ba2012-12-19 22:17:01 +0100821
Jean Delvare20ad93d2005-06-05 11:53:25 +0200822static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
823 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200825 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
826 int nr = sensor_attr->index;
827
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200828 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100829 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200830 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 u8 old;
832
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100833 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100834 return -EINVAL;
835
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100836 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200837 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200839 /* Save fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100840 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 switch (nr) {
843 case 0:
844 case 1:
845 data->fan_div[nr] = DIV_TO_REG(val);
846 break;
847 case 2:
848 if (val < 8)
849 data->fan_div[nr] = 1;
850 else
851 data->fan_div[nr] = 3;
852 }
853 val = old & 0x80;
854 val |= (data->fan_div[0] & 0x07);
855 val |= (data->fan_div[1] & 0x07) << 3;
856 if (data->fan_div[2] == 3)
857 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200858 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200860 /* Restore fan min limit */
Guenter Roecke1169ba2012-12-19 22:17:01 +0100861 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
862 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200863
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100864 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return count;
866}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100867
868/* Returns 0 if OK, -EINVAL otherwise */
869static int check_trip_points(struct device *dev, int nr)
870{
871 const struct it87_data *data = dev_get_drvdata(dev);
872 int i, err = 0;
873
874 if (has_old_autopwm(data)) {
875 for (i = 0; i < 3; i++) {
876 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
877 err = -EINVAL;
878 }
879 for (i = 0; i < 2; i++) {
880 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
881 err = -EINVAL;
882 }
883 }
884
885 if (err) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +0100886 dev_err(dev,
887 "Inconsistent trip points, not switching to automatic mode\n");
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100888 dev_err(dev, "Adjust the trip points and try again\n");
889 }
890 return err;
891}
892
Jean Delvare20ad93d2005-06-05 11:53:25 +0200893static ssize_t set_pwm_enable(struct device *dev,
894 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200896 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
897 int nr = sensor_attr->index;
898
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200899 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100900 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100902 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100903 return -EINVAL;
904
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100905 /* Check trip points before switching to automatic mode */
906 if (val == 2) {
907 if (check_trip_points(dev, nr) < 0)
908 return -EINVAL;
909 }
910
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100911 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 if (val == 0) {
914 int tmp;
915 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200916 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
917 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* set on/off mode */
919 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100920 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
921 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100922 } else {
923 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +0100924 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +0100925 data->pwm_temp_map[nr] :
926 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +0100927 else /* Automatic mode */
928 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
929 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /* set SmartGuardian mode */
931 data->fan_main_ctrl |= (1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100932 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
933 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100936 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return count;
938}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200939static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
940 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200942 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
943 int nr = sensor_attr->index;
944
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200945 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100946 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100948 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return -EINVAL;
950
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100951 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +0100952 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800953 /*
954 * If we are in automatic mode, the PWM duty cycle register
955 * is read-only so we can't write the value.
956 */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100957 if (data->pwm_ctrl[nr] & 0x80) {
958 mutex_unlock(&data->update_lock);
959 return -EBUSY;
960 }
961 data->pwm_duty[nr] = pwm_to_reg(data, val);
962 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
963 data->pwm_duty[nr]);
964 } else {
965 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800966 /*
967 * If we are in manual mode, write the duty cycle immediately;
968 * otherwise, just store it for later use.
969 */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100970 if (!(data->pwm_ctrl[nr] & 0x80)) {
971 data->pwm_ctrl[nr] = data->pwm_duty[nr];
972 it87_write_value(data, IT87_REG_PWM(nr),
973 data->pwm_ctrl[nr]);
974 }
Jean Delvareb99883d2010-03-05 22:17:15 +0100975 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100976 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return count;
978}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100979static ssize_t set_pwm_freq(struct device *dev,
980 struct device_attribute *attr, const char *buf, size_t count)
981{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200982 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100983 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100984 int i;
985
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100986 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100987 return -EINVAL;
988
Jean Delvaref8d0c192007-02-14 21:15:02 +0100989 /* Search for the nearest available frequency */
990 for (i = 0; i < 7; i++) {
991 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
992 break;
993 }
994
995 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200996 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100997 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200998 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100999 mutex_unlock(&data->update_lock);
1000
1001 return count;
1002}
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001003static ssize_t show_pwm_temp_map(struct device *dev,
1004 struct device_attribute *attr, char *buf)
1005{
1006 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1007 int nr = sensor_attr->index;
1008
1009 struct it87_data *data = it87_update_device(dev);
1010 int map;
1011
1012 if (data->pwm_temp_map[nr] < 3)
1013 map = 1 << data->pwm_temp_map[nr];
1014 else
1015 map = 0; /* Should never happen */
1016 return sprintf(buf, "%d\n", map);
1017}
1018static ssize_t set_pwm_temp_map(struct device *dev,
1019 struct device_attribute *attr, const char *buf, size_t count)
1020{
1021 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1022 int nr = sensor_attr->index;
1023
1024 struct it87_data *data = dev_get_drvdata(dev);
1025 long val;
1026 u8 reg;
1027
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001028 /*
1029 * This check can go away if we ever support automatic fan speed
1030 * control on newer chips.
1031 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001032 if (!has_old_autopwm(data)) {
1033 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1034 return -EINVAL;
1035 }
1036
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001037 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001038 return -EINVAL;
1039
1040 switch (val) {
1041 case (1 << 0):
1042 reg = 0x00;
1043 break;
1044 case (1 << 1):
1045 reg = 0x01;
1046 break;
1047 case (1 << 2):
1048 reg = 0x02;
1049 break;
1050 default:
1051 return -EINVAL;
1052 }
1053
1054 mutex_lock(&data->update_lock);
1055 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001056 /*
1057 * If we are in automatic mode, write the temp mapping immediately;
1058 * otherwise, just store it for later use.
1059 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001060 if (data->pwm_ctrl[nr] & 0x80) {
1061 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1062 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1063 }
1064 mutex_unlock(&data->update_lock);
1065 return count;
1066}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001068static ssize_t show_auto_pwm(struct device *dev,
1069 struct device_attribute *attr, char *buf)
1070{
1071 struct it87_data *data = it87_update_device(dev);
1072 struct sensor_device_attribute_2 *sensor_attr =
1073 to_sensor_dev_attr_2(attr);
1074 int nr = sensor_attr->nr;
1075 int point = sensor_attr->index;
1076
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001077 return sprintf(buf, "%d\n",
1078 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001079}
1080
1081static ssize_t set_auto_pwm(struct device *dev,
1082 struct device_attribute *attr, const char *buf, size_t count)
1083{
1084 struct it87_data *data = dev_get_drvdata(dev);
1085 struct sensor_device_attribute_2 *sensor_attr =
1086 to_sensor_dev_attr_2(attr);
1087 int nr = sensor_attr->nr;
1088 int point = sensor_attr->index;
1089 long val;
1090
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001091 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001092 return -EINVAL;
1093
1094 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001095 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001096 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1097 data->auto_pwm[nr][point]);
1098 mutex_unlock(&data->update_lock);
1099 return count;
1100}
1101
1102static ssize_t show_auto_temp(struct device *dev,
1103 struct device_attribute *attr, char *buf)
1104{
1105 struct it87_data *data = it87_update_device(dev);
1106 struct sensor_device_attribute_2 *sensor_attr =
1107 to_sensor_dev_attr_2(attr);
1108 int nr = sensor_attr->nr;
1109 int point = sensor_attr->index;
1110
1111 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1112}
1113
1114static ssize_t set_auto_temp(struct device *dev,
1115 struct device_attribute *attr, const char *buf, size_t count)
1116{
1117 struct it87_data *data = dev_get_drvdata(dev);
1118 struct sensor_device_attribute_2 *sensor_attr =
1119 to_sensor_dev_attr_2(attr);
1120 int nr = sensor_attr->nr;
1121 int point = sensor_attr->index;
1122 long val;
1123
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001124 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001125 return -EINVAL;
1126
1127 mutex_lock(&data->update_lock);
1128 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1129 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1130 data->auto_temp[nr][point]);
1131 mutex_unlock(&data->update_lock);
1132 return count;
1133}
1134
Guenter Roecke1169ba2012-12-19 22:17:01 +01001135static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1136static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1137 0, 1);
1138static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1139 set_fan_div, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Guenter Roecke1169ba2012-12-19 22:17:01 +01001141static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1142static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1143 1, 1);
1144static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1145 set_fan_div, 1);
1146
1147static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1148static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1149 2, 1);
1150static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1151 set_fan_div, 2);
1152
1153static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1154static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1155 3, 1);
1156
1157static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1158static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1159 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Guenter Roeckc4458db2012-12-19 22:17:02 +01001161static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1162 show_pwm_enable, set_pwm_enable, 0);
1163static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1164static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1165static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1166 show_pwm_temp_map, set_pwm_temp_map, 0);
1167static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1168 show_auto_pwm, set_auto_pwm, 0, 0);
1169static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1170 show_auto_pwm, set_auto_pwm, 0, 1);
1171static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1172 show_auto_pwm, set_auto_pwm, 0, 2);
1173static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1174 show_auto_pwm, NULL, 0, 3);
1175static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1176 show_auto_temp, set_auto_temp, 0, 1);
1177static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1178 show_auto_temp, set_auto_temp, 0, 0);
1179static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1180 show_auto_temp, set_auto_temp, 0, 2);
1181static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1182 show_auto_temp, set_auto_temp, 0, 3);
1183static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1184 show_auto_temp, set_auto_temp, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Guenter Roeckc4458db2012-12-19 22:17:02 +01001186static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1187 show_pwm_enable, set_pwm_enable, 1);
1188static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1189static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1190static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1191 show_pwm_temp_map, set_pwm_temp_map, 1);
1192static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1193 show_auto_pwm, set_auto_pwm, 1, 0);
1194static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1195 show_auto_pwm, set_auto_pwm, 1, 1);
1196static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1197 show_auto_pwm, set_auto_pwm, 1, 2);
1198static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1199 show_auto_pwm, NULL, 1, 3);
1200static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1201 show_auto_temp, set_auto_temp, 1, 1);
1202static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1203 show_auto_temp, set_auto_temp, 1, 0);
1204static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1205 show_auto_temp, set_auto_temp, 1, 2);
1206static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1207 show_auto_temp, set_auto_temp, 1, 3);
1208static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1209 show_auto_temp, set_auto_temp, 1, 4);
1210
1211static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1212 show_pwm_enable, set_pwm_enable, 2);
1213static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1214static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1215static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1216 show_pwm_temp_map, set_pwm_temp_map, 2);
1217static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1218 show_auto_pwm, set_auto_pwm, 2, 0);
1219static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1220 show_auto_pwm, set_auto_pwm, 2, 1);
1221static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1222 show_auto_pwm, set_auto_pwm, 2, 2);
1223static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1224 show_auto_pwm, NULL, 2, 3);
1225static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1226 show_auto_temp, set_auto_temp, 2, 1);
1227static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1228 show_auto_temp, set_auto_temp, 2, 0);
1229static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1230 show_auto_temp, set_auto_temp, 2, 2);
1231static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1232 show_auto_temp, set_auto_temp, 2, 3);
1233static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1234 show_auto_temp, set_auto_temp, 2, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001237static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1238 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001241 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
Jean Delvare1d66c642005-04-18 21:16:59 -07001243static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Jean Delvare0124dd72007-11-25 16:16:41 +01001245static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1246 char *buf)
1247{
1248 int bitnr = to_sensor_dev_attr(attr)->index;
1249 struct it87_data *data = it87_update_device(dev);
1250 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1251}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001252
1253static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1254 *attr, const char *buf, size_t count)
1255{
1256 struct it87_data *data = dev_get_drvdata(dev);
1257 long val;
1258 int config;
1259
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001260 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001261 return -EINVAL;
1262
1263 mutex_lock(&data->update_lock);
1264 config = it87_read_value(data, IT87_REG_CONFIG);
1265 if (config < 0) {
1266 count = config;
1267 } else {
1268 config |= 1 << 5;
1269 it87_write_value(data, IT87_REG_CONFIG, config);
1270 /* Invalidate cache to force re-read */
1271 data->valid = 0;
1272 }
1273 mutex_unlock(&data->update_lock);
1274
1275 return count;
1276}
1277
Jean Delvare0124dd72007-11-25 16:16:41 +01001278static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1279static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1280static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1281static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1282static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1283static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1284static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1285static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1286static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1287static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1288static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1289static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1290static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1291static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1292static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1293static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001294static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1295 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001296
Jean Delvared9b327c2010-03-05 22:17:17 +01001297static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1298 char *buf)
1299{
1300 int bitnr = to_sensor_dev_attr(attr)->index;
1301 struct it87_data *data = it87_update_device(dev);
1302 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1303}
1304static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1305 const char *buf, size_t count)
1306{
1307 int bitnr = to_sensor_dev_attr(attr)->index;
1308 struct it87_data *data = dev_get_drvdata(dev);
1309 long val;
1310
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001311 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001312 || (val != 0 && val != 1))
1313 return -EINVAL;
1314
1315 mutex_lock(&data->update_lock);
1316 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1317 if (val)
1318 data->beeps |= (1 << bitnr);
1319 else
1320 data->beeps &= ~(1 << bitnr);
1321 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1322 mutex_unlock(&data->update_lock);
1323 return count;
1324}
1325
1326static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1327 show_beep, set_beep, 1);
1328static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1329static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1330static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1331static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1332static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1333static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1334static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1335/* fanX_beep writability is set later */
1336static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1337static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1338static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1339static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1340static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1341static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1342 show_beep, set_beep, 2);
1343static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1344static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1345
Jean Delvare5f2dc792010-03-05 22:17:18 +01001346static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1347 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Jean Delvare90d66192007-10-08 18:24:35 +02001349 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001350 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001352static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1353 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001355 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001356 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001358 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001359 return -EINVAL;
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 data->vrm = val;
1362
1363 return count;
1364}
1365static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Jean Delvare5f2dc792010-03-05 22:17:18 +01001367static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1368 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct it87_data *data = it87_update_device(dev);
1371 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1372}
1373static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001374
Jean Delvare738e5e02010-08-14 21:08:50 +02001375static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1376 char *buf)
1377{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001378 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001379 "+5V",
1380 "5VSB",
1381 "Vbat",
1382 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001383 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001384 "+3.3V",
1385 "3VSB",
1386 "Vbat",
1387 };
1388 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001389 int nr = to_sensor_dev_attr(attr)->index;
1390
Jean Delvare16b5dda2012-01-16 22:51:48 +01001391 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1392 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001393}
1394static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1395static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1396static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1397
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001398static ssize_t show_name(struct device *dev, struct device_attribute
1399 *devattr, char *buf)
1400{
1401 struct it87_data *data = dev_get_drvdata(dev);
1402 return sprintf(buf, "%s\n", data->name);
1403}
1404static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1405
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001406static struct attribute *it87_attributes_in[9][5] = {
1407{
Jean Delvare87808be2006-09-24 21:17:13 +02001408 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001409 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001410 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001411 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001412 NULL
1413}, {
1414 &sensor_dev_attr_in1_input.dev_attr.attr,
1415 &sensor_dev_attr_in1_min.dev_attr.attr,
1416 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001417 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001418 NULL
1419}, {
1420 &sensor_dev_attr_in2_input.dev_attr.attr,
1421 &sensor_dev_attr_in2_min.dev_attr.attr,
1422 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001423 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001424 NULL
1425}, {
1426 &sensor_dev_attr_in3_input.dev_attr.attr,
1427 &sensor_dev_attr_in3_min.dev_attr.attr,
1428 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001429 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001430 NULL
1431}, {
1432 &sensor_dev_attr_in4_input.dev_attr.attr,
1433 &sensor_dev_attr_in4_min.dev_attr.attr,
1434 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001435 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001436 NULL
1437}, {
1438 &sensor_dev_attr_in5_input.dev_attr.attr,
1439 &sensor_dev_attr_in5_min.dev_attr.attr,
1440 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001441 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001442 NULL
1443}, {
1444 &sensor_dev_attr_in6_input.dev_attr.attr,
1445 &sensor_dev_attr_in6_min.dev_attr.attr,
1446 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001447 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001448 NULL
1449}, {
1450 &sensor_dev_attr_in7_input.dev_attr.attr,
1451 &sensor_dev_attr_in7_min.dev_attr.attr,
1452 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001453 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001454 NULL
1455}, {
1456 &sensor_dev_attr_in8_input.dev_attr.attr,
1457 NULL
1458} };
Jean Delvare87808be2006-09-24 21:17:13 +02001459
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001460static const struct attribute_group it87_group_in[9] = {
1461 { .attrs = it87_attributes_in[0] },
1462 { .attrs = it87_attributes_in[1] },
1463 { .attrs = it87_attributes_in[2] },
1464 { .attrs = it87_attributes_in[3] },
1465 { .attrs = it87_attributes_in[4] },
1466 { .attrs = it87_attributes_in[5] },
1467 { .attrs = it87_attributes_in[6] },
1468 { .attrs = it87_attributes_in[7] },
1469 { .attrs = it87_attributes_in[8] },
1470};
1471
Guenter Roeck4573acb2012-03-26 16:17:41 -07001472static struct attribute *it87_attributes_temp[3][6] = {
1473{
Jean Delvare87808be2006-09-24 21:17:13 +02001474 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001475 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001476 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001477 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001478 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001479 NULL
1480} , {
1481 &sensor_dev_attr_temp2_input.dev_attr.attr,
1482 &sensor_dev_attr_temp2_max.dev_attr.attr,
1483 &sensor_dev_attr_temp2_min.dev_attr.attr,
1484 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001485 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001486 NULL
1487} , {
1488 &sensor_dev_attr_temp3_input.dev_attr.attr,
1489 &sensor_dev_attr_temp3_max.dev_attr.attr,
1490 &sensor_dev_attr_temp3_min.dev_attr.attr,
1491 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001492 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001493 NULL
1494} };
Jean Delvare87808be2006-09-24 21:17:13 +02001495
Guenter Roeck4573acb2012-03-26 16:17:41 -07001496static const struct attribute_group it87_group_temp[3] = {
1497 { .attrs = it87_attributes_temp[0] },
1498 { .attrs = it87_attributes_temp[1] },
1499 { .attrs = it87_attributes_temp[2] },
1500};
1501
Guenter Roeck161d8982012-12-19 22:17:01 +01001502static struct attribute *it87_attributes_temp_offset[] = {
1503 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1504 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1505 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1506};
1507
Guenter Roeck4573acb2012-03-26 16:17:41 -07001508static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001509 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001510 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001511 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001512 NULL
1513};
1514
1515static const struct attribute_group it87_group = {
1516 .attrs = it87_attributes,
1517};
1518
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001519static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001520 &sensor_dev_attr_in0_beep.dev_attr.attr,
1521 &sensor_dev_attr_in1_beep.dev_attr.attr,
1522 &sensor_dev_attr_in2_beep.dev_attr.attr,
1523 &sensor_dev_attr_in3_beep.dev_attr.attr,
1524 &sensor_dev_attr_in4_beep.dev_attr.attr,
1525 &sensor_dev_attr_in5_beep.dev_attr.attr,
1526 &sensor_dev_attr_in6_beep.dev_attr.attr,
1527 &sensor_dev_attr_in7_beep.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001528 NULL
1529};
Jean Delvared9b327c2010-03-05 22:17:17 +01001530
Guenter Roeck4573acb2012-03-26 16:17:41 -07001531static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001532 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1533 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1534 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001535};
1536
Guenter Roecke1169ba2012-12-19 22:17:01 +01001537static struct attribute *it87_attributes_fan[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001538 &sensor_dev_attr_fan1_input.dev_attr.attr,
1539 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001540 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1541 NULL
1542}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001543 &sensor_dev_attr_fan2_input.dev_attr.attr,
1544 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001545 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1546 NULL
1547}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001548 &sensor_dev_attr_fan3_input.dev_attr.attr,
1549 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001550 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001551 NULL
Guenter Roecke1169ba2012-12-19 22:17:01 +01001552}, {
1553 &sensor_dev_attr_fan4_input.dev_attr.attr,
1554 &sensor_dev_attr_fan4_min.dev_attr.attr,
1555 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1556 NULL
1557}, {
1558 &sensor_dev_attr_fan5_input.dev_attr.attr,
1559 &sensor_dev_attr_fan5_min.dev_attr.attr,
1560 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1561 NULL
Jean Delvare723a0aa2010-03-05 22:17:16 +01001562} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001563
Guenter Roecke1169ba2012-12-19 22:17:01 +01001564static const struct attribute_group it87_group_fan[5] = {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001565 { .attrs = it87_attributes_fan[0] },
1566 { .attrs = it87_attributes_fan[1] },
1567 { .attrs = it87_attributes_fan[2] },
Guenter Roecke1169ba2012-12-19 22:17:01 +01001568 { .attrs = it87_attributes_fan[3] },
1569 { .attrs = it87_attributes_fan[4] },
Jean Delvare723a0aa2010-03-05 22:17:16 +01001570};
1571
Guenter Roecke1169ba2012-12-19 22:17:01 +01001572static const struct attribute *it87_attributes_fan_div[] = {
1573 &sensor_dev_attr_fan1_div.dev_attr.attr,
1574 &sensor_dev_attr_fan2_div.dev_attr.attr,
1575 &sensor_dev_attr_fan3_div.dev_attr.attr,
1576};
Jean Delvare723a0aa2010-03-05 22:17:16 +01001577
1578static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001579 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001580 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001581 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001582 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001583 NULL
1584}, {
1585 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1586 &sensor_dev_attr_pwm2.dev_attr.attr,
1587 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001588 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001589 NULL
1590}, {
1591 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1592 &sensor_dev_attr_pwm3.dev_attr.attr,
1593 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001594 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001595 NULL
1596} };
Jean Delvare87808be2006-09-24 21:17:13 +02001597
Jean Delvare723a0aa2010-03-05 22:17:16 +01001598static const struct attribute_group it87_group_pwm[3] = {
1599 { .attrs = it87_attributes_pwm[0] },
1600 { .attrs = it87_attributes_pwm[1] },
1601 { .attrs = it87_attributes_pwm[2] },
1602};
1603
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001604static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1605 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1606 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1607 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1608 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1609 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1610 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1611 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1612 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1613 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1614 NULL
1615}, {
1616 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1617 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1618 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1619 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1620 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1621 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1622 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1623 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1624 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1625 NULL
1626}, {
1627 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1628 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1629 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1630 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1631 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1632 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1633 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1634 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1635 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1636 NULL
1637} };
1638
1639static const struct attribute_group it87_group_autopwm[3] = {
1640 { .attrs = it87_attributes_autopwm[0] },
1641 { .attrs = it87_attributes_autopwm[1] },
1642 { .attrs = it87_attributes_autopwm[2] },
1643};
1644
Jean Delvared9b327c2010-03-05 22:17:17 +01001645static struct attribute *it87_attributes_fan_beep[] = {
1646 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1647 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1648 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1649 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1650 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1651};
1652
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001653static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001654 &dev_attr_vrm.attr,
1655 &dev_attr_cpu0_vid.attr,
1656 NULL
1657};
1658
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001659static const struct attribute_group it87_group_vid = {
1660 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001661};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Jean Delvare738e5e02010-08-14 21:08:50 +02001663static struct attribute *it87_attributes_label[] = {
1664 &sensor_dev_attr_in3_label.dev_attr.attr,
1665 &sensor_dev_attr_in7_label.dev_attr.attr,
1666 &sensor_dev_attr_in8_label.dev_attr.attr,
1667 NULL
1668};
1669
1670static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001671 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001672};
1673
Jean Delvare2d8672c2005-07-19 23:56:35 +02001674/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001675static int __init it87_find(unsigned short *address,
1676 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001678 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001679 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001680 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001682 err = superio_enter();
1683 if (err)
1684 return err;
1685
1686 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001687 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001688
1689 switch (chip_type) {
1690 case IT8705F_DEVID:
1691 sio_data->type = it87;
1692 break;
1693 case IT8712F_DEVID:
1694 sio_data->type = it8712;
1695 break;
1696 case IT8716F_DEVID:
1697 case IT8726F_DEVID:
1698 sio_data->type = it8716;
1699 break;
1700 case IT8718F_DEVID:
1701 sio_data->type = it8718;
1702 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001703 case IT8720F_DEVID:
1704 sio_data->type = it8720;
1705 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001706 case IT8721F_DEVID:
1707 sio_data->type = it8721;
1708 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001709 case IT8728F_DEVID:
1710 sio_data->type = it8728;
1711 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001712 case IT8782F_DEVID:
1713 sio_data->type = it8782;
1714 break;
1715 case IT8783E_DEVID:
1716 sio_data->type = it8783;
1717 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001718 case 0xffff: /* No device at all */
1719 goto exit;
1720 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001721 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001722 goto exit;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Jean Delvare87673dd2006-08-28 14:37:19 +02001725 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001727 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 goto exit;
1729 }
1730
1731 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1732 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001733 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 goto exit;
1735 }
1736
1737 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001738 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Joe Perchesa8ca1032011-01-12 21:55:10 +01001739 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001740 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Jean Delvare738e5e02010-08-14 21:08:50 +02001742 /* in8 (Vbat) is always internal */
1743 sio_data->internal = (1 << 2);
1744
Jean Delvare87673dd2006-08-28 14:37:19 +02001745 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001746 if (sio_data->type == it87) {
1747 /* The IT8705F doesn't have VID pins at all */
1748 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001749
1750 /* The IT8705F has a different LD number for GPIO */
1751 superio_select(5);
1752 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08001753 } else if (sio_data->type == it8783) {
1754 int reg25, reg27, reg2A, reg2C, regEF;
Guenter Roeck0531d982012-03-02 11:46:44 -08001755
1756 sio_data->skip_vid = 1; /* No VID */
1757
1758 superio_select(GPIO);
1759
1760 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1761 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1762 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1763 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1764 regEF = superio_inb(IT87_SIO_SPI_REG);
1765
Guenter Roeck0531d982012-03-02 11:46:44 -08001766 /* Check if fan3 is there or not */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001767 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08001768 sio_data->skip_fan |= (1 << 2);
1769 if ((reg25 & (1 << 4))
1770 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1771 sio_data->skip_pwm |= (1 << 2);
1772
1773 /* Check if fan2 is there or not */
1774 if (reg27 & (1 << 7))
1775 sio_data->skip_fan |= (1 << 1);
1776 if (reg27 & (1 << 3))
1777 sio_data->skip_pwm |= (1 << 1);
1778
1779 /* VIN5 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001780 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1781 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001782
1783 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001784 if (reg27 & (1 << 1))
1785 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001786
1787 /*
1788 * VIN7
1789 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1790 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001791 if (reg27 & (1 << 2)) {
1792 /*
1793 * The data sheet is a bit unclear regarding the
1794 * internal voltage divider for VCCH5V. It says
1795 * "This bit enables and switches VIN7 (pin 91) to the
1796 * internal voltage divider for VCCH5V".
1797 * This is different to other chips, where the internal
1798 * voltage divider would connect VIN7 to an internal
1799 * voltage source. Maybe that is the case here as well.
1800 *
1801 * Since we don't know for sure, re-route it if that is
1802 * not the case, and ask the user to report if the
1803 * resulting voltage is sane.
1804 */
1805 if (!(reg2C & (1 << 1))) {
1806 reg2C |= (1 << 1);
1807 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1808 pr_notice("Routing internal VCCH5V to in7.\n");
1809 }
1810 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1811 pr_notice("Please report if it displays a reasonable voltage.\n");
1812 }
Guenter Roeck0531d982012-03-02 11:46:44 -08001813
1814 if (reg2C & (1 << 0))
1815 sio_data->internal |= (1 << 0);
1816 if (reg2C & (1 << 1))
1817 sio_data->internal |= (1 << 1);
1818
1819 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1820
Jean Delvare895ff262009-12-09 20:35:47 +01001821 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001822 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001823 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02001824
1825 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001826
Jean Delvare895ff262009-12-09 20:35:47 +01001827 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001828 if (sio_data->type == it8721 || sio_data->type == it8728 ||
1829 sio_data->type == it8782) {
Jean Delvare16b5dda2012-01-16 22:51:48 +01001830 /*
Guenter Roeck0531d982012-03-02 11:46:44 -08001831 * IT8721F/IT8758E, and IT8782F don't have VID pins
1832 * at all, not sure about the IT8728F.
Jean Delvare16b5dda2012-01-16 22:51:48 +01001833 */
Jean Delvare895ff262009-12-09 20:35:47 +01001834 sio_data->skip_vid = 1;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001835 } else {
1836 /* We need at least 4 VID pins */
1837 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001838 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001839 sio_data->skip_vid = 1;
1840 }
Jean Delvare895ff262009-12-09 20:35:47 +01001841 }
1842
Jean Delvare591ec652009-12-09 20:35:48 +01001843 /* Check if fan3 is there or not */
1844 if (reg & (1 << 6))
1845 sio_data->skip_pwm |= (1 << 2);
1846 if (reg & (1 << 7))
1847 sio_data->skip_fan |= (1 << 2);
1848
1849 /* Check if fan2 is there or not */
1850 reg = superio_inb(IT87_SIO_GPIO5_REG);
1851 if (reg & (1 << 1))
1852 sio_data->skip_pwm |= (1 << 1);
1853 if (reg & (1 << 2))
1854 sio_data->skip_fan |= (1 << 1);
1855
Jean Delvare895ff262009-12-09 20:35:47 +01001856 if ((sio_data->type == it8718 || sio_data->type == it8720)
1857 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001858 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001859
1860 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001861
1862 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1863
Jean Delvare436cad22010-07-09 16:22:48 +02001864 /*
1865 * The IT8720F has no VIN7 pin, so VCCH should always be
1866 * routed internally to VIN7 with an internal divider.
1867 * Curiously, there still is a configuration bit to control
1868 * this, which means it can be set incorrectly. And even
1869 * more curiously, many boards out there are improperly
1870 * configured, even though the IT8720F datasheet claims
1871 * that the internal routing of VCCH to VIN7 is the default
1872 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08001873 *
1874 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001875 * If UART6 is enabled, re-route VIN7 to the internal divider
1876 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02001877 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001878 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02001879 reg |= (1 << 1);
1880 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01001881 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02001882 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001883 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001884 sio_data->internal |= (1 << 0);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001885 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1886 sio_data->type == it8728)
Jean Delvare738e5e02010-08-14 21:08:50 +02001887 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01001888
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001889 /*
1890 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1891 * While VIN7 can be routed to the internal voltage divider,
1892 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07001893 *
1894 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
1895 * is the temperature source. Since we can not read the
1896 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001897 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001898 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001899 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07001900 sio_data->skip_temp |= (1 << 2);
1901 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001902
Jean Delvared9b327c2010-03-05 22:17:17 +01001903 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02001904 }
Jean Delvared9b327c2010-03-05 22:17:17 +01001905 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01001906 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02001907
Jean Delvare98dd22c2008-10-09 15:33:58 +02001908 /* Disable specific features based on DMI strings */
1909 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1910 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1911 if (board_vendor && board_name) {
1912 if (strcmp(board_vendor, "nVIDIA") == 0
1913 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001914 /*
1915 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1916 * connected to a fan, but to something else. One user
1917 * has reported instant system power-off when changing
1918 * the PWM2 duty cycle, so we disable it.
1919 * I use the board name string as the trigger in case
1920 * the same board is ever used in other systems.
1921 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01001922 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02001923 sio_data->skip_pwm = (1 << 1);
1924 }
1925 }
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927exit:
1928 superio_exit();
1929 return err;
1930}
1931
Jean Delvare723a0aa2010-03-05 22:17:16 +01001932static void it87_remove_files(struct device *dev)
1933{
1934 struct it87_data *data = platform_get_drvdata(pdev);
1935 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001936 int i;
1937
1938 sysfs_remove_group(&dev->kobj, &it87_group);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001939 for (i = 0; i < 9; i++) {
1940 if (sio_data->skip_in & (1 << i))
1941 continue;
1942 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
1943 if (it87_attributes_in_beep[i])
1944 sysfs_remove_file(&dev->kobj,
1945 it87_attributes_in_beep[i]);
1946 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07001947 for (i = 0; i < 3; i++) {
1948 if (!(data->has_temp & (1 << i)))
1949 continue;
1950 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
Guenter Roeck161d8982012-12-19 22:17:01 +01001951 if (has_temp_offset(data))
1952 sysfs_remove_file(&dev->kobj,
1953 it87_attributes_temp_offset[i]);
Guenter Roeck4573acb2012-03-26 16:17:41 -07001954 if (sio_data->beep_pin)
1955 sysfs_remove_file(&dev->kobj,
1956 it87_attributes_temp_beep[i]);
1957 }
Jean Delvare723a0aa2010-03-05 22:17:16 +01001958 for (i = 0; i < 5; i++) {
1959 if (!(data->has_fan & (1 << i)))
1960 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01001961 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01001962 if (sio_data->beep_pin)
1963 sysfs_remove_file(&dev->kobj,
1964 it87_attributes_fan_beep[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01001965 if (i < 3 && !has_16bit_fans(data))
1966 sysfs_remove_file(&dev->kobj,
1967 it87_attributes_fan_div[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001968 }
1969 for (i = 0; i < 3; i++) {
1970 if (sio_data->skip_pwm & (1 << 0))
1971 continue;
1972 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001973 if (has_old_autopwm(data))
1974 sysfs_remove_group(&dev->kobj,
1975 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001976 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001977 if (!sio_data->skip_vid)
1978 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02001979 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001980}
1981
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001982static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001985 struct resource *res;
1986 struct device *dev = &pdev->dev;
1987 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001988 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01001990 int fan_beep_need_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001992 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07001993 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
1994 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001995 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1996 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001997 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07001998 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Guenter Roeck62a1d052012-03-24 21:54:41 -07002001 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2002 if (!data)
2003 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002005 data->addr = res->start;
2006 data->type = sio_data->type;
Guenter Roeck483db432012-12-19 22:17:02 +01002007 data->features = it87_devices[sio_data->type].features;
Guenter Roeck5d8d2f22012-12-19 22:17:02 +01002008 data->peci_mask = it87_devices[sio_data->type].peci_mask;
Guenter Roeck19529782012-12-19 22:17:02 +01002009 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
Guenter Roeck483db432012-12-19 22:17:02 +01002010 data->name = it87_devices[sio_data->type].name;
2011 /*
2012 * IT8705F Datasheet 0.4.1, 3h == Version G.
2013 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2014 * These are the first revisions with 16-bit tachometer support.
2015 */
2016 switch (data->type) {
2017 case it87:
2018 if (sio_data->revision >= 0x03) {
2019 data->features &= ~FEAT_OLD_AUTOPWM;
2020 data->features |= FEAT_16BIT_FANS;
2021 }
2022 break;
2023 case it8712:
2024 if (sio_data->revision >= 0x08) {
2025 data->features &= ~FEAT_OLD_AUTOPWM;
2026 data->features |= FEAT_16BIT_FANS;
2027 }
2028 break;
2029 default:
2030 break;
2031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002034 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002035 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2036 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002038 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002040 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002043 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002045 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01002046 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002047 if (sio_data->internal & (1 << 0))
2048 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2049 if (sio_data->internal & (1 << 1))
2050 data->in_scaled |= (1 << 7); /* in7 is VSB */
2051 if (sio_data->internal & (1 << 2))
2052 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Guenter Roeck0531d982012-03-02 11:46:44 -08002053 } else if (sio_data->type == it8782 || sio_data->type == it8783) {
2054 if (sio_data->internal & (1 << 0))
2055 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2056 if (sio_data->internal & (1 << 1))
2057 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002058 }
2059
Guenter Roeck4573acb2012-03-26 16:17:41 -07002060 data->has_temp = 0x07;
2061 if (sio_data->skip_temp & (1 << 2)) {
2062 if (sio_data->type == it8782
2063 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2064 data->has_temp &= ~(1 << 2);
2065 }
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002068 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002071 err = sysfs_create_group(&dev->kobj, &it87_group);
2072 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002073 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002074
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002075 for (i = 0; i < 9; i++) {
2076 if (sio_data->skip_in & (1 << i))
2077 continue;
2078 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2079 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002080 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002081 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2082 err = sysfs_create_file(&dev->kobj,
2083 it87_attributes_in_beep[i]);
2084 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002085 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002086 }
2087 }
2088
Guenter Roeck4573acb2012-03-26 16:17:41 -07002089 for (i = 0; i < 3; i++) {
2090 if (!(data->has_temp & (1 << i)))
2091 continue;
2092 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002093 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002094 goto error;
Guenter Roeck161d8982012-12-19 22:17:01 +01002095 if (has_temp_offset(data)) {
2096 err = sysfs_create_file(&dev->kobj,
2097 it87_attributes_temp_offset[i]);
2098 if (err)
2099 goto error;
2100 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07002101 if (sio_data->beep_pin) {
2102 err = sysfs_create_file(&dev->kobj,
2103 it87_attributes_temp_beep[i]);
2104 if (err)
2105 goto error;
2106 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002107 }
2108
Jean Delvare9060f8b2006-08-28 14:24:17 +02002109 /* Do not create fan files for disabled fans */
Jean Delvared9b327c2010-03-05 22:17:17 +01002110 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01002111 for (i = 0; i < 5; i++) {
2112 if (!(data->has_fan & (1 << i)))
2113 continue;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002114 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002115 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002116 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002117
Guenter Roecke1169ba2012-12-19 22:17:01 +01002118 if (i < 3 && !has_16bit_fans(data)) {
2119 err = sysfs_create_file(&dev->kobj,
2120 it87_attributes_fan_div[i]);
2121 if (err)
2122 goto error;
2123 }
2124
Jean Delvared9b327c2010-03-05 22:17:17 +01002125 if (sio_data->beep_pin) {
2126 err = sysfs_create_file(&dev->kobj,
2127 it87_attributes_fan_beep[i]);
2128 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002129 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002130 if (!fan_beep_need_rw)
2131 continue;
2132
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002133 /*
2134 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002135 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002136 * for it.
2137 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002138 if (sysfs_chmod_file(&dev->kobj,
2139 it87_attributes_fan_beep[i],
2140 S_IRUGO | S_IWUSR))
2141 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2142 i + 1);
2143 fan_beep_need_rw = 0;
2144 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002145 }
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002148 for (i = 0; i < 3; i++) {
2149 if (sio_data->skip_pwm & (1 << i))
2150 continue;
2151 err = sysfs_create_group(&dev->kobj,
2152 &it87_group_pwm[i]);
2153 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002154 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002155
2156 if (!has_old_autopwm(data))
2157 continue;
2158 err = sysfs_create_group(&dev->kobj,
2159 &it87_group_autopwm[i]);
2160 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002161 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164
Jean Delvare895ff262009-12-09 20:35:47 +01002165 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002166 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002167 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002168 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002169 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2170 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002171 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002172 }
2173
Jean Delvare738e5e02010-08-14 21:08:50 +02002174 /* Export labels for internal sensors */
2175 for (i = 0; i < 3; i++) {
2176 if (!(sio_data->internal & (1 << i)))
2177 continue;
2178 err = sysfs_create_file(&dev->kobj,
2179 it87_attributes_label[i]);
2180 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002181 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002182 }
2183
Tony Jones1beeffe2007-08-20 13:46:20 -07002184 data->hwmon_dev = hwmon_device_register(dev);
2185 if (IS_ERR(data->hwmon_dev)) {
2186 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002187 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189
2190 return 0;
2191
Guenter Roeck62a1d052012-03-24 21:54:41 -07002192error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002193 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return err;
2195}
2196
Bill Pemberton281dfd02012-11-19 13:25:51 -05002197static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002199 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Tony Jones1beeffe2007-08-20 13:46:20 -07002201 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002202 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return 0;
2205}
2206
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002207/*
2208 * Must be called with data->update_lock held, except during initialization.
2209 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2210 * would slow down the IT87 access and should not be necessary.
2211 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002212static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002214 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2215 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002218/*
2219 * Must be called with data->update_lock held, except during initialization.
2220 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2221 * would slow down the IT87 access and should not be necessary.
2222 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002223static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002225 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2226 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
2229/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002230static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002232 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002233 /*
2234 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002236 * disable pwm control to protect the user.
2237 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002238 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if ((tmp & 0x87) == 0) {
2240 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002241 /*
2242 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002244 * inverting all fan speed values.
2245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 int i;
2247 u8 pwm[3];
2248
2249 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002250 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 IT87_REG_PWM(i));
2252
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002253 /*
2254 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 * might be correct, as suspicious as it seems, so we
2256 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002257 * PWM interface).
2258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002260 dev_info(dev,
2261 "Reconfiguring PWM to active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002262 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 tmp | 0x87);
2264 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002265 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 IT87_REG_PWM(i),
2267 0x7f & ~pwm[i]);
2268 return 1;
2269 }
2270
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002271 dev_info(dev,
2272 "PWM configuration is too broken to be fixed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002275 dev_info(dev,
2276 "Detected broken BIOS defaults, disabling PWM interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return 0;
2278 } else if (fix_pwm_polarity) {
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002279 dev_info(dev,
2280 "PWM configuration looks sane, won't touch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
2282
2283 return 1;
2284}
2285
2286/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002287static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
Jean Delvare591ec652009-12-09 20:35:48 +01002289 struct it87_sio_data *sio_data = pdev->dev.platform_data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002290 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002292 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002294 /*
2295 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002296 * - If it is in automatic mode, setting to manual mode should set
2297 * the fan to full speed by default.
2298 * - If it is in manual mode, we need a mapping to temperature
2299 * channels to use when later setting to automatic mode later.
2300 * Use a 1:1 mapping by default (we are clueless.)
2301 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002302 * prior to switching to a different mode.
2303 * Note that this is no longer needed for the IT8721F and later, as
2304 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002305 * manual duty cycle.
2306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002308 data->pwm_temp_map[i] = i;
2309 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002310 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002313 /*
2314 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002315 * registers. For low voltage limits it makes no sense and triggers
2316 * alarms, so change to 0 instead. For high temperature limits, it
2317 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002318 * but is still confusing, so change to 127 degrees C.
2319 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002320 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002321 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002322 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002323 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002324 }
2325 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002326 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002327 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002328 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002329 }
2330
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002331 /*
2332 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002333 * set to two different sensor types and we can't guess which one
2334 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002335 * run-time through the temp{1-3}_type sysfs accessors if needed.
2336 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002339 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if ((tmp & 0xff) == 0) {
2341 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002342 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344
2345 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002346 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002347 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002348 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002350 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002351 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2352 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002354 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Jean Delvare17d648b2006-08-28 14:23:46 +02002356 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02002357 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002358 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002359 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002360 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002361 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002362 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002363 tmp | 0x07);
2364 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002365 /* IT8705F, IT8782F, and IT8783E/F only support three fans. */
2366 if (data->type != it87 && data->type != it8782 &&
2367 data->type != it8783) {
Andrew Paprocki816d8c62008-08-06 22:41:06 +02002368 if (tmp & (1 << 4))
2369 data->has_fan |= (1 << 3); /* fan4 enabled */
2370 if (tmp & (1 << 5))
2371 data->has_fan |= (1 << 4); /* fan5 enabled */
2372 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002373 }
2374
Jean Delvare591ec652009-12-09 20:35:48 +01002375 /* Fan input pins may be used for alternative functions */
2376 data->has_fan &= ~sio_data->skip_fan;
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002379 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002380 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 | (update_vbat ? 0x41 : 0x01));
2382}
2383
Jean Delvareb99883d2010-03-05 22:17:15 +01002384static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2385{
2386 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002387 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002388 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002389 data->pwm_duty[nr] = it87_read_value(data,
2390 IT87_REG_PWM_DUTY(nr));
2391 } else {
2392 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2393 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2394 else /* Manual mode */
2395 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2396 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002397
2398 if (has_old_autopwm(data)) {
2399 int i;
2400
2401 for (i = 0; i < 5 ; i++)
2402 data->auto_temp[nr][i] = it87_read_value(data,
2403 IT87_REG_AUTO_TEMP(nr, i));
2404 for (i = 0; i < 3 ; i++)
2405 data->auto_pwm[nr][i] = it87_read_value(data,
2406 IT87_REG_AUTO_PWM(nr, i));
2407 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002408}
2409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410static struct it87_data *it87_update_device(struct device *dev)
2411{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002412 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 int i;
2414
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002415 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2418 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002420 /*
2421 * Cleared after each update, so reenable. Value
2422 * returned by this read will be previous value
2423 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002424 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002425 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
2427 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002428 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002429 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002430 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002431 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002432 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002433 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
Jean Delvare3543a532006-08-28 14:27:25 +02002435 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002436 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Jean Delvarec7f1f712007-09-03 17:11:46 +02002438 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002439 /* Skip disabled fans */
2440 if (!(data->has_fan & (1 << i)))
2441 continue;
2442
Guenter Roecke1169ba2012-12-19 22:17:01 +01002443 data->fan[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002444 it87_read_value(data, IT87_REG_FAN_MIN[i]);
Guenter Roecke1169ba2012-12-19 22:17:01 +01002445 data->fan[i][0] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002446 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002447 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002448 if (has_16bit_fans(data)) {
Guenter Roecke1169ba2012-12-19 22:17:01 +01002449 data->fan[i][0] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002450 IT87_REG_FANX[i]) << 8;
Guenter Roecke1169ba2012-12-19 22:17:01 +01002451 data->fan[i][1] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002452 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
2455 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002456 if (!(data->has_temp & (1 << i)))
2457 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002458 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002459 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002460 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002461 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002462 data->temp[i][2] =
2463 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Guenter Roeck161d8982012-12-19 22:17:01 +01002464 if (has_temp_offset(data))
2465 data->temp[i][3] =
2466 it87_read_value(data,
2467 IT87_REG_TEMP_OFFSET[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
2469
Jean Delvare17d648b2006-08-28 14:23:46 +02002470 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002471 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002472 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002473 data->fan_div[0] = i & 0x07;
2474 data->fan_div[1] = (i >> 3) & 0x07;
2475 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002479 it87_read_value(data, IT87_REG_ALARM1) |
2480 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2481 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002482 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002483
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002484 data->fan_main_ctrl = it87_read_value(data,
2485 IT87_REG_FAN_MAIN_CTRL);
2486 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002487 for (i = 0; i < 3; i++)
2488 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002490 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck19529782012-12-19 22:17:02 +01002491 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002492 /*
2493 * The IT8705F does not have VID capability.
2494 * The IT8718F and later don't use IT87_REG_VID for the
2495 * same purpose.
2496 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002497 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002498 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002499 /*
2500 * The older IT8712F revisions had only 5 VID pins,
2501 * but we assume it is always safe to read 6 bits.
2502 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002503 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 }
2505 data->last_updated = jiffies;
2506 data->valid = 1;
2507 }
2508
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002509 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 return data;
2512}
2513
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002514static int __init it87_device_add(unsigned short address,
2515 const struct it87_sio_data *sio_data)
2516{
2517 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002518 .start = address + IT87_EC_OFFSET,
2519 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002520 .name = DRVNAME,
2521 .flags = IORESOURCE_IO,
2522 };
2523 int err;
2524
Jean Delvareb9acb642009-01-07 16:37:35 +01002525 err = acpi_check_resource_conflict(&res);
2526 if (err)
2527 goto exit;
2528
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002529 pdev = platform_device_alloc(DRVNAME, address);
2530 if (!pdev) {
2531 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002532 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002533 goto exit;
2534 }
2535
2536 err = platform_device_add_resources(pdev, &res, 1);
2537 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002538 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002539 goto exit_device_put;
2540 }
2541
2542 err = platform_device_add_data(pdev, sio_data,
2543 sizeof(struct it87_sio_data));
2544 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002545 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002546 goto exit_device_put;
2547 }
2548
2549 err = platform_device_add(pdev);
2550 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002551 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002552 goto exit_device_put;
2553 }
2554
2555 return 0;
2556
2557exit_device_put:
2558 platform_device_put(pdev);
2559exit:
2560 return err;
2561}
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563static int __init sm_it87_init(void)
2564{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002565 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002566 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002567 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002568
Jean Delvare98dd22c2008-10-09 15:33:58 +02002569 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002570 err = it87_find(&isa_address, &sio_data);
2571 if (err)
2572 return err;
2573 err = platform_driver_register(&it87_driver);
2574 if (err)
2575 return err;
2576
2577 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002578 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002579 platform_driver_unregister(&it87_driver);
2580 return err;
2581 }
2582
2583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
2586static void __exit sm_it87_exit(void)
2587{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002588 platform_device_unregister(pdev);
2589 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
2592
Guenter Roeck1d9bcf62012-12-19 22:17:01 +01002593MODULE_AUTHOR("Chris Gauthron, Jean Delvare <khali@linux-fr.org>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002594MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595module_param(update_vbat, bool, 0);
2596MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2597module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002598MODULE_PARM_DESC(fix_pwm_polarity,
2599 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600MODULE_LICENSE("GPL");
2601
2602module_init(sm_it87_init);
2603module_exit(sm_it87_exit);