blob: 0054d6f9cec95e4645608c7fec6fde4f646344ac [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
Jean Delvare5f2dc792010-03-05 22:17:18 +010022 * Sis950 A clone of the IT8705F
23 *
24 * Copyright (C) 2001 Chris Gauthron
25 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
26 *
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
31 *
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
40 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Joe Perchesa8ca1032011-01-12 21:55:10 +010042#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/module.h>
45#include <linux/init.h>
46#include <linux/slab.h>
47#include <linux/jiffies.h>
corentin.labbeb74f3fd2007-06-13 20:27:36 +020048#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040049#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020050#include <linux/hwmon-sysfs.h>
51#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040052#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010053#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020054#include <linux/sysfs.h>
Jean Delvare98dd22c2008-10-09 15:33:58 +020055#include <linux/string.h>
56#include <linux/dmi.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010057#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020058#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
corentin.labbeb74f3fd2007-06-13 20:27:36 +020060#define DRVNAME "it87"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Jean Delvare16b5dda2012-01-16 22:51:48 +010062enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jean Delvare67b671b2007-12-06 23:13:42 +010064static unsigned short force_id;
65module_param(force_id, ushort, 0);
66MODULE_PARM_DESC(force_id, "Override the detected device ID");
67
corentin.labbeb74f3fd2007-06-13 20:27:36 +020068static struct platform_device *pdev;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define REG 0x2e /* The register to read/write */
71#define DEV 0x07 /* Register: Logical device select */
72#define VAL 0x2f /* The value to read/write */
73#define PME 0x04 /* The device with the fan registers in it */
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +010074
75/* The device with the IT8718F/IT8720F VID value in it */
76#define GPIO 0x07
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define DEVID 0x20 /* Register: Device ID */
79#define DEVREV 0x22 /* Register: Device Revision */
80
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020081static inline int superio_inb(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 outb(reg, REG);
84 return inb(VAL);
85}
86
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +020087static inline void superio_outb(int reg, int val)
Jean Delvare436cad22010-07-09 16:22:48 +020088{
89 outb(reg, REG);
90 outb(val, VAL);
91}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int superio_inw(int reg)
94{
95 int val;
96 outb(reg++, REG);
97 val = inb(VAL) << 8;
98 outb(reg, REG);
99 val |= inb(VAL);
100 return val;
101}
102
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200103static inline void superio_select(int ldn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 outb(DEV, REG);
Jean Delvare87673dd2006-08-28 14:37:19 +0200106 outb(ldn, VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200109static inline int superio_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200111 /*
112 * Try to reserve REG and REG + 1 for exclusive access.
113 */
114 if (!request_muxed_region(REG, 2, DRVNAME))
115 return -EBUSY;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 outb(0x87, REG);
118 outb(0x01, REG);
119 outb(0x55, REG);
120 outb(0x55, REG);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200124static inline void superio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 outb(0x02, REG);
127 outb(0x02, VAL);
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +0200128 release_region(REG, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Jean Delvare87673dd2006-08-28 14:37:19 +0200131/* Logical device 4 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#define IT8712F_DEVID 0x8712
133#define IT8705F_DEVID 0x8705
Jean Delvare17d648b2006-08-28 14:23:46 +0200134#define IT8716F_DEVID 0x8716
Jean Delvare87673dd2006-08-28 14:37:19 +0200135#define IT8718F_DEVID 0x8718
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100136#define IT8720F_DEVID 0x8720
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200137#define IT8721F_DEVID 0x8721
Rudolf Marek08a8f6e2007-06-09 10:11:16 -0400138#define IT8726F_DEVID 0x8726
Jean Delvare16b5dda2012-01-16 22:51:48 +0100139#define IT8728F_DEVID 0x8728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#define IT87_ACT_REG 0x30
141#define IT87_BASE_REG 0x60
142
Jean Delvare87673dd2006-08-28 14:37:19 +0200143/* Logical device 7 registers (IT8712F and later) */
Jean Delvare895ff262009-12-09 20:35:47 +0100144#define IT87_SIO_GPIO3_REG 0x27
Jean Delvare591ec652009-12-09 20:35:48 +0100145#define IT87_SIO_GPIO5_REG 0x29
Jean Delvare87673dd2006-08-28 14:37:19 +0200146#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
147#define IT87_SIO_VID_REG 0xfc /* VID value */
Jean Delvared9b327c2010-03-05 22:17:17 +0100148#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
Jean Delvare87673dd2006-08-28 14:37:19 +0200149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* Update battery voltage after every reading if true */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030151static bool update_vbat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/* Not all BIOSes properly configure the PWM registers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030154static bool fix_pwm_polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* Many IT87 constants specified below */
157
158/* Length of ISA address segment */
159#define IT87_EXTENT 8
160
Bjorn Helgaas87b4b662008-01-22 07:21:03 -0500161/* Length of ISA address segment for Environmental Controller */
162#define IT87_EC_EXTENT 2
163
164/* Offset of EC registers from ISA base address */
165#define IT87_EC_OFFSET 5
166
167/* Where are the ISA address/data registers relative to the EC base address */
168#define IT87_ADDR_REG_OFFSET 0
169#define IT87_DATA_REG_OFFSET 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/*----- The IT87 registers -----*/
172
173#define IT87_REG_CONFIG 0x00
174
175#define IT87_REG_ALARM1 0x01
176#define IT87_REG_ALARM2 0x02
177#define IT87_REG_ALARM3 0x03
178
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100179/* The IT8718F and IT8720F have the VID value in a different register, in
180 Super-I/O configuration space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#define IT87_REG_VID 0x0a
Andrew Paprocki04751692008-08-06 22:41:06 +0200182/* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
183 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
184 mode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#define IT87_REG_FAN_DIV 0x0b
Jean Delvare17d648b2006-08-28 14:23:46 +0200186#define IT87_REG_FAN_16BIT 0x0c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
189
Jean Delvarec7f1f712007-09-03 17:11:46 +0200190static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
191static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
192static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
193static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#define IT87_REG_FAN_MAIN_CTRL 0x13
195#define IT87_REG_FAN_CTL 0x14
196#define IT87_REG_PWM(nr) (0x15 + (nr))
Jean Delvare6229cdb2010-12-08 16:27:22 +0100197#define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199#define IT87_REG_VIN(nr) (0x20 + (nr))
200#define IT87_REG_TEMP(nr) (0x29 + (nr))
201
202#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
203#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
204#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
205#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207#define IT87_REG_VIN_ENABLE 0x50
208#define IT87_REG_TEMP_ENABLE 0x51
Jean Delvared9b327c2010-03-05 22:17:17 +0100209#define IT87_REG_BEEP_ENABLE 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211#define IT87_REG_CHIPID 0x58
212
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100213#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
214#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200217struct it87_sio_data {
218 enum chips type;
219 /* Values read from Super-I/O config space */
Andrew Paprocki04751692008-08-06 22:41:06 +0200220 u8 revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200221 u8 vid_value;
Jean Delvared9b327c2010-03-05 22:17:17 +0100222 u8 beep_pin;
Jean Delvare738e5e02010-08-14 21:08:50 +0200223 u8 internal; /* Internal sensors can be labeled */
Jean Delvare591ec652009-12-09 20:35:48 +0100224 /* Features skipped based on config or DMI */
Jean Delvare895ff262009-12-09 20:35:47 +0100225 u8 skip_vid;
Jean Delvare591ec652009-12-09 20:35:48 +0100226 u8 skip_fan;
Jean Delvare98dd22c2008-10-09 15:33:58 +0200227 u8 skip_pwm;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200228};
229
Jean Delvareed6bafb2007-02-14 21:15:03 +0100230/* For each registered chip, we need to keep some data in memory.
231 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232struct it87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700233 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 enum chips type;
Andrew Paprocki04751692008-08-06 22:41:06 +0200235 u8 revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200237 unsigned short addr;
238 const char *name;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100239 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 char valid; /* !=0 if following fields are valid */
241 unsigned long last_updated; /* In jiffies */
242
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200243 u16 in_scaled; /* Internal voltage sensors are scaled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 u8 in[9]; /* Register value */
Jean Delvare3543a532006-08-28 14:27:25 +0200245 u8 in_max[8]; /* Register value */
246 u8 in_min[8]; /* Register value */
Jean Delvare9060f8b2006-08-28 14:24:17 +0200247 u8 has_fan; /* Bitfield, fans enabled */
Jean Delvarec7f1f712007-09-03 17:11:46 +0200248 u16 fan[5]; /* Register values, possibly combined */
249 u16 fan_min[5]; /* Register values, possibly combined */
Jean Delvaree267d252009-03-12 13:36:39 +0100250 s8 temp[3]; /* Register value */
251 s8 temp_high[3]; /* Register value */
252 s8 temp_low[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 u8 sensor; /* Register value */
254 u8 fan_div[3]; /* Register encoding, shifted right */
255 u8 vid; /* Register encoding, combined */
Jean Delvarea7be58a2005-12-18 16:40:14 +0100256 u8 vrm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 u32 alarms; /* Register encoding, combined */
Jean Delvared9b327c2010-03-05 22:17:17 +0100258 u8 beeps; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 u8 fan_main_ctrl; /* Register value */
Jean Delvaref8d0c192007-02-14 21:15:02 +0100260 u8 fan_ctl; /* Register value */
Jean Delvareb99883d2010-03-05 22:17:15 +0100261
Jean Delvare6229cdb2010-12-08 16:27:22 +0100262 /* The following 3 arrays correspond to the same registers up to
263 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
264 * 7, and we want to preserve settings on mode changes, so we have
265 * to track all values separately.
266 * Starting with the IT8721F, the manual PWM duty cycles are stored
267 * in separate registers (8-bit values), so the separate tracking
268 * is no longer needed, but it is still done to keep the driver
269 * simple. */
Jean Delvareb99883d2010-03-05 22:17:15 +0100270 u8 pwm_ctrl[3]; /* Register value */
Jean Delvare6229cdb2010-12-08 16:27:22 +0100271 u8 pwm_duty[3]; /* Manual PWM value set by user */
Jean Delvareb99883d2010-03-05 22:17:15 +0100272 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100273
274 /* Automatic fan speed control registers */
275 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
276 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277};
278
Jean Delvare16b5dda2012-01-16 22:51:48 +0100279static inline int has_12mv_adc(const struct it87_data *data)
280{
281 /*
282 * IT8721F and later have a 12 mV ADC, also with internal scaling
283 * on selected inputs.
284 */
285 return data->type == it8721
286 || data->type == it8728;
287}
288
289static inline int has_newer_autopwm(const struct it87_data *data)
290{
291 /*
292 * IT8721F and later have separate registers for the temperature
293 * mapping and the manual duty cycle.
294 */
295 return data->type == it8721
296 || data->type == it8728;
297}
298
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200299static u8 in_to_reg(const struct it87_data *data, int nr, long val)
300{
301 long lsb;
302
Jean Delvare16b5dda2012-01-16 22:51:48 +0100303 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200304 if (data->in_scaled & (1 << nr))
305 lsb = 24;
306 else
307 lsb = 12;
308 } else
309 lsb = 16;
310
311 val = DIV_ROUND_CLOSEST(val, lsb);
312 return SENSORS_LIMIT(val, 0, 255);
313}
314
315static int in_from_reg(const struct it87_data *data, int nr, int val)
316{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100317 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200318 if (data->in_scaled & (1 << nr))
319 return val * 24;
320 else
321 return val * 12;
322 } else
323 return val * 16;
324}
Jean Delvare0df64542010-10-28 20:31:51 +0200325
326static inline u8 FAN_TO_REG(long rpm, int div)
327{
328 if (rpm == 0)
329 return 255;
330 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
331 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
332 254);
333}
334
335static inline u16 FAN16_TO_REG(long rpm)
336{
337 if (rpm == 0)
338 return 0xffff;
339 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
340}
341
342#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
343 1350000 / ((val) * (div)))
344/* The divider is fixed to 2 in 16-bit mode */
345#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
346 1350000 / ((val) * 2))
347
348#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
349 ((val) + 500) / 1000), -128, 127))
350#define TEMP_FROM_REG(val) ((val) * 1000)
351
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200352static u8 pwm_to_reg(const struct it87_data *data, long val)
353{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100354 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200355 return val;
356 else
357 return val >> 1;
358}
359
360static int pwm_from_reg(const struct it87_data *data, u8 reg)
361{
Jean Delvare16b5dda2012-01-16 22:51:48 +0100362 if (has_newer_autopwm(data))
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200363 return reg;
364 else
365 return (reg & 0x7f) << 1;
366}
367
Jean Delvare0df64542010-10-28 20:31:51 +0200368
369static int DIV_TO_REG(int val)
370{
371 int answer = 0;
372 while (answer < 7 && (val >>= 1))
373 answer++;
374 return answer;
375}
376#define DIV_FROM_REG(val) (1 << (val))
377
378static const unsigned int pwm_freq[8] = {
379 48000000 / 128,
380 24000000 / 128,
381 12000000 / 128,
382 8000000 / 128,
383 6000000 / 128,
384 3000000 / 128,
385 1500000 / 128,
386 750000 / 128,
387};
388
Andrew Paprocki04751692008-08-06 22:41:06 +0200389static inline int has_16bit_fans(const struct it87_data *data)
390{
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200391 /* IT8705F Datasheet 0.4.1, 3h == Version G.
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200392 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
Andrew Paprocki816d8c62008-08-06 22:41:06 +0200393 These are the first revisions with 16bit tachometer support. */
394 return (data->type == it87 && data->revision >= 0x03)
Andrew Paprocki859b9ef2008-09-20 10:25:19 +0200395 || (data->type == it8712 && data->revision >= 0x08)
Andrew Paprocki04751692008-08-06 22:41:06 +0200396 || data->type == it8716
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +0100397 || data->type == it8718
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200398 || data->type == it8720
Jean Delvare16b5dda2012-01-16 22:51:48 +0100399 || data->type == it8721
400 || data->type == it8728;
Andrew Paprocki04751692008-08-06 22:41:06 +0200401}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100403static inline int has_old_autopwm(const struct it87_data *data)
404{
405 /* The old automatic fan speed control interface is implemented
406 by IT8705F chips up to revision F and IT8712F chips up to
407 revision G. */
408 return (data->type == it87 && data->revision < 0x03)
409 || (data->type == it8712 && data->revision < 0x08);
410}
411
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200412static int it87_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200413static int __devexit it87_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200415static int it87_read_value(struct it87_data *data, u8 reg);
416static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static struct it87_data *it87_update_device(struct device *dev);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200418static int it87_check_pwm(struct device *dev);
419static void it87_init_device(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200422static struct platform_driver it87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100423 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200424 .owner = THIS_MODULE,
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200425 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100426 },
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200427 .probe = it87_probe,
428 .remove = __devexit_p(it87_remove),
Jean Delvarefde09502005-07-19 23:51:07 +0200429};
430
Jean Delvare20ad93d2005-06-05 11:53:25 +0200431static ssize_t show_in(struct device *dev, struct device_attribute *attr,
432 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200434 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
435 int nr = sensor_attr->index;
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200438 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Jean Delvare20ad93d2005-06-05 11:53:25 +0200441static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
442 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200444 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
445 int nr = sensor_attr->index;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200448 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Jean Delvare20ad93d2005-06-05 11:53:25 +0200451static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
452 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200454 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
455 int nr = sensor_attr->index;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200458 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Jean Delvare20ad93d2005-06-05 11:53:25 +0200461static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
462 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200464 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
465 int nr = sensor_attr->index;
466
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200467 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100468 unsigned long val;
469
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100470 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100471 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100473 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200474 data->in_min[nr] = in_to_reg(data, nr, val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200475 it87_write_value(data, IT87_REG_VIN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100477 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return count;
479}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200480static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
481 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200483 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
484 int nr = sensor_attr->index;
485
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200486 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100487 unsigned long val;
488
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100489 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100492 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200493 data->in_max[nr] = in_to_reg(data, nr, val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200494 it87_write_value(data, IT87_REG_VIN_MAX(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100496 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return count;
498}
499
500#define show_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200501static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
502 show_in, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504#define limit_in_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200505static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
506 show_in_min, set_in_min, offset); \
507static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
508 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510show_in_offset(0);
511limit_in_offset(0);
512show_in_offset(1);
513limit_in_offset(1);
514show_in_offset(2);
515limit_in_offset(2);
516show_in_offset(3);
517limit_in_offset(3);
518show_in_offset(4);
519limit_in_offset(4);
520show_in_offset(5);
521limit_in_offset(5);
522show_in_offset(6);
523limit_in_offset(6);
524show_in_offset(7);
525limit_in_offset(7);
526show_in_offset(8);
527
528/* 3 temperatures */
Jean Delvare20ad93d2005-06-05 11:53:25 +0200529static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
530 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200532 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
533 int nr = sensor_attr->index;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 struct it87_data *data = it87_update_device(dev);
536 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
537}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200538static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
539 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200541 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
542 int nr = sensor_attr->index;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct it87_data *data = it87_update_device(dev);
545 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
546}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200547static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
548 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200550 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
551 int nr = sensor_attr->index;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct it87_data *data = it87_update_device(dev);
554 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
555}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200556static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
557 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200559 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
560 int nr = sensor_attr->index;
561
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 data->temp_high[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200570 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100571 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return count;
573}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200574static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
575 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200577 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
578 int nr = sensor_attr->index;
579
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200580 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100581 long val;
582
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100583 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100584 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100586 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 data->temp_low[nr] = TEMP_TO_REG(val);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200588 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100589 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return count;
591}
592#define show_temp_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200593static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
594 show_temp, NULL, offset - 1); \
595static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
596 show_temp_max, set_temp_max, offset - 1); \
597static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
598 show_temp_min, set_temp_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600show_temp_offset(1);
601show_temp_offset(2);
602show_temp_offset(3);
603
Jean Delvare20ad93d2005-06-05 11:53:25 +0200604static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
605 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200607 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
608 int nr = sensor_attr->index;
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100611 u8 reg = data->sensor; /* In case the value is updated while
612 we use it */
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (reg & (1 << nr))
615 return sprintf(buf, "3\n"); /* thermal diode */
616 if (reg & (8 << nr))
Jean Delvare4ed10772008-10-17 17:51:16 +0200617 return sprintf(buf, "4\n"); /* thermistor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return sprintf(buf, "0\n"); /* disabled */
619}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200620static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
621 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200623 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
624 int nr = sensor_attr->index;
625
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200626 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100627 long val;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200628 u8 reg;
Jean Delvaref5f64502010-03-05 22:17:19 +0100629
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100630 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100631 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jean Delvare8acf07c2010-04-14 16:14:09 +0200633 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
634 reg &= ~(1 << nr);
635 reg &= ~(8 << nr);
Jean Delvare4ed10772008-10-17 17:51:16 +0200636 if (val == 2) { /* backwards compatibility */
637 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
638 "instead\n");
639 val = 4;
640 }
641 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (val == 3)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200643 reg |= 1 << nr;
Jean Delvare4ed10772008-10-17 17:51:16 +0200644 else if (val == 4)
Jean Delvare8acf07c2010-04-14 16:14:09 +0200645 reg |= 8 << nr;
646 else if (val != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return -EINVAL;
Jean Delvare8acf07c2010-04-14 16:14:09 +0200648
649 mutex_lock(&data->update_lock);
650 data->sensor = reg;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200651 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
Jean Delvare2b3d1d82010-04-14 16:14:10 +0200652 data->valid = 0; /* Force cache refresh */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100653 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return count;
655}
656#define show_sensor_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +0200657static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
658 show_sensor, set_sensor, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660show_sensor_offset(1);
661show_sensor_offset(2);
662show_sensor_offset(3);
663
664/* 3 Fans */
Jean Delvareb99883d2010-03-05 22:17:15 +0100665
666static int pwm_mode(const struct it87_data *data, int nr)
667{
668 int ctrl = data->fan_main_ctrl & (1 << nr);
669
670 if (ctrl == 0) /* Full speed */
671 return 0;
672 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
673 return 2;
674 else /* Manual mode */
675 return 1;
676}
677
Jean Delvare20ad93d2005-06-05 11:53:25 +0200678static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
679 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200681 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
682 int nr = sensor_attr->index;
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100685 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 DIV_FROM_REG(data->fan_div[nr])));
687}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200688static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
689 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200691 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
692 int nr = sensor_attr->index;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct it87_data *data = it87_update_device(dev);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100695 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
696 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200698static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
699 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200701 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
702 int nr = sensor_attr->index;
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct it87_data *data = it87_update_device(dev);
705 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
706}
Jean Delvare5f2dc792010-03-05 22:17:18 +0100707static ssize_t show_pwm_enable(struct device *dev,
708 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200710 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
711 int nr = sensor_attr->index;
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct it87_data *data = it87_update_device(dev);
Jean Delvareb99883d2010-03-05 22:17:15 +0100714 return sprintf(buf, "%d\n", pwm_mode(data, nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200716static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
717 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200719 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
720 int nr = sensor_attr->index;
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct it87_data *data = it87_update_device(dev);
Jean Delvare44c1bcd2010-10-28 20:31:51 +0200723 return sprintf(buf, "%d\n",
724 pwm_from_reg(data, data->pwm_duty[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100726static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
727 char *buf)
728{
729 struct it87_data *data = it87_update_device(dev);
730 int index = (data->fan_ctl >> 4) & 0x07;
731
732 return sprintf(buf, "%u\n", pwm_freq[index]);
733}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200734static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
735 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200737 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
738 int nr = sensor_attr->index;
739
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200740 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100741 long val;
Jean Delvare7f999aa2007-02-14 21:15:03 +0100742 u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100744 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100745 return -EINVAL;
746
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100747 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200748 reg = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare07eab462005-11-23 15:44:31 -0800749 switch (nr) {
Jean Delvare5f2dc792010-03-05 22:17:18 +0100750 case 0:
751 data->fan_div[nr] = reg & 0x07;
752 break;
753 case 1:
754 data->fan_div[nr] = (reg >> 3) & 0x07;
755 break;
756 case 2:
757 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
758 break;
Jean Delvare07eab462005-11-23 15:44:31 -0800759 }
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200762 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100763 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return count;
765}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200766static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
767 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200769 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
770 int nr = sensor_attr->index;
771
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200772 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100773 unsigned long val;
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200774 int min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 u8 old;
776
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100777 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100778 return -EINVAL;
779
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100780 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200781 old = it87_read_value(data, IT87_REG_FAN_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200783 /* Save fan min limit */
784 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 switch (nr) {
787 case 0:
788 case 1:
789 data->fan_div[nr] = DIV_TO_REG(val);
790 break;
791 case 2:
792 if (val < 8)
793 data->fan_div[nr] = 1;
794 else
795 data->fan_div[nr] = 3;
796 }
797 val = old & 0x80;
798 val |= (data->fan_div[0] & 0x07);
799 val |= (data->fan_div[1] & 0x07) << 3;
800 if (data->fan_div[2] == 3)
801 val |= 0x1 << 6;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200802 it87_write_value(data, IT87_REG_FAN_DIV, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200804 /* Restore fan min limit */
805 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec7f1f712007-09-03 17:11:46 +0200806 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
Jean Delvare8ab4ec32006-08-28 14:35:46 +0200807
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100808 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return count;
810}
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100811
812/* Returns 0 if OK, -EINVAL otherwise */
813static int check_trip_points(struct device *dev, int nr)
814{
815 const struct it87_data *data = dev_get_drvdata(dev);
816 int i, err = 0;
817
818 if (has_old_autopwm(data)) {
819 for (i = 0; i < 3; i++) {
820 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
821 err = -EINVAL;
822 }
823 for (i = 0; i < 2; i++) {
824 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
825 err = -EINVAL;
826 }
827 }
828
829 if (err) {
830 dev_err(dev, "Inconsistent trip points, not switching to "
831 "automatic mode\n");
832 dev_err(dev, "Adjust the trip points and try again\n");
833 }
834 return err;
835}
836
Jean Delvare20ad93d2005-06-05 11:53:25 +0200837static ssize_t set_pwm_enable(struct device *dev,
838 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200840 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
841 int nr = sensor_attr->index;
842
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200843 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100844 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100846 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
Jean Delvareb99883d2010-03-05 22:17:15 +0100847 return -EINVAL;
848
Jean Delvarecccfc9c2010-03-05 22:17:21 +0100849 /* Check trip points before switching to automatic mode */
850 if (val == 2) {
851 if (check_trip_points(dev, nr) < 0)
852 return -EINVAL;
853 }
854
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100855 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 if (val == 0) {
858 int tmp;
859 /* make sure the fan is on when in on/off mode */
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200860 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
861 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* set on/off 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);
Jean Delvareb99883d2010-03-05 22:17:15 +0100866 } else {
867 if (val == 1) /* Manual mode */
Jean Delvare16b5dda2012-01-16 22:51:48 +0100868 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
Jean Delvare6229cdb2010-12-08 16:27:22 +0100869 data->pwm_temp_map[nr] :
870 data->pwm_duty[nr];
Jean Delvareb99883d2010-03-05 22:17:15 +0100871 else /* Automatic mode */
872 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
873 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /* set SmartGuardian mode */
875 data->fan_main_ctrl |= (1 << nr);
Jean Delvare5f2dc792010-03-05 22:17:18 +0100876 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
877 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100880 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return count;
882}
Jean Delvare20ad93d2005-06-05 11:53:25 +0200883static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
884 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Jean Delvare20ad93d2005-06-05 11:53:25 +0200886 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
887 int nr = sensor_attr->index;
888
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200889 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100890 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100892 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return -EINVAL;
894
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100895 mutex_lock(&data->update_lock);
Jean Delvare16b5dda2012-01-16 22:51:48 +0100896 if (has_newer_autopwm(data)) {
Jean Delvare6229cdb2010-12-08 16:27:22 +0100897 /* If we are in automatic mode, the PWM duty cycle register
898 * is read-only so we can't write the value */
899 if (data->pwm_ctrl[nr] & 0x80) {
900 mutex_unlock(&data->update_lock);
901 return -EBUSY;
902 }
903 data->pwm_duty[nr] = pwm_to_reg(data, val);
904 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
905 data->pwm_duty[nr]);
906 } else {
907 data->pwm_duty[nr] = pwm_to_reg(data, val);
908 /* If we are in manual mode, write the duty cycle immediately;
909 * otherwise, just store it for later use. */
910 if (!(data->pwm_ctrl[nr] & 0x80)) {
911 data->pwm_ctrl[nr] = data->pwm_duty[nr];
912 it87_write_value(data, IT87_REG_PWM(nr),
913 data->pwm_ctrl[nr]);
914 }
Jean Delvareb99883d2010-03-05 22:17:15 +0100915 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100916 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return count;
918}
Jean Delvaref8d0c192007-02-14 21:15:02 +0100919static ssize_t set_pwm_freq(struct device *dev,
920 struct device_attribute *attr, const char *buf, size_t count)
921{
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200922 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +0100923 unsigned long val;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100924 int i;
925
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100926 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +0100927 return -EINVAL;
928
Jean Delvaref8d0c192007-02-14 21:15:02 +0100929 /* Search for the nearest available frequency */
930 for (i = 0; i < 7; i++) {
931 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
932 break;
933 }
934
935 mutex_lock(&data->update_lock);
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200936 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
Jean Delvaref8d0c192007-02-14 21:15:02 +0100937 data->fan_ctl |= i << 4;
corentin.labbeb74f3fd2007-06-13 20:27:36 +0200938 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
Jean Delvaref8d0c192007-02-14 21:15:02 +0100939 mutex_unlock(&data->update_lock);
940
941 return count;
942}
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100943static ssize_t show_pwm_temp_map(struct device *dev,
944 struct device_attribute *attr, char *buf)
945{
946 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
947 int nr = sensor_attr->index;
948
949 struct it87_data *data = it87_update_device(dev);
950 int map;
951
952 if (data->pwm_temp_map[nr] < 3)
953 map = 1 << data->pwm_temp_map[nr];
954 else
955 map = 0; /* Should never happen */
956 return sprintf(buf, "%d\n", map);
957}
958static ssize_t set_pwm_temp_map(struct device *dev,
959 struct device_attribute *attr, const char *buf, size_t count)
960{
961 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
962 int nr = sensor_attr->index;
963
964 struct it87_data *data = dev_get_drvdata(dev);
965 long val;
966 u8 reg;
967
Jean Delvare4f3f51b2010-03-05 22:17:21 +0100968 /* This check can go away if we ever support automatic fan speed
969 control on newer chips. */
970 if (!has_old_autopwm(data)) {
971 dev_notice(dev, "Mapping change disabled for safety reasons\n");
972 return -EINVAL;
973 }
974
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100975 if (kstrtol(buf, 10, &val) < 0)
Jean Delvare94ac7ee2010-03-05 22:17:16 +0100976 return -EINVAL;
977
978 switch (val) {
979 case (1 << 0):
980 reg = 0x00;
981 break;
982 case (1 << 1):
983 reg = 0x01;
984 break;
985 case (1 << 2):
986 reg = 0x02;
987 break;
988 default:
989 return -EINVAL;
990 }
991
992 mutex_lock(&data->update_lock);
993 data->pwm_temp_map[nr] = reg;
994 /* If we are in automatic mode, write the temp mapping immediately;
995 * otherwise, just store it for later use. */
996 if (data->pwm_ctrl[nr] & 0x80) {
997 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
998 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
999 }
1000 mutex_unlock(&data->update_lock);
1001 return count;
1002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001004static ssize_t show_auto_pwm(struct device *dev,
1005 struct device_attribute *attr, char *buf)
1006{
1007 struct it87_data *data = it87_update_device(dev);
1008 struct sensor_device_attribute_2 *sensor_attr =
1009 to_sensor_dev_attr_2(attr);
1010 int nr = sensor_attr->nr;
1011 int point = sensor_attr->index;
1012
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001013 return sprintf(buf, "%d\n",
1014 pwm_from_reg(data, data->auto_pwm[nr][point]));
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001015}
1016
1017static ssize_t set_auto_pwm(struct device *dev,
1018 struct device_attribute *attr, const char *buf, size_t count)
1019{
1020 struct it87_data *data = dev_get_drvdata(dev);
1021 struct sensor_device_attribute_2 *sensor_attr =
1022 to_sensor_dev_attr_2(attr);
1023 int nr = sensor_attr->nr;
1024 int point = sensor_attr->index;
1025 long val;
1026
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001027 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001028 return -EINVAL;
1029
1030 mutex_lock(&data->update_lock);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001031 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001032 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1033 data->auto_pwm[nr][point]);
1034 mutex_unlock(&data->update_lock);
1035 return count;
1036}
1037
1038static ssize_t show_auto_temp(struct device *dev,
1039 struct device_attribute *attr, char *buf)
1040{
1041 struct it87_data *data = it87_update_device(dev);
1042 struct sensor_device_attribute_2 *sensor_attr =
1043 to_sensor_dev_attr_2(attr);
1044 int nr = sensor_attr->nr;
1045 int point = sensor_attr->index;
1046
1047 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1048}
1049
1050static ssize_t set_auto_temp(struct device *dev,
1051 struct device_attribute *attr, const char *buf, size_t count)
1052{
1053 struct it87_data *data = dev_get_drvdata(dev);
1054 struct sensor_device_attribute_2 *sensor_attr =
1055 to_sensor_dev_attr_2(attr);
1056 int nr = sensor_attr->nr;
1057 int point = sensor_attr->index;
1058 long val;
1059
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001060 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001061 return -EINVAL;
1062
1063 mutex_lock(&data->update_lock);
1064 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1065 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1066 data->auto_temp[nr][point]);
1067 mutex_unlock(&data->update_lock);
1068 return count;
1069}
1070
Jean Delvare20ad93d2005-06-05 11:53:25 +02001071#define show_fan_offset(offset) \
1072static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1073 show_fan, NULL, offset - 1); \
1074static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1075 show_fan_min, set_fan_min, offset - 1); \
1076static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1077 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079show_fan_offset(1);
1080show_fan_offset(2);
1081show_fan_offset(3);
1082
1083#define show_pwm_offset(offset) \
Jean Delvare20ad93d2005-06-05 11:53:25 +02001084static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1085 show_pwm_enable, set_pwm_enable, offset - 1); \
1086static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
Jean Delvaref8d0c192007-02-14 21:15:02 +01001087 show_pwm, set_pwm, offset - 1); \
1088static DEVICE_ATTR(pwm##offset##_freq, \
1089 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001090 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1091static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001092 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1093 offset - 1); \
1094static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1095 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1096 offset - 1, 0); \
1097static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1098 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1099 offset - 1, 1); \
1100static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1101 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1102 offset - 1, 2); \
1103static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1104 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1105static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1106 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1107 offset - 1, 1); \
1108static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1109 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1110 offset - 1, 0); \
1111static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1112 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1113 offset - 1, 2); \
1114static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1115 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1116 offset - 1, 3); \
1117static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1118 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1119 offset - 1, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121show_pwm_offset(1);
1122show_pwm_offset(2);
1123show_pwm_offset(3);
1124
Jean Delvare17d648b2006-08-28 14:23:46 +02001125/* A different set of callbacks for 16-bit fans */
1126static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1127 char *buf)
1128{
1129 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1130 int nr = sensor_attr->index;
1131 struct it87_data *data = it87_update_device(dev);
1132 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1133}
1134
1135static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1136 char *buf)
1137{
1138 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1139 int nr = sensor_attr->index;
1140 struct it87_data *data = it87_update_device(dev);
1141 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1142}
1143
1144static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1145 const char *buf, size_t count)
1146{
1147 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1148 int nr = sensor_attr->index;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001149 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001150 long val;
1151
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001152 if (kstrtol(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001153 return -EINVAL;
Jean Delvare17d648b2006-08-28 14:23:46 +02001154
1155 mutex_lock(&data->update_lock);
1156 data->fan_min[nr] = FAN16_TO_REG(val);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001157 it87_write_value(data, IT87_REG_FAN_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001158 data->fan_min[nr] & 0xff);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001159 it87_write_value(data, IT87_REG_FANX_MIN[nr],
Jean Delvare17d648b2006-08-28 14:23:46 +02001160 data->fan_min[nr] >> 8);
1161 mutex_unlock(&data->update_lock);
1162 return count;
1163}
1164
1165/* We want to use the same sysfs file names as 8-bit fans, but we need
1166 different variable names, so we have to use SENSOR_ATTR instead of
1167 SENSOR_DEVICE_ATTR. */
1168#define show_fan16_offset(offset) \
1169static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1170 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1171 show_fan16, NULL, offset - 1); \
1172static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1173 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1174 show_fan16_min, set_fan16_min, offset - 1)
1175
1176show_fan16_offset(1);
1177show_fan16_offset(2);
1178show_fan16_offset(3);
Jean Delvarec7f1f712007-09-03 17:11:46 +02001179show_fan16_offset(4);
1180show_fan16_offset(5);
Jean Delvare17d648b2006-08-28 14:23:46 +02001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182/* Alarms */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001183static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1184 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 struct it87_data *data = it87_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +02001187 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
Jean Delvare1d66c642005-04-18 21:16:59 -07001189static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Jean Delvare0124dd72007-11-25 16:16:41 +01001191static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1192 char *buf)
1193{
1194 int bitnr = to_sensor_dev_attr(attr)->index;
1195 struct it87_data *data = it87_update_device(dev);
1196 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1197}
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001198
1199static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1200 *attr, const char *buf, size_t count)
1201{
1202 struct it87_data *data = dev_get_drvdata(dev);
1203 long val;
1204 int config;
1205
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001206 if (kstrtol(buf, 10, &val) < 0 || val != 0)
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001207 return -EINVAL;
1208
1209 mutex_lock(&data->update_lock);
1210 config = it87_read_value(data, IT87_REG_CONFIG);
1211 if (config < 0) {
1212 count = config;
1213 } else {
1214 config |= 1 << 5;
1215 it87_write_value(data, IT87_REG_CONFIG, config);
1216 /* Invalidate cache to force re-read */
1217 data->valid = 0;
1218 }
1219 mutex_unlock(&data->update_lock);
1220
1221 return count;
1222}
1223
Jean Delvare0124dd72007-11-25 16:16:41 +01001224static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1225static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1226static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1227static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1228static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1229static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1230static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1231static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1232static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1233static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1234static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1235static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1236static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1237static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1238static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1239static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001240static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1241 show_alarm, clear_intrusion, 4);
Jean Delvare0124dd72007-11-25 16:16:41 +01001242
Jean Delvared9b327c2010-03-05 22:17:17 +01001243static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1244 char *buf)
1245{
1246 int bitnr = to_sensor_dev_attr(attr)->index;
1247 struct it87_data *data = it87_update_device(dev);
1248 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1249}
1250static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1251 const char *buf, size_t count)
1252{
1253 int bitnr = to_sensor_dev_attr(attr)->index;
1254 struct it87_data *data = dev_get_drvdata(dev);
1255 long val;
1256
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001257 if (kstrtol(buf, 10, &val) < 0
Jean Delvared9b327c2010-03-05 22:17:17 +01001258 || (val != 0 && val != 1))
1259 return -EINVAL;
1260
1261 mutex_lock(&data->update_lock);
1262 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1263 if (val)
1264 data->beeps |= (1 << bitnr);
1265 else
1266 data->beeps &= ~(1 << bitnr);
1267 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1268 mutex_unlock(&data->update_lock);
1269 return count;
1270}
1271
1272static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1273 show_beep, set_beep, 1);
1274static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1275static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1276static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1277static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1278static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1279static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1280static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1281/* fanX_beep writability is set later */
1282static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1283static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1284static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1285static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1286static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1287static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1288 show_beep, set_beep, 2);
1289static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1290static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1291
Jean Delvare5f2dc792010-03-05 22:17:18 +01001292static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1293 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Jean Delvare90d66192007-10-08 18:24:35 +02001295 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvarea7be58a2005-12-18 16:40:14 +01001296 return sprintf(buf, "%u\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
Jean Delvare5f2dc792010-03-05 22:17:18 +01001298static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1299 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001301 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvaref5f64502010-03-05 22:17:19 +01001302 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001304 if (kstrtoul(buf, 10, &val) < 0)
Jean Delvaref5f64502010-03-05 22:17:19 +01001305 return -EINVAL;
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 data->vrm = val;
1308
1309 return count;
1310}
1311static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Jean Delvare5f2dc792010-03-05 22:17:18 +01001313static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1314 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 struct it87_data *data = it87_update_device(dev);
1317 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1318}
1319static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Jean Delvare87808be2006-09-24 21:17:13 +02001320
Jean Delvare738e5e02010-08-14 21:08:50 +02001321static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1322 char *buf)
1323{
1324 static const char *labels[] = {
1325 "+5V",
1326 "5VSB",
1327 "Vbat",
1328 };
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001329 static const char *labels_it8721[] = {
1330 "+3.3V",
1331 "3VSB",
1332 "Vbat",
1333 };
1334 struct it87_data *data = dev_get_drvdata(dev);
Jean Delvare738e5e02010-08-14 21:08:50 +02001335 int nr = to_sensor_dev_attr(attr)->index;
1336
Jean Delvare16b5dda2012-01-16 22:51:48 +01001337 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1338 : labels[nr]);
Jean Delvare738e5e02010-08-14 21:08:50 +02001339}
1340static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1341static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1342static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1343
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001344static ssize_t show_name(struct device *dev, struct device_attribute
1345 *devattr, char *buf)
1346{
1347 struct it87_data *data = dev_get_drvdata(dev);
1348 return sprintf(buf, "%s\n", data->name);
1349}
1350static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1351
Jean Delvare87808be2006-09-24 21:17:13 +02001352static struct attribute *it87_attributes[] = {
1353 &sensor_dev_attr_in0_input.dev_attr.attr,
1354 &sensor_dev_attr_in1_input.dev_attr.attr,
1355 &sensor_dev_attr_in2_input.dev_attr.attr,
1356 &sensor_dev_attr_in3_input.dev_attr.attr,
1357 &sensor_dev_attr_in4_input.dev_attr.attr,
1358 &sensor_dev_attr_in5_input.dev_attr.attr,
1359 &sensor_dev_attr_in6_input.dev_attr.attr,
1360 &sensor_dev_attr_in7_input.dev_attr.attr,
1361 &sensor_dev_attr_in8_input.dev_attr.attr,
1362 &sensor_dev_attr_in0_min.dev_attr.attr,
1363 &sensor_dev_attr_in1_min.dev_attr.attr,
1364 &sensor_dev_attr_in2_min.dev_attr.attr,
1365 &sensor_dev_attr_in3_min.dev_attr.attr,
1366 &sensor_dev_attr_in4_min.dev_attr.attr,
1367 &sensor_dev_attr_in5_min.dev_attr.attr,
1368 &sensor_dev_attr_in6_min.dev_attr.attr,
1369 &sensor_dev_attr_in7_min.dev_attr.attr,
1370 &sensor_dev_attr_in0_max.dev_attr.attr,
1371 &sensor_dev_attr_in1_max.dev_attr.attr,
1372 &sensor_dev_attr_in2_max.dev_attr.attr,
1373 &sensor_dev_attr_in3_max.dev_attr.attr,
1374 &sensor_dev_attr_in4_max.dev_attr.attr,
1375 &sensor_dev_attr_in5_max.dev_attr.attr,
1376 &sensor_dev_attr_in6_max.dev_attr.attr,
1377 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001378 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1379 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1380 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1381 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1382 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1383 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1384 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1385 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001386
1387 &sensor_dev_attr_temp1_input.dev_attr.attr,
1388 &sensor_dev_attr_temp2_input.dev_attr.attr,
1389 &sensor_dev_attr_temp3_input.dev_attr.attr,
1390 &sensor_dev_attr_temp1_max.dev_attr.attr,
1391 &sensor_dev_attr_temp2_max.dev_attr.attr,
1392 &sensor_dev_attr_temp3_max.dev_attr.attr,
1393 &sensor_dev_attr_temp1_min.dev_attr.attr,
1394 &sensor_dev_attr_temp2_min.dev_attr.attr,
1395 &sensor_dev_attr_temp3_min.dev_attr.attr,
1396 &sensor_dev_attr_temp1_type.dev_attr.attr,
1397 &sensor_dev_attr_temp2_type.dev_attr.attr,
1398 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001399 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1400 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1401 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001402
1403 &dev_attr_alarms.attr,
Jean Delvare3d30f9e2011-07-25 21:46:10 +02001404 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001405 &dev_attr_name.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001406 NULL
1407};
1408
1409static const struct attribute_group it87_group = {
1410 .attrs = it87_attributes,
1411};
1412
Jean Delvared9b327c2010-03-05 22:17:17 +01001413static struct attribute *it87_attributes_beep[] = {
1414 &sensor_dev_attr_in0_beep.dev_attr.attr,
1415 &sensor_dev_attr_in1_beep.dev_attr.attr,
1416 &sensor_dev_attr_in2_beep.dev_attr.attr,
1417 &sensor_dev_attr_in3_beep.dev_attr.attr,
1418 &sensor_dev_attr_in4_beep.dev_attr.attr,
1419 &sensor_dev_attr_in5_beep.dev_attr.attr,
1420 &sensor_dev_attr_in6_beep.dev_attr.attr,
1421 &sensor_dev_attr_in7_beep.dev_attr.attr,
1422
1423 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1424 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1425 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1426 NULL
1427};
1428
1429static const struct attribute_group it87_group_beep = {
1430 .attrs = it87_attributes_beep,
1431};
1432
Jean Delvare723a0aa2010-03-05 22:17:16 +01001433static struct attribute *it87_attributes_fan16[5][3+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001434 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1435 &sensor_dev_attr_fan1_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001436 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1437 NULL
1438}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001439 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1440 &sensor_dev_attr_fan2_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001441 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1442 NULL
1443}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001444 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1445 &sensor_dev_attr_fan3_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001446 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1447 NULL
1448}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001449 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1450 &sensor_dev_attr_fan4_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001451 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1452 NULL
1453}, {
Jean Delvarec7f1f712007-09-03 17:11:46 +02001454 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1455 &sensor_dev_attr_fan5_min16.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001456 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1457 NULL
1458} };
Jean Delvare87808be2006-09-24 21:17:13 +02001459
Jean Delvare723a0aa2010-03-05 22:17:16 +01001460static const struct attribute_group it87_group_fan16[5] = {
1461 { .attrs = it87_attributes_fan16[0] },
1462 { .attrs = it87_attributes_fan16[1] },
1463 { .attrs = it87_attributes_fan16[2] },
1464 { .attrs = it87_attributes_fan16[3] },
1465 { .attrs = it87_attributes_fan16[4] },
1466};
1467
1468static struct attribute *it87_attributes_fan[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001469 &sensor_dev_attr_fan1_input.dev_attr.attr,
1470 &sensor_dev_attr_fan1_min.dev_attr.attr,
1471 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001472 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1473 NULL
1474}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001475 &sensor_dev_attr_fan2_input.dev_attr.attr,
1476 &sensor_dev_attr_fan2_min.dev_attr.attr,
1477 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001478 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1479 NULL
1480}, {
Jean Delvare87808be2006-09-24 21:17:13 +02001481 &sensor_dev_attr_fan3_input.dev_attr.attr,
1482 &sensor_dev_attr_fan3_min.dev_attr.attr,
1483 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare0124dd72007-11-25 16:16:41 +01001484 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001485 NULL
1486} };
Jean Delvare0124dd72007-11-25 16:16:41 +01001487
Jean Delvare723a0aa2010-03-05 22:17:16 +01001488static const struct attribute_group it87_group_fan[3] = {
1489 { .attrs = it87_attributes_fan[0] },
1490 { .attrs = it87_attributes_fan[1] },
1491 { .attrs = it87_attributes_fan[2] },
1492};
1493
1494static const struct attribute_group *
1495it87_get_fan_group(const struct it87_data *data)
1496{
1497 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1498}
1499
1500static struct attribute *it87_attributes_pwm[3][4+1] = { {
Jean Delvare87808be2006-09-24 21:17:13 +02001501 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +02001502 &sensor_dev_attr_pwm1.dev_attr.attr,
Jean Delvared5b0b5d2007-12-14 14:41:53 +01001503 &dev_attr_pwm1_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001504 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001505 NULL
1506}, {
1507 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1508 &sensor_dev_attr_pwm2.dev_attr.attr,
1509 &dev_attr_pwm2_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001510 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001511 NULL
1512}, {
1513 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1514 &sensor_dev_attr_pwm3.dev_attr.attr,
1515 &dev_attr_pwm3_freq.attr,
Jean Delvare94ac7ee2010-03-05 22:17:16 +01001516 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jean Delvare723a0aa2010-03-05 22:17:16 +01001517 NULL
1518} };
Jean Delvare87808be2006-09-24 21:17:13 +02001519
Jean Delvare723a0aa2010-03-05 22:17:16 +01001520static const struct attribute_group it87_group_pwm[3] = {
1521 { .attrs = it87_attributes_pwm[0] },
1522 { .attrs = it87_attributes_pwm[1] },
1523 { .attrs = it87_attributes_pwm[2] },
1524};
1525
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001526static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1527 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1528 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1529 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1530 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1531 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1532 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1533 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1534 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1535 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1536 NULL
1537}, {
1538 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1539 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1540 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1541 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1542 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1543 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1544 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1545 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1546 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1547 NULL
1548}, {
1549 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1550 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1551 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1552 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1553 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1554 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1555 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1556 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1557 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1558 NULL
1559} };
1560
1561static const struct attribute_group it87_group_autopwm[3] = {
1562 { .attrs = it87_attributes_autopwm[0] },
1563 { .attrs = it87_attributes_autopwm[1] },
1564 { .attrs = it87_attributes_autopwm[2] },
1565};
1566
Jean Delvared9b327c2010-03-05 22:17:17 +01001567static struct attribute *it87_attributes_fan_beep[] = {
1568 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1569 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1570 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1571 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1572 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1573};
1574
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001575static struct attribute *it87_attributes_vid[] = {
Jean Delvare87808be2006-09-24 21:17:13 +02001576 &dev_attr_vrm.attr,
1577 &dev_attr_cpu0_vid.attr,
1578 NULL
1579};
1580
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001581static const struct attribute_group it87_group_vid = {
1582 .attrs = it87_attributes_vid,
Jean Delvare87808be2006-09-24 21:17:13 +02001583};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Jean Delvare738e5e02010-08-14 21:08:50 +02001585static struct attribute *it87_attributes_label[] = {
1586 &sensor_dev_attr_in3_label.dev_attr.attr,
1587 &sensor_dev_attr_in7_label.dev_attr.attr,
1588 &sensor_dev_attr_in8_label.dev_attr.attr,
1589 NULL
1590};
1591
1592static const struct attribute_group it87_group_label = {
Jean Delvarefa8b6972011-07-17 18:39:19 +02001593 .attrs = it87_attributes_label,
Jean Delvare738e5e02010-08-14 21:08:50 +02001594};
1595
Jean Delvare2d8672c2005-07-19 23:56:35 +02001596/* SuperIO detection - will change isa_address if a chip is found */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001597static int __init it87_find(unsigned short *address,
1598 struct it87_sio_data *sio_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001600 int err;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001601 u16 chip_type;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001602 const char *board_vendor, *board_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Nat Gurumoorthy5b0380c2011-05-25 20:43:33 +02001604 err = superio_enter();
1605 if (err)
1606 return err;
1607
1608 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001609 chip_type = force_id ? force_id : superio_inw(DEVID);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001610
1611 switch (chip_type) {
1612 case IT8705F_DEVID:
1613 sio_data->type = it87;
1614 break;
1615 case IT8712F_DEVID:
1616 sio_data->type = it8712;
1617 break;
1618 case IT8716F_DEVID:
1619 case IT8726F_DEVID:
1620 sio_data->type = it8716;
1621 break;
1622 case IT8718F_DEVID:
1623 sio_data->type = it8718;
1624 break;
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001625 case IT8720F_DEVID:
1626 sio_data->type = it8720;
1627 break;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001628 case IT8721F_DEVID:
1629 sio_data->type = it8721;
1630 break;
Jean Delvare16b5dda2012-01-16 22:51:48 +01001631 case IT8728F_DEVID:
1632 sio_data->type = it8728;
1633 break;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001634 case 0xffff: /* No device at all */
1635 goto exit;
1636 default:
Joe Perchesa8ca1032011-01-12 21:55:10 +01001637 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001638 goto exit;
1639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Jean Delvare87673dd2006-08-28 14:37:19 +02001641 superio_select(PME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001643 pr_info("Device not activated, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 goto exit;
1645 }
1646
1647 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1648 if (*address == 0) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001649 pr_info("Base address not set, skipping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 goto exit;
1651 }
1652
1653 err = 0;
Andrew Paprocki04751692008-08-06 22:41:06 +02001654 sio_data->revision = superio_inb(DEVREV) & 0x0f;
Joe Perchesa8ca1032011-01-12 21:55:10 +01001655 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
Andrew Paprocki04751692008-08-06 22:41:06 +02001656 chip_type, *address, sio_data->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Jean Delvare738e5e02010-08-14 21:08:50 +02001658 /* in8 (Vbat) is always internal */
1659 sio_data->internal = (1 << 2);
1660
Jean Delvare87673dd2006-08-28 14:37:19 +02001661 /* Read GPIO config and VID value from LDN 7 (GPIO) */
Jean Delvare895ff262009-12-09 20:35:47 +01001662 if (sio_data->type == it87) {
1663 /* The IT8705F doesn't have VID pins at all */
1664 sio_data->skip_vid = 1;
Jean Delvared9b327c2010-03-05 22:17:17 +01001665
1666 /* The IT8705F has a different LD number for GPIO */
1667 superio_select(5);
1668 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare895ff262009-12-09 20:35:47 +01001669 } else {
Jean Delvare87673dd2006-08-28 14:37:19 +02001670 int reg;
1671
1672 superio_select(GPIO);
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001673
Jean Delvare895ff262009-12-09 20:35:47 +01001674 reg = superio_inb(IT87_SIO_GPIO3_REG);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001675 if (sio_data->type == it8721 || sio_data->type == it8728) {
1676 /*
1677 * The IT8721F/IT8758E doesn't have VID pins at all,
1678 * not sure about the IT8728F.
1679 */
Jean Delvare895ff262009-12-09 20:35:47 +01001680 sio_data->skip_vid = 1;
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001681 } else {
1682 /* We need at least 4 VID pins */
1683 if (reg & 0x0f) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01001684 pr_info("VID is disabled (pins used for GPIO)\n");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001685 sio_data->skip_vid = 1;
1686 }
Jean Delvare895ff262009-12-09 20:35:47 +01001687 }
1688
Jean Delvare591ec652009-12-09 20:35:48 +01001689 /* Check if fan3 is there or not */
1690 if (reg & (1 << 6))
1691 sio_data->skip_pwm |= (1 << 2);
1692 if (reg & (1 << 7))
1693 sio_data->skip_fan |= (1 << 2);
1694
1695 /* Check if fan2 is there or not */
1696 reg = superio_inb(IT87_SIO_GPIO5_REG);
1697 if (reg & (1 << 1))
1698 sio_data->skip_pwm |= (1 << 1);
1699 if (reg & (1 << 2))
1700 sio_data->skip_fan |= (1 << 1);
1701
Jean Delvare895ff262009-12-09 20:35:47 +01001702 if ((sio_data->type == it8718 || sio_data->type == it8720)
1703 && !(sio_data->skip_vid))
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001704 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
Jean Delvare87673dd2006-08-28 14:37:19 +02001705
1706 reg = superio_inb(IT87_SIO_PINX2_REG);
Jean Delvare436cad22010-07-09 16:22:48 +02001707 /*
1708 * The IT8720F has no VIN7 pin, so VCCH should always be
1709 * routed internally to VIN7 with an internal divider.
1710 * Curiously, there still is a configuration bit to control
1711 * this, which means it can be set incorrectly. And even
1712 * more curiously, many boards out there are improperly
1713 * configured, even though the IT8720F datasheet claims
1714 * that the internal routing of VCCH to VIN7 is the default
1715 * setting. So we force the internal routing in this case.
1716 */
1717 if (sio_data->type == it8720 && !(reg & (1 << 1))) {
1718 reg |= (1 << 1);
1719 superio_outb(IT87_SIO_PINX2_REG, reg);
Joe Perchesa8ca1032011-01-12 21:55:10 +01001720 pr_notice("Routing internal VCCH to in7\n");
Jean Delvare436cad22010-07-09 16:22:48 +02001721 }
Jean Delvare87673dd2006-08-28 14:37:19 +02001722 if (reg & (1 << 0))
Jean Delvare738e5e02010-08-14 21:08:50 +02001723 sio_data->internal |= (1 << 0);
Jean Delvare16b5dda2012-01-16 22:51:48 +01001724 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1725 sio_data->type == it8728)
Jean Delvare738e5e02010-08-14 21:08:50 +02001726 sio_data->internal |= (1 << 1);
Jean Delvared9b327c2010-03-05 22:17:17 +01001727
1728 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
Jean Delvare87673dd2006-08-28 14:37:19 +02001729 }
Jean Delvared9b327c2010-03-05 22:17:17 +01001730 if (sio_data->beep_pin)
Joe Perchesa8ca1032011-01-12 21:55:10 +01001731 pr_info("Beeping is supported\n");
Jean Delvare87673dd2006-08-28 14:37:19 +02001732
Jean Delvare98dd22c2008-10-09 15:33:58 +02001733 /* Disable specific features based on DMI strings */
1734 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1735 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1736 if (board_vendor && board_name) {
1737 if (strcmp(board_vendor, "nVIDIA") == 0
1738 && strcmp(board_name, "FN68PT") == 0) {
1739 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1740 connected to a fan, but to something else. One user
1741 has reported instant system power-off when changing
1742 the PWM2 duty cycle, so we disable it.
1743 I use the board name string as the trigger in case
1744 the same board is ever used in other systems. */
Joe Perchesa8ca1032011-01-12 21:55:10 +01001745 pr_info("Disabling pwm2 due to hardware constraints\n");
Jean Delvare98dd22c2008-10-09 15:33:58 +02001746 sio_data->skip_pwm = (1 << 1);
1747 }
1748 }
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750exit:
1751 superio_exit();
1752 return err;
1753}
1754
Jean Delvare723a0aa2010-03-05 22:17:16 +01001755static void it87_remove_files(struct device *dev)
1756{
1757 struct it87_data *data = platform_get_drvdata(pdev);
1758 struct it87_sio_data *sio_data = dev->platform_data;
1759 const struct attribute_group *fan_group = it87_get_fan_group(data);
1760 int i;
1761
1762 sysfs_remove_group(&dev->kobj, &it87_group);
Jean Delvared9b327c2010-03-05 22:17:17 +01001763 if (sio_data->beep_pin)
1764 sysfs_remove_group(&dev->kobj, &it87_group_beep);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001765 for (i = 0; i < 5; i++) {
1766 if (!(data->has_fan & (1 << i)))
1767 continue;
1768 sysfs_remove_group(&dev->kobj, &fan_group[i]);
Jean Delvared9b327c2010-03-05 22:17:17 +01001769 if (sio_data->beep_pin)
1770 sysfs_remove_file(&dev->kobj,
1771 it87_attributes_fan_beep[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001772 }
1773 for (i = 0; i < 3; i++) {
1774 if (sio_data->skip_pwm & (1 << 0))
1775 continue;
1776 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001777 if (has_old_autopwm(data))
1778 sysfs_remove_group(&dev->kobj,
1779 &it87_group_autopwm[i]);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001780 }
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001781 if (!sio_data->skip_vid)
1782 sysfs_remove_group(&dev->kobj, &it87_group_vid);
Jean Delvare738e5e02010-08-14 21:08:50 +02001783 sysfs_remove_group(&dev->kobj, &it87_group_label);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001784}
1785
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001786static int __devinit it87_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 struct it87_data *data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001789 struct resource *res;
1790 struct device *dev = &pdev->dev;
1791 struct it87_sio_data *sio_data = dev->platform_data;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001792 const struct attribute_group *fan_group;
1793 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 int enable_pwm_interface;
Jean Delvared9b327c2010-03-05 22:17:17 +01001795 int fan_beep_need_rw;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001796 static const char *names[] = {
1797 "it87",
1798 "it8712",
1799 "it8716",
1800 "it8718",
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01001801 "it8720",
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001802 "it8721",
Jean Delvare16b5dda2012-01-16 22:51:48 +01001803 "it8728",
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001804 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001806 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001807 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001808 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1809 (unsigned long)res->start,
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001810 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001811 err = -EBUSY;
1812 goto ERROR0;
1813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Jean Delvare5f2dc792010-03-05 22:17:18 +01001815 data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
1816 if (!data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 err = -ENOMEM;
1818 goto ERROR1;
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001821 data->addr = res->start;
1822 data->type = sio_data->type;
Andrew Paprocki04751692008-08-06 22:41:06 +02001823 data->revision = sio_data->revision;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001824 data->name = names[sio_data->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 /* Now, we do the remaining detection. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001827 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1828 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
Jean Delvare8e9afcb2006-12-12 18:18:28 +01001829 err = -ENODEV;
1830 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001833 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001835 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /* Check PWM configuration */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001838 enable_pwm_interface = it87_check_pwm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001840 /* Starting with IT8721F, we handle scaling of internal voltages */
Jean Delvare16b5dda2012-01-16 22:51:48 +01001841 if (has_12mv_adc(data)) {
Jean Delvare44c1bcd2010-10-28 20:31:51 +02001842 if (sio_data->internal & (1 << 0))
1843 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1844 if (sio_data->internal & (1 << 1))
1845 data->in_scaled |= (1 << 7); /* in7 is VSB */
1846 if (sio_data->internal & (1 << 2))
1847 data->in_scaled |= (1 << 8); /* in8 is Vbat */
1848 }
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /* Initialize the IT87 chip */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001851 it87_init_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 /* Register sysfs hooks */
Jean Delvare5f2dc792010-03-05 22:17:18 +01001854 err = sysfs_create_group(&dev->kobj, &it87_group);
1855 if (err)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001856 goto ERROR2;
Jean Delvare17d648b2006-08-28 14:23:46 +02001857
Jean Delvared9b327c2010-03-05 22:17:17 +01001858 if (sio_data->beep_pin) {
1859 err = sysfs_create_group(&dev->kobj, &it87_group_beep);
1860 if (err)
1861 goto ERROR4;
1862 }
1863
Jean Delvare9060f8b2006-08-28 14:24:17 +02001864 /* Do not create fan files for disabled fans */
Jean Delvare723a0aa2010-03-05 22:17:16 +01001865 fan_group = it87_get_fan_group(data);
Jean Delvared9b327c2010-03-05 22:17:17 +01001866 fan_beep_need_rw = 1;
Jean Delvare723a0aa2010-03-05 22:17:16 +01001867 for (i = 0; i < 5; i++) {
1868 if (!(data->has_fan & (1 << i)))
1869 continue;
1870 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
1871 if (err)
1872 goto ERROR4;
Jean Delvared9b327c2010-03-05 22:17:17 +01001873
1874 if (sio_data->beep_pin) {
1875 err = sysfs_create_file(&dev->kobj,
1876 it87_attributes_fan_beep[i]);
1877 if (err)
1878 goto ERROR4;
1879 if (!fan_beep_need_rw)
1880 continue;
1881
1882 /* As we have a single beep enable bit for all fans,
1883 * only the first enabled fan has a writable attribute
1884 * for it. */
1885 if (sysfs_chmod_file(&dev->kobj,
1886 it87_attributes_fan_beep[i],
1887 S_IRUGO | S_IWUSR))
1888 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
1889 i + 1);
1890 fan_beep_need_rw = 0;
1891 }
Jean Delvare17d648b2006-08-28 14:23:46 +02001892 }
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (enable_pwm_interface) {
Jean Delvare723a0aa2010-03-05 22:17:16 +01001895 for (i = 0; i < 3; i++) {
1896 if (sio_data->skip_pwm & (1 << i))
1897 continue;
1898 err = sysfs_create_group(&dev->kobj,
1899 &it87_group_pwm[i]);
1900 if (err)
Jean Delvare98dd22c2008-10-09 15:33:58 +02001901 goto ERROR4;
Jean Delvare4f3f51b2010-03-05 22:17:21 +01001902
1903 if (!has_old_autopwm(data))
1904 continue;
1905 err = sysfs_create_group(&dev->kobj,
1906 &it87_group_autopwm[i]);
1907 if (err)
1908 goto ERROR4;
Jean Delvare98dd22c2008-10-09 15:33:58 +02001909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911
Jean Delvare895ff262009-12-09 20:35:47 +01001912 if (!sio_data->skip_vid) {
Jean Delvare303760b2005-07-31 21:52:01 +02001913 data->vrm = vid_which_vrm();
Jean Delvare87673dd2006-08-28 14:37:19 +02001914 /* VID reading from Super-I/O config space if available */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001915 data->vid = sio_data->vid_value;
Jean Delvare6a8d7ac2010-03-05 22:17:16 +01001916 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
1917 if (err)
Jean Delvare87808be2006-09-24 21:17:13 +02001918 goto ERROR4;
1919 }
1920
Jean Delvare738e5e02010-08-14 21:08:50 +02001921 /* Export labels for internal sensors */
1922 for (i = 0; i < 3; i++) {
1923 if (!(sio_data->internal & (1 << i)))
1924 continue;
1925 err = sysfs_create_file(&dev->kobj,
1926 it87_attributes_label[i]);
1927 if (err)
1928 goto ERROR4;
1929 }
1930
Tony Jones1beeffe2007-08-20 13:46:20 -07001931 data->hwmon_dev = hwmon_device_register(dev);
1932 if (IS_ERR(data->hwmon_dev)) {
1933 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +02001934 goto ERROR4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
1936
1937 return 0;
1938
Jean Delvare87808be2006-09-24 21:17:13 +02001939ERROR4:
Jean Delvare723a0aa2010-03-05 22:17:16 +01001940 it87_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941ERROR2:
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001942 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 kfree(data);
1944ERROR1:
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001945 release_region(res->start, IT87_EC_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946ERROR0:
1947 return err;
1948}
1949
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001950static int __devexit it87_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001952 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Tony Jones1beeffe2007-08-20 13:46:20 -07001954 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare723a0aa2010-03-05 22:17:16 +01001955 it87_remove_files(&pdev->dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001956
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05001957 release_region(data->addr, IT87_EC_EXTENT);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001958 platform_set_drvdata(pdev, NULL);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001959 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 return 0;
1962}
1963
Jean Delvare7f999aa2007-02-14 21:15:03 +01001964/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1966 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001967static int it87_read_value(struct it87_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001969 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1970 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
Jean Delvare7f999aa2007-02-14 21:15:03 +01001973/* Must be called with data->update_lock held, except during initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1975 would slow down the IT87 access and should not be necessary. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001976static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001978 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1979 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
1982/* Return 1 if and only if the PWM interface is safe to use */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001983static int __devinit it87_check_pwm(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001985 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1987 * and polarity set to active low is sign that this is the case so we
1988 * disable pwm control to protect the user. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001989 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if ((tmp & 0x87) == 0) {
1991 if (fix_pwm_polarity) {
1992 /* The user asks us to attempt a chip reconfiguration.
1993 * This means switching to active high polarity and
1994 * inverting all fan speed values. */
1995 int i;
1996 u8 pwm[3];
1997
1998 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02001999 pwm[i] = it87_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 IT87_REG_PWM(i));
2001
2002 /* If any fan is in automatic pwm mode, the polarity
2003 * might be correct, as suspicious as it seems, so we
2004 * better don't change anything (but still disable the
2005 * PWM interface). */
2006 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002007 dev_info(dev, "Reconfiguring PWM to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 "active high polarity\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002009 it87_write_value(data, IT87_REG_FAN_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 tmp | 0x87);
2011 for (i = 0; i < 3; i++)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002012 it87_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 IT87_REG_PWM(i),
2014 0x7f & ~pwm[i]);
2015 return 1;
2016 }
2017
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002018 dev_info(dev, "PWM configuration is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 "too broken to be fixed\n");
2020 }
2021
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002022 dev_info(dev, "Detected broken BIOS "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 "defaults, disabling PWM interface\n");
2024 return 0;
2025 } else if (fix_pwm_polarity) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002026 dev_info(dev, "PWM configuration looks "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 "sane, won't touch\n");
2028 }
2029
2030 return 1;
2031}
2032
2033/* Called when we have found a new IT87. */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002034static void __devinit it87_init_device(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Jean Delvare591ec652009-12-09 20:35:48 +01002036 struct it87_sio_data *sio_data = pdev->dev.platform_data;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002037 struct it87_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 int tmp, i;
Jean Delvare591ec652009-12-09 20:35:48 +01002039 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Jean Delvareb99883d2010-03-05 22:17:15 +01002041 /* For each PWM channel:
2042 * - If it is in automatic mode, setting to manual mode should set
2043 * the fan to full speed by default.
2044 * - If it is in manual mode, we need a mapping to temperature
2045 * channels to use when later setting to automatic mode later.
2046 * Use a 1:1 mapping by default (we are clueless.)
2047 * In both cases, the value can (and should) be changed by the user
Jean Delvare6229cdb2010-12-08 16:27:22 +01002048 * prior to switching to a different mode.
2049 * Note that this is no longer needed for the IT8721F and later, as
2050 * these have separate registers for the temperature mapping and the
2051 * manual duty cycle. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 for (i = 0; i < 3; i++) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002053 data->pwm_temp_map[i] = i;
2054 data->pwm_duty[i] = 0x7f; /* Full speed */
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002055 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057
Jean Delvarec5df9b72006-08-28 14:37:54 +02002058 /* Some chips seem to have default value 0xff for all limit
2059 * registers. For low voltage limits it makes no sense and triggers
2060 * alarms, so change to 0 instead. For high temperature limits, it
2061 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2062 * but is still confusing, so change to 127 degrees C. */
2063 for (i = 0; i < 8; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002064 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002065 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002066 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002067 }
2068 for (i = 0; i < 3; i++) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002069 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Jean Delvarec5df9b72006-08-28 14:37:54 +02002070 if (tmp == 0xff)
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002071 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
Jean Delvarec5df9b72006-08-28 14:37:54 +02002072 }
2073
Jean Delvarea00afb92010-04-14 16:14:09 +02002074 /* Temperature channels are not forcibly enabled, as they can be
2075 * set to two different sensor types and we can't guess which one
2076 * is correct for a given system. These channels can be enabled at
2077 * run-time through the temp{1-3}_type sysfs accessors if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 /* Check if voltage monitors are reset manually or by some reason */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002080 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if ((tmp & 0xff) == 0) {
2082 /* Enable all voltage monitors */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002083 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
2085
2086 /* Check if tachometers are reset manually or by some reason */
Jean Delvare591ec652009-12-09 20:35:48 +01002087 mask = 0x70 & ~(sio_data->skip_fan << 4);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002088 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
Jean Delvare591ec652009-12-09 20:35:48 +01002089 if ((data->fan_main_ctrl & mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 /* Enable all fan tachometers */
Jean Delvare591ec652009-12-09 20:35:48 +01002091 data->fan_main_ctrl |= mask;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002092 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2093 data->fan_main_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
Jean Delvare9060f8b2006-08-28 14:24:17 +02002095 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Jean Delvare17d648b2006-08-28 14:23:46 +02002097 /* Set tachometers to 16-bit mode if needed */
Andrew Paprocki04751692008-08-06 22:41:06 +02002098 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002099 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
Jean Delvare9060f8b2006-08-28 14:24:17 +02002100 if (~tmp & 0x07 & data->has_fan) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002101 dev_dbg(&pdev->dev,
Jean Delvare17d648b2006-08-28 14:23:46 +02002102 "Setting fan1-3 to 16-bit mode\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002103 it87_write_value(data, IT87_REG_FAN_16BIT,
Jean Delvare17d648b2006-08-28 14:23:46 +02002104 tmp | 0x07);
2105 }
Andrew Paprocki816d8c62008-08-06 22:41:06 +02002106 /* IT8705F only supports three fans. */
2107 if (data->type != it87) {
2108 if (tmp & (1 << 4))
2109 data->has_fan |= (1 << 3); /* fan4 enabled */
2110 if (tmp & (1 << 5))
2111 data->has_fan |= (1 << 4); /* fan5 enabled */
2112 }
Jean Delvare17d648b2006-08-28 14:23:46 +02002113 }
2114
Jean Delvare591ec652009-12-09 20:35:48 +01002115 /* Fan input pins may be used for alternative functions */
2116 data->has_fan &= ~sio_data->skip_fan;
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 /* Start monitoring */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002119 it87_write_value(data, IT87_REG_CONFIG,
2120 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 | (update_vbat ? 0x41 : 0x01));
2122}
2123
Jean Delvareb99883d2010-03-05 22:17:15 +01002124static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2125{
2126 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
Jean Delvare16b5dda2012-01-16 22:51:48 +01002127 if (has_newer_autopwm(data)) {
Jean Delvareb99883d2010-03-05 22:17:15 +01002128 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
Jean Delvare6229cdb2010-12-08 16:27:22 +01002129 data->pwm_duty[nr] = it87_read_value(data,
2130 IT87_REG_PWM_DUTY(nr));
2131 } else {
2132 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2133 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2134 else /* Manual mode */
2135 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2136 }
Jean Delvare4f3f51b2010-03-05 22:17:21 +01002137
2138 if (has_old_autopwm(data)) {
2139 int i;
2140
2141 for (i = 0; i < 5 ; i++)
2142 data->auto_temp[nr][i] = it87_read_value(data,
2143 IT87_REG_AUTO_TEMP(nr, i));
2144 for (i = 0; i < 3 ; i++)
2145 data->auto_pwm[nr][i] = it87_read_value(data,
2146 IT87_REG_AUTO_PWM(nr, i));
2147 }
Jean Delvareb99883d2010-03-05 22:17:15 +01002148}
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150static struct it87_data *it87_update_device(struct device *dev)
2151{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002152 struct it87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 int i;
2154
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002155 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2158 || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (update_vbat) {
2160 /* Cleared after each update, so reenable. Value
Jean Delvare5f2dc792010-03-05 22:17:18 +01002161 returned by this read will be previous value */
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002162 it87_write_value(data, IT87_REG_CONFIG,
Jean Delvare5f2dc792010-03-05 22:17:18 +01002163 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165 for (i = 0; i <= 7; i++) {
2166 data->in[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002167 it87_read_value(data, IT87_REG_VIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 data->in_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002169 it87_read_value(data, IT87_REG_VIN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 data->in_max[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002171 it87_read_value(data, IT87_REG_VIN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
Jean Delvare3543a532006-08-28 14:27:25 +02002173 /* in8 (battery) has no limit registers */
Jean Delvare5f2dc792010-03-05 22:17:18 +01002174 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Jean Delvarec7f1f712007-09-03 17:11:46 +02002176 for (i = 0; i < 5; i++) {
Jean Delvare9060f8b2006-08-28 14:24:17 +02002177 /* Skip disabled fans */
2178 if (!(data->has_fan & (1 << i)))
2179 continue;
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 data->fan_min[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002182 it87_read_value(data, IT87_REG_FAN_MIN[i]);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002183 data->fan[i] = it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002184 IT87_REG_FAN[i]);
Jean Delvare17d648b2006-08-28 14:23:46 +02002185 /* Add high byte if in 16-bit mode */
Andrew Paprocki04751692008-08-06 22:41:06 +02002186 if (has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002187 data->fan[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002188 IT87_REG_FANX[i]) << 8;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002189 data->fan_min[i] |= it87_read_value(data,
Jean Delvarec7f1f712007-09-03 17:11:46 +02002190 IT87_REG_FANX_MIN[i]) << 8;
Jean Delvare17d648b2006-08-28 14:23:46 +02002191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193 for (i = 0; i < 3; i++) {
2194 data->temp[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002195 it87_read_value(data, IT87_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 data->temp_high[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002197 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 data->temp_low[i] =
Jean Delvare5f2dc792010-03-05 22:17:18 +01002199 it87_read_value(data, IT87_REG_TEMP_LOW(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201
Jean Delvare17d648b2006-08-28 14:23:46 +02002202 /* Newer chips don't have clock dividers */
Andrew Paprocki04751692008-08-06 22:41:06 +02002203 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002204 i = it87_read_value(data, IT87_REG_FAN_DIV);
Jean Delvare17d648b2006-08-28 14:23:46 +02002205 data->fan_div[0] = i & 0x07;
2206 data->fan_div[1] = (i >> 3) & 0x07;
2207 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 data->alarms =
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002211 it87_read_value(data, IT87_REG_ALARM1) |
2212 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2213 (it87_read_value(data, IT87_REG_ALARM3) << 16);
Jean Delvared9b327c2010-03-05 22:17:17 +01002214 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
Jean Delvareb99883d2010-03-05 22:17:15 +01002215
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002216 data->fan_main_ctrl = it87_read_value(data,
2217 IT87_REG_FAN_MAIN_CTRL);
2218 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
Jean Delvareb99883d2010-03-05 22:17:15 +01002219 for (i = 0; i < 3; i++)
2220 it87_update_pwm_ctrl(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002222 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
Andrew Paprocki04751692008-08-06 22:41:06 +02002223 /* The 8705 does not have VID capability.
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002224 The 8718 and later don't use IT87_REG_VID for the
Jean-Marc Spaggiarib4da93e2009-01-07 16:37:32 +01002225 same purpose. */
Jean Delvare17d648b2006-08-28 14:23:46 +02002226 if (data->type == it8712 || data->type == it8716) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002227 data->vid = it87_read_value(data, IT87_REG_VID);
Jean Delvare17d648b2006-08-28 14:23:46 +02002228 /* The older IT8712F revisions had only 5 VID pins,
2229 but we assume it is always safe to read 6 bits. */
2230 data->vid &= 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232 data->last_updated = jiffies;
2233 data->valid = 1;
2234 }
2235
Ingo Molnar9a61bf62006-01-18 23:19:26 +01002236 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 return data;
2239}
2240
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002241static int __init it87_device_add(unsigned short address,
2242 const struct it87_sio_data *sio_data)
2243{
2244 struct resource res = {
Bjorn Helgaas87b4b662008-01-22 07:21:03 -05002245 .start = address + IT87_EC_OFFSET,
2246 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002247 .name = DRVNAME,
2248 .flags = IORESOURCE_IO,
2249 };
2250 int err;
2251
Jean Delvareb9acb642009-01-07 16:37:35 +01002252 err = acpi_check_resource_conflict(&res);
2253 if (err)
2254 goto exit;
2255
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002256 pdev = platform_device_alloc(DRVNAME, address);
2257 if (!pdev) {
2258 err = -ENOMEM;
Joe Perchesa8ca1032011-01-12 21:55:10 +01002259 pr_err("Device allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002260 goto exit;
2261 }
2262
2263 err = platform_device_add_resources(pdev, &res, 1);
2264 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002265 pr_err("Device resource addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002266 goto exit_device_put;
2267 }
2268
2269 err = platform_device_add_data(pdev, sio_data,
2270 sizeof(struct it87_sio_data));
2271 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002272 pr_err("Platform data allocation failed\n");
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002273 goto exit_device_put;
2274 }
2275
2276 err = platform_device_add(pdev);
2277 if (err) {
Joe Perchesa8ca1032011-01-12 21:55:10 +01002278 pr_err("Device addition failed (%d)\n", err);
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002279 goto exit_device_put;
2280 }
2281
2282 return 0;
2283
2284exit_device_put:
2285 platform_device_put(pdev);
2286exit:
2287 return err;
2288}
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290static int __init sm_it87_init(void)
2291{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002292 int err;
Jean Delvare5f2dc792010-03-05 22:17:18 +01002293 unsigned short isa_address = 0;
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002294 struct it87_sio_data sio_data;
Jean Delvarefde09502005-07-19 23:51:07 +02002295
Jean Delvare98dd22c2008-10-09 15:33:58 +02002296 memset(&sio_data, 0, sizeof(struct it87_sio_data));
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002297 err = it87_find(&isa_address, &sio_data);
2298 if (err)
2299 return err;
2300 err = platform_driver_register(&it87_driver);
2301 if (err)
2302 return err;
2303
2304 err = it87_device_add(isa_address, &sio_data);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002305 if (err) {
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002306 platform_driver_unregister(&it87_driver);
2307 return err;
2308 }
2309
2310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
2313static void __exit sm_it87_exit(void)
2314{
corentin.labbeb74f3fd2007-06-13 20:27:36 +02002315 platform_device_unregister(pdev);
2316 platform_driver_unregister(&it87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
2319
Jean Delvaref1d8e332007-11-25 16:14:44 +01002320MODULE_AUTHOR("Chris Gauthron, "
Jean Delvareb19367c2006-08-28 14:39:26 +02002321 "Jean Delvare <khali@linux-fr.org>");
Jean Delvare44c1bcd2010-10-28 20:31:51 +02002322MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323module_param(update_vbat, bool, 0);
2324MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2325module_param(fix_pwm_polarity, bool, 0);
Jean Delvare5f2dc792010-03-05 22:17:18 +01002326MODULE_PARM_DESC(fix_pwm_polarity,
2327 "Force PWM polarity to active high (DANGEROUS)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328MODULE_LICENSE("GPL");
2329
2330module_init(sm_it87_init);
2331module_exit(sm_it87_exit);