blob: 76ab1d0e42d02b788e6095ca3e29662fb7eb160b [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200262 u8 in_max[8]; /* Register value */
263 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200264 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvarec7f1f712007-09-03 17:11:46 +0200265 u16 fan[5]; /* Register values, possibly combined */
266 u16 fan_min[5]; /* Register values, possibly combined */
Guenter Roeck4573acb2012-03-26 16:17:41 -0700267 u8 has_temp; /* Bitfield, temp sensors enabled */
Guenter Roeck60ca3852012-12-19 22:17:00 +0100268 s8 temp[3][3]; /* [nr][0]=temp, [1]=min, [2]=max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 u8 sensor; /* Register value */
270 u8 fan_div[3]; /* Register encoding, shifted right */
271 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100272 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100274 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100276 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100277
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800278 /*
279 * The following 3 arrays correspond to the same registers up to
Jean Delvare6229cdb2010-12-08 16:27:22 +0100280 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
281 * 7, and we want to preserve settings on mode changes, so we have
282 * to track all values separately.
283 * Starting with the IT8721F, the manual PWM duty cycles are stored
284 * in separate registers (8-bit values), so the separate tracking
285 * is no longer needed, but it is still done to keep the driver
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800286 * simple.
287 */
Jean Delvareb99883d2010-03-05 22:17:15 +0100288 u8 pwm_ctrl[3]; /* Register value */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100289 u8 pwm_duty[3]; /* Manual PWM value set by user */
Jean Delvareb99883d2010-03-05 22:17:15 +0100290 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100291
292 /* Automatic fan speed control registers */
293 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
294 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295};
296
Jean Delvare16b5dda2012-01-16 22:51:48 +0100297static inline int has_12mv_adc(const struct it87_data *data)
298{
299 /*
300 * IT8721F and later have a 12 mV ADC, also with internal scaling
301 * on selected inputs.
302 */
303 return data->type == it8721
304 || data->type == it8728;
305}
306
307static inline int has_newer_autopwm(const struct it87_data *data)
308{
309 /*
310 * IT8721F and later have separate registers for the temperature
311 * mapping and the manual duty cycle.
312 */
313 return data->type == it8721
314 || data->type == it8728;
315}
316
Guenter Roeck0531d982012-03-02 11:46:44 -0800317static int adc_lsb(const struct it87_data *data, int nr)
318{
319 int lsb = has_12mv_adc(data) ? 12 : 16;
320 if (data->in_scaled & (1 << nr))
321 lsb <<= 1;
322 return lsb;
323}
324
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200325static u8 in_to_reg(const struct it87_data *data, int nr, long val)
326{
Guenter Roeck0531d982012-03-02 11:46:44 -0800327 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200328 return SENSORS_LIMIT(val, 0, 255);
329}
330
331static int in_from_reg(const struct it87_data *data, int nr, int val)
332{
Guenter Roeck0531d982012-03-02 11:46:44 -0800333 return val * adc_lsb(data, nr);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200334}
Jean Delvare0df6454d2010-10-28 20:31:51 +0200335
336static inline u8 FAN_TO_REG(long rpm, int div)
337{
338 if (rpm == 0)
339 return 255;
340 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
341 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
342 254);
343}
344
345static inline u16 FAN16_TO_REG(long rpm)
346{
347 if (rpm == 0)
348 return 0xffff;
349 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
350}
351
352#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
353 1350000 / ((val) * (div)))
354/* The divider is fixed to 2 in 16-bit mode */
355#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
356 1350000 / ((val) * 2))
357
358#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
359 ((val) + 500) / 1000), -128, 127))
360#define TEMP_FROM_REG(val) ((val) * 1000)
361
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200362static u8 pwm_to_reg(const struct it87_data *data, long val)
363{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100364 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200365 return val;
366 else
367 return val >> 1;
368}
369
370static int pwm_from_reg(const struct it87_data *data, u8 reg)
371{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100372 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200373 return reg;
374 else
375 return (reg & 0x7f) << 1;
376}
377
Jean Delvare0df6454d2010-10-28 20:31:51 +0200378
379static int DIV_TO_REG(int val)
380{
381 int answer = 0;
382 while (answer < 7 && (val >>= 1))
383 answer++;
384 return answer;
385}
386#define DIV_FROM_REG(val) (1 << (val))
387
388static const unsigned int pwm_freq[8] = {
389 48000000 / 128,
390 24000000 / 128,
391 12000000 / 128,
392 8000000 / 128,
393 6000000 / 128,
394 3000000 / 128,
395 1500000 / 128,
396 750000 / 128,
397};
398
Andrew Paprocki04751692008-08-06 22:41:06 +0200399static inline int has_16bit_fans(const struct it87_data *data)
400{
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800401 /*
402 * IT8705F Datasheet 0.4.1, 3h == Version G.
403 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
404 * These are the first revisions with 16-bit tachometer support.
405 */
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200406 return (data->type == it87 && data->revision >= 0x03)
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200407 || (data->type == it8712 && data->revision >= 0x08)
Andrew Paprocki04751692008-08-06 22:41:06 +0200408 || data->type == it8716
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100409 || data->type == it8718
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200410 || data->type == it8720
Jean Delvare16b5dda2012-01-16 22:51:48 +0100411 || data->type == it8721
Guenter Roeck0531d982012-03-02 11:46:44 -0800412 || data->type == it8728
413 || data->type == it8782
414 || data->type == it8783;
Andrew Paprocki04751692008-08-06 22:41:06 +0200415}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100417static inline int has_old_autopwm(const struct it87_data *data)
418{
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800419 /*
420 * The old automatic fan speed control interface is implemented
421 * by IT8705F chips up to revision F and IT8712F chips up to
422 * revision G.
423 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100424 return (data->type == it87 && data->revision < 0x03)
425 || (data->type == it8712 && data->revision < 0x08);
426}
427
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200428static int it87_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500429static int it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200431static int it87_read_value(struct it87_data *data, u8 reg);
432static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200434static int it87_check_pwm(struct device *dev);
435static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200438static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100439 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200440 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200441 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100442 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200443 .probe = it87_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500444 .remove = it87_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200445};
446
Jean Delvare20ad93d2005-06-05 11:53:25 +0200447static ssize_t show_in(struct device *dev, struct device_attribute *attr,
448 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200450 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
451 int nr = sensor_attr->index;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200454 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Jean Delvare20ad93d2005-06-05 11:53:25 +0200457static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
458 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200460 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
461 int nr = sensor_attr->index;
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200464 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Jean Delvare20ad93d2005-06-05 11:53:25 +0200467static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
468 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200470 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
471 int nr = sensor_attr->index;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200474 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Jean Delvare20ad93d2005-06-05 11:53:25 +0200477static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
478 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200480 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
481 int nr = sensor_attr->index;
482
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200483 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100484 unsigned long val;
485
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100486 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100487 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100489 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200490 data->in_min[nr] = in_to_reg(data, nr, val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200491 it87_write_value(data, IT87_REG_VIN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100493 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return count;
495}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200496static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
497 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200499 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
500 int nr = sensor_attr->index;
501
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200502 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100503 unsigned long val;
504
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100505 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100506 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100508 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200509 data->in_max[nr] = in_to_reg(data, nr, val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200510 it87_write_value(data, IT87_REG_VIN_MAX(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100512 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return count;
514}
515
516#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200517static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
518 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200521static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
522 show_in_min, set_in_min, offset); \
523static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
524 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526show_in_offset(0);
527limit_in_offset(0);
528show_in_offset(1);
529limit_in_offset(1);
530show_in_offset(2);
531limit_in_offset(2);
532show_in_offset(3);
533limit_in_offset(3);
534show_in_offset(4);
535limit_in_offset(4);
536show_in_offset(5);
537limit_in_offset(5);
538show_in_offset(6);
539limit_in_offset(6);
540show_in_offset(7);
541limit_in_offset(7);
542show_in_offset(8);
543
544/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200545static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
Guenter Roeck60ca3852012-12-19 22:17:00 +0100546 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100548 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
549 int nr = sattr->nr;
550 int index = sattr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct it87_data *data = it87_update_device(dev);
Jean Delvare20ad93d2005-06-05 11:53:25 +0200552
Guenter Roeck60ca3852012-12-19 22:17:00 +0100553 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200555
Guenter Roeck60ca3852012-12-19 22:17:00 +0100556static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
557 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Guenter Roeck60ca3852012-12-19 22:17:00 +0100559 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
560 int nr = sattr->nr;
561 int index = sattr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200562 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100563 long val;
564
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100565 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100566 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100568 mutex_lock(&data->update_lock);
Guenter Roeck60ca3852012-12-19 22:17:00 +0100569 data->temp[nr][index] = TEMP_TO_REG(val);
570 it87_write_value(data,
571 index == 1 ? IT87_REG_TEMP_LOW(nr)
572 : IT87_REG_TEMP_HIGH(nr),
573 data->temp[nr][index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100574 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return count;
576}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200577
Guenter Roeck60ca3852012-12-19 22:17:00 +0100578static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
579static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
580 0, 1);
581static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
582 0, 2);
583static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
584static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
585 1, 1);
586static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
587 1, 2);
588static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
589static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
590 2, 1);
591static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
592 2, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Jean Delvare20ad93d2005-06-05 11:53:25 +0200594static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
595 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200597 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
598 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct it87_data *data = it87_update_device(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800600 u8 reg = data->sensor; /* In case value is updated while used */
Jean Delvare5f2dc792010-03-05 22:17:18 +0100601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (reg & (1 << nr))
603 return sprintf(buf, "3\n"); /* thermal diode */
604 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200605 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return sprintf(buf, "0\n"); /* disabled */
607}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200608static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
609 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200611 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
612 int nr = sensor_attr->index;
613
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200614 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100615 long val;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200616 u8 reg;
Jean Delvaref5f64502010-03-05 22:17:19 +0100617
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100618 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100619 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Jean Delvare8acf07c2010-04-14 16:14:09 +0200621 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
622 reg &= ~(1 << nr);
623 reg &= ~(8 << nr);
Jean Delvare4ed10772008-10-17 17:51:16 +0200624 if (val == 2) { /* backwards compatibility */
625 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
626 "instead\n");
627 val = 4;
628 }
629 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200631 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200632 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200633 reg |= 8 << nr;
634 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200636
637 mutex_lock(&data->update_lock);
638 data->sensor = reg;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200639 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200640 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100641 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return count;
643}
644#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200645static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
646 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648show_sensor_offset(1);
649show_sensor_offset(2);
650show_sensor_offset(3);
651
652/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100653
654static int pwm_mode(const struct it87_data *data, int nr)
655{
656 int ctrl = data->fan_main_ctrl & (1 << nr);
657
658 if (ctrl == 0) /* Full speed */
659 return 0;
660 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
661 return 2;
662 else /* Manual mode */
663 return 1;
664}
665
Jean Delvare20ad93d2005-06-05 11:53:25 +0200666static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
667 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200669 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
670 int nr = sensor_attr->index;
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100673 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 DIV_FROM_REG(data->fan_div[nr])));
675}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200676static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
677 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200679 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
680 int nr = sensor_attr->index;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100683 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
684 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200686static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
687 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200689 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
690 int nr = sensor_attr->index;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct it87_data *data = it87_update_device(dev);
693 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
694}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100695static ssize_t show_pwm_enable(struct device *dev,
696 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200698 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
699 int nr = sensor_attr->index;
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100702 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200704static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
705 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200707 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
708 int nr = sensor_attr->index;
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200711 return sprintf(buf, "%d\n",
712 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100714static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
715 char *buf)
716{
717 struct it87_data *data = it87_update_device(dev);
718 int index = (data->fan_ctl >> 4) & 0x07;
719
720 return sprintf(buf, "%u\n", pwm_freq[index]);
721}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200722static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
723 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200725 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
726 int nr = sensor_attr->index;
727
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200728 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100729 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100730 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100732 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100733 return -EINVAL;
734
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100735 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200736 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800737 switch (nr) {
Jean Delvare5f2dc792010-03-05 22:17:18 +0100738 case 0:
739 data->fan_div[nr] = reg & 0x07;
740 break;
741 case 1:
742 data->fan_div[nr] = (reg >> 3) & 0x07;
743 break;
744 case 2:
745 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
746 break;
Jean Delvare07eab462005-11-23 15:44:31 -0800747 }
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200750 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100751 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return count;
753}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200754static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
755 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200757 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
758 int nr = sensor_attr->index;
759
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200760 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100761 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200762 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 u8 old;
764
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100765 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100766 return -EINVAL;
767
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100768 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200769 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200771 /* Save fan min limit */
772 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 switch (nr) {
775 case 0:
776 case 1:
777 data->fan_div[nr] = DIV_TO_REG(val);
778 break;
779 case 2:
780 if (val < 8)
781 data->fan_div[nr] = 1;
782 else
783 data->fan_div[nr] = 3;
784 }
785 val = old & 0x80;
786 val |= (data->fan_div[0] & 0x07);
787 val |= (data->fan_div[1] & 0x07) << 3;
788 if (data->fan_div[2] == 3)
789 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200790 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200792 /* Restore fan min limit */
793 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200794 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200795
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100796 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return count;
798}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100799
800/* Returns 0 if OK, -EINVAL otherwise */
801static int check_trip_points(struct device *dev, int nr)
802{
803 const struct it87_data *data = dev_get_drvdata(dev);
804 int i, err = 0;
805
806 if (has_old_autopwm(data)) {
807 for (i = 0; i < 3; i++) {
808 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
809 err = -EINVAL;
810 }
811 for (i = 0; i < 2; i++) {
812 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
813 err = -EINVAL;
814 }
815 }
816
817 if (err) {
818 dev_err(dev, "Inconsistent trip points, not switching to "
819 "automatic mode\n");
820 dev_err(dev, "Adjust the trip points and try again\n");
821 }
822 return err;
823}
824
Jean Delvare20ad93d2005-06-05 11:53:25 +0200825static ssize_t set_pwm_enable(struct device *dev,
826 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200828 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
829 int nr = sensor_attr->index;
830
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200831 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100832 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100834 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100835 return -EINVAL;
836
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100837 /* Check trip points before switching to automatic mode */
838 if (val == 2) {
839 if (check_trip_points(dev, nr) < 0)
840 return -EINVAL;
841 }
842
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100843 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 if (val == 0) {
846 int tmp;
847 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200848 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
849 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* set on/off mode */
851 data->fan_main_ctrl &= ~(1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100852 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
853 data->fan_main_ctrl);
Jean Delvareb99883d2010-03-05 22:17:15 +0100854 } else {
855 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +0100856 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +0100857 data->pwm_temp_map[nr] :
858 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +0100859 else /* Automatic mode */
860 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
861 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* set SmartGuardian mode */
863 data->fan_main_ctrl |= (1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100864 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
865 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100868 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return count;
870}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200871static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
872 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200874 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
875 int nr = sensor_attr->index;
876
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200877 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100878 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100880 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -EINVAL;
882
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100883 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +0100884 if (has_newer_autopwm(data)) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800885 /*
886 * If we are in automatic mode, the PWM duty cycle register
887 * is read-only so we can't write the value.
888 */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100889 if (data->pwm_ctrl[nr] & 0x80) {
890 mutex_unlock(&data->update_lock);
891 return -EBUSY;
892 }
893 data->pwm_duty[nr] = pwm_to_reg(data, val);
894 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
895 data->pwm_duty[nr]);
896 } else {
897 data->pwm_duty[nr] = pwm_to_reg(data, val);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800898 /*
899 * If we are in manual mode, write the duty cycle immediately;
900 * otherwise, just store it for later use.
901 */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100902 if (!(data->pwm_ctrl[nr] & 0x80)) {
903 data->pwm_ctrl[nr] = data->pwm_duty[nr];
904 it87_write_value(data, IT87_REG_PWM(nr),
905 data->pwm_ctrl[nr]);
906 }
Jean Delvareb99883d2010-03-05 22:17:15 +0100907 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100908 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return count;
910}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100911static ssize_t set_pwm_freq(struct device *dev,
912 struct device_attribute *attr, const char *buf, size_t count)
913{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200914 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100915 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100916 int i;
917
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100918 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100919 return -EINVAL;
920
Jean Delvaref8d0c192007-02-14 21:15:02 +0100921 /* Search for the nearest available frequency */
922 for (i = 0; i < 7; i++) {
923 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
924 break;
925 }
926
927 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200928 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100929 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200930 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100931 mutex_unlock(&data->update_lock);
932
933 return count;
934}
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100935static ssize_t show_pwm_temp_map(struct device *dev,
936 struct device_attribute *attr, char *buf)
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 = it87_update_device(dev);
942 int map;
943
944 if (data->pwm_temp_map[nr] < 3)
945 map = 1 << data->pwm_temp_map[nr];
946 else
947 map = 0; /* Should never happen */
948 return sprintf(buf, "%d\n", map);
949}
950static ssize_t set_pwm_temp_map(struct device *dev,
951 struct device_attribute *attr, const char *buf, size_t count)
952{
953 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
954 int nr = sensor_attr->index;
955
956 struct it87_data *data = dev_get_drvdata(dev);
957 long val;
958 u8 reg;
959
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800960 /*
961 * This check can go away if we ever support automatic fan speed
962 * control on newer chips.
963 */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100964 if (!has_old_autopwm(data)) {
965 dev_notice(dev, "Mapping change disabled for safety reasons\n");
966 return -EINVAL;
967 }
968
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100969 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100970 return -EINVAL;
971
972 switch (val) {
973 case (1 << 0):
974 reg = 0x00;
975 break;
976 case (1 << 1):
977 reg = 0x01;
978 break;
979 case (1 << 2):
980 reg = 0x02;
981 break;
982 default:
983 return -EINVAL;
984 }
985
986 mutex_lock(&data->update_lock);
987 data->pwm_temp_map[nr] = reg;
Guenter Roeck4a0d71c2012-01-19 11:02:18 -0800988 /*
989 * If we are in automatic mode, write the temp mapping immediately;
990 * otherwise, just store it for later use.
991 */
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100992 if (data->pwm_ctrl[nr] & 0x80) {
993 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
994 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
995 }
996 mutex_unlock(&data->update_lock);
997 return count;
998}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001000static ssize_t show_auto_pwm(struct device *dev,
1001 struct device_attribute *attr, char *buf)
1002{
1003 struct it87_data *data = it87_update_device(dev);
1004 struct sensor_device_attribute_2 *sensor_attr =
1005 to_sensor_dev_attr_2(attr);
1006 int nr = sensor_attr->nr;
1007 int point = sensor_attr->index;
1008
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001009 return sprintf(buf, "%d\n",
1010 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001011}
1012
1013static ssize_t set_auto_pwm(struct device *dev,
1014 struct device_attribute *attr, const char *buf, size_t count)
1015{
1016 struct it87_data *data = dev_get_drvdata(dev);
1017 struct sensor_device_attribute_2 *sensor_attr =
1018 to_sensor_dev_attr_2(attr);
1019 int nr = sensor_attr->nr;
1020 int point = sensor_attr->index;
1021 long val;
1022
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001023 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001024 return -EINVAL;
1025
1026 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001027 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001028 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1029 data->auto_pwm[nr][point]);
1030 mutex_unlock(&data->update_lock);
1031 return count;
1032}
1033
1034static ssize_t show_auto_temp(struct device *dev,
1035 struct device_attribute *attr, char *buf)
1036{
1037 struct it87_data *data = it87_update_device(dev);
1038 struct sensor_device_attribute_2 *sensor_attr =
1039 to_sensor_dev_attr_2(attr);
1040 int nr = sensor_attr->nr;
1041 int point = sensor_attr->index;
1042
1043 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1044}
1045
1046static ssize_t set_auto_temp(struct device *dev,
1047 struct device_attribute *attr, const char *buf, size_t count)
1048{
1049 struct it87_data *data = dev_get_drvdata(dev);
1050 struct sensor_device_attribute_2 *sensor_attr =
1051 to_sensor_dev_attr_2(attr);
1052 int nr = sensor_attr->nr;
1053 int point = sensor_attr->index;
1054 long val;
1055
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001056 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001057 return -EINVAL;
1058
1059 mutex_lock(&data->update_lock);
1060 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1061 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1062 data->auto_temp[nr][point]);
1063 mutex_unlock(&data->update_lock);
1064 return count;
1065}
1066
Jean Delvare20ad93d2005-06-05 11:53:25 +02001067#define show_fan_offset(offset) \
1068static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1069 show_fan, NULL, offset - 1); \
1070static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1071 show_fan_min, set_fan_min, offset - 1); \
1072static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1073 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075show_fan_offset(1);
1076show_fan_offset(2);
1077show_fan_offset(3);
1078
1079#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +02001080static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1081 show_pwm_enable, set_pwm_enable, offset - 1); \
1082static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +01001083 show_pwm, set_pwm, offset - 1); \
1084static DEVICE_ATTR(pwm##offset##_freq, \
1085 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001086 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1087static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001088 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1089 offset - 1); \
1090static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1091 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1092 offset - 1, 0); \
1093static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1094 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1095 offset - 1, 1); \
1096static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1097 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1098 offset - 1, 2); \
1099static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1100 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1101static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1102 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1103 offset - 1, 1); \
1104static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1105 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1106 offset - 1, 0); \
1107static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1108 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1109 offset - 1, 2); \
1110static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1111 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1112 offset - 1, 3); \
1113static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1114 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1115 offset - 1, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117show_pwm_offset(1);
1118show_pwm_offset(2);
1119show_pwm_offset(3);
1120
Jean Delvare17d648b2006-08-28 14:23:46 +02001121/* A different set of callbacks for 16-bit fans */
1122static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1123 char *buf)
1124{
1125 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1126 int nr = sensor_attr->index;
1127 struct it87_data *data = it87_update_device(dev);
1128 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1129}
1130
1131static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1132 char *buf)
1133{
1134 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1135 int nr = sensor_attr->index;
1136 struct it87_data *data = it87_update_device(dev);
1137 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1138}
1139
1140static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1141 const char *buf, size_t count)
1142{
1143 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1144 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001145 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001146 long val;
1147
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001148 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001149 return -EINVAL;
Jean Delvare17d648b2006-08-28 14:23:46 +02001150
1151 mutex_lock(&data->update_lock);
1152 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001153 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001154 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001155 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001156 data->fan_min[nr] >> 8);
1157 mutex_unlock(&data->update_lock);
1158 return count;
1159}
1160
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001161/*
1162 * We want to use the same sysfs file names as 8-bit fans, but we need
1163 * different variable names, so we have to use SENSOR_ATTR instead of
1164 * SENSOR_DEVICE_ATTR.
1165 */
Jean Delvare17d648b2006-08-28 14:23:46 +02001166#define show_fan16_offset(offset) \
1167static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1168 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1169 show_fan16, NULL, offset - 1); \
1170static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1171 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1172 show_fan16_min, set_fan16_min, offset - 1)
1173
1174show_fan16_offset(1);
1175show_fan16_offset(2);
1176show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001177show_fan16_offset(4);
1178show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +02001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001181static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1182 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001185 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
Jean Delvare1d66c642005-04-18 21:16:59 -07001187static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Jean Delvare0124dd72007-11-25 16:16:41 +01001189static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1190 char *buf)
1191{
1192 int bitnr = to_sensor_dev_attr(attr)->index;
1193 struct it87_data *data = it87_update_device(dev);
1194 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1195}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001196
1197static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1198 *attr, const char *buf, size_t count)
1199{
1200 struct it87_data *data = dev_get_drvdata(dev);
1201 long val;
1202 int config;
1203
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001204 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001205 return -EINVAL;
1206
1207 mutex_lock(&data->update_lock);
1208 config = it87_read_value(data, IT87_REG_CONFIG);
1209 if (config < 0) {
1210 count = config;
1211 } else {
1212 config |= 1 << 5;
1213 it87_write_value(data, IT87_REG_CONFIG, config);
1214 /* Invalidate cache to force re-read */
1215 data->valid = 0;
1216 }
1217 mutex_unlock(&data->update_lock);
1218
1219 return count;
1220}
1221
Jean Delvare0124dd72007-11-25 16:16:41 +01001222static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1223static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1224static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1225static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1226static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1227static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1228static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1229static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1230static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1231static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1232static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1233static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1234static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1235static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1236static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1237static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001238static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1239 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001240
Jean Delvared9b327c2010-03-05 22:17:17 +01001241static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1242 char *buf)
1243{
1244 int bitnr = to_sensor_dev_attr(attr)->index;
1245 struct it87_data *data = it87_update_device(dev);
1246 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1247}
1248static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1249 const char *buf, size_t count)
1250{
1251 int bitnr = to_sensor_dev_attr(attr)->index;
1252 struct it87_data *data = dev_get_drvdata(dev);
1253 long val;
1254
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001255 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001256 || (val != 0 && val != 1))
1257 return -EINVAL;
1258
1259 mutex_lock(&data->update_lock);
1260 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1261 if (val)
1262 data->beeps |= (1 << bitnr);
1263 else
1264 data->beeps &= ~(1 << bitnr);
1265 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1266 mutex_unlock(&data->update_lock);
1267 return count;
1268}
1269
1270static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1271 show_beep, set_beep, 1);
1272static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1273static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1274static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1275static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1276static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1277static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1278static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1279/* fanX_beep writability is set later */
1280static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1281static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1282static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1283static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1284static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1285static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1286 show_beep, set_beep, 2);
1287static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1288static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1289
Jean Delvare5f2dc792010-03-05 22:17:18 +01001290static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1291 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Jean Delvare90d66192007-10-08 18:24:35 +02001293 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001294 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001296static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1297 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001299 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001300 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001302 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001303 return -EINVAL;
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 data->vrm = val;
1306
1307 return count;
1308}
1309static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Jean Delvare5f2dc792010-03-05 22:17:18 +01001311static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1312 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 struct it87_data *data = it87_update_device(dev);
1315 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1316}
1317static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001318
Jean Delvare738e5e02010-08-14 21:08:50 +02001319static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1320 char *buf)
1321{
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001322 static const char * const labels[] = {
Jean Delvare738e5e02010-08-14 21:08:50 +02001323 "+5V",
1324 "5VSB",
1325 "Vbat",
1326 };
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001327 static const char * const labels_it8721[] = {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001328 "+3.3V",
1329 "3VSB",
1330 "Vbat",
1331 };
1332 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001333 int nr = to_sensor_dev_attr(attr)->index;
1334
Jean Delvare16b5dda2012-01-16 22:51:48 +01001335 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1336 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001337}
1338static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1339static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1340static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1341
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001342static ssize_t show_name(struct device *dev, struct device_attribute
1343 *devattr, char *buf)
1344{
1345 struct it87_data *data = dev_get_drvdata(dev);
1346 return sprintf(buf, "%s\n", data->name);
1347}
1348static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1349
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001350static struct attribute *it87_attributes_in[9][5] = {
1351{
Jean Delvare87808be2006-09-24 21:17:13 +02001352 &sensor_dev_attr_in0_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001353 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001354 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001355 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001356 NULL
1357}, {
1358 &sensor_dev_attr_in1_input.dev_attr.attr,
1359 &sensor_dev_attr_in1_min.dev_attr.attr,
1360 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001361 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001362 NULL
1363}, {
1364 &sensor_dev_attr_in2_input.dev_attr.attr,
1365 &sensor_dev_attr_in2_min.dev_attr.attr,
1366 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001367 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001368 NULL
1369}, {
1370 &sensor_dev_attr_in3_input.dev_attr.attr,
1371 &sensor_dev_attr_in3_min.dev_attr.attr,
1372 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001373 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001374 NULL
1375}, {
1376 &sensor_dev_attr_in4_input.dev_attr.attr,
1377 &sensor_dev_attr_in4_min.dev_attr.attr,
1378 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001379 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001380 NULL
1381}, {
1382 &sensor_dev_attr_in5_input.dev_attr.attr,
1383 &sensor_dev_attr_in5_min.dev_attr.attr,
1384 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001385 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001386 NULL
1387}, {
1388 &sensor_dev_attr_in6_input.dev_attr.attr,
1389 &sensor_dev_attr_in6_min.dev_attr.attr,
1390 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001391 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001392 NULL
1393}, {
1394 &sensor_dev_attr_in7_input.dev_attr.attr,
1395 &sensor_dev_attr_in7_min.dev_attr.attr,
1396 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001397 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001398 NULL
1399}, {
1400 &sensor_dev_attr_in8_input.dev_attr.attr,
1401 NULL
1402} };
Jean Delvare87808be2006-09-24 21:17:13 +02001403
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001404static const struct attribute_group it87_group_in[9] = {
1405 { .attrs = it87_attributes_in[0] },
1406 { .attrs = it87_attributes_in[1] },
1407 { .attrs = it87_attributes_in[2] },
1408 { .attrs = it87_attributes_in[3] },
1409 { .attrs = it87_attributes_in[4] },
1410 { .attrs = it87_attributes_in[5] },
1411 { .attrs = it87_attributes_in[6] },
1412 { .attrs = it87_attributes_in[7] },
1413 { .attrs = it87_attributes_in[8] },
1414};
1415
Guenter Roeck4573acb2012-03-26 16:17:41 -07001416static struct attribute *it87_attributes_temp[3][6] = {
1417{
Jean Delvare87808be2006-09-24 21:17:13 +02001418 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001419 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001420 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001421 &sensor_dev_attr_temp1_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001422 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001423 NULL
1424} , {
1425 &sensor_dev_attr_temp2_input.dev_attr.attr,
1426 &sensor_dev_attr_temp2_max.dev_attr.attr,
1427 &sensor_dev_attr_temp2_min.dev_attr.attr,
1428 &sensor_dev_attr_temp2_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001429 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001430 NULL
1431} , {
1432 &sensor_dev_attr_temp3_input.dev_attr.attr,
1433 &sensor_dev_attr_temp3_max.dev_attr.attr,
1434 &sensor_dev_attr_temp3_min.dev_attr.attr,
1435 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001436 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Guenter Roeck4573acb2012-03-26 16:17:41 -07001437 NULL
1438} };
Jean Delvare87808be2006-09-24 21:17:13 +02001439
Guenter Roeck4573acb2012-03-26 16:17:41 -07001440static const struct attribute_group it87_group_temp[3] = {
1441 { .attrs = it87_attributes_temp[0] },
1442 { .attrs = it87_attributes_temp[1] },
1443 { .attrs = it87_attributes_temp[2] },
1444};
1445
1446static struct attribute *it87_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001447 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001448 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001449 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001450 NULL
1451};
1452
1453static const struct attribute_group it87_group = {
1454 .attrs = it87_attributes,
1455};
1456
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001457static struct attribute *it87_attributes_in_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001458 &sensor_dev_attr_in0_beep.dev_attr.attr,
1459 &sensor_dev_attr_in1_beep.dev_attr.attr,
1460 &sensor_dev_attr_in2_beep.dev_attr.attr,
1461 &sensor_dev_attr_in3_beep.dev_attr.attr,
1462 &sensor_dev_attr_in4_beep.dev_attr.attr,
1463 &sensor_dev_attr_in5_beep.dev_attr.attr,
1464 &sensor_dev_attr_in6_beep.dev_attr.attr,
1465 &sensor_dev_attr_in7_beep.dev_attr.attr,
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001466 NULL
1467};
Jean Delvared9b327c2010-03-05 22:17:17 +01001468
Guenter Roeck4573acb2012-03-26 16:17:41 -07001469static struct attribute *it87_attributes_temp_beep[] = {
Jean Delvared9b327c2010-03-05 22:17:17 +01001470 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1471 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1472 &sensor_dev_attr_temp3_beep.dev_attr.attr,
Jean Delvared9b327c2010-03-05 22:17:17 +01001473};
1474
Jean Delvare723a0aa2010-03-05 22:17:16 +01001475static struct attribute *it87_attributes_fan16[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001476 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1477 &sensor_dev_attr_fan1_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001478 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1479 NULL
1480}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001481 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1482 &sensor_dev_attr_fan2_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001483 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1484 NULL
1485}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001486 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1487 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001488 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1489 NULL
1490}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001491 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1492 &sensor_dev_attr_fan4_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001493 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1494 NULL
1495}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001496 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1497 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001498 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1499 NULL
1500} };
Jean Delvare87808be2006-09-24 21:17:13 +02001501
Jean Delvare723a0aa2010-03-05 22:17:16 +01001502static const struct attribute_group it87_group_fan16[5] = {
1503 { .attrs = it87_attributes_fan16[0] },
1504 { .attrs = it87_attributes_fan16[1] },
1505 { .attrs = it87_attributes_fan16[2] },
1506 { .attrs = it87_attributes_fan16[3] },
1507 { .attrs = it87_attributes_fan16[4] },
1508};
1509
1510static struct attribute *it87_attributes_fan[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001511 &sensor_dev_attr_fan1_input.dev_attr.attr,
1512 &sensor_dev_attr_fan1_min.dev_attr.attr,
1513 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001514 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1515 NULL
1516}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001517 &sensor_dev_attr_fan2_input.dev_attr.attr,
1518 &sensor_dev_attr_fan2_min.dev_attr.attr,
1519 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001520 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1521 NULL
1522}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001523 &sensor_dev_attr_fan3_input.dev_attr.attr,
1524 &sensor_dev_attr_fan3_min.dev_attr.attr,
1525 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001526 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001527 NULL
1528} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001529
Jean Delvare723a0aa2010-03-05 22:17:16 +01001530static const struct attribute_group it87_group_fan[3] = {
1531 { .attrs = it87_attributes_fan[0] },
1532 { .attrs = it87_attributes_fan[1] },
1533 { .attrs = it87_attributes_fan[2] },
1534};
1535
1536static const struct attribute_group *
1537it87_get_fan_group(const struct it87_data *data)
1538{
1539 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1540}
1541
1542static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001543 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001544 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001545 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001546 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001547 NULL
1548}, {
1549 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1550 &sensor_dev_attr_pwm2.dev_attr.attr,
1551 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001552 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001553 NULL
1554}, {
1555 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1556 &sensor_dev_attr_pwm3.dev_attr.attr,
1557 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001558 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001559 NULL
1560} };
Jean Delvare87808be2006-09-24 21:17:13 +02001561
Jean Delvare723a0aa2010-03-05 22:17:16 +01001562static const struct attribute_group it87_group_pwm[3] = {
1563 { .attrs = it87_attributes_pwm[0] },
1564 { .attrs = it87_attributes_pwm[1] },
1565 { .attrs = it87_attributes_pwm[2] },
1566};
1567
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001568static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1569 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1570 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1571 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1572 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1573 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1574 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1575 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1576 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1577 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1578 NULL
1579}, {
1580 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1581 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1582 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1583 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1584 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1585 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1586 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1587 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1588 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1589 NULL
1590}, {
1591 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1592 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1593 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1594 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1595 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1596 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1597 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1598 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1599 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1600 NULL
1601} };
1602
1603static const struct attribute_group it87_group_autopwm[3] = {
1604 { .attrs = it87_attributes_autopwm[0] },
1605 { .attrs = it87_attributes_autopwm[1] },
1606 { .attrs = it87_attributes_autopwm[2] },
1607};
1608
Jean Delvared9b327c2010-03-05 22:17:17 +01001609static struct attribute *it87_attributes_fan_beep[] = {
1610 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1611 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1612 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1613 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1614 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1615};
1616
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001617static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001618 &dev_attr_vrm.attr,
1619 &dev_attr_cpu0_vid.attr,
1620 NULL
1621};
1622
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001623static const struct attribute_group it87_group_vid = {
1624 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001625};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Jean Delvare738e5e02010-08-14 21:08:50 +02001627static struct attribute *it87_attributes_label[] = {
1628 &sensor_dev_attr_in3_label.dev_attr.attr,
1629 &sensor_dev_attr_in7_label.dev_attr.attr,
1630 &sensor_dev_attr_in8_label.dev_attr.attr,
1631 NULL
1632};
1633
1634static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001635 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001636};
1637
Jean Delvare2d8672c2005-07-19 23:56:35 +02001638/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001639static int __init it87_find(unsigned short *address,
1640 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001642 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001643 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001644 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001646 err = superio_enter();
1647 if (err)
1648 return err;
1649
1650 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001651 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001652
1653 switch (chip_type) {
1654 case IT8705F_DEVID:
1655 sio_data->type = it87;
1656 break;
1657 case IT8712F_DEVID:
1658 sio_data->type = it8712;
1659 break;
1660 case IT8716F_DEVID:
1661 case IT8726F_DEVID:
1662 sio_data->type = it8716;
1663 break;
1664 case IT8718F_DEVID:
1665 sio_data->type = it8718;
1666 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001667 case IT8720F_DEVID:
1668 sio_data->type = it8720;
1669 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001670 case IT8721F_DEVID:
1671 sio_data->type = it8721;
1672 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001673 case IT8728F_DEVID:
1674 sio_data->type = it8728;
1675 break;
Guenter Roeck0531d982012-03-02 11:46:44 -08001676 case IT8782F_DEVID:
1677 sio_data->type = it8782;
1678 break;
1679 case IT8783E_DEVID:
1680 sio_data->type = it8783;
1681 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001682 case 0xffff: /* No device at all */
1683 goto exit;
1684 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001685 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001686 goto exit;
1687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Jean Delvare87673dd2006-08-28 14:37:19 +02001689 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001691 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 goto exit;
1693 }
1694
1695 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1696 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001697 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 goto exit;
1699 }
1700
1701 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001702 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Joe Perchesa8ca1032011-01-12 21:55:10 +01001703 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001704 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Jean Delvare738e5e02010-08-14 21:08:50 +02001706 /* in8 (Vbat) is always internal */
1707 sio_data->internal = (1 << 2);
1708
Jean Delvare87673dd2006-08-28 14:37:19 +02001709 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001710 if (sio_data->type == it87) {
1711 /* The IT8705F doesn't have VID pins at all */
1712 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001713
1714 /* The IT8705F has a different LD number for GPIO */
1715 superio_select(5);
1716 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Guenter Roeck0531d982012-03-02 11:46:44 -08001717 } else if (sio_data->type == it8783) {
1718 int reg25, reg27, reg2A, reg2C, regEF;
Guenter Roeck0531d982012-03-02 11:46:44 -08001719
1720 sio_data->skip_vid = 1; /* No VID */
1721
1722 superio_select(GPIO);
1723
1724 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1725 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1726 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1727 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1728 regEF = superio_inb(IT87_SIO_SPI_REG);
1729
Guenter Roeck0531d982012-03-02 11:46:44 -08001730 /* Check if fan3 is there or not */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001731 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
Guenter Roeck0531d982012-03-02 11:46:44 -08001732 sio_data->skip_fan |= (1 << 2);
1733 if ((reg25 & (1 << 4))
1734 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1735 sio_data->skip_pwm |= (1 << 2);
1736
1737 /* Check if fan2 is there or not */
1738 if (reg27 & (1 << 7))
1739 sio_data->skip_fan |= (1 << 1);
1740 if (reg27 & (1 << 3))
1741 sio_data->skip_pwm |= (1 << 1);
1742
1743 /* VIN5 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001744 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1745 sio_data->skip_in |= (1 << 5); /* No VIN5 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001746
1747 /* VIN6 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001748 if (reg27 & (1 << 1))
1749 sio_data->skip_in |= (1 << 6); /* No VIN6 */
Guenter Roeck0531d982012-03-02 11:46:44 -08001750
1751 /*
1752 * VIN7
1753 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1754 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001755 if (reg27 & (1 << 2)) {
1756 /*
1757 * The data sheet is a bit unclear regarding the
1758 * internal voltage divider for VCCH5V. It says
1759 * "This bit enables and switches VIN7 (pin 91) to the
1760 * internal voltage divider for VCCH5V".
1761 * This is different to other chips, where the internal
1762 * voltage divider would connect VIN7 to an internal
1763 * voltage source. Maybe that is the case here as well.
1764 *
1765 * Since we don't know for sure, re-route it if that is
1766 * not the case, and ask the user to report if the
1767 * resulting voltage is sane.
1768 */
1769 if (!(reg2C & (1 << 1))) {
1770 reg2C |= (1 << 1);
1771 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1772 pr_notice("Routing internal VCCH5V to in7.\n");
1773 }
1774 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1775 pr_notice("Please report if it displays a reasonable voltage.\n");
1776 }
Guenter Roeck0531d982012-03-02 11:46:44 -08001777
1778 if (reg2C & (1 << 0))
1779 sio_data->internal |= (1 << 0);
1780 if (reg2C & (1 << 1))
1781 sio_data->internal |= (1 << 1);
1782
1783 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1784
Jean Delvare895ff262009-12-09 20:35:47 +01001785 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001786 int reg;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001787 bool uart6;
Jean Delvare87673dd2006-08-28 14:37:19 +02001788
1789 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001790
Jean Delvare895ff262009-12-09 20:35:47 +01001791 reg = superio_inb(IT87_SIO_GPIO3_REG);
Guenter Roeck0531d982012-03-02 11:46:44 -08001792 if (sio_data->type == it8721 || sio_data->type == it8728 ||
1793 sio_data->type == it8782) {
Jean Delvare16b5dda2012-01-16 22:51:48 +01001794 /*
Guenter Roeck0531d982012-03-02 11:46:44 -08001795 * IT8721F/IT8758E, and IT8782F don't have VID pins
1796 * at all, not sure about the IT8728F.
Jean Delvare16b5dda2012-01-16 22:51:48 +01001797 */
Jean Delvare895ff262009-12-09 20:35:47 +01001798 sio_data->skip_vid = 1;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001799 } else {
1800 /* We need at least 4 VID pins */
1801 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001802 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001803 sio_data->skip_vid = 1;
1804 }
Jean Delvare895ff262009-12-09 20:35:47 +01001805 }
1806
Jean Delvare591ec652009-12-09 20:35:48 +01001807 /* Check if fan3 is there or not */
1808 if (reg & (1 << 6))
1809 sio_data->skip_pwm |= (1 << 2);
1810 if (reg & (1 << 7))
1811 sio_data->skip_fan |= (1 << 2);
1812
1813 /* Check if fan2 is there or not */
1814 reg = superio_inb(IT87_SIO_GPIO5_REG);
1815 if (reg & (1 << 1))
1816 sio_data->skip_pwm |= (1 << 1);
1817 if (reg & (1 << 2))
1818 sio_data->skip_fan |= (1 << 1);
1819
Jean Delvare895ff262009-12-09 20:35:47 +01001820 if ((sio_data->type == it8718 || sio_data->type == it8720)
1821 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001822 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001823
1824 reg = superio_inb(IT87_SIO_PINX2_REG);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001825
1826 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1827
Jean Delvare436cad22010-07-09 16:22:48 +02001828 /*
1829 * The IT8720F has no VIN7 pin, so VCCH should always be
1830 * routed internally to VIN7 with an internal divider.
1831 * Curiously, there still is a configuration bit to control
1832 * this, which means it can be set incorrectly. And even
1833 * more curiously, many boards out there are improperly
1834 * configured, even though the IT8720F datasheet claims
1835 * that the internal routing of VCCH to VIN7 is the default
1836 * setting. So we force the internal routing in this case.
Guenter Roeck0531d982012-03-02 11:46:44 -08001837 *
1838 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001839 * If UART6 is enabled, re-route VIN7 to the internal divider
1840 * if that is not already the case.
Jean Delvare436cad22010-07-09 16:22:48 +02001841 */
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001842 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
Jean Delvare436cad22010-07-09 16:22:48 +02001843 reg |= (1 << 1);
1844 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01001845 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02001846 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001847 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001848 sio_data->internal |= (1 << 0);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001849 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1850 sio_data->type == it8728)
Jean Delvare738e5e02010-08-14 21:08:50 +02001851 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01001852
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001853 /*
1854 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1855 * While VIN7 can be routed to the internal voltage divider,
1856 * VIN5 and VIN6 are not available if UART6 is enabled.
Guenter Roeck4573acb2012-03-26 16:17:41 -07001857 *
1858 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
1859 * is the temperature source. Since we can not read the
1860 * temperature source here, skip_temp is preliminary.
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001861 */
Guenter Roeck4573acb2012-03-26 16:17:41 -07001862 if (uart6) {
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001863 sio_data->skip_in |= (1 << 5) | (1 << 6);
Guenter Roeck4573acb2012-03-26 16:17:41 -07001864 sio_data->skip_temp |= (1 << 2);
1865 }
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001866
Jean Delvared9b327c2010-03-05 22:17:17 +01001867 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02001868 }
Jean Delvared9b327c2010-03-05 22:17:17 +01001869 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01001870 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02001871
Jean Delvare98dd22c2008-10-09 15:33:58 +02001872 /* Disable specific features based on DMI strings */
1873 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1874 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1875 if (board_vendor && board_name) {
1876 if (strcmp(board_vendor, "nVIDIA") == 0
1877 && strcmp(board_name, "FN68PT") == 0) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08001878 /*
1879 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1880 * connected to a fan, but to something else. One user
1881 * has reported instant system power-off when changing
1882 * the PWM2 duty cycle, so we disable it.
1883 * I use the board name string as the trigger in case
1884 * the same board is ever used in other systems.
1885 */
Joe Perchesa8ca1032011-01-12 21:55:10 +01001886 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02001887 sio_data->skip_pwm = (1 << 1);
1888 }
1889 }
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891exit:
1892 superio_exit();
1893 return err;
1894}
1895
Jean Delvare723a0aa2010-03-05 22:17:16 +01001896static void it87_remove_files(struct device *dev)
1897{
1898 struct it87_data *data = platform_get_drvdata(pdev);
1899 struct it87_sio_data *sio_data = dev->platform_data;
1900 const struct attribute_group *fan_group = it87_get_fan_group(data);
1901 int i;
1902
1903 sysfs_remove_group(&dev->kobj, &it87_group);
Guenter Roeck9172b5d2012-03-24 21:49:54 -07001904 for (i = 0; i < 9; i++) {
1905 if (sio_data->skip_in & (1 << i))
1906 continue;
1907 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
1908 if (it87_attributes_in_beep[i])
1909 sysfs_remove_file(&dev->kobj,
1910 it87_attributes_in_beep[i]);
1911 }
Guenter Roeck4573acb2012-03-26 16:17:41 -07001912 for (i = 0; i < 3; i++) {
1913 if (!(data->has_temp & (1 << i)))
1914 continue;
1915 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
1916 if (sio_data->beep_pin)
1917 sysfs_remove_file(&dev->kobj,
1918 it87_attributes_temp_beep[i]);
1919 }
Jean Delvare723a0aa2010-03-05 22:17:16 +01001920 for (i = 0; i < 5; i++) {
1921 if (!(data->has_fan & (1 << i)))
1922 continue;
1923 sysfs_remove_group(&dev->kobj, &fan_group[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01001924 if (sio_data->beep_pin)
1925 sysfs_remove_file(&dev->kobj,
1926 it87_attributes_fan_beep[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001927 }
1928 for (i = 0; i < 3; i++) {
1929 if (sio_data->skip_pwm & (1 << 0))
1930 continue;
1931 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001932 if (has_old_autopwm(data))
1933 sysfs_remove_group(&dev->kobj,
1934 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001935 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001936 if (!sio_data->skip_vid)
1937 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02001938 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001939}
1940
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001941static int it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001944 struct resource *res;
1945 struct device *dev = &pdev->dev;
1946 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001947 const struct attribute_group *fan_group;
1948 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01001950 int fan_beep_need_rw;
Guenter Roeck3c4c4972012-01-20 09:29:44 -08001951 static const char * const names[] = {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001952 "it87",
1953 "it8712",
1954 "it8716",
1955 "it8718",
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001956 "it8720",
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001957 "it8721",
Jean Delvare16b5dda2012-01-16 22:51:48 +01001958 "it8728",
Guenter Roeck0531d982012-03-02 11:46:44 -08001959 "it8782",
1960 "it8783",
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001961 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001963 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck62a1d052012-03-24 21:54:41 -07001964 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
1965 DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001966 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1967 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001968 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Guenter Roeck62a1d052012-03-24 21:54:41 -07001969 return -EBUSY;
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Guenter Roeck62a1d052012-03-24 21:54:41 -07001972 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
1973 if (!data)
1974 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001976 data->addr = res->start;
1977 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001978 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001979 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001982 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
Guenter Roeck62a1d052012-03-24 21:54:41 -07001983 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
1984 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001986 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001988 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001991 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001993 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001994 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001995 if (sio_data->internal & (1 << 0))
1996 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1997 if (sio_data->internal & (1 << 1))
1998 data->in_scaled |= (1 << 7); /* in7 is VSB */
1999 if (sio_data->internal & (1 << 2))
2000 data->in_scaled |= (1 << 8); /* in8 is Vbat */
Guenter Roeck0531d982012-03-02 11:46:44 -08002001 } else if (sio_data->type == it8782 || sio_data->type == it8783) {
2002 if (sio_data->internal & (1 << 0))
2003 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2004 if (sio_data->internal & (1 << 1))
2005 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002006 }
2007
Guenter Roeck4573acb2012-03-26 16:17:41 -07002008 data->has_temp = 0x07;
2009 if (sio_data->skip_temp & (1 << 2)) {
2010 if (sio_data->type == it8782
2011 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2012 data->has_temp &= ~(1 << 2);
2013 }
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002016 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002019 err = sysfs_create_group(&dev->kobj, &it87_group);
2020 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002021 return err;
Jean Delvare17d648b2006-08-28 14:23:46 +02002022
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002023 for (i = 0; i < 9; i++) {
2024 if (sio_data->skip_in & (1 << i))
2025 continue;
2026 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2027 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002028 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002029 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2030 err = sysfs_create_file(&dev->kobj,
2031 it87_attributes_in_beep[i]);
2032 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002033 goto error;
Guenter Roeck9172b5d2012-03-24 21:49:54 -07002034 }
2035 }
2036
Guenter Roeck4573acb2012-03-26 16:17:41 -07002037 for (i = 0; i < 3; i++) {
2038 if (!(data->has_temp & (1 << i)))
2039 continue;
2040 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01002041 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002042 goto error;
Guenter Roeck4573acb2012-03-26 16:17:41 -07002043 if (sio_data->beep_pin) {
2044 err = sysfs_create_file(&dev->kobj,
2045 it87_attributes_temp_beep[i]);
2046 if (err)
2047 goto error;
2048 }
Jean Delvared9b327c2010-03-05 22:17:17 +01002049 }
2050
Jean Delvare9060f8b2006-08-28 14:24:17 +02002051 /* Do not create fan files for disabled fans */
Jean Delvare723a0aa2010-03-05 22:17:16 +01002052 fan_group = it87_get_fan_group(data);
Jean Delvared9b327c2010-03-05 22:17:17 +01002053 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01002054 for (i = 0; i < 5; i++) {
2055 if (!(data->has_fan & (1 << i)))
2056 continue;
2057 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
2058 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002059 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002060
2061 if (sio_data->beep_pin) {
2062 err = sysfs_create_file(&dev->kobj,
2063 it87_attributes_fan_beep[i]);
2064 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002065 goto error;
Jean Delvared9b327c2010-03-05 22:17:17 +01002066 if (!fan_beep_need_rw)
2067 continue;
2068
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002069 /*
2070 * As we have a single beep enable bit for all fans,
Jean Delvared9b327c2010-03-05 22:17:17 +01002071 * only the first enabled fan has a writable attribute
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002072 * for it.
2073 */
Jean Delvared9b327c2010-03-05 22:17:17 +01002074 if (sysfs_chmod_file(&dev->kobj,
2075 it87_attributes_fan_beep[i],
2076 S_IRUGO | S_IWUSR))
2077 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2078 i + 1);
2079 fan_beep_need_rw = 0;
2080 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002081 }
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01002084 for (i = 0; i < 3; i++) {
2085 if (sio_data->skip_pwm & (1 << i))
2086 continue;
2087 err = sysfs_create_group(&dev->kobj,
2088 &it87_group_pwm[i]);
2089 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002090 goto error;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002091
2092 if (!has_old_autopwm(data))
2093 continue;
2094 err = sysfs_create_group(&dev->kobj,
2095 &it87_group_autopwm[i]);
2096 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002097 goto error;
Jean Delvare98dd22c2008-10-09 15:33:58 +02002098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
2100
Jean Delvare895ff262009-12-09 20:35:47 +01002101 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02002102 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02002103 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002104 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01002105 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2106 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002107 goto error;
Jean Delvare87808be2006-09-24 21:17:13 +02002108 }
2109
Jean Delvare738e5e02010-08-14 21:08:50 +02002110 /* Export labels for internal sensors */
2111 for (i = 0; i < 3; i++) {
2112 if (!(sio_data->internal & (1 << i)))
2113 continue;
2114 err = sysfs_create_file(&dev->kobj,
2115 it87_attributes_label[i]);
2116 if (err)
Guenter Roeck62a1d052012-03-24 21:54:41 -07002117 goto error;
Jean Delvare738e5e02010-08-14 21:08:50 +02002118 }
2119
Tony Jones1beeffe2007-08-20 13:46:20 -07002120 data->hwmon_dev = hwmon_device_register(dev);
2121 if (IS_ERR(data->hwmon_dev)) {
2122 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck62a1d052012-03-24 21:54:41 -07002123 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125
2126 return 0;
2127
Guenter Roeck62a1d052012-03-24 21:54:41 -07002128error:
Jean Delvare723a0aa2010-03-05 22:17:16 +01002129 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return err;
2131}
2132
Bill Pemberton281dfd02012-11-19 13:25:51 -05002133static int it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002135 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Tony Jones1beeffe2007-08-20 13:46:20 -07002137 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01002138 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return 0;
2141}
2142
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002143/*
2144 * Must be called with data->update_lock held, except during initialization.
2145 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2146 * would slow down the IT87 access and should not be necessary.
2147 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002148static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002150 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2151 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002154/*
2155 * Must be called with data->update_lock held, except during initialization.
2156 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2157 * would slow down the IT87 access and should not be necessary.
2158 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002159static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002161 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2162 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
2165/* Return 1 if and only if the PWM interface is safe to use */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002166static int it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002168 struct it87_data *data = dev_get_drvdata(dev);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002169 /*
2170 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 * and polarity set to active low is sign that this is the case so we
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002172 * disable pwm control to protect the user.
2173 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002174 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if ((tmp & 0x87) == 0) {
2176 if (fix_pwm_polarity) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002177 /*
2178 * The user asks us to attempt a chip reconfiguration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 * This means switching to active high polarity and
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002180 * inverting all fan speed values.
2181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 int i;
2183 u8 pwm[3];
2184
2185 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002186 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 IT87_REG_PWM(i));
2188
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002189 /*
2190 * If any fan is in automatic pwm mode, the polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 * might be correct, as suspicious as it seems, so we
2192 * better don't change anything (but still disable the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002193 * PWM interface).
2194 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002196 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002198 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 tmp | 0x87);
2200 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002201 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 IT87_REG_PWM(i),
2203 0x7f & ~pwm[i]);
2204 return 1;
2205 }
2206
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002207 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 "too broken to be fixed\n");
2209 }
2210
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002211 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 "defaults, disabling PWM interface\n");
2213 return 0;
2214 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002215 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 "sane, won't touch\n");
2217 }
2218
2219 return 1;
2220}
2221
2222/* Called when we have found a new IT87. */
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002223static void it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Jean Delvare591ec652009-12-09 20:35:48 +01002225 struct it87_sio_data *sio_data = pdev->dev.platform_data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002226 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002228 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002230 /*
2231 * For each PWM channel:
Jean Delvareb99883d2010-03-05 22:17:15 +01002232 * - If it is in automatic mode, setting to manual mode should set
2233 * the fan to full speed by default.
2234 * - If it is in manual mode, we need a mapping to temperature
2235 * channels to use when later setting to automatic mode later.
2236 * Use a 1:1 mapping by default (we are clueless.)
2237 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002238 * prior to switching to a different mode.
2239 * Note that this is no longer needed for the IT8721F and later, as
2240 * these have separate registers for the temperature mapping and the
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002241 * manual duty cycle.
2242 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002244 data->pwm_temp_map[i] = i;
2245 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002246 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002249 /*
2250 * Some chips seem to have default value 0xff for all limit
Jean Delvarec5df9b72006-08-28 14:37:54 +02002251 * registers. For low voltage limits it makes no sense and triggers
2252 * alarms, so change to 0 instead. For high temperature limits, it
2253 * means -1 degree C, which surprisingly doesn't trigger an alarm,
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002254 * but is still confusing, so change to 127 degrees C.
2255 */
Jean Delvarec5df9b72006-08-28 14:37:54 +02002256 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002257 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002258 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002259 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002260 }
2261 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002262 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002263 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002264 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002265 }
2266
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002267 /*
2268 * Temperature channels are not forcibly enabled, as they can be
Jean Delvarea00afb92010-04-14 16:14:09 +02002269 * set to two different sensor types and we can't guess which one
2270 * is correct for a given system. These channels can be enabled at
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002271 * run-time through the temp{1-3}_type sysfs accessors if needed.
2272 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002275 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if ((tmp & 0xff) == 0) {
2277 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002278 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280
2281 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002282 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002283 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002284 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002286 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002287 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2288 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002290 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Jean Delvare17d648b2006-08-28 14:23:46 +02002292 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02002293 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002294 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002295 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002296 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002297 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002298 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002299 tmp | 0x07);
2300 }
Guenter Roeck0531d982012-03-02 11:46:44 -08002301 /* IT8705F, IT8782F, and IT8783E/F only support three fans. */
2302 if (data->type != it87 && data->type != it8782 &&
2303 data->type != it8783) {
Andrew Paprocki816d8c62008-08-06 22:41:06 +02002304 if (tmp & (1 << 4))
2305 data->has_fan |= (1 << 3); /* fan4 enabled */
2306 if (tmp & (1 << 5))
2307 data->has_fan |= (1 << 4); /* fan5 enabled */
2308 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002309 }
2310
Jean Delvare591ec652009-12-09 20:35:48 +01002311 /* Fan input pins may be used for alternative functions */
2312 data->has_fan &= ~sio_data->skip_fan;
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002315 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare41002f82012-07-12 22:47:37 +02002316 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 | (update_vbat ? 0x41 : 0x01));
2318}
2319
Jean Delvareb99883d2010-03-05 22:17:15 +01002320static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2321{
2322 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002323 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002324 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002325 data->pwm_duty[nr] = it87_read_value(data,
2326 IT87_REG_PWM_DUTY(nr));
2327 } else {
2328 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2329 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2330 else /* Manual mode */
2331 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2332 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002333
2334 if (has_old_autopwm(data)) {
2335 int i;
2336
2337 for (i = 0; i < 5 ; i++)
2338 data->auto_temp[nr][i] = it87_read_value(data,
2339 IT87_REG_AUTO_TEMP(nr, i));
2340 for (i = 0; i < 3 ; i++)
2341 data->auto_pwm[nr][i] = it87_read_value(data,
2342 IT87_REG_AUTO_PWM(nr, i));
2343 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002344}
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346static struct it87_data *it87_update_device(struct device *dev)
2347{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002348 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 int i;
2350
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002351 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2354 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (update_vbat) {
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002356 /*
2357 * Cleared after each update, so reenable. Value
2358 * returned by this read will be previous value
2359 */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002360 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002361 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363 for (i = 0; i <= 7; i++) {
2364 data->in[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002365 it87_read_value(data, IT87_REG_VIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 data->in_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002367 it87_read_value(data, IT87_REG_VIN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 data->in_max[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002369 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
Jean Delvare3543a532006-08-28 14:27:25 +02002371 /* in8 (battery) has no limit registers */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002372 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Jean Delvarec7f1f712007-09-03 17:11:46 +02002374 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002375 /* Skip disabled fans */
2376 if (!(data->has_fan & (1 << i)))
2377 continue;
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 data->fan_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002380 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002381 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002382 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002383 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002384 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002385 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002386 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002387 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002388 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
2391 for (i = 0; i < 3; i++) {
Guenter Roeck4573acb2012-03-26 16:17:41 -07002392 if (!(data->has_temp & (1 << i)))
2393 continue;
Guenter Roeck60ca3852012-12-19 22:17:00 +01002394 data->temp[i][0] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002395 it87_read_value(data, IT87_REG_TEMP(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002396 data->temp[i][1] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002397 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Guenter Roeck60ca3852012-12-19 22:17:00 +01002398 data->temp[i][2] =
2399 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401
Jean Delvare17d648b2006-08-28 14:23:46 +02002402 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002403 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002404 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002405 data->fan_div[0] = i & 0x07;
2406 data->fan_div[1] = (i >> 3) & 0x07;
2407 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002411 it87_read_value(data, IT87_REG_ALARM1) |
2412 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2413 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002414 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002415
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002416 data->fan_main_ctrl = it87_read_value(data,
2417 IT87_REG_FAN_MAIN_CTRL);
2418 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002419 for (i = 0; i < 3; i++)
2420 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002422 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002423 /*
2424 * The IT8705F does not have VID capability.
2425 * The IT8718F and later don't use IT87_REG_VID for the
2426 * same purpose.
2427 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002428 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002429 data->vid = it87_read_value(data, IT87_REG_VID);
Guenter Roeck4a0d71c2012-01-19 11:02:18 -08002430 /*
2431 * The older IT8712F revisions had only 5 VID pins,
2432 * but we assume it is always safe to read 6 bits.
2433 */
Jean Delvare17d648b2006-08-28 14:23:46 +02002434 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
2436 data->last_updated = jiffies;
2437 data->valid = 1;
2438 }
2439
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002440 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 return data;
2443}
2444
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002445static int __init it87_device_add(unsigned short address,
2446 const struct it87_sio_data *sio_data)
2447{
2448 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002449 .start = address + IT87_EC_OFFSET,
2450 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002451 .name = DRVNAME,
2452 .flags = IORESOURCE_IO,
2453 };
2454 int err;
2455
Jean Delvareb9acb642009-01-07 16:37:35 +01002456 err = acpi_check_resource_conflict(&res);
2457 if (err)
2458 goto exit;
2459
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002460 pdev = platform_device_alloc(DRVNAME, address);
2461 if (!pdev) {
2462 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002463 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002464 goto exit;
2465 }
2466
2467 err = platform_device_add_resources(pdev, &res, 1);
2468 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002469 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002470 goto exit_device_put;
2471 }
2472
2473 err = platform_device_add_data(pdev, sio_data,
2474 sizeof(struct it87_sio_data));
2475 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002476 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002477 goto exit_device_put;
2478 }
2479
2480 err = platform_device_add(pdev);
2481 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002482 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002483 goto exit_device_put;
2484 }
2485
2486 return 0;
2487
2488exit_device_put:
2489 platform_device_put(pdev);
2490exit:
2491 return err;
2492}
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494static int __init sm_it87_init(void)
2495{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002496 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002497 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002498 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002499
Jean Delvare98dd22c2008-10-09 15:33:58 +02002500 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002501 err = it87_find(&isa_address, &sio_data);
2502 if (err)
2503 return err;
2504 err = platform_driver_register(&it87_driver);
2505 if (err)
2506 return err;
2507
2508 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002509 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002510 platform_driver_unregister(&it87_driver);
2511 return err;
2512 }
2513
2514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
2516
2517static void __exit sm_it87_exit(void)
2518{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002519 platform_device_unregister(pdev);
2520 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
2523
Jean Delvaref1d8e332007-11-25 16:14:44 +01002524MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02002525 "Jean Delvare <khali@linux-fr.org>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002526MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527module_param(update_vbat, bool, 0);
2528MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2529module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002530MODULE_PARM_DESC(fix_pwm_polarity,
2531 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532MODULE_LICENSE("GPL");
2533
2534module_init(sm_it87_init);
2535module_exit(sm_it87_exit);