blob: 79a1229d34993e81083501281f160081ba25a476 [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
Jean Delvare20ad93d2005-06-05 11:53:25 +0200579static ssize_t show_sensor(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}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200593static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
594 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200596 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
597 int nr = sensor_attr->index;
598
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200599 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100600 long val;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200601 u8 reg;
Jean Delvaref5f64502010-03-05 22:17:19 +0100602
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100603 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100604 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Jean Delvare8acf07c2010-04-14 16:14:09 +0200606 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
607 reg &= ~(1 << nr);
608 reg &= ~(8 << nr);
Jean Delvare4ed10772008-10-17 17:51:16 +0200609 if (val == 2) { /* backwards compatibility */
610 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
611 "instead\n");
612 val = 4;
613 }
614 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200616 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200617 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200618 reg |= 8 << nr;
619 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200621
622 mutex_lock(&data->update_lock);
623 data->sensor = reg;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200624 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200625 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100626 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return count;
628}
629#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200630static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
631 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633show_sensor_offset(1);
634show_sensor_offset(2);
635show_sensor_offset(3);
636
637/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100638
639static int pwm_mode(const struct it87_data *data, int nr)
640{
641 int ctrl = data->fan_main_ctrl & (1 << nr);
642
643 if (ctrl == 0) /* Full speed */
644 return 0;
645 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
646 return 2;
647 else /* Manual mode */
648 return 1;
649}
650
Jean Delvare20ad93d2005-06-05 11:53:25 +0200651static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
652 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200654 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
655 int nr = sensor_attr->index;
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100658 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 DIV_FROM_REG(data->fan_div[nr])));
660}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200661static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
662 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200664 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
665 int nr = sensor_attr->index;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100668 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
669 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200671static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
672 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200674 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
675 int nr = sensor_attr->index;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct it87_data *data = it87_update_device(dev);
678 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
679}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100680static ssize_t show_pwm_enable(struct device *dev,
681 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200683 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
684 int nr = sensor_attr->index;
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100687 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200689static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
690 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200692 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
693 int nr = sensor_attr->index;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200696 return sprintf(buf, "%d\n",
697 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100699static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
700 char *buf)
701{
702 struct it87_data *data = it87_update_device(dev);
703 int index = (data->fan_ctl >> 4) & 0x07;
704
705 return sprintf(buf, "%u\n", pwm_freq[index]);
706}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200707static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
708 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200710 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
711 int nr = sensor_attr->index;
712
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200713 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100714 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100715 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100717 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100718 return -EINVAL;
719
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100720 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200721 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800722 switch (nr) {
Jean Delvare5f2dc792010-03-05 22:17:18 +0100723 case 0:
724 data->fan_div[nr] = reg & 0x07;
725 break;
726 case 1:
727 data->fan_div[nr] = (reg >> 3) & 0x07;
728 break;
729 case 2:
730 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
731 break;
Jean Delvare07eab462005-11-23 15:44:31 -0800732 }
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200735 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100736 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return count;
738}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200739static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
740 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200742 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
743 int nr = sensor_attr->index;
744
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200745 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100746 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200747 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 u8 old;
749
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100750 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100751 return -EINVAL;
752
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100753 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200754 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200756 /* Save fan min limit */
757 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 switch (nr) {
760 case 0:
761 case 1:
762 data->fan_div[nr] = DIV_TO_REG(val);
763 break;
764 case 2:
765 if (val < 8)
766 data->fan_div[nr] = 1;
767 else
768 data->fan_div[nr] = 3;
769 }
770 val = old & 0x80;
771 val |= (data->fan_div[0] & 0x07);
772 val |= (data->fan_div[1] & 0x07) << 3;
773 if (data->fan_div[2] == 3)
774 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200775 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200777 /* Restore fan min limit */
778 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200779 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200780
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100781 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return count;
783}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100784
785/* Returns 0 if OK, -EINVAL otherwise */
786static int check_trip_points(struct device *dev, int nr)
787{
788 const struct it87_data *data = dev_get_drvdata(dev);
789 int i, err = 0;
790
791 if (has_old_autopwm(data)) {
792 for (i = 0; i < 3; i++) {
793 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
794 err = -EINVAL;
795 }
796 for (i = 0; i < 2; i++) {
797 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
798 err = -EINVAL;
799 }
800 }
801
802 if (err) {
803 dev_err(dev, "Inconsistent trip points, not switching to "
804 "automatic mode\n");
805 dev_err(dev, "Adjust the trip points and try again\n");
806 }
807 return err;
808}
809
Jean Delvare20ad93d2005-06-05 11:53:25 +0200810static ssize_t set_pwm_enable(struct device *dev,
811 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200813 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
814 int nr = sensor_attr->index;
815
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200816 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100817 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100819 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100820 return -EINVAL;
821
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100822 /* Check trip points before switching to automatic mode */
823 if (val == 2) {
824 if (check_trip_points(dev, nr) < 0)
825 return -EINVAL;
826 }
827
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100828 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 if (val == 0) {
831 int tmp;
832 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200833 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
834 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* set on/off mode */
836 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100837 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
838 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100839 } else {
840 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +0100841 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +0100842 data->pwm_temp_map[nr] :
843 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +0100844 else /* Automatic mode */
845 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
846 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /* set SmartGuardian mode */
848 data->fan_main_ctrl |= (1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100849 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
850 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100853 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return count;
855}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200856static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
857 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200859 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
860 int nr = sensor_attr->index;
861
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200862 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100863 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100865 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return -EINVAL;
867
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100868 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +0100869 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800870 /*
871 * If we are in automatic mode, the PWM duty cycle register
872 * is read-only so we can't write the value.
873 */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100874 if (data->pwm_ctrl[nr] & 0x80) {
875 mutex_unlock(&data->update_lock);
876 return -EBUSY;
877 }
878 data->pwm_duty[nr] = pwm_to_reg(data, val);
879 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
880 data->pwm_duty[nr]);
881 } else {
882 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800883 /*
884 * If we are in manual mode, write the duty cycle immediately;
885 * otherwise, just store it for later use.
886 */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100887 if (!(data->pwm_ctrl[nr] & 0x80)) {
888 data->pwm_ctrl[nr] = data->pwm_duty[nr];
889 it87_write_value(data, IT87_REG_PWM(nr),
890 data->pwm_ctrl[nr]);
891 }
Jean Delvareb99883d2010-03-05 22:17:15 +0100892 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100893 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return count;
895}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100896static ssize_t set_pwm_freq(struct device *dev,
897 struct device_attribute *attr, const char *buf, size_t count)
898{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200899 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100900 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100901 int i;
902
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100903 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100904 return -EINVAL;
905
Jean Delvaref8d0c192007-02-14 21:15:02 +0100906 /* Search for the nearest available frequency */
907 for (i = 0; i < 7; i++) {
908 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
909 break;
910 }
911
912 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200913 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100914 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200915 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100916 mutex_unlock(&data->update_lock);
917
918 return count;
919}
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100920static ssize_t show_pwm_temp_map(struct device *dev,
921 struct device_attribute *attr, char *buf)
922{
923 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
924 int nr = sensor_attr->index;
925
926 struct it87_data *data = it87_update_device(dev);
927 int map;
928
929 if (data->pwm_temp_map[nr] < 3)
930 map = 1 << data->pwm_temp_map[nr];
931 else
932 map = 0; /* Should never happen */
933 return sprintf(buf, "%d\n", map);
934}
935static ssize_t set_pwm_temp_map(struct device *dev,
936 struct device_attribute *attr, const char *buf, size_t count)
937{
938 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
939 int nr = sensor_attr->index;
940
941 struct it87_data *data = dev_get_drvdata(dev);
942 long val;
943 u8 reg;
944
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800945 /*
946 * This check can go away if we ever support automatic fan speed
947 * control on newer chips.
948 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100949 if (!has_old_autopwm(data)) {
950 dev_notice(dev, "Mapping change disabled for safety reasons\n");
951 return -EINVAL;
952 }
953
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100954 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100955 return -EINVAL;
956
957 switch (val) {
958 case (1 << 0):
959 reg = 0x00;
960 break;
961 case (1 << 1):
962 reg = 0x01;
963 break;
964 case (1 << 2):
965 reg = 0x02;
966 break;
967 default:
968 return -EINVAL;
969 }
970
971 mutex_lock(&data->update_lock);
972 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800973 /*
974 * If we are in automatic mode, write the temp mapping immediately;
975 * otherwise, just store it for later use.
976 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100977 if (data->pwm_ctrl[nr] & 0x80) {
978 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
979 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
980 }
981 mutex_unlock(&data->update_lock);
982 return count;
983}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100985static ssize_t show_auto_pwm(struct device *dev,
986 struct device_attribute *attr, char *buf)
987{
988 struct it87_data *data = it87_update_device(dev);
989 struct sensor_device_attribute_2 *sensor_attr =
990 to_sensor_dev_attr_2(attr);
991 int nr = sensor_attr->nr;
992 int point = sensor_attr->index;
993
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200994 return sprintf(buf, "%d\n",
995 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100996}
997
998static ssize_t set_auto_pwm(struct device *dev,
999 struct device_attribute *attr, const char *buf, size_t count)
1000{
1001 struct it87_data *data = dev_get_drvdata(dev);
1002 struct sensor_device_attribute_2 *sensor_attr =
1003 to_sensor_dev_attr_2(attr);
1004 int nr = sensor_attr->nr;
1005 int point = sensor_attr->index;
1006 long val;
1007
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001008 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001009 return -EINVAL;
1010
1011 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001012 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001013 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1014 data->auto_pwm[nr][point]);
1015 mutex_unlock(&data->update_lock);
1016 return count;
1017}
1018
1019static ssize_t show_auto_temp(struct device *dev,
1020 struct device_attribute *attr, char *buf)
1021{
1022 struct it87_data *data = it87_update_device(dev);
1023 struct sensor_device_attribute_2 *sensor_attr =
1024 to_sensor_dev_attr_2(attr);
1025 int nr = sensor_attr->nr;
1026 int point = sensor_attr->index;
1027
1028 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1029}
1030
1031static ssize_t set_auto_temp(struct device *dev,
1032 struct device_attribute *attr, const char *buf, size_t count)
1033{
1034 struct it87_data *data = dev_get_drvdata(dev);
1035 struct sensor_device_attribute_2 *sensor_attr =
1036 to_sensor_dev_attr_2(attr);
1037 int nr = sensor_attr->nr;
1038 int point = sensor_attr->index;
1039 long val;
1040
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001041 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001042 return -EINVAL;
1043
1044 mutex_lock(&data->update_lock);
1045 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1046 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1047 data->auto_temp[nr][point]);
1048 mutex_unlock(&data->update_lock);
1049 return count;
1050}
1051
Jean Delvare20ad93d2005-06-05 11:53:25 +02001052#define show_fan_offset(offset) \
1053static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1054 show_fan, NULL, offset - 1); \
1055static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1056 show_fan_min, set_fan_min, offset - 1); \
1057static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1058 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060show_fan_offset(1);
1061show_fan_offset(2);
1062show_fan_offset(3);
1063
1064#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +02001065static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1066 show_pwm_enable, set_pwm_enable, offset - 1); \
1067static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +01001068 show_pwm, set_pwm, offset - 1); \
1069static DEVICE_ATTR(pwm##offset##_freq, \
1070 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001071 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1072static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001073 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1074 offset - 1); \
1075static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1076 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1077 offset - 1, 0); \
1078static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1079 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1080 offset - 1, 1); \
1081static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1082 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1083 offset - 1, 2); \
1084static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1085 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1086static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1087 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1088 offset - 1, 1); \
1089static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1090 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1091 offset - 1, 0); \
1092static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1093 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1094 offset - 1, 2); \
1095static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1096 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1097 offset - 1, 3); \
1098static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1099 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1100 offset - 1, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102show_pwm_offset(1);
1103show_pwm_offset(2);
1104show_pwm_offset(3);
1105
Jean Delvare17d648b2006-08-28 14:23:46 +02001106/* A different set of callbacks for 16-bit fans */
1107static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1108 char *buf)
1109{
1110 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1111 int nr = sensor_attr->index;
1112 struct it87_data *data = it87_update_device(dev);
1113 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1114}
1115
1116static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1117 char *buf)
1118{
1119 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1120 int nr = sensor_attr->index;
1121 struct it87_data *data = it87_update_device(dev);
1122 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1123}
1124
1125static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1126 const char *buf, size_t count)
1127{
1128 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1129 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001130 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001131 long val;
1132
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001133 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001134 return -EINVAL;
Jean Delvare17d648b2006-08-28 14:23:46 +02001135
1136 mutex_lock(&data->update_lock);
1137 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001138 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001139 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001140 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001141 data->fan_min[nr] >> 8);
1142 mutex_unlock(&data->update_lock);
1143 return count;
1144}
1145
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001146/*
1147 * We want to use the same sysfs file names as 8-bit fans, but we need
1148 * different variable names, so we have to use SENSOR_ATTR instead of
1149 * SENSOR_DEVICE_ATTR.
1150 */
Jean Delvare17d648b2006-08-28 14:23:46 +02001151#define show_fan16_offset(offset) \
1152static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1153 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1154 show_fan16, NULL, offset - 1); \
1155static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1156 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1157 show_fan16_min, set_fan16_min, offset - 1)
1158
1159show_fan16_offset(1);
1160show_fan16_offset(2);
1161show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001162show_fan16_offset(4);
1163show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +02001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001166static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1167 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001170 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
Jean Delvare1d66c642005-04-18 21:16:59 -07001172static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Jean Delvare0124dd72007-11-25 16:16:41 +01001174static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1175 char *buf)
1176{
1177 int bitnr = to_sensor_dev_attr(attr)->index;
1178 struct it87_data *data = it87_update_device(dev);
1179 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1180}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001181
1182static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1183 *attr, const char *buf, size_t count)
1184{
1185 struct it87_data *data = dev_get_drvdata(dev);
1186 long val;
1187 int config;
1188
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001189 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001190 return -EINVAL;
1191
1192 mutex_lock(&data->update_lock);
1193 config = it87_read_value(data, IT87_REG_CONFIG);
1194 if (config < 0) {
1195 count = config;
1196 } else {
1197 config |= 1 << 5;
1198 it87_write_value(data, IT87_REG_CONFIG, config);
1199 /* Invalidate cache to force re-read */
1200 data->valid = 0;
1201 }
1202 mutex_unlock(&data->update_lock);
1203
1204 return count;
1205}
1206
Jean Delvare0124dd72007-11-25 16:16:41 +01001207static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1208static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1209static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1210static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1211static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1212static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1213static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1214static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1215static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1216static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1217static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1218static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1219static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1220static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1221static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1222static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001223static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1224 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001225
Jean Delvared9b327c2010-03-05 22:17:17 +01001226static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1227 char *buf)
1228{
1229 int bitnr = to_sensor_dev_attr(attr)->index;
1230 struct it87_data *data = it87_update_device(dev);
1231 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1232}
1233static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1234 const char *buf, size_t count)
1235{
1236 int bitnr = to_sensor_dev_attr(attr)->index;
1237 struct it87_data *data = dev_get_drvdata(dev);
1238 long val;
1239
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001240 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001241 || (val != 0 && val != 1))
1242 return -EINVAL;
1243
1244 mutex_lock(&data->update_lock);
1245 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1246 if (val)
1247 data->beeps |= (1 << bitnr);
1248 else
1249 data->beeps &= ~(1 << bitnr);
1250 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1251 mutex_unlock(&data->update_lock);
1252 return count;
1253}
1254
1255static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1256 show_beep, set_beep, 1);
1257static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1258static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1259static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1260static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1261static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1262static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1263static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1264/* fanX_beep writability is set later */
1265static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1266static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1267static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1268static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1269static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1270static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1271 show_beep, set_beep, 2);
1272static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1273static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1274
Jean Delvare5f2dc792010-03-05 22:17:18 +01001275static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1276 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Jean Delvare90d66192007-10-08 18:24:35 +02001278 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001279 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001281static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1282 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001284 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001285 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001287 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001288 return -EINVAL;
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 data->vrm = val;
1291
1292 return count;
1293}
1294static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Jean Delvare5f2dc792010-03-05 22:17:18 +01001296static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1297 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 struct it87_data *data = it87_update_device(dev);
1300 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1301}
1302static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001303
Jean Delvare738e5e02010-08-14 21:08:50 +02001304static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1305 char *buf)
1306{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001307 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001308 "+5V",
1309 "5VSB",
1310 "Vbat",
1311 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001312 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001313 "+3.3V",
1314 "3VSB",
1315 "Vbat",
1316 };
1317 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001318 int nr = to_sensor_dev_attr(attr)->index;
1319
Jean Delvare16b5dda2012-01-16 22:51:48 +01001320 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1321 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001322}
1323static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1324static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1325static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1326
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001327static ssize_t show_name(struct device *dev, struct device_attribute
1328 *devattr, char *buf)
1329{
1330 struct it87_data *data = dev_get_drvdata(dev);
1331 return sprintf(buf, "%s\n", data->name);
1332}
1333static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1334
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001335static struct attribute *it87_attributes_in[9][5] = {
1336{
Jean Delvare87808be2006-09-24 21:17:13 +02001337 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001338 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001339 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001340 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001341 NULL
1342}, {
1343 &sensor_dev_attr_in1_input.dev_attr.attr,
1344 &sensor_dev_attr_in1_min.dev_attr.attr,
1345 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001346 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001347 NULL
1348}, {
1349 &sensor_dev_attr_in2_input.dev_attr.attr,
1350 &sensor_dev_attr_in2_min.dev_attr.attr,
1351 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001352 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001353 NULL
1354}, {
1355 &sensor_dev_attr_in3_input.dev_attr.attr,
1356 &sensor_dev_attr_in3_min.dev_attr.attr,
1357 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001358 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001359 NULL
1360}, {
1361 &sensor_dev_attr_in4_input.dev_attr.attr,
1362 &sensor_dev_attr_in4_min.dev_attr.attr,
1363 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001364 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001365 NULL
1366}, {
1367 &sensor_dev_attr_in5_input.dev_attr.attr,
1368 &sensor_dev_attr_in5_min.dev_attr.attr,
1369 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001370 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001371 NULL
1372}, {
1373 &sensor_dev_attr_in6_input.dev_attr.attr,
1374 &sensor_dev_attr_in6_min.dev_attr.attr,
1375 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001376 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001377 NULL
1378}, {
1379 &sensor_dev_attr_in7_input.dev_attr.attr,
1380 &sensor_dev_attr_in7_min.dev_attr.attr,
1381 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001382 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001383 NULL
1384}, {
1385 &sensor_dev_attr_in8_input.dev_attr.attr,
1386 NULL
1387} };
Jean Delvare87808be2006-09-24 21:17:13 +02001388
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001389static const struct attribute_group it87_group_in[9] = {
1390 { .attrs = it87_attributes_in[0] },
1391 { .attrs = it87_attributes_in[1] },
1392 { .attrs = it87_attributes_in[2] },
1393 { .attrs = it87_attributes_in[3] },
1394 { .attrs = it87_attributes_in[4] },
1395 { .attrs = it87_attributes_in[5] },
1396 { .attrs = it87_attributes_in[6] },
1397 { .attrs = it87_attributes_in[7] },
1398 { .attrs = it87_attributes_in[8] },
1399};
1400
Guenter Roeck4573acb2012-03-26 16:17:41 -07001401static struct attribute *it87_attributes_temp[3][6] = {
1402{
Jean Delvare87808be2006-09-24 21:17:13 +02001403 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001404 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001405 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001406 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001407 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001408 NULL
1409} , {
1410 &sensor_dev_attr_temp2_input.dev_attr.attr,
1411 &sensor_dev_attr_temp2_max.dev_attr.attr,
1412 &sensor_dev_attr_temp2_min.dev_attr.attr,
1413 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001414 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001415 NULL
1416} , {
1417 &sensor_dev_attr_temp3_input.dev_attr.attr,
1418 &sensor_dev_attr_temp3_max.dev_attr.attr,
1419 &sensor_dev_attr_temp3_min.dev_attr.attr,
1420 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001421 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001422 NULL
1423} };
Jean Delvare87808be2006-09-24 21:17:13 +02001424
Guenter Roeck4573acb2012-03-26 16:17:41 -07001425static const struct attribute_group it87_group_temp[3] = {
1426 { .attrs = it87_attributes_temp[0] },
1427 { .attrs = it87_attributes_temp[1] },
1428 { .attrs = it87_attributes_temp[2] },
1429};
1430
1431static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001432 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001433 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001434 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001435 NULL
1436};
1437
1438static const struct attribute_group it87_group = {
1439 .attrs = it87_attributes,
1440};
1441
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001442static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001443 &sensor_dev_attr_in0_beep.dev_attr.attr,
1444 &sensor_dev_attr_in1_beep.dev_attr.attr,
1445 &sensor_dev_attr_in2_beep.dev_attr.attr,
1446 &sensor_dev_attr_in3_beep.dev_attr.attr,
1447 &sensor_dev_attr_in4_beep.dev_attr.attr,
1448 &sensor_dev_attr_in5_beep.dev_attr.attr,
1449 &sensor_dev_attr_in6_beep.dev_attr.attr,
1450 &sensor_dev_attr_in7_beep.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001451 NULL
1452};
Jean Delvared9b327c2010-03-05 22:17:17 +01001453
Guenter Roeck4573acb2012-03-26 16:17:41 -07001454static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001455 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1456 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1457 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001458};
1459
Jean Delvare723a0aa2010-03-05 22:17:16 +01001460static struct attribute *it87_attributes_fan16[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001461 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1462 &sensor_dev_attr_fan1_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001463 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1464 NULL
1465}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001466 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1467 &sensor_dev_attr_fan2_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001468 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1469 NULL
1470}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001471 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1472 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001473 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1474 NULL
1475}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001476 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1477 &sensor_dev_attr_fan4_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001478 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1479 NULL
1480}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001481 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1482 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001483 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1484 NULL
1485} };
Jean Delvare87808be2006-09-24 21:17:13 +02001486
Jean Delvare723a0aa2010-03-05 22:17:16 +01001487static const struct attribute_group it87_group_fan16[5] = {
1488 { .attrs = it87_attributes_fan16[0] },
1489 { .attrs = it87_attributes_fan16[1] },
1490 { .attrs = it87_attributes_fan16[2] },
1491 { .attrs = it87_attributes_fan16[3] },
1492 { .attrs = it87_attributes_fan16[4] },
1493};
1494
1495static struct attribute *it87_attributes_fan[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001496 &sensor_dev_attr_fan1_input.dev_attr.attr,
1497 &sensor_dev_attr_fan1_min.dev_attr.attr,
1498 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001499 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1500 NULL
1501}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001502 &sensor_dev_attr_fan2_input.dev_attr.attr,
1503 &sensor_dev_attr_fan2_min.dev_attr.attr,
1504 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001505 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1506 NULL
1507}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001508 &sensor_dev_attr_fan3_input.dev_attr.attr,
1509 &sensor_dev_attr_fan3_min.dev_attr.attr,
1510 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001511 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001512 NULL
1513} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001514
Jean Delvare723a0aa2010-03-05 22:17:16 +01001515static const struct attribute_group it87_group_fan[3] = {
1516 { .attrs = it87_attributes_fan[0] },
1517 { .attrs = it87_attributes_fan[1] },
1518 { .attrs = it87_attributes_fan[2] },
1519};
1520
1521static const struct attribute_group *
1522it87_get_fan_group(const struct it87_data *data)
1523{
1524 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1525}
1526
1527static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001528 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001529 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001530 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001531 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001532 NULL
1533}, {
1534 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1535 &sensor_dev_attr_pwm2.dev_attr.attr,
1536 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001537 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001538 NULL
1539}, {
1540 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1541 &sensor_dev_attr_pwm3.dev_attr.attr,
1542 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001543 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001544 NULL
1545} };
Jean Delvare87808be2006-09-24 21:17:13 +02001546
Jean Delvare723a0aa2010-03-05 22:17:16 +01001547static const struct attribute_group it87_group_pwm[3] = {
1548 { .attrs = it87_attributes_pwm[0] },
1549 { .attrs = it87_attributes_pwm[1] },
1550 { .attrs = it87_attributes_pwm[2] },
1551};
1552
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001553static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1554 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1555 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1556 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1557 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1558 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1559 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1560 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1561 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1562 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1563 NULL
1564}, {
1565 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1566 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1567 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1568 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1569 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1570 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1571 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1572 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1573 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1574 NULL
1575}, {
1576 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1577 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1578 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1579 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1580 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1581 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1582 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1583 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1584 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1585 NULL
1586} };
1587
1588static const struct attribute_group it87_group_autopwm[3] = {
1589 { .attrs = it87_attributes_autopwm[0] },
1590 { .attrs = it87_attributes_autopwm[1] },
1591 { .attrs = it87_attributes_autopwm[2] },
1592};
1593
Jean Delvared9b327c2010-03-05 22:17:17 +01001594static struct attribute *it87_attributes_fan_beep[] = {
1595 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1596 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1597 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1598 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1599 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1600};
1601
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001602static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001603 &dev_attr_vrm.attr,
1604 &dev_attr_cpu0_vid.attr,
1605 NULL
1606};
1607
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001608static const struct attribute_group it87_group_vid = {
1609 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001610};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Jean Delvare738e5e02010-08-14 21:08:50 +02001612static struct attribute *it87_attributes_label[] = {
1613 &sensor_dev_attr_in3_label.dev_attr.attr,
1614 &sensor_dev_attr_in7_label.dev_attr.attr,
1615 &sensor_dev_attr_in8_label.dev_attr.attr,
1616 NULL
1617};
1618
1619static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001620 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001621};
1622
Jean Delvare2d8672c2005-07-19 23:56:35 +02001623/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001624static int __init it87_find(unsigned short *address,
1625 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001627 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001628 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001629 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001631 err = superio_enter();
1632 if (err)
1633 return err;
1634
1635 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001636 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001637
1638 switch (chip_type) {
1639 case IT8705F_DEVID:
1640 sio_data->type = it87;
1641 break;
1642 case IT8712F_DEVID:
1643 sio_data->type = it8712;
1644 break;
1645 case IT8716F_DEVID:
1646 case IT8726F_DEVID:
1647 sio_data->type = it8716;
1648 break;
1649 case IT8718F_DEVID:
1650 sio_data->type = it8718;
1651 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001652 case IT8720F_DEVID:
1653 sio_data->type = it8720;
1654 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001655 case IT8721F_DEVID:
1656 sio_data->type = it8721;
1657 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001658 case IT8728F_DEVID:
1659 sio_data->type = it8728;
1660 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001661 case IT8782F_DEVID:
1662 sio_data->type = it8782;
1663 break;
1664 case IT8783E_DEVID:
1665 sio_data->type = it8783;
1666 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001667 case 0xffff: /* No device at all */
1668 goto exit;
1669 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001670 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001671 goto exit;
1672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Jean Delvare87673dd2006-08-28 14:37:19 +02001674 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001676 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 goto exit;
1678 }
1679
1680 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1681 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001682 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 goto exit;
1684 }
1685
1686 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001687 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Joe Perchesa8ca1032011-01-12 21:55:10 +01001688 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001689 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Jean Delvare738e5e02010-08-14 21:08:50 +02001691 /* in8 (Vbat) is always internal */
1692 sio_data->internal = (1 << 2);
1693
Jean Delvare87673dd2006-08-28 14:37:19 +02001694 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001695 if (sio_data->type == it87) {
1696 /* The IT8705F doesn't have VID pins at all */
1697 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001698
1699 /* The IT8705F has a different LD number for GPIO */
1700 superio_select(5);
1701 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08001702 } else if (sio_data->type == it8783) {
1703 int reg25, reg27, reg2A, reg2C, regEF;
Guenter Roeck0531d982012-03-02 11:46:44 -08001704
1705 sio_data->skip_vid = 1; /* No VID */
1706
1707 superio_select(GPIO);
1708
1709 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1710 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1711 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1712 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1713 regEF = superio_inb(IT87_SIO_SPI_REG);
1714
Guenter Roeck0531d982012-03-02 11:46:44 -08001715 /* Check if fan3 is there or not */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001716 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08001717 sio_data->skip_fan |= (1 << 2);
1718 if ((reg25 & (1 << 4))
1719 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1720 sio_data->skip_pwm |= (1 << 2);
1721
1722 /* Check if fan2 is there or not */
1723 if (reg27 & (1 << 7))
1724 sio_data->skip_fan |= (1 << 1);
1725 if (reg27 & (1 << 3))
1726 sio_data->skip_pwm |= (1 << 1);
1727
1728 /* VIN5 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001729 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1730 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001731
1732 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001733 if (reg27 & (1 << 1))
1734 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001735
1736 /*
1737 * VIN7
1738 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1739 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001740 if (reg27 & (1 << 2)) {
1741 /*
1742 * The data sheet is a bit unclear regarding the
1743 * internal voltage divider for VCCH5V. It says
1744 * "This bit enables and switches VIN7 (pin 91) to the
1745 * internal voltage divider for VCCH5V".
1746 * This is different to other chips, where the internal
1747 * voltage divider would connect VIN7 to an internal
1748 * voltage source. Maybe that is the case here as well.
1749 *
1750 * Since we don't know for sure, re-route it if that is
1751 * not the case, and ask the user to report if the
1752 * resulting voltage is sane.
1753 */
1754 if (!(reg2C & (1 << 1))) {
1755 reg2C |= (1 << 1);
1756 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1757 pr_notice("Routing internal VCCH5V to in7.\n");
1758 }
1759 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1760 pr_notice("Please report if it displays a reasonable voltage.\n");
1761 }
Guenter Roeck0531d982012-03-02 11:46:44 -08001762
1763 if (reg2C & (1 << 0))
1764 sio_data->internal |= (1 << 0);
1765 if (reg2C & (1 << 1))
1766 sio_data->internal |= (1 << 1);
1767
1768 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1769
Jean Delvare895ff262009-12-09 20:35:47 +01001770 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001771 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001772 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02001773
1774 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001775
Jean Delvare895ff262009-12-09 20:35:47 +01001776 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001777 if (sio_data->type == it8721 || sio_data->type == it8728 ||
1778 sio_data->type == it8782) {
Jean Delvare16b5dda2012-01-16 22:51:48 +01001779 /*
Guenter Roeck0531d982012-03-02 11:46:44 -08001780 * IT8721F/IT8758E, and IT8782F don't have VID pins
1781 * at all, not sure about the IT8728F.
Jean Delvare16b5dda2012-01-16 22:51:48 +01001782 */
Jean Delvare895ff262009-12-09 20:35:47 +01001783 sio_data->skip_vid = 1;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001784 } else {
1785 /* We need at least 4 VID pins */
1786 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001787 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001788 sio_data->skip_vid = 1;
1789 }
Jean Delvare895ff262009-12-09 20:35:47 +01001790 }
1791
Jean Delvare591ec652009-12-09 20:35:48 +01001792 /* Check if fan3 is there or not */
1793 if (reg & (1 << 6))
1794 sio_data->skip_pwm |= (1 << 2);
1795 if (reg & (1 << 7))
1796 sio_data->skip_fan |= (1 << 2);
1797
1798 /* Check if fan2 is there or not */
1799 reg = superio_inb(IT87_SIO_GPIO5_REG);
1800 if (reg & (1 << 1))
1801 sio_data->skip_pwm |= (1 << 1);
1802 if (reg & (1 << 2))
1803 sio_data->skip_fan |= (1 << 1);
1804
Jean Delvare895ff262009-12-09 20:35:47 +01001805 if ((sio_data->type == it8718 || sio_data->type == it8720)
1806 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001807 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001808
1809 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001810
1811 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1812
Jean Delvare436cad22010-07-09 16:22:48 +02001813 /*
1814 * The IT8720F has no VIN7 pin, so VCCH should always be
1815 * routed internally to VIN7 with an internal divider.
1816 * Curiously, there still is a configuration bit to control
1817 * this, which means it can be set incorrectly. And even
1818 * more curiously, many boards out there are improperly
1819 * configured, even though the IT8720F datasheet claims
1820 * that the internal routing of VCCH to VIN7 is the default
1821 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08001822 *
1823 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001824 * If UART6 is enabled, re-route VIN7 to the internal divider
1825 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02001826 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001827 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02001828 reg |= (1 << 1);
1829 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01001830 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02001831 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001832 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001833 sio_data->internal |= (1 << 0);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001834 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1835 sio_data->type == it8728)
Jean Delvare738e5e02010-08-14 21:08:50 +02001836 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01001837
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001838 /*
1839 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1840 * While VIN7 can be routed to the internal voltage divider,
1841 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07001842 *
1843 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
1844 * is the temperature source. Since we can not read the
1845 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001846 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001847 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001848 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07001849 sio_data->skip_temp |= (1 << 2);
1850 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001851
Jean Delvared9b327c2010-03-05 22:17:17 +01001852 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02001853 }
Jean Delvared9b327c2010-03-05 22:17:17 +01001854 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01001855 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02001856
Jean Delvare98dd22c2008-10-09 15:33:58 +02001857 /* Disable specific features based on DMI strings */
1858 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1859 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1860 if (board_vendor && board_name) {
1861 if (strcmp(board_vendor, "nVIDIA") == 0
1862 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001863 /*
1864 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1865 * connected to a fan, but to something else. One user
1866 * has reported instant system power-off when changing
1867 * the PWM2 duty cycle, so we disable it.
1868 * I use the board name string as the trigger in case
1869 * the same board is ever used in other systems.
1870 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01001871 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02001872 sio_data->skip_pwm = (1 << 1);
1873 }
1874 }
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876exit:
1877 superio_exit();
1878 return err;
1879}
1880
Jean Delvare723a0aa2010-03-05 22:17:16 +01001881static void it87_remove_files(struct device *dev)
1882{
1883 struct it87_data *data = platform_get_drvdata(pdev);
1884 struct it87_sio_data *sio_data = dev->platform_data;
1885 const struct attribute_group *fan_group = it87_get_fan_group(data);
1886 int i;
1887
1888 sysfs_remove_group(&dev->kobj, &it87_group);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001889 for (i = 0; i < 9; i++) {
1890 if (sio_data->skip_in & (1 << i))
1891 continue;
1892 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
1893 if (it87_attributes_in_beep[i])
1894 sysfs_remove_file(&dev->kobj,
1895 it87_attributes_in_beep[i]);
1896 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07001897 for (i = 0; i < 3; i++) {
1898 if (!(data->has_temp & (1 << i)))
1899 continue;
1900 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
1901 if (sio_data->beep_pin)
1902 sysfs_remove_file(&dev->kobj,
1903 it87_attributes_temp_beep[i]);
1904 }
Jean Delvare723a0aa2010-03-05 22:17:16 +01001905 for (i = 0; i < 5; i++) {
1906 if (!(data->has_fan & (1 << i)))
1907 continue;
1908 sysfs_remove_group(&dev->kobj, &fan_group[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01001909 if (sio_data->beep_pin)
1910 sysfs_remove_file(&dev->kobj,
1911 it87_attributes_fan_beep[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001912 }
1913 for (i = 0; i < 3; i++) {
1914 if (sio_data->skip_pwm & (1 << 0))
1915 continue;
1916 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001917 if (has_old_autopwm(data))
1918 sysfs_remove_group(&dev->kobj,
1919 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001920 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001921 if (!sio_data->skip_vid)
1922 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02001923 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001924}
1925
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001926static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001929 struct resource *res;
1930 struct device *dev = &pdev->dev;
1931 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001932 const struct attribute_group *fan_group;
1933 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01001935 int fan_beep_need_rw;
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001936 static const char * const names[] = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001937 "it87",
1938 "it8712",
1939 "it8716",
1940 "it8718",
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001941 "it8720",
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001942 "it8721",
Jean Delvare16b5dda2012-01-16 22:51:48 +01001943 "it8728",
Guenter Roeck0531d982012-03-02 11:46:44 -08001944 "it8782",
1945 "it8783",
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001946 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001948 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07001949 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
1950 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001951 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1952 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001953 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07001954 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Guenter Roeck62a1d052012-03-24 21:54:41 -07001957 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
1958 if (!data)
1959 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001961 data->addr = res->start;
1962 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001963 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001964 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001967 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07001968 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
1969 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001971 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001973 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001976 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001978 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001979 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001980 if (sio_data->internal & (1 << 0))
1981 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1982 if (sio_data->internal & (1 << 1))
1983 data->in_scaled |= (1 << 7); /* in7 is VSB */
1984 if (sio_data->internal & (1 << 2))
1985 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Guenter Roeck0531d982012-03-02 11:46:44 -08001986 } else if (sio_data->type == it8782 || sio_data->type == it8783) {
1987 if (sio_data->internal & (1 << 0))
1988 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
1989 if (sio_data->internal & (1 << 1))
1990 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001991 }
1992
Guenter Roeck4573acb2012-03-26 16:17:41 -07001993 data->has_temp = 0x07;
1994 if (sio_data->skip_temp & (1 << 2)) {
1995 if (sio_data->type == it8782
1996 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
1997 data->has_temp &= ~(1 << 2);
1998 }
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002001 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002004 err = sysfs_create_group(&dev->kobj, &it87_group);
2005 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002006 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002007
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002008 for (i = 0; i < 9; i++) {
2009 if (sio_data->skip_in & (1 << i))
2010 continue;
2011 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2012 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002013 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002014 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2015 err = sysfs_create_file(&dev->kobj,
2016 it87_attributes_in_beep[i]);
2017 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002018 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002019 }
2020 }
2021
Guenter Roeck4573acb2012-03-26 16:17:41 -07002022 for (i = 0; i < 3; i++) {
2023 if (!(data->has_temp & (1 << i)))
2024 continue;
2025 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002026 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002027 goto error;
Guenter Roeck4573acb2012-03-26 16:17:41 -07002028 if (sio_data->beep_pin) {
2029 err = sysfs_create_file(&dev->kobj,
2030 it87_attributes_temp_beep[i]);
2031 if (err)
2032 goto error;
2033 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002034 }
2035
Jean Delvare9060f8b2006-08-28 14:24:17 +02002036 /* Do not create fan files for disabled fans */
Jean Delvare723a0aa2010-03-05 22:17:16 +01002037 fan_group = it87_get_fan_group(data);
Jean Delvared9b327c2010-03-05 22:17:17 +01002038 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01002039 for (i = 0; i < 5; i++) {
2040 if (!(data->has_fan & (1 << i)))
2041 continue;
2042 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
2043 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002044 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002045
2046 if (sio_data->beep_pin) {
2047 err = sysfs_create_file(&dev->kobj,
2048 it87_attributes_fan_beep[i]);
2049 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002050 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002051 if (!fan_beep_need_rw)
2052 continue;
2053
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002054 /*
2055 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002056 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002057 * for it.
2058 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002059 if (sysfs_chmod_file(&dev->kobj,
2060 it87_attributes_fan_beep[i],
2061 S_IRUGO | S_IWUSR))
2062 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2063 i + 1);
2064 fan_beep_need_rw = 0;
2065 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002066 }
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002069 for (i = 0; i < 3; i++) {
2070 if (sio_data->skip_pwm & (1 << i))
2071 continue;
2072 err = sysfs_create_group(&dev->kobj,
2073 &it87_group_pwm[i]);
2074 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002075 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002076
2077 if (!has_old_autopwm(data))
2078 continue;
2079 err = sysfs_create_group(&dev->kobj,
2080 &it87_group_autopwm[i]);
2081 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002082 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
2085
Jean Delvare895ff262009-12-09 20:35:47 +01002086 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002087 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002088 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002089 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002090 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2091 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002092 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002093 }
2094
Jean Delvare738e5e02010-08-14 21:08:50 +02002095 /* Export labels for internal sensors */
2096 for (i = 0; i < 3; i++) {
2097 if (!(sio_data->internal & (1 << i)))
2098 continue;
2099 err = sysfs_create_file(&dev->kobj,
2100 it87_attributes_label[i]);
2101 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002102 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002103 }
2104
Tony Jones1beeffe2007-08-20 13:46:20 -07002105 data->hwmon_dev = hwmon_device_register(dev);
2106 if (IS_ERR(data->hwmon_dev)) {
2107 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002108 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
2110
2111 return 0;
2112
Guenter Roeck62a1d052012-03-24 21:54:41 -07002113error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002114 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return err;
2116}
2117
Bill Pemberton281dfd02012-11-19 13:25:51 -05002118static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002120 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Tony Jones1beeffe2007-08-20 13:46:20 -07002122 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002123 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return 0;
2126}
2127
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002128/*
2129 * Must be called with data->update_lock held, except during initialization.
2130 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2131 * would slow down the IT87 access and should not be necessary.
2132 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002133static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002135 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2136 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002139/*
2140 * Must be called with data->update_lock held, except during initialization.
2141 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2142 * would slow down the IT87 access and should not be necessary.
2143 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002144static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002146 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2147 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
2150/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002151static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002153 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002154 /*
2155 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002157 * disable pwm control to protect the user.
2158 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002159 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if ((tmp & 0x87) == 0) {
2161 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002162 /*
2163 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002165 * inverting all fan speed values.
2166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 int i;
2168 u8 pwm[3];
2169
2170 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002171 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 IT87_REG_PWM(i));
2173
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002174 /*
2175 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 * might be correct, as suspicious as it seems, so we
2177 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002178 * PWM interface).
2179 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002181 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002183 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 tmp | 0x87);
2185 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002186 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 IT87_REG_PWM(i),
2188 0x7f & ~pwm[i]);
2189 return 1;
2190 }
2191
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002192 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 "too broken to be fixed\n");
2194 }
2195
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002196 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 "defaults, disabling PWM interface\n");
2198 return 0;
2199 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002200 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 "sane, won't touch\n");
2202 }
2203
2204 return 1;
2205}
2206
2207/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002208static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Jean Delvare591ec652009-12-09 20:35:48 +01002210 struct it87_sio_data *sio_data = pdev->dev.platform_data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002211 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002213 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002215 /*
2216 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002217 * - If it is in automatic mode, setting to manual mode should set
2218 * the fan to full speed by default.
2219 * - If it is in manual mode, we need a mapping to temperature
2220 * channels to use when later setting to automatic mode later.
2221 * Use a 1:1 mapping by default (we are clueless.)
2222 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002223 * prior to switching to a different mode.
2224 * Note that this is no longer needed for the IT8721F and later, as
2225 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002226 * manual duty cycle.
2227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002229 data->pwm_temp_map[i] = i;
2230 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002231 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
2233
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002234 /*
2235 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002236 * registers. For low voltage limits it makes no sense and triggers
2237 * alarms, so change to 0 instead. For high temperature limits, it
2238 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002239 * but is still confusing, so change to 127 degrees C.
2240 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002241 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002242 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002243 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002244 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002245 }
2246 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002247 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002248 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002249 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002250 }
2251
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002252 /*
2253 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002254 * set to two different sensor types and we can't guess which one
2255 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002256 * run-time through the temp{1-3}_type sysfs accessors if needed.
2257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002260 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if ((tmp & 0xff) == 0) {
2262 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002263 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
2265
2266 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002267 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002268 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002269 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002271 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002272 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2273 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002275 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Jean Delvare17d648b2006-08-28 14:23:46 +02002277 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02002278 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002279 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002280 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002281 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002282 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002283 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002284 tmp | 0x07);
2285 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002286 /* IT8705F, IT8782F, and IT8783E/F only support three fans. */
2287 if (data->type != it87 && data->type != it8782 &&
2288 data->type != it8783) {
Andrew Paprocki816d8c62008-08-06 22:41:06 +02002289 if (tmp & (1 << 4))
2290 data->has_fan |= (1 << 3); /* fan4 enabled */
2291 if (tmp & (1 << 5))
2292 data->has_fan |= (1 << 4); /* fan5 enabled */
2293 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002294 }
2295
Jean Delvare591ec652009-12-09 20:35:48 +01002296 /* Fan input pins may be used for alternative functions */
2297 data->has_fan &= ~sio_data->skip_fan;
2298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002300 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002301 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 | (update_vbat ? 0x41 : 0x01));
2303}
2304
Jean Delvareb99883d2010-03-05 22:17:15 +01002305static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2306{
2307 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002308 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002309 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002310 data->pwm_duty[nr] = it87_read_value(data,
2311 IT87_REG_PWM_DUTY(nr));
2312 } else {
2313 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2314 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2315 else /* Manual mode */
2316 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2317 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002318
2319 if (has_old_autopwm(data)) {
2320 int i;
2321
2322 for (i = 0; i < 5 ; i++)
2323 data->auto_temp[nr][i] = it87_read_value(data,
2324 IT87_REG_AUTO_TEMP(nr, i));
2325 for (i = 0; i < 3 ; i++)
2326 data->auto_pwm[nr][i] = it87_read_value(data,
2327 IT87_REG_AUTO_PWM(nr, i));
2328 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002329}
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331static struct it87_data *it87_update_device(struct device *dev)
2332{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002333 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 int i;
2335
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002336 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2339 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002341 /*
2342 * Cleared after each update, so reenable. Value
2343 * returned by this read will be previous value
2344 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002345 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002346 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348 for (i = 0; i <= 7; i++) {
Guenter Roeck929c6a52012-12-19 22:17:00 +01002349 data->in[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002350 it87_read_value(data, IT87_REG_VIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002351 data->in[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002352 it87_read_value(data, IT87_REG_VIN_MIN(i));
Guenter Roeck929c6a52012-12-19 22:17:00 +01002353 data->in[i][2] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002354 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
Jean Delvare3543a532006-08-28 14:27:25 +02002356 /* in8 (battery) has no limit registers */
Guenter Roeck929c6a52012-12-19 22:17:00 +01002357 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Jean Delvarec7f1f712007-09-03 17:11:46 +02002359 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002360 /* Skip disabled fans */
2361 if (!(data->has_fan & (1 << i)))
2362 continue;
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 data->fan_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002365 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002366 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002367 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002368 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002369 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002370 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002371 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002372 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002373 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002377 if (!(data->has_temp & (1 << i)))
2378 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002379 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002380 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002381 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002382 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002383 data->temp[i][2] =
2384 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
2386
Jean Delvare17d648b2006-08-28 14:23:46 +02002387 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002388 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002389 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002390 data->fan_div[0] = i & 0x07;
2391 data->fan_div[1] = (i >> 3) & 0x07;
2392 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002396 it87_read_value(data, IT87_REG_ALARM1) |
2397 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2398 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002399 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002400
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002401 data->fan_main_ctrl = it87_read_value(data,
2402 IT87_REG_FAN_MAIN_CTRL);
2403 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002404 for (i = 0; i < 3; i++)
2405 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002407 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002408 /*
2409 * The IT8705F does not have VID capability.
2410 * The IT8718F and later don't use IT87_REG_VID for the
2411 * same purpose.
2412 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002413 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002414 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002415 /*
2416 * The older IT8712F revisions had only 5 VID pins,
2417 * but we assume it is always safe to read 6 bits.
2418 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002419 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421 data->last_updated = jiffies;
2422 data->valid = 1;
2423 }
2424
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002425 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 return data;
2428}
2429
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002430static int __init it87_device_add(unsigned short address,
2431 const struct it87_sio_data *sio_data)
2432{
2433 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002434 .start = address + IT87_EC_OFFSET,
2435 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002436 .name = DRVNAME,
2437 .flags = IORESOURCE_IO,
2438 };
2439 int err;
2440
Jean Delvareb9acb642009-01-07 16:37:35 +01002441 err = acpi_check_resource_conflict(&res);
2442 if (err)
2443 goto exit;
2444
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002445 pdev = platform_device_alloc(DRVNAME, address);
2446 if (!pdev) {
2447 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002448 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002449 goto exit;
2450 }
2451
2452 err = platform_device_add_resources(pdev, &res, 1);
2453 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002454 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002455 goto exit_device_put;
2456 }
2457
2458 err = platform_device_add_data(pdev, sio_data,
2459 sizeof(struct it87_sio_data));
2460 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002461 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002462 goto exit_device_put;
2463 }
2464
2465 err = platform_device_add(pdev);
2466 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002467 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002468 goto exit_device_put;
2469 }
2470
2471 return 0;
2472
2473exit_device_put:
2474 platform_device_put(pdev);
2475exit:
2476 return err;
2477}
2478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479static int __init sm_it87_init(void)
2480{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002481 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002482 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002483 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002484
Jean Delvare98dd22c2008-10-09 15:33:58 +02002485 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002486 err = it87_find(&isa_address, &sio_data);
2487 if (err)
2488 return err;
2489 err = platform_driver_register(&it87_driver);
2490 if (err)
2491 return err;
2492
2493 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002494 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002495 platform_driver_unregister(&it87_driver);
2496 return err;
2497 }
2498
2499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
2502static void __exit sm_it87_exit(void)
2503{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002504 platform_device_unregister(pdev);
2505 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
2508
Jean Delvaref1d8e332007-11-25 16:14:44 +01002509MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02002510 "Jean Delvare <khali@linux-fr.org>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002511MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512module_param(update_vbat, bool, 0);
2513MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2514module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002515MODULE_PARM_DESC(fix_pwm_polarity,
2516 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517MODULE_LICENSE("GPL");
2518
2519module_init(sm_it87_init);
2520module_exit(sm_it87_exit);