blob: 21fb7f24152c5a43cd72d7f4f0a7a6440b3c6ecd [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 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206#define IT87_REG_FAN_MAIN_CTRL 0x13
207#define IT87_REG_FAN_CTL 0x14
208#define IT87_REG_PWM(nr) (0x15 + (nr))
Jean Delvare6229cdb2010-12-08 16:27:22 +0100209#define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211#define IT87_REG_VIN(nr) (0x20 + (nr))
212#define IT87_REG_TEMP(nr) (0x29 + (nr))
213
214#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
215#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
216#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
217#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#define IT87_REG_VIN_ENABLE 0x50
220#define IT87_REG_TEMP_ENABLE 0x51
Guenter Roeck4573acb2012-03-26 16:17:41 -0700221#define IT87_REG_TEMP_EXTRA 0x55
Jean Delvared9b327c2010-03-05 22:17:17 +0100222#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224#define IT87_REG_CHIPID 0x58
225
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100226#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
227#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200230struct it87_sio_data {
231 enum chips type;
232 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200233 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200234 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100235 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200236 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100237 /* Features skipped based on config or DMI */
Guenter Roeck9172b5d2012-03-24 21:49:54 -0700238 u16 skip_in;
Jean Delvare895ff262009-12-09 20:35:47 +0100239 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100240 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200241 u8 skip_pwm;
Guenter Roeck4573acb2012-03-26 16:17:41 -0700242 u8 skip_temp;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200243};
244
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800245/*
246 * For each registered chip, we need to keep some data in memory.
247 * The structure is dynamically allocated.
248 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700250 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 enum chips type;
Andrew Paprocki04751692008-08-06 22:41:06 +0200252 u8 revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200254 unsigned short addr;
255 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100256 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 char valid; /* !=0 if following fields are valid */
258 unsigned long last_updated; /* In jiffies */
259
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200260 u16 in_scaled; /* Internal voltage sensors are scaled */
Guenter Roeck929c6a52012-12-19 22:17:00 +0100261 u8 in[9][3]; /* [nr][0]=in, [1]=min, [2]=max */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200262 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvarec7f1f712007-09-03 17:11:46 +0200263 u16 fan[5]; /* Register values, possibly combined */
264 u16 fan_min[5]; /* Register values, possibly combined */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700265 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck60ca3852012-12-19 22:17:00 +0100266 s8 temp[3][3]; /* [nr][0]=temp, [1]=min, [2]=max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 u8 sensor; /* Register value */
268 u8 fan_div[3]; /* Register encoding, shifted right */
269 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100270 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100272 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100274 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100275
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800276 /*
277 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100278 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
279 * 7, and we want to preserve settings on mode changes, so we have
280 * to track all values separately.
281 * Starting with the IT8721F, the manual PWM duty cycles are stored
282 * in separate registers (8-bit values), so the separate tracking
283 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800284 * simple.
285 */
Jean Delvareb99883d2010-03-05 22:17:15 +0100286 u8 pwm_ctrl[3]; /* Register value */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100287 u8 pwm_duty[3]; /* Manual PWM value set by user */
Jean Delvareb99883d2010-03-05 22:17:15 +0100288 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100289
290 /* Automatic fan speed control registers */
291 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
292 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
Jean Delvare16b5dda2012-01-16 22:51:48 +0100295static inline int has_12mv_adc(const struct it87_data *data)
296{
297 /*
298 * IT8721F and later have a 12 mV ADC, also with internal scaling
299 * on selected inputs.
300 */
301 return data->type == it8721
302 || data->type == it8728;
303}
304
305static inline int has_newer_autopwm(const struct it87_data *data)
306{
307 /*
308 * IT8721F and later have separate registers for the temperature
309 * mapping and the manual duty cycle.
310 */
311 return data->type == it8721
312 || data->type == it8728;
313}
314
Guenter Roeck0531d982012-03-02 11:46:44 -0800315static int adc_lsb(const struct it87_data *data, int nr)
316{
317 int lsb = has_12mv_adc(data) ? 12 : 16;
318 if (data->in_scaled & (1 << nr))
319 lsb <<= 1;
320 return lsb;
321}
322
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200323static u8 in_to_reg(const struct it87_data *data, int nr, long val)
324{
Guenter Roeck0531d982012-03-02 11:46:44 -0800325 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200326 return SENSORS_LIMIT(val, 0, 255);
327}
328
329static int in_from_reg(const struct it87_data *data, int nr, int val)
330{
Guenter Roeck0531d982012-03-02 11:46:44 -0800331 return val * adc_lsb(data, nr);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200332}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200333
334static inline u8 FAN_TO_REG(long rpm, int div)
335{
336 if (rpm == 0)
337 return 255;
338 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
339 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
340 254);
341}
342
343static inline u16 FAN16_TO_REG(long rpm)
344{
345 if (rpm == 0)
346 return 0xffff;
347 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
348}
349
350#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
351 1350000 / ((val) * (div)))
352/* The divider is fixed to 2 in 16-bit mode */
353#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
354 1350000 / ((val) * 2))
355
356#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
357 ((val) + 500) / 1000), -128, 127))
358#define TEMP_FROM_REG(val) ((val) * 1000)
359
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200360static u8 pwm_to_reg(const struct it87_data *data, long val)
361{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100362 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200363 return val;
364 else
365 return val >> 1;
366}
367
368static int pwm_from_reg(const struct it87_data *data, u8 reg)
369{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100370 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200371 return reg;
372 else
373 return (reg & 0x7f) << 1;
374}
375
Jean Delvare0df6454d2010-10-28 20:31:51 +0200376
377static int DIV_TO_REG(int val)
378{
379 int answer = 0;
380 while (answer < 7 && (val >>= 1))
381 answer++;
382 return answer;
383}
384#define DIV_FROM_REG(val) (1 << (val))
385
386static const unsigned int pwm_freq[8] = {
387 48000000 / 128,
388 24000000 / 128,
389 12000000 / 128,
390 8000000 / 128,
391 6000000 / 128,
392 3000000 / 128,
393 1500000 / 128,
394 750000 / 128,
395};
396
Andrew Paprocki04751692008-08-06 22:41:06 +0200397static inline int has_16bit_fans(const struct it87_data *data)
398{
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800399 /*
400 * IT8705F Datasheet 0.4.1, 3h == Version G.
401 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
402 * These are the first revisions with 16-bit tachometer support.
403 */
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200404 return (data->type == it87 && data->revision >= 0x03)
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200405 || (data->type == it8712 && data->revision >= 0x08)
Andrew Paprocki04751692008-08-06 22:41:06 +0200406 || data->type == it8716
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100407 || data->type == it8718
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200408 || data->type == it8720
Jean Delvare16b5dda2012-01-16 22:51:48 +0100409 || data->type == it8721
Guenter Roeck0531d982012-03-02 11:46:44 -0800410 || data->type == it8728
411 || data->type == it8782
412 || data->type == it8783;
Andrew Paprocki04751692008-08-06 22:41:06 +0200413}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100415static inline int has_old_autopwm(const struct it87_data *data)
416{
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800417 /*
418 * The old automatic fan speed control interface is implemented
419 * by IT8705F chips up to revision F and IT8712F chips up to
420 * revision G.
421 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100422 return (data->type == it87 && data->revision < 0x03)
423 || (data->type == it8712 && data->revision < 0x08);
424}
425
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200426static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500427static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200429static int it87_read_value(struct it87_data *data, u8 reg);
430static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200432static int it87_check_pwm(struct device *dev);
433static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200436static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100437 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200438 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200439 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100440 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200441 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500442 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200443};
444
Jean Delvare20ad93d2005-06-05 11:53:25 +0200445static ssize_t show_in(struct device *dev, struct device_attribute *attr,
Guenter Roeck929c6a52012-12-19 22:17:00 +0100446 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100448 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
449 int nr = sattr->nr;
450 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct it87_data *data = it87_update_device(dev);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100453 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Guenter Roeck929c6a52012-12-19 22:17:00 +0100456static ssize_t set_in(struct device *dev, struct device_attribute *attr,
457 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Guenter Roeck929c6a52012-12-19 22:17:00 +0100459 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
460 int nr = sattr->nr;
461 int index = sattr->index;
Jean Delvare20ad93d2005-06-05 11:53:25 +0200462
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200463 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100464 unsigned long val;
465
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100466 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100467 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100469 mutex_lock(&data->update_lock);
Guenter Roeck929c6a52012-12-19 22:17:00 +0100470 data->in[nr][index] = in_to_reg(data, nr, val);
471 it87_write_value(data,
472 index == 1 ? IT87_REG_VIN_MIN(nr)
473 : IT87_REG_VIN_MAX(nr),
474 data->in[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100475 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return count;
477}
478
Guenter Roeck929c6a52012-12-19 22:17:00 +0100479static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
480static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
481 0, 1);
482static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
483 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Guenter Roeck929c6a52012-12-19 22:17:00 +0100485static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
486static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
487 1, 1);
488static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
489 1, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Guenter Roeck929c6a52012-12-19 22:17:00 +0100491static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
492static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
493 2, 1);
494static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
495 2, 2);
496
497static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
498static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
499 3, 1);
500static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
501 3, 2);
502
503static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
504static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
505 4, 1);
506static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
507 4, 2);
508
509static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
510static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
511 5, 1);
512static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
513 5, 2);
514
515static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
516static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
517 6, 1);
518static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
519 6, 2);
520
521static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
522static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
523 7, 1);
524static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
525 7, 2);
526
527static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200530static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100531 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100533 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
534 int nr = sattr->nr;
535 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200537
Guenter Roeck60ca3852012-12-19 22:17:00 +0100538 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200540
Guenter Roeck60ca3852012-12-19 22:17:00 +0100541static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
542 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100544 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
545 int nr = sattr->nr;
546 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200547 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100548 long val;
549
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100550 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100551 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100553 mutex_lock(&data->update_lock);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100554 data->temp[nr][index] = TEMP_TO_REG(val);
555 it87_write_value(data,
556 index == 1 ? IT87_REG_TEMP_LOW(nr)
557 : IT87_REG_TEMP_HIGH(nr),
558 data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100559 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return count;
561}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200562
Guenter Roeck60ca3852012-12-19 22:17:00 +0100563static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
564static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
565 0, 1);
566static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
567 0, 2);
568static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
569static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
570 1, 1);
571static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
572 1, 2);
573static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
574static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
575 2, 1);
576static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
577 2, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Guenter Roeck2cece012012-12-19 22:17:01 +0100579static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
580 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200582 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
583 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800585 u8 reg = data->sensor; /* In case value is updated while used */
Jean Delvare5f2dc792010-03-05 22:17:18 +0100586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (reg & (1 << nr))
588 return sprintf(buf, "3\n"); /* thermal diode */
589 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200590 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return sprintf(buf, "0\n"); /* disabled */
592}
Guenter Roeck2cece012012-12-19 22:17:01 +0100593
594static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
595 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200597 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
598 int nr = sensor_attr->index;
599
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200600 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100601 long val;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200602 u8 reg;
Jean Delvaref5f64502010-03-05 22:17:19 +0100603
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100604 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100605 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Jean Delvare8acf07c2010-04-14 16:14:09 +0200607 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
608 reg &= ~(1 << nr);
609 reg &= ~(8 << nr);
Jean Delvare4ed10772008-10-17 17:51:16 +0200610 if (val == 2) { /* backwards compatibility */
611 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
612 "instead\n");
613 val = 4;
614 }
615 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200617 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200618 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200619 reg |= 8 << nr;
620 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200622
623 mutex_lock(&data->update_lock);
624 data->sensor = reg;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200625 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200626 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100627 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return count;
629}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Guenter Roeck2cece012012-12-19 22:17:01 +0100631static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
632 set_temp_type, 0);
633static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
634 set_temp_type, 1);
635static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
636 set_temp_type, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100639
640static int pwm_mode(const struct it87_data *data, int nr)
641{
642 int ctrl = data->fan_main_ctrl & (1 << nr);
643
644 if (ctrl == 0) /* Full speed */
645 return 0;
646 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
647 return 2;
648 else /* Manual mode */
649 return 1;
650}
651
Jean Delvare20ad93d2005-06-05 11:53:25 +0200652static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
653 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200655 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
656 int nr = sensor_attr->index;
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100659 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 DIV_FROM_REG(data->fan_div[nr])));
661}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200662static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
663 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200665 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
666 int nr = sensor_attr->index;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100669 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
670 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200672static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
673 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200675 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
676 int nr = sensor_attr->index;
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 struct it87_data *data = it87_update_device(dev);
679 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
680}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100681static ssize_t show_pwm_enable(struct device *dev,
682 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200684 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
685 int nr = sensor_attr->index;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100688 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200690static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
691 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200693 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
694 int nr = sensor_attr->index;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200697 return sprintf(buf, "%d\n",
698 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100700static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
701 char *buf)
702{
703 struct it87_data *data = it87_update_device(dev);
704 int index = (data->fan_ctl >> 4) & 0x07;
705
706 return sprintf(buf, "%u\n", pwm_freq[index]);
707}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200708static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
709 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200711 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
712 int nr = sensor_attr->index;
713
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200714 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100715 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100716 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100718 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100719 return -EINVAL;
720
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100721 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200722 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800723 switch (nr) {
Jean Delvare5f2dc792010-03-05 22:17:18 +0100724 case 0:
725 data->fan_div[nr] = reg & 0x07;
726 break;
727 case 1:
728 data->fan_div[nr] = (reg >> 3) & 0x07;
729 break;
730 case 2:
731 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
732 break;
Jean Delvare07eab462005-11-23 15:44:31 -0800733 }
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200736 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100737 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return count;
739}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200740static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
741 const char *buf, size_t count)
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
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200746 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100747 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200748 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 u8 old;
750
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100751 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100752 return -EINVAL;
753
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100754 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200755 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200757 /* Save fan min limit */
758 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 switch (nr) {
761 case 0:
762 case 1:
763 data->fan_div[nr] = DIV_TO_REG(val);
764 break;
765 case 2:
766 if (val < 8)
767 data->fan_div[nr] = 1;
768 else
769 data->fan_div[nr] = 3;
770 }
771 val = old & 0x80;
772 val |= (data->fan_div[0] & 0x07);
773 val |= (data->fan_div[1] & 0x07) << 3;
774 if (data->fan_div[2] == 3)
775 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200776 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200778 /* Restore fan min limit */
779 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200780 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200781
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100782 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return count;
784}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100785
786/* Returns 0 if OK, -EINVAL otherwise */
787static int check_trip_points(struct device *dev, int nr)
788{
789 const struct it87_data *data = dev_get_drvdata(dev);
790 int i, err = 0;
791
792 if (has_old_autopwm(data)) {
793 for (i = 0; i < 3; i++) {
794 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
795 err = -EINVAL;
796 }
797 for (i = 0; i < 2; i++) {
798 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
799 err = -EINVAL;
800 }
801 }
802
803 if (err) {
804 dev_err(dev, "Inconsistent trip points, not switching to "
805 "automatic mode\n");
806 dev_err(dev, "Adjust the trip points and try again\n");
807 }
808 return err;
809}
810
Jean Delvare20ad93d2005-06-05 11:53:25 +0200811static ssize_t set_pwm_enable(struct device *dev,
812 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200814 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
815 int nr = sensor_attr->index;
816
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200817 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100818 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100820 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100821 return -EINVAL;
822
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100823 /* Check trip points before switching to automatic mode */
824 if (val == 2) {
825 if (check_trip_points(dev, nr) < 0)
826 return -EINVAL;
827 }
828
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100829 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 if (val == 0) {
832 int tmp;
833 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200834 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
835 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* set on/off mode */
837 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100838 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
839 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100840 } else {
841 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +0100842 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +0100843 data->pwm_temp_map[nr] :
844 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +0100845 else /* Automatic mode */
846 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
847 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* set SmartGuardian mode */
849 data->fan_main_ctrl |= (1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100850 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
851 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100854 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return count;
856}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200857static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
858 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200860 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
861 int nr = sensor_attr->index;
862
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200863 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100864 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100866 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return -EINVAL;
868
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100869 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +0100870 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800871 /*
872 * If we are in automatic mode, the PWM duty cycle register
873 * is read-only so we can't write the value.
874 */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100875 if (data->pwm_ctrl[nr] & 0x80) {
876 mutex_unlock(&data->update_lock);
877 return -EBUSY;
878 }
879 data->pwm_duty[nr] = pwm_to_reg(data, val);
880 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
881 data->pwm_duty[nr]);
882 } else {
883 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800884 /*
885 * If we are in manual mode, write the duty cycle immediately;
886 * otherwise, just store it for later use.
887 */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100888 if (!(data->pwm_ctrl[nr] & 0x80)) {
889 data->pwm_ctrl[nr] = data->pwm_duty[nr];
890 it87_write_value(data, IT87_REG_PWM(nr),
891 data->pwm_ctrl[nr]);
892 }
Jean Delvareb99883d2010-03-05 22:17:15 +0100893 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100894 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return count;
896}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100897static ssize_t set_pwm_freq(struct device *dev,
898 struct device_attribute *attr, const char *buf, size_t count)
899{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200900 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100901 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100902 int i;
903
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100904 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100905 return -EINVAL;
906
Jean Delvaref8d0c192007-02-14 21:15:02 +0100907 /* Search for the nearest available frequency */
908 for (i = 0; i < 7; i++) {
909 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
910 break;
911 }
912
913 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200914 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100915 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200916 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100917 mutex_unlock(&data->update_lock);
918
919 return count;
920}
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100921static ssize_t show_pwm_temp_map(struct device *dev,
922 struct device_attribute *attr, char *buf)
923{
924 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
925 int nr = sensor_attr->index;
926
927 struct it87_data *data = it87_update_device(dev);
928 int map;
929
930 if (data->pwm_temp_map[nr] < 3)
931 map = 1 << data->pwm_temp_map[nr];
932 else
933 map = 0; /* Should never happen */
934 return sprintf(buf, "%d\n", map);
935}
936static ssize_t set_pwm_temp_map(struct device *dev,
937 struct device_attribute *attr, const char *buf, size_t count)
938{
939 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
940 int nr = sensor_attr->index;
941
942 struct it87_data *data = dev_get_drvdata(dev);
943 long val;
944 u8 reg;
945
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800946 /*
947 * This check can go away if we ever support automatic fan speed
948 * control on newer chips.
949 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100950 if (!has_old_autopwm(data)) {
951 dev_notice(dev, "Mapping change disabled for safety reasons\n");
952 return -EINVAL;
953 }
954
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100955 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100956 return -EINVAL;
957
958 switch (val) {
959 case (1 << 0):
960 reg = 0x00;
961 break;
962 case (1 << 1):
963 reg = 0x01;
964 break;
965 case (1 << 2):
966 reg = 0x02;
967 break;
968 default:
969 return -EINVAL;
970 }
971
972 mutex_lock(&data->update_lock);
973 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800974 /*
975 * If we are in automatic mode, write the temp mapping immediately;
976 * otherwise, just store it for later use.
977 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100978 if (data->pwm_ctrl[nr] & 0x80) {
979 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
980 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
981 }
982 mutex_unlock(&data->update_lock);
983 return count;
984}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100986static ssize_t show_auto_pwm(struct device *dev,
987 struct device_attribute *attr, char *buf)
988{
989 struct it87_data *data = it87_update_device(dev);
990 struct sensor_device_attribute_2 *sensor_attr =
991 to_sensor_dev_attr_2(attr);
992 int nr = sensor_attr->nr;
993 int point = sensor_attr->index;
994
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200995 return sprintf(buf, "%d\n",
996 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100997}
998
999static ssize_t set_auto_pwm(struct device *dev,
1000 struct device_attribute *attr, const char *buf, size_t count)
1001{
1002 struct it87_data *data = dev_get_drvdata(dev);
1003 struct sensor_device_attribute_2 *sensor_attr =
1004 to_sensor_dev_attr_2(attr);
1005 int nr = sensor_attr->nr;
1006 int point = sensor_attr->index;
1007 long val;
1008
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001009 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001010 return -EINVAL;
1011
1012 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001013 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001014 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1015 data->auto_pwm[nr][point]);
1016 mutex_unlock(&data->update_lock);
1017 return count;
1018}
1019
1020static ssize_t show_auto_temp(struct device *dev,
1021 struct device_attribute *attr, char *buf)
1022{
1023 struct it87_data *data = it87_update_device(dev);
1024 struct sensor_device_attribute_2 *sensor_attr =
1025 to_sensor_dev_attr_2(attr);
1026 int nr = sensor_attr->nr;
1027 int point = sensor_attr->index;
1028
1029 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1030}
1031
1032static ssize_t set_auto_temp(struct device *dev,
1033 struct device_attribute *attr, const char *buf, size_t count)
1034{
1035 struct it87_data *data = dev_get_drvdata(dev);
1036 struct sensor_device_attribute_2 *sensor_attr =
1037 to_sensor_dev_attr_2(attr);
1038 int nr = sensor_attr->nr;
1039 int point = sensor_attr->index;
1040 long val;
1041
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001042 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001043 return -EINVAL;
1044
1045 mutex_lock(&data->update_lock);
1046 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1047 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1048 data->auto_temp[nr][point]);
1049 mutex_unlock(&data->update_lock);
1050 return count;
1051}
1052
Jean Delvare20ad93d2005-06-05 11:53:25 +02001053#define show_fan_offset(offset) \
1054static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1055 show_fan, NULL, offset - 1); \
1056static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1057 show_fan_min, set_fan_min, offset - 1); \
1058static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1059 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061show_fan_offset(1);
1062show_fan_offset(2);
1063show_fan_offset(3);
1064
1065#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +02001066static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1067 show_pwm_enable, set_pwm_enable, offset - 1); \
1068static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +01001069 show_pwm, set_pwm, offset - 1); \
1070static DEVICE_ATTR(pwm##offset##_freq, \
1071 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001072 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1073static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001074 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1075 offset - 1); \
1076static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1077 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1078 offset - 1, 0); \
1079static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1080 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1081 offset - 1, 1); \
1082static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1083 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1084 offset - 1, 2); \
1085static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1086 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1087static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1088 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1089 offset - 1, 1); \
1090static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1091 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1092 offset - 1, 0); \
1093static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1094 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1095 offset - 1, 2); \
1096static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1097 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1098 offset - 1, 3); \
1099static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1100 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1101 offset - 1, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103show_pwm_offset(1);
1104show_pwm_offset(2);
1105show_pwm_offset(3);
1106
Jean Delvare17d648b2006-08-28 14:23:46 +02001107/* A different set of callbacks for 16-bit fans */
1108static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1109 char *buf)
1110{
1111 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1112 int nr = sensor_attr->index;
1113 struct it87_data *data = it87_update_device(dev);
1114 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1115}
1116
1117static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1118 char *buf)
1119{
1120 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1121 int nr = sensor_attr->index;
1122 struct it87_data *data = it87_update_device(dev);
1123 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1124}
1125
1126static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1127 const char *buf, size_t count)
1128{
1129 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1130 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001131 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001132 long val;
1133
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001134 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001135 return -EINVAL;
Jean Delvare17d648b2006-08-28 14:23:46 +02001136
1137 mutex_lock(&data->update_lock);
1138 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001139 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001140 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001141 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001142 data->fan_min[nr] >> 8);
1143 mutex_unlock(&data->update_lock);
1144 return count;
1145}
1146
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001147/*
1148 * We want to use the same sysfs file names as 8-bit fans, but we need
1149 * different variable names, so we have to use SENSOR_ATTR instead of
1150 * SENSOR_DEVICE_ATTR.
1151 */
Jean Delvare17d648b2006-08-28 14:23:46 +02001152#define show_fan16_offset(offset) \
1153static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1154 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1155 show_fan16, NULL, offset - 1); \
1156static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1157 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1158 show_fan16_min, set_fan16_min, offset - 1)
1159
1160show_fan16_offset(1);
1161show_fan16_offset(2);
1162show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001163show_fan16_offset(4);
1164show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +02001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001167static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1168 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001171 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
Jean Delvare1d66c642005-04-18 21:16:59 -07001173static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Jean Delvare0124dd72007-11-25 16:16:41 +01001175static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1176 char *buf)
1177{
1178 int bitnr = to_sensor_dev_attr(attr)->index;
1179 struct it87_data *data = it87_update_device(dev);
1180 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1181}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001182
1183static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1184 *attr, const char *buf, size_t count)
1185{
1186 struct it87_data *data = dev_get_drvdata(dev);
1187 long val;
1188 int config;
1189
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001190 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001191 return -EINVAL;
1192
1193 mutex_lock(&data->update_lock);
1194 config = it87_read_value(data, IT87_REG_CONFIG);
1195 if (config < 0) {
1196 count = config;
1197 } else {
1198 config |= 1 << 5;
1199 it87_write_value(data, IT87_REG_CONFIG, config);
1200 /* Invalidate cache to force re-read */
1201 data->valid = 0;
1202 }
1203 mutex_unlock(&data->update_lock);
1204
1205 return count;
1206}
1207
Jean Delvare0124dd72007-11-25 16:16:41 +01001208static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1209static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1210static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1211static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1212static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1213static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1214static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1215static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1216static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1217static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1218static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1219static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1220static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1221static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1222static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1223static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001224static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1225 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001226
Jean Delvared9b327c2010-03-05 22:17:17 +01001227static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1228 char *buf)
1229{
1230 int bitnr = to_sensor_dev_attr(attr)->index;
1231 struct it87_data *data = it87_update_device(dev);
1232 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1233}
1234static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1235 const char *buf, size_t count)
1236{
1237 int bitnr = to_sensor_dev_attr(attr)->index;
1238 struct it87_data *data = dev_get_drvdata(dev);
1239 long val;
1240
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001241 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001242 || (val != 0 && val != 1))
1243 return -EINVAL;
1244
1245 mutex_lock(&data->update_lock);
1246 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1247 if (val)
1248 data->beeps |= (1 << bitnr);
1249 else
1250 data->beeps &= ~(1 << bitnr);
1251 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1252 mutex_unlock(&data->update_lock);
1253 return count;
1254}
1255
1256static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1257 show_beep, set_beep, 1);
1258static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1259static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1260static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1261static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1262static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1263static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1264static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1265/* fanX_beep writability is set later */
1266static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1267static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1268static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1269static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1270static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1271static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1272 show_beep, set_beep, 2);
1273static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1274static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1275
Jean Delvare5f2dc792010-03-05 22:17:18 +01001276static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1277 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Jean Delvare90d66192007-10-08 18:24:35 +02001279 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001280 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001282static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1283 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001285 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001286 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001288 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001289 return -EINVAL;
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 data->vrm = val;
1292
1293 return count;
1294}
1295static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Jean Delvare5f2dc792010-03-05 22:17:18 +01001297static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1298 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 struct it87_data *data = it87_update_device(dev);
1301 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1302}
1303static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001304
Jean Delvare738e5e02010-08-14 21:08:50 +02001305static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1306 char *buf)
1307{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001308 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001309 "+5V",
1310 "5VSB",
1311 "Vbat",
1312 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001313 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001314 "+3.3V",
1315 "3VSB",
1316 "Vbat",
1317 };
1318 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001319 int nr = to_sensor_dev_attr(attr)->index;
1320
Jean Delvare16b5dda2012-01-16 22:51:48 +01001321 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1322 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001323}
1324static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1325static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1326static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1327
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001328static ssize_t show_name(struct device *dev, struct device_attribute
1329 *devattr, char *buf)
1330{
1331 struct it87_data *data = dev_get_drvdata(dev);
1332 return sprintf(buf, "%s\n", data->name);
1333}
1334static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1335
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001336static struct attribute *it87_attributes_in[9][5] = {
1337{
Jean Delvare87808be2006-09-24 21:17:13 +02001338 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001339 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001340 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001341 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001342 NULL
1343}, {
1344 &sensor_dev_attr_in1_input.dev_attr.attr,
1345 &sensor_dev_attr_in1_min.dev_attr.attr,
1346 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001347 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001348 NULL
1349}, {
1350 &sensor_dev_attr_in2_input.dev_attr.attr,
1351 &sensor_dev_attr_in2_min.dev_attr.attr,
1352 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001353 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001354 NULL
1355}, {
1356 &sensor_dev_attr_in3_input.dev_attr.attr,
1357 &sensor_dev_attr_in3_min.dev_attr.attr,
1358 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001359 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001360 NULL
1361}, {
1362 &sensor_dev_attr_in4_input.dev_attr.attr,
1363 &sensor_dev_attr_in4_min.dev_attr.attr,
1364 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001365 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001366 NULL
1367}, {
1368 &sensor_dev_attr_in5_input.dev_attr.attr,
1369 &sensor_dev_attr_in5_min.dev_attr.attr,
1370 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001371 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001372 NULL
1373}, {
1374 &sensor_dev_attr_in6_input.dev_attr.attr,
1375 &sensor_dev_attr_in6_min.dev_attr.attr,
1376 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001377 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001378 NULL
1379}, {
1380 &sensor_dev_attr_in7_input.dev_attr.attr,
1381 &sensor_dev_attr_in7_min.dev_attr.attr,
1382 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001383 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001384 NULL
1385}, {
1386 &sensor_dev_attr_in8_input.dev_attr.attr,
1387 NULL
1388} };
Jean Delvare87808be2006-09-24 21:17:13 +02001389
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001390static const struct attribute_group it87_group_in[9] = {
1391 { .attrs = it87_attributes_in[0] },
1392 { .attrs = it87_attributes_in[1] },
1393 { .attrs = it87_attributes_in[2] },
1394 { .attrs = it87_attributes_in[3] },
1395 { .attrs = it87_attributes_in[4] },
1396 { .attrs = it87_attributes_in[5] },
1397 { .attrs = it87_attributes_in[6] },
1398 { .attrs = it87_attributes_in[7] },
1399 { .attrs = it87_attributes_in[8] },
1400};
1401
Guenter Roeck4573acb2012-03-26 16:17:41 -07001402static struct attribute *it87_attributes_temp[3][6] = {
1403{
Jean Delvare87808be2006-09-24 21:17:13 +02001404 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001405 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001406 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001407 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001408 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001409 NULL
1410} , {
1411 &sensor_dev_attr_temp2_input.dev_attr.attr,
1412 &sensor_dev_attr_temp2_max.dev_attr.attr,
1413 &sensor_dev_attr_temp2_min.dev_attr.attr,
1414 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001415 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001416 NULL
1417} , {
1418 &sensor_dev_attr_temp3_input.dev_attr.attr,
1419 &sensor_dev_attr_temp3_max.dev_attr.attr,
1420 &sensor_dev_attr_temp3_min.dev_attr.attr,
1421 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001422 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001423 NULL
1424} };
Jean Delvare87808be2006-09-24 21:17:13 +02001425
Guenter Roeck4573acb2012-03-26 16:17:41 -07001426static const struct attribute_group it87_group_temp[3] = {
1427 { .attrs = it87_attributes_temp[0] },
1428 { .attrs = it87_attributes_temp[1] },
1429 { .attrs = it87_attributes_temp[2] },
1430};
1431
1432static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001433 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001434 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001435 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001436 NULL
1437};
1438
1439static const struct attribute_group it87_group = {
1440 .attrs = it87_attributes,
1441};
1442
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001443static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001444 &sensor_dev_attr_in0_beep.dev_attr.attr,
1445 &sensor_dev_attr_in1_beep.dev_attr.attr,
1446 &sensor_dev_attr_in2_beep.dev_attr.attr,
1447 &sensor_dev_attr_in3_beep.dev_attr.attr,
1448 &sensor_dev_attr_in4_beep.dev_attr.attr,
1449 &sensor_dev_attr_in5_beep.dev_attr.attr,
1450 &sensor_dev_attr_in6_beep.dev_attr.attr,
1451 &sensor_dev_attr_in7_beep.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001452 NULL
1453};
Jean Delvared9b327c2010-03-05 22:17:17 +01001454
Guenter Roeck4573acb2012-03-26 16:17:41 -07001455static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001456 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1457 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1458 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001459};
1460
Jean Delvare723a0aa2010-03-05 22:17:16 +01001461static struct attribute *it87_attributes_fan16[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001462 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1463 &sensor_dev_attr_fan1_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001464 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1465 NULL
1466}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001467 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1468 &sensor_dev_attr_fan2_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001469 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1470 NULL
1471}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001472 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1473 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001474 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1475 NULL
1476}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001477 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1478 &sensor_dev_attr_fan4_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001479 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1480 NULL
1481}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001482 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1483 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001484 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1485 NULL
1486} };
Jean Delvare87808be2006-09-24 21:17:13 +02001487
Jean Delvare723a0aa2010-03-05 22:17:16 +01001488static const struct attribute_group it87_group_fan16[5] = {
1489 { .attrs = it87_attributes_fan16[0] },
1490 { .attrs = it87_attributes_fan16[1] },
1491 { .attrs = it87_attributes_fan16[2] },
1492 { .attrs = it87_attributes_fan16[3] },
1493 { .attrs = it87_attributes_fan16[4] },
1494};
1495
1496static struct attribute *it87_attributes_fan[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001497 &sensor_dev_attr_fan1_input.dev_attr.attr,
1498 &sensor_dev_attr_fan1_min.dev_attr.attr,
1499 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001500 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1501 NULL
1502}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001503 &sensor_dev_attr_fan2_input.dev_attr.attr,
1504 &sensor_dev_attr_fan2_min.dev_attr.attr,
1505 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001506 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1507 NULL
1508}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001509 &sensor_dev_attr_fan3_input.dev_attr.attr,
1510 &sensor_dev_attr_fan3_min.dev_attr.attr,
1511 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001512 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001513 NULL
1514} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001515
Jean Delvare723a0aa2010-03-05 22:17:16 +01001516static const struct attribute_group it87_group_fan[3] = {
1517 { .attrs = it87_attributes_fan[0] },
1518 { .attrs = it87_attributes_fan[1] },
1519 { .attrs = it87_attributes_fan[2] },
1520};
1521
1522static const struct attribute_group *
1523it87_get_fan_group(const struct it87_data *data)
1524{
1525 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1526}
1527
1528static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001529 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001530 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001531 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001532 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001533 NULL
1534}, {
1535 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1536 &sensor_dev_attr_pwm2.dev_attr.attr,
1537 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001538 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001539 NULL
1540}, {
1541 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1542 &sensor_dev_attr_pwm3.dev_attr.attr,
1543 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001544 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001545 NULL
1546} };
Jean Delvare87808be2006-09-24 21:17:13 +02001547
Jean Delvare723a0aa2010-03-05 22:17:16 +01001548static const struct attribute_group it87_group_pwm[3] = {
1549 { .attrs = it87_attributes_pwm[0] },
1550 { .attrs = it87_attributes_pwm[1] },
1551 { .attrs = it87_attributes_pwm[2] },
1552};
1553
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001554static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1555 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1556 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1557 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1558 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1559 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1560 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1561 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1562 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1563 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1564 NULL
1565}, {
1566 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1567 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1568 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1569 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1570 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1571 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1572 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1573 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1574 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1575 NULL
1576}, {
1577 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1578 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1579 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1580 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1581 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1582 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1583 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1584 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1585 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1586 NULL
1587} };
1588
1589static const struct attribute_group it87_group_autopwm[3] = {
1590 { .attrs = it87_attributes_autopwm[0] },
1591 { .attrs = it87_attributes_autopwm[1] },
1592 { .attrs = it87_attributes_autopwm[2] },
1593};
1594
Jean Delvared9b327c2010-03-05 22:17:17 +01001595static struct attribute *it87_attributes_fan_beep[] = {
1596 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1597 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1598 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1599 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1600 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1601};
1602
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001603static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001604 &dev_attr_vrm.attr,
1605 &dev_attr_cpu0_vid.attr,
1606 NULL
1607};
1608
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001609static const struct attribute_group it87_group_vid = {
1610 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001611};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Jean Delvare738e5e02010-08-14 21:08:50 +02001613static struct attribute *it87_attributes_label[] = {
1614 &sensor_dev_attr_in3_label.dev_attr.attr,
1615 &sensor_dev_attr_in7_label.dev_attr.attr,
1616 &sensor_dev_attr_in8_label.dev_attr.attr,
1617 NULL
1618};
1619
1620static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001621 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001622};
1623
Jean Delvare2d8672c2005-07-19 23:56:35 +02001624/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001625static int __init it87_find(unsigned short *address,
1626 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001628 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001629 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001630 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001632 err = superio_enter();
1633 if (err)
1634 return err;
1635
1636 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001637 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001638
1639 switch (chip_type) {
1640 case IT8705F_DEVID:
1641 sio_data->type = it87;
1642 break;
1643 case IT8712F_DEVID:
1644 sio_data->type = it8712;
1645 break;
1646 case IT8716F_DEVID:
1647 case IT8726F_DEVID:
1648 sio_data->type = it8716;
1649 break;
1650 case IT8718F_DEVID:
1651 sio_data->type = it8718;
1652 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001653 case IT8720F_DEVID:
1654 sio_data->type = it8720;
1655 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001656 case IT8721F_DEVID:
1657 sio_data->type = it8721;
1658 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001659 case IT8728F_DEVID:
1660 sio_data->type = it8728;
1661 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001662 case IT8782F_DEVID:
1663 sio_data->type = it8782;
1664 break;
1665 case IT8783E_DEVID:
1666 sio_data->type = it8783;
1667 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001668 case 0xffff: /* No device at all */
1669 goto exit;
1670 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001671 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001672 goto exit;
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Jean Delvare87673dd2006-08-28 14:37:19 +02001675 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001677 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 goto exit;
1679 }
1680
1681 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1682 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001683 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 goto exit;
1685 }
1686
1687 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001688 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Joe Perchesa8ca1032011-01-12 21:55:10 +01001689 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001690 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Jean Delvare738e5e02010-08-14 21:08:50 +02001692 /* in8 (Vbat) is always internal */
1693 sio_data->internal = (1 << 2);
1694
Jean Delvare87673dd2006-08-28 14:37:19 +02001695 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001696 if (sio_data->type == it87) {
1697 /* The IT8705F doesn't have VID pins at all */
1698 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001699
1700 /* The IT8705F has a different LD number for GPIO */
1701 superio_select(5);
1702 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08001703 } else if (sio_data->type == it8783) {
1704 int reg25, reg27, reg2A, reg2C, regEF;
Guenter Roeck0531d982012-03-02 11:46:44 -08001705
1706 sio_data->skip_vid = 1; /* No VID */
1707
1708 superio_select(GPIO);
1709
1710 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1711 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1712 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1713 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1714 regEF = superio_inb(IT87_SIO_SPI_REG);
1715
Guenter Roeck0531d982012-03-02 11:46:44 -08001716 /* Check if fan3 is there or not */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001717 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08001718 sio_data->skip_fan |= (1 << 2);
1719 if ((reg25 & (1 << 4))
1720 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1721 sio_data->skip_pwm |= (1 << 2);
1722
1723 /* Check if fan2 is there or not */
1724 if (reg27 & (1 << 7))
1725 sio_data->skip_fan |= (1 << 1);
1726 if (reg27 & (1 << 3))
1727 sio_data->skip_pwm |= (1 << 1);
1728
1729 /* VIN5 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001730 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1731 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001732
1733 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001734 if (reg27 & (1 << 1))
1735 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001736
1737 /*
1738 * VIN7
1739 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1740 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001741 if (reg27 & (1 << 2)) {
1742 /*
1743 * The data sheet is a bit unclear regarding the
1744 * internal voltage divider for VCCH5V. It says
1745 * "This bit enables and switches VIN7 (pin 91) to the
1746 * internal voltage divider for VCCH5V".
1747 * This is different to other chips, where the internal
1748 * voltage divider would connect VIN7 to an internal
1749 * voltage source. Maybe that is the case here as well.
1750 *
1751 * Since we don't know for sure, re-route it if that is
1752 * not the case, and ask the user to report if the
1753 * resulting voltage is sane.
1754 */
1755 if (!(reg2C & (1 << 1))) {
1756 reg2C |= (1 << 1);
1757 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1758 pr_notice("Routing internal VCCH5V to in7.\n");
1759 }
1760 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1761 pr_notice("Please report if it displays a reasonable voltage.\n");
1762 }
Guenter Roeck0531d982012-03-02 11:46:44 -08001763
1764 if (reg2C & (1 << 0))
1765 sio_data->internal |= (1 << 0);
1766 if (reg2C & (1 << 1))
1767 sio_data->internal |= (1 << 1);
1768
1769 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1770
Jean Delvare895ff262009-12-09 20:35:47 +01001771 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001772 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001773 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02001774
1775 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001776
Jean Delvare895ff262009-12-09 20:35:47 +01001777 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001778 if (sio_data->type == it8721 || sio_data->type == it8728 ||
1779 sio_data->type == it8782) {
Jean Delvare16b5dda2012-01-16 22:51:48 +01001780 /*
Guenter Roeck0531d982012-03-02 11:46:44 -08001781 * IT8721F/IT8758E, and IT8782F don't have VID pins
1782 * at all, not sure about the IT8728F.
Jean Delvare16b5dda2012-01-16 22:51:48 +01001783 */
Jean Delvare895ff262009-12-09 20:35:47 +01001784 sio_data->skip_vid = 1;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001785 } else {
1786 /* We need at least 4 VID pins */
1787 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001788 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001789 sio_data->skip_vid = 1;
1790 }
Jean Delvare895ff262009-12-09 20:35:47 +01001791 }
1792
Jean Delvare591ec652009-12-09 20:35:48 +01001793 /* Check if fan3 is there or not */
1794 if (reg & (1 << 6))
1795 sio_data->skip_pwm |= (1 << 2);
1796 if (reg & (1 << 7))
1797 sio_data->skip_fan |= (1 << 2);
1798
1799 /* Check if fan2 is there or not */
1800 reg = superio_inb(IT87_SIO_GPIO5_REG);
1801 if (reg & (1 << 1))
1802 sio_data->skip_pwm |= (1 << 1);
1803 if (reg & (1 << 2))
1804 sio_data->skip_fan |= (1 << 1);
1805
Jean Delvare895ff262009-12-09 20:35:47 +01001806 if ((sio_data->type == it8718 || sio_data->type == it8720)
1807 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001808 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001809
1810 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001811
1812 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1813
Jean Delvare436cad22010-07-09 16:22:48 +02001814 /*
1815 * The IT8720F has no VIN7 pin, so VCCH should always be
1816 * routed internally to VIN7 with an internal divider.
1817 * Curiously, there still is a configuration bit to control
1818 * this, which means it can be set incorrectly. And even
1819 * more curiously, many boards out there are improperly
1820 * configured, even though the IT8720F datasheet claims
1821 * that the internal routing of VCCH to VIN7 is the default
1822 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08001823 *
1824 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001825 * If UART6 is enabled, re-route VIN7 to the internal divider
1826 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02001827 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001828 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02001829 reg |= (1 << 1);
1830 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01001831 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02001832 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001833 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001834 sio_data->internal |= (1 << 0);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001835 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1836 sio_data->type == it8728)
Jean Delvare738e5e02010-08-14 21:08:50 +02001837 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01001838
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001839 /*
1840 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1841 * While VIN7 can be routed to the internal voltage divider,
1842 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07001843 *
1844 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
1845 * is the temperature source. Since we can not read the
1846 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001847 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001848 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001849 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07001850 sio_data->skip_temp |= (1 << 2);
1851 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001852
Jean Delvared9b327c2010-03-05 22:17:17 +01001853 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02001854 }
Jean Delvared9b327c2010-03-05 22:17:17 +01001855 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01001856 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02001857
Jean Delvare98dd22c2008-10-09 15:33:58 +02001858 /* Disable specific features based on DMI strings */
1859 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1860 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1861 if (board_vendor && board_name) {
1862 if (strcmp(board_vendor, "nVIDIA") == 0
1863 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001864 /*
1865 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1866 * connected to a fan, but to something else. One user
1867 * has reported instant system power-off when changing
1868 * the PWM2 duty cycle, so we disable it.
1869 * I use the board name string as the trigger in case
1870 * the same board is ever used in other systems.
1871 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01001872 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02001873 sio_data->skip_pwm = (1 << 1);
1874 }
1875 }
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877exit:
1878 superio_exit();
1879 return err;
1880}
1881
Jean Delvare723a0aa2010-03-05 22:17:16 +01001882static void it87_remove_files(struct device *dev)
1883{
1884 struct it87_data *data = platform_get_drvdata(pdev);
1885 struct it87_sio_data *sio_data = dev->platform_data;
1886 const struct attribute_group *fan_group = it87_get_fan_group(data);
1887 int i;
1888
1889 sysfs_remove_group(&dev->kobj, &it87_group);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001890 for (i = 0; i < 9; i++) {
1891 if (sio_data->skip_in & (1 << i))
1892 continue;
1893 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
1894 if (it87_attributes_in_beep[i])
1895 sysfs_remove_file(&dev->kobj,
1896 it87_attributes_in_beep[i]);
1897 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07001898 for (i = 0; i < 3; i++) {
1899 if (!(data->has_temp & (1 << i)))
1900 continue;
1901 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
1902 if (sio_data->beep_pin)
1903 sysfs_remove_file(&dev->kobj,
1904 it87_attributes_temp_beep[i]);
1905 }
Jean Delvare723a0aa2010-03-05 22:17:16 +01001906 for (i = 0; i < 5; i++) {
1907 if (!(data->has_fan & (1 << i)))
1908 continue;
1909 sysfs_remove_group(&dev->kobj, &fan_group[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01001910 if (sio_data->beep_pin)
1911 sysfs_remove_file(&dev->kobj,
1912 it87_attributes_fan_beep[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001913 }
1914 for (i = 0; i < 3; i++) {
1915 if (sio_data->skip_pwm & (1 << 0))
1916 continue;
1917 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001918 if (has_old_autopwm(data))
1919 sysfs_remove_group(&dev->kobj,
1920 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001921 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001922 if (!sio_data->skip_vid)
1923 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02001924 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001925}
1926
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001927static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001930 struct resource *res;
1931 struct device *dev = &pdev->dev;
1932 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001933 const struct attribute_group *fan_group;
1934 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01001936 int fan_beep_need_rw;
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001937 static const char * const names[] = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001938 "it87",
1939 "it8712",
1940 "it8716",
1941 "it8718",
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001942 "it8720",
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001943 "it8721",
Jean Delvare16b5dda2012-01-16 22:51:48 +01001944 "it8728",
Guenter Roeck0531d982012-03-02 11:46:44 -08001945 "it8782",
1946 "it8783",
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001947 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001949 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07001950 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
1951 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001952 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1953 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001954 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07001955 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Guenter Roeck62a1d052012-03-24 21:54:41 -07001958 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
1959 if (!data)
1960 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001962 data->addr = res->start;
1963 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001964 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001965 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001968 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07001969 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
1970 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001972 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001974 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001977 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001979 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001980 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001981 if (sio_data->internal & (1 << 0))
1982 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1983 if (sio_data->internal & (1 << 1))
1984 data->in_scaled |= (1 << 7); /* in7 is VSB */
1985 if (sio_data->internal & (1 << 2))
1986 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Guenter Roeck0531d982012-03-02 11:46:44 -08001987 } else if (sio_data->type == it8782 || sio_data->type == it8783) {
1988 if (sio_data->internal & (1 << 0))
1989 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
1990 if (sio_data->internal & (1 << 1))
1991 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001992 }
1993
Guenter Roeck4573acb2012-03-26 16:17:41 -07001994 data->has_temp = 0x07;
1995 if (sio_data->skip_temp & (1 << 2)) {
1996 if (sio_data->type == it8782
1997 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
1998 data->has_temp &= ~(1 << 2);
1999 }
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002002 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002005 err = sysfs_create_group(&dev->kobj, &it87_group);
2006 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002007 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002008
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002009 for (i = 0; i < 9; i++) {
2010 if (sio_data->skip_in & (1 << i))
2011 continue;
2012 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2013 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002014 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002015 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2016 err = sysfs_create_file(&dev->kobj,
2017 it87_attributes_in_beep[i]);
2018 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002019 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002020 }
2021 }
2022
Guenter Roeck4573acb2012-03-26 16:17:41 -07002023 for (i = 0; i < 3; i++) {
2024 if (!(data->has_temp & (1 << i)))
2025 continue;
2026 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002027 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002028 goto error;
Guenter Roeck4573acb2012-03-26 16:17:41 -07002029 if (sio_data->beep_pin) {
2030 err = sysfs_create_file(&dev->kobj,
2031 it87_attributes_temp_beep[i]);
2032 if (err)
2033 goto error;
2034 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002035 }
2036
Jean Delvare9060f8b2006-08-28 14:24:17 +02002037 /* Do not create fan files for disabled fans */
Jean Delvare723a0aa2010-03-05 22:17:16 +01002038 fan_group = it87_get_fan_group(data);
Jean Delvared9b327c2010-03-05 22:17:17 +01002039 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01002040 for (i = 0; i < 5; i++) {
2041 if (!(data->has_fan & (1 << i)))
2042 continue;
2043 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
2044 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002045 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002046
2047 if (sio_data->beep_pin) {
2048 err = sysfs_create_file(&dev->kobj,
2049 it87_attributes_fan_beep[i]);
2050 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002051 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002052 if (!fan_beep_need_rw)
2053 continue;
2054
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002055 /*
2056 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002057 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002058 * for it.
2059 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002060 if (sysfs_chmod_file(&dev->kobj,
2061 it87_attributes_fan_beep[i],
2062 S_IRUGO | S_IWUSR))
2063 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2064 i + 1);
2065 fan_beep_need_rw = 0;
2066 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002067 }
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002070 for (i = 0; i < 3; i++) {
2071 if (sio_data->skip_pwm & (1 << i))
2072 continue;
2073 err = sysfs_create_group(&dev->kobj,
2074 &it87_group_pwm[i]);
2075 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002076 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002077
2078 if (!has_old_autopwm(data))
2079 continue;
2080 err = sysfs_create_group(&dev->kobj,
2081 &it87_group_autopwm[i]);
2082 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002083 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086
Jean Delvare895ff262009-12-09 20:35:47 +01002087 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002088 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002089 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002090 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002091 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2092 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002093 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002094 }
2095
Jean Delvare738e5e02010-08-14 21:08:50 +02002096 /* Export labels for internal sensors */
2097 for (i = 0; i < 3; i++) {
2098 if (!(sio_data->internal & (1 << i)))
2099 continue;
2100 err = sysfs_create_file(&dev->kobj,
2101 it87_attributes_label[i]);
2102 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002103 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002104 }
2105
Tony Jones1beeffe2007-08-20 13:46:20 -07002106 data->hwmon_dev = hwmon_device_register(dev);
2107 if (IS_ERR(data->hwmon_dev)) {
2108 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002109 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111
2112 return 0;
2113
Guenter Roeck62a1d052012-03-24 21:54:41 -07002114error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002115 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return err;
2117}
2118
Bill Pemberton281dfd02012-11-19 13:25:51 -05002119static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002121 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Tony Jones1beeffe2007-08-20 13:46:20 -07002123 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002124 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return 0;
2127}
2128
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002129/*
2130 * Must be called with data->update_lock held, except during initialization.
2131 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2132 * would slow down the IT87 access and should not be necessary.
2133 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002134static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002136 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2137 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002140/*
2141 * Must be called with data->update_lock held, except during initialization.
2142 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2143 * would slow down the IT87 access and should not be necessary.
2144 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002145static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002147 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2148 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
2151/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002152static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002154 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002155 /*
2156 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002158 * disable pwm control to protect the user.
2159 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002160 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if ((tmp & 0x87) == 0) {
2162 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002163 /*
2164 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002166 * inverting all fan speed values.
2167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 int i;
2169 u8 pwm[3];
2170
2171 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002172 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 IT87_REG_PWM(i));
2174
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002175 /*
2176 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 * might be correct, as suspicious as it seems, so we
2178 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002179 * PWM interface).
2180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002182 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002184 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 tmp | 0x87);
2186 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002187 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 IT87_REG_PWM(i),
2189 0x7f & ~pwm[i]);
2190 return 1;
2191 }
2192
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002193 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 "too broken to be fixed\n");
2195 }
2196
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002197 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 "defaults, disabling PWM interface\n");
2199 return 0;
2200 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002201 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 "sane, won't touch\n");
2203 }
2204
2205 return 1;
2206}
2207
2208/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002209static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Jean Delvare591ec652009-12-09 20:35:48 +01002211 struct it87_sio_data *sio_data = pdev->dev.platform_data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002212 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002214 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002216 /*
2217 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002218 * - If it is in automatic mode, setting to manual mode should set
2219 * the fan to full speed by default.
2220 * - If it is in manual mode, we need a mapping to temperature
2221 * channels to use when later setting to automatic mode later.
2222 * Use a 1:1 mapping by default (we are clueless.)
2223 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002224 * prior to switching to a different mode.
2225 * Note that this is no longer needed for the IT8721F and later, as
2226 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002227 * manual duty cycle.
2228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002230 data->pwm_temp_map[i] = i;
2231 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002232 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
2234
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002235 /*
2236 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002237 * registers. For low voltage limits it makes no sense and triggers
2238 * alarms, so change to 0 instead. For high temperature limits, it
2239 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002240 * but is still confusing, so change to 127 degrees C.
2241 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002242 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002243 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002244 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002245 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002246 }
2247 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002248 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002249 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002250 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002251 }
2252
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002253 /*
2254 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002255 * set to two different sensor types and we can't guess which one
2256 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002257 * run-time through the temp{1-3}_type sysfs accessors if needed.
2258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002261 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if ((tmp & 0xff) == 0) {
2263 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002264 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
2266
2267 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002268 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002269 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002270 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002272 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002273 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2274 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002276 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Jean Delvare17d648b2006-08-28 14:23:46 +02002278 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02002279 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002280 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002281 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002282 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002283 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002284 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002285 tmp | 0x07);
2286 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002287 /* IT8705F, IT8782F, and IT8783E/F only support three fans. */
2288 if (data->type != it87 && data->type != it8782 &&
2289 data->type != it8783) {
Andrew Paprocki816d8c62008-08-06 22:41:06 +02002290 if (tmp & (1 << 4))
2291 data->has_fan |= (1 << 3); /* fan4 enabled */
2292 if (tmp & (1 << 5))
2293 data->has_fan |= (1 << 4); /* fan5 enabled */
2294 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002295 }
2296
Jean Delvare591ec652009-12-09 20:35:48 +01002297 /* Fan input pins may be used for alternative functions */
2298 data->has_fan &= ~sio_data->skip_fan;
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002301 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002302 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 | (update_vbat ? 0x41 : 0x01));
2304}
2305
Jean Delvareb99883d2010-03-05 22:17:15 +01002306static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2307{
2308 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002309 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002310 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002311 data->pwm_duty[nr] = it87_read_value(data,
2312 IT87_REG_PWM_DUTY(nr));
2313 } else {
2314 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2315 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2316 else /* Manual mode */
2317 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2318 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002319
2320 if (has_old_autopwm(data)) {
2321 int i;
2322
2323 for (i = 0; i < 5 ; i++)
2324 data->auto_temp[nr][i] = it87_read_value(data,
2325 IT87_REG_AUTO_TEMP(nr, i));
2326 for (i = 0; i < 3 ; i++)
2327 data->auto_pwm[nr][i] = it87_read_value(data,
2328 IT87_REG_AUTO_PWM(nr, i));
2329 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002330}
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332static struct it87_data *it87_update_device(struct device *dev)
2333{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002334 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 int i;
2336
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002337 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2340 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002342 /*
2343 * Cleared after each update, so reenable. Value
2344 * returned by this read will be previous value
2345 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002346 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002347 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002350 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002351 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002352 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002353 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002354 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002355 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
Jean Delvare3543a532006-08-28 14:27:25 +02002357 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002358 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Jean Delvarec7f1f712007-09-03 17:11:46 +02002360 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002361 /* Skip disabled fans */
2362 if (!(data->has_fan & (1 << i)))
2363 continue;
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 data->fan_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002366 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002367 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002368 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002369 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002370 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002371 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002372 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002373 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002374 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
2377 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002378 if (!(data->has_temp & (1 << i)))
2379 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002380 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002381 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002382 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002383 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002384 data->temp[i][2] =
2385 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387
Jean Delvare17d648b2006-08-28 14:23:46 +02002388 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002389 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002390 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002391 data->fan_div[0] = i & 0x07;
2392 data->fan_div[1] = (i >> 3) & 0x07;
2393 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002397 it87_read_value(data, IT87_REG_ALARM1) |
2398 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2399 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002400 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002401
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002402 data->fan_main_ctrl = it87_read_value(data,
2403 IT87_REG_FAN_MAIN_CTRL);
2404 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002405 for (i = 0; i < 3; i++)
2406 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002408 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002409 /*
2410 * The IT8705F does not have VID capability.
2411 * The IT8718F and later don't use IT87_REG_VID for the
2412 * same purpose.
2413 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002414 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002415 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002416 /*
2417 * The older IT8712F revisions had only 5 VID pins,
2418 * but we assume it is always safe to read 6 bits.
2419 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002420 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422 data->last_updated = jiffies;
2423 data->valid = 1;
2424 }
2425
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002426 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 return data;
2429}
2430
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002431static int __init it87_device_add(unsigned short address,
2432 const struct it87_sio_data *sio_data)
2433{
2434 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002435 .start = address + IT87_EC_OFFSET,
2436 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002437 .name = DRVNAME,
2438 .flags = IORESOURCE_IO,
2439 };
2440 int err;
2441
Jean Delvareb9acb642009-01-07 16:37:35 +01002442 err = acpi_check_resource_conflict(&res);
2443 if (err)
2444 goto exit;
2445
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002446 pdev = platform_device_alloc(DRVNAME, address);
2447 if (!pdev) {
2448 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002449 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002450 goto exit;
2451 }
2452
2453 err = platform_device_add_resources(pdev, &res, 1);
2454 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002455 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002456 goto exit_device_put;
2457 }
2458
2459 err = platform_device_add_data(pdev, sio_data,
2460 sizeof(struct it87_sio_data));
2461 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002462 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002463 goto exit_device_put;
2464 }
2465
2466 err = platform_device_add(pdev);
2467 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002468 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002469 goto exit_device_put;
2470 }
2471
2472 return 0;
2473
2474exit_device_put:
2475 platform_device_put(pdev);
2476exit:
2477 return err;
2478}
2479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480static int __init sm_it87_init(void)
2481{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002482 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002483 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002484 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002485
Jean Delvare98dd22c2008-10-09 15:33:58 +02002486 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002487 err = it87_find(&isa_address, &sio_data);
2488 if (err)
2489 return err;
2490 err = platform_driver_register(&it87_driver);
2491 if (err)
2492 return err;
2493
2494 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002495 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002496 platform_driver_unregister(&it87_driver);
2497 return err;
2498 }
2499
2500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
2503static void __exit sm_it87_exit(void)
2504{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002505 platform_device_unregister(pdev);
2506 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508
2509
Jean Delvaref1d8e332007-11-25 16:14:44 +01002510MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02002511 "Jean Delvare <khali@linux-fr.org>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002512MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513module_param(update_vbat, bool, 0);
2514MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2515module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002516MODULE_PARM_DESC(fix_pwm_polarity,
2517 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518MODULE_LICENSE("GPL");
2519
2520module_init(sm_it87_init);
2521module_exit(sm_it87_exit);